convert from bitmap to mat

asked 2018-02-08 05:41:19 -0500

wandCoz gravatar image

updated 2018-02-08 05:44:03 -0500

I am using Android Intent to take a photo.

startActivityForResult(intent, 0);

This opens the native camera and i can take a picture. In the Activity where the picture is returned to , in the onActivityResult method , I can get a hold of the returned Bitmap like so.

if (requestCode == 0 && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");

I then wish to convert this to a Mat to do some openCV image processing. I am attempting to convert like this.

Mat src = new Mat(imageBitmap.getHeight(), imageBitmap.getWidth(), CvType.CV_8UC4);
Utils.bitmapToMat(imageBitmap, src);

The line above causes a crash with the following stack trace .

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.i330155.testing123, PID: 16349
              java.lang.UnsatisfiedLinkError: No implementation found for long org.opencv.core.Mat.n_Mat(int, int, int) 
(tried Java_org_opencv_core_Mat_n_1Mat and Java_org_opencv_core_Mat_n_1Mat__III)
                  at org.opencv.core.Mat.n_Mat(Native Method)
                  at org.opencv.core.Mat.<init>(
                  at com.example.testing123.MainActivity.onActivityResult(
                  at android.os.Handler.dispatchMessage(
                  at android.os.Looper.loop(
                  at java.lang.reflect.Method.invoke(Native Method)

After searching i found the following post.

Which says that.

mat is a valid input Mat object of the types 'CV_8UC1', 'CV_8UC3' or 'CV_8UC4'.
bmp is a valid Bitmap object of the same size as the Mat and of type 'ARGB_8888' or 'RGB_565'.

As you can see in the constructor for the Mat I supply these args. I have checked the config of the imageBitmap , which confirms that it is indeed a 'RGB_565'.

What am i missing here? I dont understand why this does not work. Thanks in advance.

edit retag flag offensive close merge delete