2011-05-25 101 views
12

我有這兩張圖片,我基本上在畫布上合併。現在我想將該畫布保存爲圖像。我應該怎麼做,或者如果有任何其他方式來合併兩個圖像。Android合併兩張圖片

我的示例代碼 -

  Bitmap bmp1 = BitmapFactory.decodeResource(getResources(), 
       R.drawable.duckpic); 
     Bitmap bmp2 = BitmapFactory.decodeResource(getResources(), 
       R.drawable.img); 
     // canvas.drawColor(Color.BLACK); 
     // canvas.drawBitmap(_scratch, 10, 10, null); 
     Bitmap bmOverlay = Bitmap.createBitmap(bmp2.getWidth(), bmp2 
       .getHeight(), bmp2.getConfig()); 
     // Canvas cs = new Canvas(bmp2); 
     canvas.scale((float) 0.5, (float) 0.5); 
     canvas.drawBitmap(bmp2, new Matrix(), null); 
     canvas.drawBitmap(bmp1, new Matrix(), null); 
     canvas.save(); 

我得到它的工作通過這樣做 -

cs = Bitmap.createBitmap(c.getWidth(), c.getHeight(), Bitmap.Config.ARGB_8888); 

    Canvas comboImage = new Canvas(cs); 

    comboImage.drawBitmap(s, new Matrix(), null); 
    comboImage.drawBitmap(c, new Matrix(), null); 
    comboImage.save(); 
    // this is an extra bit I added, just incase you want to save the new 
    // image somewhere and then return the location 

    String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png"; 

    OutputStream os = null; 
    try { 
     os = new FileOutputStream("/sdcard/" + tmpImg); 
     cs.compress(CompressFormat.PNG, 100, os); 
    } catch (IOException e) { 
     Log.e("combineImages", "problem combining images", e); 
    } 

基本上,它在這裏給出 - http://www.jondev.net/articles/Combining_2_Images_in_Android_using_Canvas

+0

http://kyogs.blogspot.in/2012/08/mearge-images.html請參考這一個。 – kyogs 2012-10-23 13:50:05

回答

3

使用canvas.setBitmap(Bitmap bitmap)。這會將畫布發送到指定的位圖。您需要爲此創建一個新的可變位圖。在您致電setBitmap之後,您可以將該位圖保存到文件中。