Creation of Mat Object from Uri returned by Intent

R.S.S.H KRISHNA :

I'm a beginner in Android Programming. I have a code for processing an image written in Java using OpenCV. I'm thinking to reuse the code. For this, I have to select an image and have to create Mat Object for it.

I have setup an OnClick Event Listener and calling a function, which in turn uses Intent to select an image. The Function call is as follows.

selectImage.setOnClickListener(
        new Button.OnClickListener() {
                public void onClick(View v){
                    selectImageFromGallery();
                }
        }
);

The Code for selectImageFromGallery() is as follows:

private void selectImageFromGallery(){
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/png");
        if(intent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(intent , SELCT_IMAGE_CODE);
        }
 }

I'm processing the result returned by Intent as follows.

@Override
protected void onActivityResult(int requestCode , int resultCode , Intent data){
        if(resultCode == RESULT_OK){
            Uri imageUri;
            if(data == null || data.getData()== null){
                imageUri = uriPhoto;
//                Log.i("URI","HERE");
            }else{
                imageUri = data.getData();

                Log.i("URI",imageUri.toString());

// I'm GETTING URI OF THE SELECTED IMAGE,BEING LOGGED SUCCESSFULLY !

                Imgcodecs imageCodecs = new Imgcodecs();
                Mat obj = imageCodecs.imread(imageUri.getPath());
                Log.i("URI" , "MAT OBJECT CREATED SUCCESSFULLY");
                Log.i("URI" , new Integer((int) obj.size().height).toString());
                Log.i("URI" , new Integer((int) obj.size().width).toString());
            }
            Intent intent = new Intent();
            intent.setData(imageUri);
            setResult(RESULT_OK , intent);
            finish();
        }
 }

But, in LogCat I'm getting the size of the image as 0 (Size of the selected image is 2160 x 1080) as I'm Logging the Height and Width of the Mat Object.

The corresponding LogCat info is

2019-02-06 23:48:21.927 27321-27321/com.example.hari.imagesteganography I/URI: content://com.android.providers.media.documents/document/image%3A110235
2019-02-06 23:48:21.938 27321-27321/com.example.hari.imagesteganography I/URI: MAT OBJECT CREATED SUCCESSFULLY
2019-02-06 23:48:21.940 27321-27321/com.example.hari.imagesteganography I/URI: 0
2019-02-06 23:48:21.940 27321-27321/com.example.hari.imagesteganography I/URI: 0

I have Configured OpenCV successfully with my project and loaded it correctly by System.loadLibrary("opencv_java3")

Is this the correct way to create an Mat Object from an Image selected by the User?

If not, how do I create Mat object in this scenario?

Thanks.

Raskilas :

I just always use convertion to Bitmap. CvType.CV_8UC4 will work for ARGB/RGB (Bitmap.Config.ARGB_8888).

import org.opencv.android.Utils

    @Override
    protected void onActivityResult(int requestCode , int resultCode , Intent data){
        if(resultCode == RESULT_OK){
            Uri imageUri;
            if(data == null || data.getData()== null){
                imageUri = uriPhoto;
//                Log.i("URI","HERE");
            }else{
                imageUri = data.getData();
            Log.i("URI",imageUri.toString());

            BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
            bmpFactoryOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;

            Uri imageUri = data.getData();
            Bitmap bmp = MediaStore.Images.Media.getBitmap(
                                                  this.getContentResolver(),
                                                  imageUri);

            Mat obj = new Mat(bmp.width, bmp.height, CvType.CV_8UC4)
            Utils.bitmapToMat(bmp, obj)
            Log.i("URI" , "MAT OBJECT CREATED SUCCESSFULLY");
            Log.i("URI" , String.valueOf(obj.cols()));
            Log.i("URI" , String.valueOf(obj.rows()));
        }
        Intent intent = new Intent();
        intent.setData(imageUri);
        setResult(RESULT_OK , intent);
        finish();
    }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=152356&siteId=1