我有一个小型的Android应用程序来从图库中选择图像并在imageview中显示它。现在我想在使用OpenCV显示它之前对图像进行一些处理,但我无法使用OpenCV显示任何内容!以下是一些相关代码:
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();
}
}
现在if语句末尾的注释行确实有效,但就像我说的那样我想在显示它之前处理它。
以下是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
当我运行应用程序并从库中选择一个图像时,我收到一条Toast消息,说“出错了”并且图像视图中没有显示图像。
我是Android应用程序开发的新手,所以我可能会遗漏一些简单的东西。提前谢谢。
答案 0 :(得分:0)
让我回答我自己的问题。似乎我用于将OpenCV mat转换为android Bitmap的方法是错误的。以下代码将完成这项工作:
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);