基于Tesseract的OCR应用显示无法读取位图错误

时间:2016-11-16 11:40:22

标签: android bitmap ocr tesseract

我正在开发一个Android应用程序,我想在其中捕获图像并从中提取文本。我使用了tessaract库。我把一个图像放在drawable文件夹中并检查它是否正常工作但是当涉及扫描捕获的图像时应用程序关闭。这是我的代码

     public class MainActivity extends AppCompatActivity {
           EditText editText;
           ImageView imageView;
           Uri outuri;
         File imgdir;
        private static final int CAMERA_REQUEST = 1888;
       Bitmap bitmap;
       File imagename;
       private TessBaseAPI vinodtessbaseapi;
       // String lang = "eng";
        String path;
        File DATA_PATH;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = (EditText) findViewById(R.id.editText);
        imageView = (ImageView) findViewById(R.id.imageView);
        path = Environment.getExternalStorageDirectory().toString()+"/VinodOCR";
       // path = getFilesDir()+"/VinodOCR/tessdata";
        checkFile(new File(path+"tessdata/"));
        String lang = "eng";
        vinodtessbaseapi = new TessBaseAPI();
        vinodtessbaseapi.init(path,lang);

    }

    public void imageok(View view) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        imgdir = new File(Environment.getExternalStorageDirectory().toString(), "VinodOCR/imgs");
        imgdir.mkdirs();
        imagename = new File(imgdir, "Vinod_" + timestamp + ".jpg");
        outuri = Uri.fromFile(imagename);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outuri);
        startActivityForResult(intent, 1);
        Toast.makeText(this, "Image saved in directory",     Toast.LENGTH_LONG).show();
    }

      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
            onPhotoTaken();
        }

    }

    protected void onPhotoTaken() {

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        options.inSampleSize = 4;

        bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/VinodOCR/imgs"+imagename, options);

        try {
            ExifInterface exif = new ExifInterface(String.valueOf(Environment.getExternalStorageDirectory()+"/VinodOCR/imgs"+imagename));
            int exifOrientation = exif.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);


            Toast.makeText(this, "Orient", Toast.LENGTH_LONG).show();
            int rotate = 0;

            switch (exifOrientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    rotate = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    rotate = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    rotate = 270;
                    break;
            }

            // Log.v(TAG, "Rotation: " + rotate);

            if (rotate != 0) {

                // Getting width & height of the given image.
                int w = bitmap.getWidth();
                int h = bitmap.getHeight();

                // Setting pre rotate
                Matrix mtx = new Matrix();
                mtx.preRotate(rotate);

                // Rotating Bitmap
                bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false);
            }

            // Convert to ARGB_8888, required by tess
            bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);

        } catch (IOException e) {
            Toast.makeText(this, "Couldn't correct orientation:" + e.toString(), Toast.LENGTH_LONG).show();
        }
    }
    private  void  checkFile(File dir){
        if(!dir.exists()&&dir.mkdirs()){
            copyFile();

        }
        if(dir.exists()){
            String dfpath = DATA_PATH+"/tessdata/eng.traineddata";
            File datafile = new File(dfpath);
            if(!datafile.exists()){
                copyFile();
            }
        }
    }

    private void copyFile(){
        try {
            String filepath = DATA_PATH+"/tessdata/eng.traineddata";
            AssetManager assetManager = getAssets();
            InputStream inputStream = assetManager.open("tessdata/eng.traineddata");
            OutputStream outputStream = new FileOutputStream(filepath);
            byte[] buffer = new byte[1024];
            int reads;
            while ((reads = inputStream.read(buffer))!= -1){
                outputStream.write(buffer,0,reads);
            }
            outputStream.flush();
            outputStream.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void processImage(View view){
        vinodtessbaseapi.setImage(bitmap);
        String OCRResult = vinodtessbaseapi.getUTF8Text();
      //  TextView ocrtextview = (TextView)findViewById(R.id.OCRTextView);
        editText.setText(OCRResult);
    }


}

我也附上错误日志。请帮我摆脱这个错误。 任何帮助表示感谢。提前感谢您。enter image description here

0 个答案:

没有答案