合并在android系统两个位图 [英] Merge two bitmaps in android

查看:180
本文介绍了合并在android系统两个位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要合并两个位图,这是我的code

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

摄像机图像应该成为背景和前景的最上面的图片。我从尝试
结合在Android中2图像使用Canvas 但它并没有帮助我。任何想法。?谢谢

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

推荐答案

从你的榜样,你忘了加上下一行:

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

您还可以通过首先从您的位图,在未来code创建拖拉的对象,像这样做:

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

然后当你有拖拉的对象,您可以设置thier范围,并设置这样的地方做你想做的显示图像。

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

和最后得出的画布上:

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

编辑:

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

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;

这是我从上面的code得到:

This is what I get from the code above:

这篇关于合并在android系统两个位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆