java - android opencv Utils.BitmapToMap 运行时异常帮助?

标签 java android image-processing opencv

我正在尝试将位图中的 JPEG 转换为垫子。我知道 Utils 方法仅支持 RGB888 格式的图像,因此我强制相机采用 JPEG 格式,拍摄照片,解码并将其转换为 RGB 888 格式,然后调用 utils 方法从中获取垫子。这是基本代码:

Bitmap imageBitmap = BitmapFactory.decodeByteArray(mData, 0, mData.length);
double scale = 0.5; // Make the image smaller incase I was running out of memeory.
imageBitmap = Bitmap.createScaledBitmap(imageBitmap, (int) (imageBitmap.getWidth() * scale),
                    (int) (imageBitmap.getHeight() * scale), false);

imageBitmap = PNGtoRGB888(imageBitmap);
Mat m = Utils.bitmapToMat(imageBitmap); // I get a runtime exception

这是 PNGtoRGB888 方法:

private Bitmap PNGtoRGB888(Bitmap _img)
    {
        int numPixels = _img.getWidth() * _img.getHeight();
        int[] pixels = new int[numPixels];

        // Get Bitmap's pixels. Each int is the color values for one pixel.
        _img.getPixels(pixels, 0, _img.getWidth(), 0, 0, _img.getWidth(), _img.getHeight());

        // Create a Bitmap of the appropriate format.
        Bitmap result = Bitmap.createBitmap(_img.getWidth(), _img.getHeight(), Config.ARGB_8888);

        // Set RGB pixels.
        result.setPixels(pixels, 0, result.getWidth(), 0, 0, result.getWidth(), result.getHeight());
        return result;
    }

有什么想法为什么我会收到运行时异常吗?!?!?!?

最佳答案

您可能想为 ImageBitMap 创建 SoftReference

SoftReference<BitMap> bimap = 
    new SoftRerenceBitmap<Bitmap>(BitmapFactory.decodeByteArray(
            mData, 0, mData.length);

关于java - android opencv Utils.BitmapToMap 运行时异常帮助?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8021747/

相关文章:

Python PILLOW 库使用 CPU 进行图像处理,可以在 GPU 上完成吗?

java - 如何使用servlet从服务器读取pdf文件?

android - Mediaplayer setOnCompletionListener 重复调用

java - Hibernate @GenerateValue 尝试修改标识列

android - 使用 Debug.MemoryInfo 时遇到问题

android - assets文件夹的绝对路径是什么?

c++ - 谁能解释一下这个图像分辨率设置代码?这到底是怎么回事

matlab - Matlab中运动物体的亚像素位移估计

java - 调整固定到 TextView 的可绘制顶部图像的大小

java - 具有多个数据源但只有一组存储库的 Spring Data + JPA