【问题标题】:Merging image from camera with image from drawables将来自相机的图像与来自drawables的图像合并
【发布时间】:2013-02-02 13:57:48
【问题描述】:

我正在尝试合并 2 个图像,一个是来自相机的位图,第二个是存储在 drawables 中的 .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张图片的方法吗?我需要做的很简单 - 我需要拍照并将其与存储在可绘制对象中的 .PNG 图像合并保存。例如,这个应用程序非常接近我的需要 - https://play.google.com/store/apps/details?id=com.hl2.hud&feature=search_result#?t=W251bGwsMSwyLDEsImNvbS5obDIuaHVkIl0

谢谢:)

【问题讨论】:

    标签: android bitmap


    【解决方案1】:

    请参阅下面的代码来组合两个图像。 此方法返回组合位图

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

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

    【讨论】:

      【解决方案2】:

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

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

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

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

      另见这个问题:Capture Image from Camera and Display in Activity

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

      【讨论】:

      • 好吧,我将这个示例代码用于我的目的 - android-er.blogspot.sk/2011/01/… 它正是我需要的 - 从相机预览图像,在按钮点击时拍照,在屏幕点击时自动对焦。问题在于将来自此代码的照片与存储在可绘制对象中的 .PNG 图像合并。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 2013-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多