2013-11-27 63 views
2

我試圖將其中一個下方的圖像拼接成一個並在Android的WebView中呈現最終圖像。在Android中拼接圖像

這是我相同的代碼:

File f1 = new File(Environment.getExternalStorageDirectory() + "/mydownload/"+"1.jpg"); 
File f2 = new File(Environment.getExternalStorageDirectory() + "/mydownload/"+"2.jpg"); 
File f3 = new File(Environment.getExternalStorageDirectory() + "/mydownload/"+"3.jpg"); 
try { 
    joinImages(f1, f2, f3); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

private void joinImages(File first, File second , File third) throws IOException 
{ 
    System.out.println("in join images------------------------------"); 
    Bitmap bmp1, bmp2, bmp3; 
    bmp1 = BitmapFactory.decodeFile(first.getPath()); 
    bmp2 = BitmapFactory.decodeFile(second.getPath()); 
    bmp3 = BitmapFactory.decodeFile(third.getPath()); 
    /*if (bmp1 == null || bmp2 == null) 
     return bmp1;*/ 
    int height = bmp1.getHeight()+bmp2.getHeight()+bmp3.getHeight(); 
    System.out.println("height-========================== "+height); 
    System.out.println("widht================ "+bmp1.getWidth()); 
    /* if (height < bmp2.getHeight()) 
     height = bmp2.getHeight();*/ 

    Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), height, Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(bmOverlay); 
    canvas.drawBitmap(bmp1, 0, 0, null); 
    canvas.drawBitmap(bmp2, bmp1.getHeight(), 0, null); 
    canvas.drawBitmap(bmp3,bmp1.getHeight()+bmp2.getHeight() , 0, null); 

    FileOutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/mydownload/"+"final.jpg"); 
    bmOverlay.compress(CompressFormat.JPEG, 80, out); 
    out.close(); 

    //return bmOverlay; 
} 

但它正在保存圖像只包含一個圖像,而不是三個。

回答

0

您的drawBitmap調用是否偏移寬度而不是高度?

假設你想要的位圖一個在另一個之上,你應該做:的

canvas.drawBitmap(bmp1, 0, 0, null); 
canvas.drawBitmap(bmp2, 0, bmp1.getHeight(), null); 
canvas.drawBitmap(bmp3,0, bmp1.getHeight()+bmp2.getHeight() , null); 

代替

canvas.drawBitmap(bmp1, 0, 0, null); 
canvas.drawBitmap(bmp2, bmp1.getHeight(), 0, null); 
canvas.drawBitmap(bmp3,bmp1.getHeight()+bmp2.getHeight() , 0, null); 
+0

耶這並獲得成功:) –