2016-08-14 5 views
0

私は、カメラで画像をキャプチャできるアプリを作成しています。クロップされた画像がtess-twoで機能しない

イメージをトリミングするオプションを追加するまで、コードは正常に機能します。画像をトリミングした後にエラーがライン上で発生し

tessbaseAPI.setImage(bitmap) 

ログは以下

Failed to read bitmap 

は画像

private void performCrop() { 
    // take care of exceptions 
    try { 
     Log.v(TAG, "Inside try of performCrop"); 
     // call the standard crop action intent (the user device may not 
     // support it) 
     Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
     // indicate image type and Uri 
     cropIntent.setDataAndType(picUri, "image/*"); 
     // set crop properties 
     cropIntent.putExtra("crop", "true"); 
     // indicate aspect of desired crop 
     cropIntent.putExtra("aspectX", 2); 
     cropIntent.putExtra("aspectY", 1); 
     // indicate output X and Y 
     cropIntent.putExtra("outputX", 256); 
     cropIntent.putExtra("outputY", 256); 
     // retrieve data on return 
     cropIntent.putExtra("return-data", true); 
     // start the activity - we handle returning in onActivityResult 
     Log.v(TAG, "Going to onActivityResult now"); 
     startActivityForResult(cropIntent, CROP_PIC); 
    } 
    // respond to users whose devices do not support the crop action 
    catch (ActivityNotFoundException anfe) { 
     Toast toast = Toast 
       .makeText(this, "This device doesn't support the crop action!", Toast.LENGTH_SHORT); 
     toast.show(); 
    } 
} 

を切り出すためと

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     String state = Environment.getExternalStorageState(); 
     if (Environment.MEDIA_MOUNTED.equals(state)) { 
      if (requestCode == REQUEST_IMAGE_CAPTURE) { 
       Log.v(TAG, "Request Code is REQUEST_IMAGE_CAPTURE"); 
       picUri = data.getData(); 
       performCrop(); 
      } else if (requestCode == CROP_PIC) { 
       Log.v(TAG, "Request Code is CROP_PIC"); 
       Bundle extras = data.getExtras(); 
       Bitmap bitmap = extras.getParcelable("data"); 
       TextView res = (TextView) findViewById(R.id.hello); 
       //imageView.setImageBitmap(imageBitmap); 
       //Image image = ImageIO.read(imageFile); 
       //BufferedImage buffimg = (BufferedImage) image; 
       //BufferedImage img = ImageHelper.convertImageToGrayscale(buffimg); 
       //ITesseract instance = new Tesseract(); // JNA Interface Mapping 
       //ITesseract instance = new Tesseract1(); // JNA Direct Mapping 

       //TessDataManager.initTessTrainedData(context); 
       if (isStoragePermissionGranted() == true) { 
        TessBaseAPI tessBaseAPI = new TessBaseAPI(); 

        String path = Environment.getExternalStorageDirectory() + "/"; 
        //String path = "/mnt/sdcard/"; 

        tessBaseAPI.setDebug(true); 
        tessBaseAPI.init(path, "eng"); 


        tessBaseAPI.setImage(bitmap); 

        String text = tessBaseAPI.getUTF8Text(); 
        tessBaseAPI.end(); 
        res.setText(text); 
       } 
      }} else { 
       TextView res = (TextView) findViewById(R.id.hello); 
       res.setText("Well damn"); 
      } 
     } 

    } 
OCR

実行するための私のコードであると言います

Aからのラインog

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.abc.snaptravel/com.example.abc.snaptravel.MainActivity}: java.lang.RuntimeException: Failed to read bitmap 

私は助けていただきありがとうございます。ありがとうございました!

答えて

0

まあ、出てくる問題はたくさんありました。全体的に私の主な問題は、私がSDカードにアクセスできないということでした。私はどこかでHTCモデルのSDカードにアクセスするのは難しく、HTCモデルのみを使用していたと読んでいます。

私はクロップ機能は、このになった内部メモリに私のトリミングされた画像を保存し、その後tessbaseAPI.setImage(File file)

にそのパスを与えることによって、問題を解決 -

private void performCrop() { 
    // take care of exceptions 
    try { 
     Log.v(TAG, "Inside try of performCrop"); 
     // call the standard crop action intent (the user device may not 
     // support it) 
     Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
     // indicate image type and Uri 
     cropIntent.setDataAndType(picUri, "image/*"); 
     // set crop properties 
     cropIntent.putExtra("crop", "true"); 
     // indicate aspect of desired crop 
     cropIntent.putExtra("aspectX", 2); 
     cropIntent.putExtra("aspectY", 1); 
     // indicate output X and Y 
     cropIntent.putExtra("outputX", 256); 
     cropIntent.putExtra("outputY", 256); 
     File dir= 
       Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); 

     output=new File(dir, "CameraContentDemo.jpg"); 
     cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output)); 
     // retrieve data on return 
     cropIntent.putExtra("return-data", false); 
     // start the activity - we handle returning in onActivityResult 
     Log.v(TAG, "Going to onActivityResult now"); 
     startActivityForResult(cropIntent, CROP_PIC); 
    } 
    // respond to users whose devices do not support the crop action 
    catch (ActivityNotFoundException anfe) { 
     Toast toast = Toast 
       .makeText(this, "This device doesn't support the crop action!", Toast.LENGTH_SHORT); 
     toast.show(); 
    } 
} 

とonActivityResult()このなりました -

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     String state = Environment.getExternalStorageState(); 
     if (Environment.MEDIA_MOUNTED.equals(state)) { 
      if (requestCode == REQUEST_IMAGE_CAPTURE) { 
       Log.v(TAG, "Request Code is REQUEST_IMAGE_CAPTURE"); 
       picUri = data.getData(); 
       performCrop(); 
      } else if (requestCode == CROP_PIC) { 
       Log.v(TAG, "Request Code is CROP_PIC"); 
       Bundle extras = data.getExtras(); 
       //Bitmap bitmap = (Bitmap)extras.get("data"); 
       TextView res = (TextView) findViewById(R.id.hello); 
       ImageView im = (ImageView)findViewById(R.id.imageView); 
       //im.setImageBitmap(bitmap); 
       //imageView.setImageBitmap(imageBitmap); 
       //Image image = ImageIO.read(imageFile); 
       //BufferedImage buffimg = (BufferedImage) image; 
       //BufferedImage img = ImageHelper.convertImageToGrayscale(buffimg); 
       //ITesseract instance = new Tesseract(); // JNA Interface Mapping 
       //ITesseract instance = new Tesseract1(); // JNA Direct Mapping 

       //TessDataManager.initTessTrainedData(context); 
       if (isStoragePermissionGranted() == true) { 
        TessBaseAPI tessBaseAPI = new TessBaseAPI(); 

        String path = Environment.getExternalStorageDirectory() + "/"; 
        //String path = "/mnt/sdcard/"; 

        tessBaseAPI.setDebug(true); 
        tessBaseAPI.init(path, "eng"); 
        File file = new File(picUri.getPath()); 
        tessBaseAPI.setImage(output); 

        String text = tessBaseAPI.getUTF8Text(); 
        tessBaseAPI.end(); 
        res.setText(text); 
       } 
      }} else { 
       TextView res = (TextView) findViewById(R.id.hello); 
       res.setText("Well damn"); 
      } 
     } 

    } 

私はこれを行っている注 -

cropIntent.putExtra("return-data", false); 

保存されているファイルを提供する際に、この機能のデータは必要ありません。

関連する問題