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>(Mat.java:37)
                  at com.example.testing123.MainActivity.onActivityResult(MainActivity.java:48)
                  at android.app.Activity.dispatchActivityResult(Activity.java:7022)
                  at android.app.ActivityThread.deliverResults(ActivityThread.java:4248)
                  at android.app.ActivityThread.handleSendResult(ActivityThread.java:4295)
                  at android.app.ActivityThread.-wrap20(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1583)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6290)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

After searching i found the following post. http://answers.opencv.org/question/52...

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