android - 如何使用OpenCV处理自定义相机拍摄的照片

标签 android opencv camera

我在 Android 上使用 OpenCV,我想使用 OpenCV 来处理我的自定义相机拍摄的照片,下面是我的代码:

//mOriginBitmap is taken by my custom camera.
mOriginMat = new Mat(mOriginBitmap.getHeight(), mOriginBitmap.getWidth(), CvType.CV_8UC4);

Utils.bitmapToMat(mOriginBitmap,mOriginMat);

mDestMat=new Mat(mOriginBitmap.getHeight(), mOriginBitmap.getWidth(), CvType.CV_8UC4);

Imgproc.GaussianBlur(mOriginMat,mDestMat,new Size(3,3),0);

Utils.matToBitmap(mDestMat,mDestBitmap);

mDestImage.setImageBitmap(mDestBitmap);

提前致谢。

更多细节,下面是异常,由Utils.bitmapToMat(mOriginBitmap,mOriginMat);抛出:

com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1037) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832) 
         Caused by: CvException [org.opencv.core.CvException: OpenCV(3.4.4) /build/3_4_pack-android/opencv/modules/java/generator/src/cpp/utils.cpp:38: error: (-215:Assertion failed) AndroidBitmap_lockPixels(env, bitmap, &pixels) >= 0 in function 'void Java_org_opencv_android_Utils_nBitmapToMat2(JNIEnv*, jclass, jobject, jlong, jboolean)'
        ]
            at org.opencv.android.Utils.nBitmapToMat2(Native Method)
            at org.opencv.android.Utils.bitmapToMat(Utils.java:93)
            at org.opencv.android.Utils.bitmapToMat(Utils.java:102)
            at com.example.dell.suppercamera.PictureActivity.onGaussianBlur(PictureActivity.java:103)

最佳答案

我解决了,Android相机拍的图片是YUV420sp,但是Mat需要ARGB888,所以我用Imgcodecs.imread方法加载图片:

File file = new File(getCacheDir(), "text.jpg");
mOriginMat = Imgcodecs.imread(file.getAbsolutePath());
Imgproc.cvtColor(mOriginMat,mOriginMat,Imgproc.COLOR_BGR2RGB);
mDestMat=new Mat(mOriginMat.rows(), mOriginMat.cols(), CvType.CV_8UC4);
Imgproc.GaussianBlur(mOriginMat,mDestMat,new Size(3,3),0);
mDestBitmap = Bitmap.createBitmap(mOriginMat.cols(), mOriginMat.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(mDestMat,mDestBitmap);
mDestImage.setImageBitmap(mDestBitmap);

关于android - 如何使用OpenCV处理自定义相机拍摄的照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53646735/

相关文章:

android - Xamarin.Forms 使用相机拍照显示方向错误并在后退按钮上崩溃

ios - 在 iOS8 中使用 ImagePicker Controller 捕获后图像向下移动

android - 从库中选择图像在 Android 的 fragment 类中不起作用

android - Libgdx 或原生的

python - 即使在使用 pip 成功安装后,也没有名为 'cv2' 的模块

objective-c - Apple Mach-O 链接器和使用 OpenCV 的多个 "undefined symbols"错误

linux - 如何在 Linux 上获取相机缓冲区?

android - 使用 ADB 使用 obb 安装 apk?

android - ConstraintLayout 截断 TextView 中过长的文本

c++ - 生成不同格式的图像字节数据