Reputation: 21
I am using OpenCV on Android, I want to use OpenCV to handle the picture taken by my custom camera, below is my code:
//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);
Thanks in advance.
For more detail, below is exception, throw by 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)
Upvotes: 1
Views: 542
Reputation: 21
I solved it, picture taken by Android camera is YUV420sp, but Mat need ARGB888, so I use Imgcodecs.imread
method to load the picture:
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);
Upvotes: 1
Reputation: 317
It would be nice to know from where the mOriginalBitmap came from, and its value too. However, you can try using the Mat default contructor, it will take care of the width and height itself and the type itself.
mOriginMat = new Mat();
Utils.bitmapToMat(mOriginBitmap,mOriginMat);
This should work if mOriginBitmap is correctly set.
Upvotes: 0
Reputation: 2503
When you look closely at the error message, the core part is this:
Assertion failed) AndroidBitmap_lockPixels(env, bitmap, &pixels) >= 0
The lockPixels method is described here: https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1ga2908d42fa4db286c34b7f8c11f29206f
Since you didn't post the code for your custom camera class, I can only guess, but I would assume that the bitmap you're creating doesn't use a direct buffer (see also here: https://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html), and that would cause lockPixels to fail.
Upvotes: 0