【发布时间】:2013-01-29 18:41:25
【问题描述】:
我正在尝试通过以下步骤处理从 Android 的相机意图获取的 bmp 图像:
- 从intent获取
bmp图片 - 将
bmp图像转换为Mat对象以进行OpenCV 处理(问题从这里开始) - 做需要的OpenCV处理(通过将
Mat对象,obj,作为obj.getNativeObjAddr()发送到本地处理,或者在java中本地执行)。李> - 将
Mat对象转换回bmp
这个问题确实不新鲜。我在网上找到了无数类似的问题,但是似乎没有一个可以解决这种情况。
结果(问题)
目的是在ImageView 对象中显示处理后的图像(经过上述 4 个步骤)。运行后,ImageView 保持不变,logcat 在到达调用Utils.bitmapToMat() 的行时发出以下警告
W/System.err(3872): java.lang.IllegalArgumentException: mat == null
代码
下面是onActivityResult方法中使用的代码大纲(resultant_bmp是从相机intent中获取的bmp,它自己显示成功)。filePath是文件路径,名称包括resultant_bmp 的扩展名。
起始 if 条件之后的前 3 行来自 here,在上述问题中它的使用似乎可以正常工作。
Bitmap resultant_bmp /*image from camera*/,
bmp /*image after opencv processing*/;
Mat rgb_img, gray_img;
if (OpenCVLoader.initDebug()) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Config.RGB_565;
resultant_bmp= BitmapFactory.decodeFile(filePath, options);
/****************** Problem starts HERE ******************
Last point reachable before Logcat states:
W/System.err(4460):java.lang.IllegalArgumentException: mat == null
After this point, the program doesn't crash, but expected results
in the imageView (last line of code) do not result.
**********************************************************/
Utils.bitmapToMat(resultant_bmp, rgb_img);
Imgproc.cvtColor(rgb_img, gray_img, Imgproc.COLOR_RGBA2GRAY);
/*Do opencv processing (on gray_img) here*/
Utils.matToBitmap(gray_img, bmp);
imageView.setImageBitmap(bmp);
}
以往的尝试和研究
- 我发现了一个类似的问题here 并尝试按照建议将文件
libopencv_java.so推送到设备上的system/lib,在命令行中使用adb 并收到错误:
failed to copy 'libopencv_java.so' to 'system/lib/libopencv_java.so': Read-only file system
System.loadLibrary("opencv_java");
System.loadLibrary("libopencv_java");
我已将 OpenCV 库添加到项目属性中;以及指示 NDK 根路径所在位置的 NDKROOT 变量,如进一步解释 here。
我已经在 OpenCV 的另一个工作示例中尝试了所需的图像处理(在上面的步骤 3 中),所以问题肯定出在 步骤 2(上面);将
bmp转换为Mat对象。
应该有一个简单的解决方案,但我似乎找不到它。如果可能的话,我们将不胜感激。
感谢您的宝贵时间。
【问题讨论】:
标签: android opencv bitmap android-ndk android-camera-intent