裁剪后的图像无法使用tess-two

时间:2016-08-14 08:11:03

标签: android ocr tesseract tess-two

我正在创建一个可以通过相机捕获图像的应用程序,并且可以使用tess-two android of android来提取图像中的文本。

代码正常工作,直到我添加了裁剪图像的选项。裁剪图像后,错误发生在

行上
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();
    }
}

并执行OCR

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

    }

日志中的一行

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 个答案:

没有答案