[英]Show image from gallery, Android, OpenCV
I have a small android app to select image from gallery and show it in imageview. 我有一个小型的Android应用程序,可从图库中选择图像并在imageview中显示。 Now I want to do some processing on the image before showing it using OpenCV, but I can't get it show anything using OpenCV!
现在,我想在使用OpenCV显示图像之前对图像进行一些处理,但是我无法使用OpenCV显示任何图像! Below is some relevant code:
以下是一些相关代码:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
File file = new File(imgDecodableString);
Log.e(getString(R.string.app_name), "File exists: " + file.exists());
Log.e(getString(R.string.app_name), "Trying to read: " + file.getAbsolutePath());
image = Imgcodecs.imread(file.getAbsolutePath(),Imgcodecs.CV_LOAD_IMAGE_COLOR);
ImageView imgView = (ImageView) findViewById(R.id.imgView);
// Set the Image in ImageView after decoding the String
float[] matrixValuesF = new float[image.cols()*image.rows()];
double[] matrixValuesD = new double[matrixValuesF.length];
image.get(0, 0, matrixValuesD);
for (int i=0; i<matrixValuesD.length; i++) {
matrixValuesF[i] = (float) matrixValuesD[i];
}
Matrix android_matrix = new Matrix();
android_matrix.setValues(matrixValuesF);
imgView.setImageMatrix(android_matrix);
// imgView.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString));
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
}
Now the commented line at the end of if statement does work, but like I said I want to process the image before showing it. 现在,if语句末尾的注释行确实起作用,但是就像我说的那样,我想在显示图像之前对其进行处理。
Below is the logcat file: 下面是logcat文件:
E/MyApp: File exists: true
E/MyApp: Trying to read: /storage/emulated/0/images/img.jpg
D/OpenCV/StaticHelper: Trying to get library list
E/OpenCV/StaticHelper: OpenCV error: Cannot load info library for OpenCV
D/OpenCV/StaticHelper: Library list: ""
D/OpenCV/StaticHelper: First attempt to load libs
D/OpenCV/StaticHelper: Trying to init OpenCV libs
D/OpenCV/StaticHelper: Trying to load library opencv_java3
D/OpenCV/StaticHelper: Library opencv_java3 loaded
D/OpenCV/StaticHelper: First attempt to load libs is OK
When I run the app and select an image from the gallery I get a Toast message saying 'Something went wrong' and no image is displayed in the imageview. 当我运行该应用程序并从图库中选择一张图像时,我收到一条Toast消息,提示“出现问题”,并且在imageview中没有显示图像。
I am new to android app development so I might be missing something simple. 我是android应用程序开发的新手,所以我可能缺少一些简单的东西。 Thanks in advance.
提前致谢。
Let me answer my own question. 让我回答我自己的问题。 It seems that the method I was using for converting OpenCV mat to android Bitmap was wrong.
看来我用来将OpenCV mat转换为android Bitmap的方法是错误的。 The below code will do the job:
下面的代码将完成这项工作:
File file = new File(imgDecodableString);
Log.e(getString(R.string.app_name), "File exists: " + file.exists());
Log.e(getString(R.string.app_name), "Trying to read: " + file.getAbsolutePath());
image = Imgcodecs.imread(file.getAbsolutePath(),Imgcodecs.CV_LOAD_IMAGE_COLOR);
resultBitmap = Bitmap.createBitmap(image.cols(), image.rows(),Bitmap.Config.ARGB_8888);;
Utils.matToBitmap(image, resultBitmap);
Bitmap mResult = resultBitmap;
ImageView imgView = (ImageView) findViewById(R.id.imgView);
imgView.setImageBitmap(mResult);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.