我正在尝试将位图中的 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/