android opencv Utils.BitmapToMap 运行时异常帮助?
我正在尝试将位图中的 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;
}
有什么想法为什么我会得到运行时异常吗?!?!?!?
I am trying to convert a JPEG that is in a bitmap to a mat. I know that the Utils method only supports images in RGB888 format so I take force my camera to be in JPEG format, take the picture, decode it and the convert it to RGB 888 format and then call the utils method to get a mat from it. Here is the basic code:
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
Here is the PNGtoRGB888 method:
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;
}
Any ideas why I would get the runtime exception?!?!?!?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能想为 ImageBitMap 创建 SoftReference
You might want to create a SoftReference for ImageBitMap
我必须重新组织我的构建路径并且它起作用了。
I had to reorganize my build path and it worked.