将来自相机的图像与来自drawables的图像合并

时间:2013-02-02 13:57:49

标签: android bitmap

我正在尝试合并2个图像,一个是来自相机的位图,第二个是存储在drawable中的.png文件。我做的是我使用两个图像作为位图,我尝试使用画布合并它们,如下所示:

Bitmap topImage = BitmapFactory.decodeFile("gui.png");
Bitmap bottomImage = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);

Canvas canvas = new Canvas(bottomImage);
canvas.drawBitmap(topImage, 0, 0, null);

但我一直都在“比特图大小超过VM预算”错误。我尝试了几乎所有的东西,但仍然,它一直在抛出这个错误。还有另一种合并2张图片的方法吗?我需要做的很简单 - 我需要拍照并将其与存储在drawable中的.PNG图像合并。例如,这个应用程序非常接近我所需要的 - https://play.google.com/store/apps/details?id=com.hl2.hud&feature=search_result#?t=W251bGwsMSwyLDEsImNvbS5obDIuaHVkIl0

谢谢:)

2 个答案:

答案 0 :(得分:3)

请参阅以下代码以合并两个图像。 此方法返回组合位图

public Bitmap combineImages(Bitmap frame, Bitmap image) {
        Bitmap cs = null;
        Bitmap rs = null;

        rs = Bitmap.createScaledBitmap(frame, image.getWidth() + 50,
                image.getHeight() + 50, true);

        cs = Bitmap.createBitmap(rs.getWidth(), rs.getHeight(),
                Bitmap.Config.RGB_565);

        Canvas comboImage = new Canvas(cs);

        comboImage.drawBitmap(image, 25, 25, null);
        comboImage.drawBitmap(rs, 0, 0, null);
        if (rs != null) {
            rs.recycle();
            rs = null;
        }
        Runtime.getRuntime().gc();
        return cs;
    }

您可以根据自己的要求更改高度和宽度 希望这会有所帮助......

答案 1 :(得分:0)

图像有多大?我在尝试将大图像加载到内存时只遇到了这个问题。

您解码的字节数组实际上是图像吗?

通过快速查看android文档,您可以使用默认的相机应用程序捕获图像,这可能适用于这种情况。

http://developer.android.com/training/camera/photobasics.html

另请参阅此问题:Capture Image from Camera and Display in Activity

编辑:如果图像非常大,您可能还需要将相机中的图像缩小。有关详细信息,请参阅我链接到的Android页面的末尾。

相关问题