在android中合并两个位图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11740362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:40:33  来源:igfitidea点击:

Merge two bitmaps in android

androidcanvasbitmapmerge

提问by Numair

I want to merge two bitmaps, here is my code

我想合并两个位图,这是我的代码

// 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();
                    }   

The camera image should become background, and foreground as top image. I've tried from Combining 2 Images in Android using Canvasbut it didn't help me. Any idea.? Thanks

相机图像应该成为背景,前景作为顶部图像。我尝试过 使用 Canvas 在 Android 中组合 2 个图像,但它没有帮助我。任何的想法。?谢谢

回答by Ofir A.

From your example, you forgot to add the next lines:

从您的示例中,您忘记添加下一行:

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

In your example above you don't draw your image in the canvas, and that is the problem. You can think that your canvas i your sketchbook. For now you didn't paint anything, and you ask yourself, way I can't see any colors.

在上面的示例中,您没有在画布中绘制图像,这就是问题所在。你可以认为你的画布就是你的速写本。现在你没有画任何东西,你问自己,我看不到任何颜色。

So, for my advice, first create the two bitmaps, then, do the next thing:

因此,根据我的建议,首先创建两个位图,然后执行以下操作:

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

You can also do this by first create the drawable objects from your bitmaps, like in the next code:

您也可以通过首先从位图创建可绘制对象来执行此操作,如下面的代码所示:

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

Then when you have the drawable objects, you can set thier bounds, and that way you set where do you want to show that image.

然后,当您拥有可绘制对象时,您可以设置它们的边界,这样您就可以设置要在何处显示该图像。

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

and finally draw that on the canvas:

最后在画布上绘制:

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

EDIT:

编辑:

This is an example, use this to understand your implementation:

这是一个示例,使用它来了解您的实现:

    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;

This is what I get from the code above:

这是我从上面的代码中得到的:

enter image description here

在此处输入图片说明

回答by Najaf Ali

Merging Two Bitmap vertically when one is large and second is small
follow this method

当一个大第二个小时垂直合并两个位图
遵循此方法

 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

在此处输入图片说明

回答by Christopher Guray

Please note that the BitmapDrawable(Bitmap) has been deprecated. Kinldy check thisfor the alternative.

请注意 BitmapDrawable(Bitmap) 已被弃用。Kinldy 检查替代方案。

BitmapDrawable(Bitmap bitmap)This constructor was deprecated in API level 4. Use BitmapDrawable(Resources, Bitmap)to ensure that the drawable has correctly set its target density.

BitmapDrawable(Bitmap bitmap)此构造函数在 API 级别 4 中已弃用。BitmapDrawable(Resources, Bitmap)用于确保可绘制对象已正确设置其目标密度。

回答by ashish

Resize watermark image same size as original image

调整与原始图像大小相同的水印图像

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