从 Uri 创建 Mat 对象由 Intent 返回



我是Android编程的初学者。我有一个代码,用于处理使用OpenCV用Java编写的图像。我正在考虑重用代码。为此,我必须选择一个图像并为其创建Mat对象。

我已经设置了一个 OnClick 事件侦听器并调用了一个函数,该函数又使用 Intent 来选择图像。函数调用如下所示。

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

selectImageFromGallery()守则如下:

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

我正在处理 Intent 返回的结果,如下所示。

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

但是,在 LogCat 中,当我记录Mat对象的高度和宽度时,我将图像的大小设置为 0(所选图像的大小为 2160 x 1080(。

对应的日志猫信息是

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

我已经成功地配置了我的项目OpenCV并通过System.loadLibrary("opencv_java3")正确加载了它

这是从用户选择的图像创建Mat对象的正确方法吗?

如果没有,在这种情况下如何创建Mat对象?

谢谢。

我总是使用转换为位图。CvType.CV_8UC4将适用于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();
    }

}

最新更新