2012-07-31 7 views
10

में दो बिटमैप्स मर्ज मैं दो बिटमैप्स मर्ज करना चाहते हैं, यहाँ मेरी कोडएंड्रॉयड

// Camera arg conversion to Bitmap 
        Bitmap cameraBitmap = BitmapFactory.decodeByteArray(arg0, 0, 
          arg0.length); 
        Bitmap back = Bitmap.createBitmap(cameraBitmap.getWidth(), 
         cameraBitmap.getHeight(), Bitmap.Config.ARGB_8888); 
        Canvas cam = new Canvas(back); 
        cam.drawBitmap(cameraBitmap, matrix, null); 


        // FrameLayout to Bitmap 
        FrameLayout mainLayout = (FrameLayout) findViewById(R.id.frame); 
        Bitmap foreground = Bitmap.createBitmap(mainLayout.getWidth(), 
          mainLayout.getHeight(), Bitmap.Config.ARGB_8888); 
        Canvas c = new Canvas(foreground); 
        mainLayout.draw(c); 

        Bitmap cs = null; 
        cs = Bitmap.createBitmap(foreground.getWidth(), cameraBitmap.getHeight(), Bitmap.Config.ARGB_8888); 

        Canvas comboImage = new Canvas(cs); 
        comboImage.drawBitmap(cameraBitmap, 0f, 0f, null); 
        comboImage.drawBitmap(foreground, 0f, cameraBitmap.getHeight(), null); 

        FileOutputStream fos = null; 
        try { 
         fos = new FileOutputStream(file); 
         if (fos != null) { 
          cs.compress(Bitmap.CompressFormat.PNG, 90, fos); 
          fos.close(); 
         } 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 

कैमरा छवि पृष्ठभूमि हो जाना चाहिए, और शीर्ष छवि के रूप में अग्रभूमि है। मैंने Combining 2 Images in Android using Canvas से कोशिश की है लेकिन इससे मेरी मदद नहीं हुई। कोई उपाय।? ऊपर आप कैनवास में अपनी छवि आकर्षित नहीं करते हैं, और कहा कि समस्या यह है अपने उदाहरण में

comboImage.drawBitmap(c, 0f, 0f, null); 
comboImage.drawBitmap(s, 0f, c.getHeight(), null); 

: धन्यवाद

उत्तर

29

अपने उदाहरण से, आप अगली पंक्तियों में जोड़ने के लिए भूल गया था। आप सोच सकते हैं कि आपका कैनवास मैं आपकी स्केचबुक हूं। अभी के लिए आपने कुछ भी पेंट नहीं किया है, और आप खुद से पूछते हैं, जिस तरह से मैं कोई रंग नहीं देख सकता।

c.drawBitmap(cameraBitmap, top point, left point, null); 
c.drawBitmap(foreground, top point, left point, null); 

तुम भी अगले कोड में की तरह पहली बार अपने बिटमैप्स से drawable वस्तुओं को बनाने के द्वारा यह कर सकता हूँ,:

तो, मेरी सलाह के लिए, पहले दो बिटमैप बनाने के लिए, तो, अगली बात करना :

Drawable cameraBitmap = BitmapDrawable(cameraBitmap); 
Drawable foreground= BitmapDrawable(foreground); 

तो फिर तुम drawable वस्तुओं हो तब आप उनके सीमा निर्धारित कर सकते हैं, और कहा कि जिस तरह से आप सेट जहां आप उस छवि को दिखाना चाहते हैं।

cameraBitmap.setBounds(left, top, right, bottom); 
foreground.setBounds(left, top, right, bottom); 

और अंत में आकर्षित कि कैनवास पर:

cameraBitmap.draw(canvas); 
foreground.draw(canvas); 

संपादित करें:

यह एक उदाहरण है, इस का उपयोग अपने कार्यान्वयन को समझने के लिए:

Bitmap bitmap = null; 
    try { 

     bitmap = Bitmap.createBitmap(500, 500, Config.ARGB_8888); 
     Canvas c = new Canvas(bitmap); 
     Resources res = getResources(); 


     Bitmap bitmap1 = BitmapFactory.decodeResource(res, R.drawable.test1); //blue 

     Bitmap bitmap2 = BitmapFactory.decodeResource(res, R.drawable.test2); //green 
     Drawable drawable1 = new BitmapDrawable(bitmap1); 
     Drawable drawable2 = new BitmapDrawable(bitmap2); 


     drawable1.setBounds(100, 100, 400, 400); 
     drawable2.setBounds(150, 150, 350, 350); 
     drawable1.draw(c); 
     drawable2.draw(c); 


    } catch (Exception e) { 
    } 
    return bitmap; 

यह मैं उपरोक्त कोड से क्या प्राप्त करता हूं:

enter image description here

+0

मैं करने की कोशिश की अनुसरण करते हुए अपनी उत्तर, मेरे संपादित प्रश्न की जांच करें। लेकिन किसी मर्ज छवि को देखने में असमर्थ, क्या आप कृपया मुझे – Numair

+0

@Numair मार्गदर्शन कर सकते हैं क्या आपको अभी भी मदद चाहिए? –

+0

हां @ ओफिर ए। मुझे अभी भी समस्या है :( – Numair

0

कृपया ध्यान दें कि BitmapDrawable (बिटमैप) मान्य नहीं है। वैकल्पिक के लिए Kinldy this चेक करें।

BitmapDrawable(Bitmap bitmap) यह निर्माता एपीआई स्तर 4. उपयोग BitmapDrawable(Resources, Bitmap) में पदावनत किया गया था सुनिश्चित करना है कि drawable सही ढंग से अपने लक्ष्य घनत्व निर्धारित किया है।

0

आकार वॉटरमार्क छवि मूल छवि के रूप में एक ही आकार

Uri bmpUri1 = getLocalBitmapUri(ivImage); 
Uri bmpUri2 = getLocalBitmapUri(watermark_imageview); 

try { 
    bm1 = BitmapFactory.decodeStream(
      getContentResolver().openInputStream(bmpUri1)); 
    bm2 = BitmapFactory.decodeStream(
      getContentResolver().openInputStream(bmpUri2)); 

    Bitmap bmOverlay = Bitmap.createBitmap(bm1.getWidth(), bm1.getHeight(), bm1.getConfig()); 
    bm2 = Bitmap.createScaledBitmap(bm2, bm1.getWidth(), bm1.getHeight(), 
      true); 

    Canvas canvas = new Canvas(bmOverlay); 
    canvas.drawBitmap(bm1, 0,0, null); 
    canvas.drawBitmap(bm2, 0,0, null); 
    watermarkimage.setVisibility(View.GONE); 
    im =new ImageView(ImageClick.this); 
    im.setImageBitmap(bmOverlay); 
    bmpUri = getLocalBitmapUri(im); 
} catch (FileNotFoundException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

private Uri getLocalBitmapUri(ImageView imageView) { 
    Drawable drawable = imageView.getDrawable(); 
    Bitmap bmp = null; 
    if (drawable instanceof BitmapDrawable){ 
     bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap(); 
    } else { 
     return null; 
    } 
    // Store image to default external storage directory 
    Uri bmpUri = null; 
    try { 
     File file = new File(Environment.getExternalStoragePublicDirectory(
       Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png"); 
     file.getParentFile().mkdirs(); 
     FileOutputStream out = new FileOutputStream(file); 
     bmp.compress(Bitmap.CompressFormat.PNG, 90, out); 
     out.close(); 
     bmpUri = Uri.fromFile(file); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return bmpUri; 
} 
0

विलय दो बिटमैप खड़ी है जब एक बड़े और दूसरा है छोटा है
इस विधि

public Bitmap finalcombieimage(Bitmap c, Bitmap s) { 
    Bitmap cs = null; 
    DisplayMetrics metrics = getBaseContext().getResources().getDisplayMetrics(); 
    int width = metrics.widthPixels; 
    int height = metrics.heightPixels; 
    cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
    Canvas comboImage = new Canvas(cs); 
    Rect dest1 = new Rect(0, 0, width, height); // left,top,right,bottom 
    comboImage.drawBitmap(c, null, dest1, null); 
    Rect dest2 = new Rect(0, height-400/2, width, height); 
    comboImage.drawBitmap(s, null, dest2, null); 
    return cs; 
} 

enter image description here