Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
410 views
in Technique[技术] by (71.8m points)

canvas - Merge two bitmaps in android

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 Canvas but it didn't help me. Any idea.? Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...