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();
    }

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Taking audio uri from intent

Angular Mat AutoComplete doesn't display/show drop down list of returned Object from Back End

Mat Image creation from raw buffer data

Undefined returned from object

Getting the URI from an Android NFC Intent

how to get data from intent in the form of uri?

How to get the URI from a camera intent?

Android: How to share an Uri returned from SAF?

How is an object returned from a function?

Object returned from GitVersion not consistent

Object returned from Firebase signIn

is the Uri returned by Intent.ACTION_GET_CONTENT always somewhere on disk, or can it be anywhere?

Get returned data from Jquery Returned Object for each object

How to release a Mat that's being returned from a function?

How to get the actual kernel value of the Mat returned from OpenCV getGaussianKernel?

Can't get file uri from intent onActivityResult

Change an Android intent action from opening URI to displaying a toast?

ADB: Launching VIEW intent from adb with multiple parameters in Uri

How do you get uri data from content intent in Kotlin?

Activity is destroyed on intent creation

Get value from json result returned by web.DownloadString(uri);

UWP OnNavigatedTo is not firing when returned to from an Oauth redirect Uri

Can't get URI of captured screenshot from intent.getParcelableExtra(Intent.EXTRA_STREAM)!

Get object from intent throws null

How to pass an object from an intent to onActivityResult() method

Passing an ArrayList<Object> from one Intent to another

Update an EditText of an Intent from a variable of an object

Creation of big.matrix object from RasterBrick

js object property creation from keys

TOP Ranking

HotTag

Archive