简体   繁体   English

裁剪后的图片无法与TESS-T一起使用

[英]Cropped image not working with tess-two

I am creating an app that can capture image through camera and can extract the text in the image using tess-two library of android. 我正在创建一个可以通过相机捕获图像并可以使用tess-two android库提取图像中文本的应用程序。

The code works fine until I added the option of cropping the image. 在添加添加裁剪图像的选项之前,代码工作正常。 After cropping the image the error occurs on the line 裁剪图像后,在行上发生错误

tessbaseAPI.setImage(bitmap)

The log says 日志说

Failed to read bitmap

Below is my code for cropping the image 下面是我裁剪图像的代码

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

and for performing OCR 和执行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");
            }
        }

    }

A line from log 日志中的一行

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

I will appreciate any help. 我将不胜感激。 Thank you! 谢谢!

Well there were a LOT of problems that were coming up. 好了,现在出现了很多问题。 Overall I think the main problem was that I wasn't able to access my SD card. 总的来说,我认为主要的问题是我无法访问我的SD卡。 I read somewhere that it is tough to access SD cards in HTC models and I was using an HTC model only. 我在某处读到很难在HTC型号中访问SD卡,而我仅使用HTC型号。

I solved the problem by saving my cropped image in the internal memory and then giving that path to the tessbaseAPI.setImage(File file) 我通过将裁剪后的图像保存在内部存储器中,然后将该路径提供给tessbaseAPI.setImage(File file)来解决了该问题。

The crop function became this - 作物功能变成了-

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

and the onActivityResult() became this - 并且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");
            }
        }

    }

Note that I have done this - 请注意,我已经做到了-

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

We don't need data from this function when we are providing the file it is stored in. 当我们提供存储该文件的文件时,我们不需要此函数的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM