在Android中拼接图像

时间:2013-11-27 04:54:44

标签: android bitmap

我正在尝试将图像拼接在另一张下方,并在Android的WebView中渲染最终图像。

这是我的相同代码:

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;
}

但正在保存的图像只包含一个图像而不是三个。

1 个答案:

答案 0 :(得分: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);