Broomer
Broomer

Reputation: 107

Convert from bitmap rgb_565 to Mat

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/52722/what-is-the-correct-way-to-convert-a-mat-to-a-bitmap/

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.

import android.content.Intent;
import android.graphics.Bitmap; 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import org.opencv.android.Utils;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;


public class MainActivity extends AppCompatActivity {

   Button button ;
   ImageView mImageView ;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.capture) ;
        mImageView = (ImageView)findViewById(R.id.imageView) ;
        button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
            startActivityForResult(intent, 0);
        }
    });

}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 0 && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");

        Bitmap.Config config = imageBitmap.getConfig();
        //this is throwing error
        Mat src = new Mat(imageBitmap.getHeight(), imageBitmap.getWidth(), CvType.CV_8UC3);
        Utils.bitmapToMat(imageBitmap, src);

        Imgproc.Canny(src, src, 10, 100);
        Bitmap edgeBitmap = Bitmap.createBitmap(imageBitmap.getWidth(), imageBitmap.getHeight(), Bitmap.Config.ARGB_8888);
       // Utils.matToBitmap(dest, edgeBitmap);

        mImageView.setImageBitmap(imageBitmap);
    }
}

}

When debugging this method call : Mat src = new Mat(imageBitmap.getHeight(), imageBitmap.getWidth(), CvType.CV_8UC3);

calls another method

 nativeObj = n_Mat(rows, cols, type);

this n_Mat is declared like so.

   // C++: Mat::Mat(int rows, int cols, int type)
   private static native long n_Mat(int rows, int cols, int type);

and is highlighted in red.

Upvotes: 2

Views: 751

Answers (1)

Broomer
Broomer

Reputation: 107

The application no longer crashes and functions as expected.

This post covers it quite well. No implementation found for long org.opencv.core.Mat.n_Mat() error Using OpenCV org-opencv-core-mat-n-mat-error-using-opencv

Upvotes: 1

Related Questions