Android的合并两个图像 [英] Android merge two images

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

问题描述

我有这两个图像,这是我在画布上基本上合并。现在我想的画布保存成图像。我应该怎么做,或是否有合并两个图像的任何其他方式。

I have these two images, which I basically merge on canvas. Now i want to save that canvas into an image. How should i do it or if there is any other way to merge two images.

我的样品code为 -

My sample code is -

            Bitmap bmp1 = BitmapFactory.decodeResource(getResources(),
                R.drawable.duckpic);
        Bitmap bmp2 = BitmapFactory.decodeResource(getResources(),
                R.drawable.img);
        // canvas.drawColor(Color.BLACK);
        // canvas.drawBitmap(_scratch, 10, 10, null);
        Bitmap bmOverlay = Bitmap.createBitmap(bmp2.getWidth(), bmp2
                .getHeight(), bmp2.getConfig());
        // Canvas cs = new Canvas(bmp2);
        canvas.scale((float) 0.5, (float) 0.5);
        canvas.drawBitmap(bmp2, new Matrix(), null);
        canvas.drawBitmap(bmp1, new Matrix(), null);
        canvas.save();

我得到它的工作通过这样做 -

I got it working by doing this -

    cs = Bitmap.createBitmap(c.getWidth(), c.getHeight(), Bitmap.Config.ARGB_8888);

    Canvas comboImage = new Canvas(cs);

    comboImage.drawBitmap(s, new Matrix(), null);
    comboImage.drawBitmap(c, new Matrix(), null);
    comboImage.save();
    // this is an extra bit I added, just incase you want to save the new
    // image somewhere and then return the location

    String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png";

    OutputStream os = null;
    try {
        os = new FileOutputStream("/sdcard/" + tmpImg);
        cs.compress(CompressFormat.PNG, 100, os);
    } catch (IOException e) {
        Log.e("combineImages", "problem combining images", e);
    }

基本上,它被赋予在这里 - <一个href="http://www.jondev.net/articles/Combining_2_Images_in_Android_using_Canvas">http://www.jondev.net/articles/Combining_2_Images_in_Android_using_Canvas

推荐答案

使用<一个href="http://developer.android.com/reference/android/graphics/Canvas.html#setBitmap%28android.graphics.Bitmap%29"相对=nofollow> canvas.setBitmap(位图位图)。这将发送画布到指定的位图。你要创建一个新的,可变的位图这一点。在调用 setBitmap 则可以将该位图,然后保存到一个文件中。

Use canvas.setBitmap(Bitmap bitmap). This will send the canvas to the specified Bitmap. You'll want to create a new, mutable bitmap for this. After you call setBitmap you can then save that Bitmap to a file.

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

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