2017-06-15 105 views
0

我有一个小的Android应用程序,从图库中选择图像并在图像查看中显示它。现在我想在使用OpenCV展示图片之前对图片进行一些处理,但是我无法使用OpenCV展示任何内容!下面是一些相关的代码:从图库,Android,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 

当我运行应用程序,并选择从图库我得到一个吐司消息,说一个形象“出事了”,没有图像显示在ImageView的。

我是新来android应用程序开发,所以我可能会错过简单的东西。提前致谢。

回答

0

让我回答我自己的问题。看来,我用来将OpenCV mat转换为android位图的方法是错误的。下面的代码将完成这项工作:

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