2017-06-15 78 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);