我正在使用tess-two进行图像到文本转换

时间:2017-11-13 16:28:12

标签: android tess-two

我尝试从图库中获取照片并存储在viewImage中并更改为文本,其加载很长时间并提示没有响应,如果我尝试使用小尺寸jpg文件进行转换。

`findViewById(R.id.gallery).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                photoPickerIntent.setType("image/*");
                startActivityForResult(photoPickerIntent, REQUEST_GALLERY);
            }
        });`

for calling gallery
`String language = "eng";
        datapath = getFilesDir()+ "/tesseract";
        mTess = new TessBaseAPI();
        checkFile(new File(datapath + "/tessdata/"));
        mTess.init(datapath, language);` 

用于初始化Tesseract API

`Uri selectedImage = data.getData();
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
                    System.out.println("image:" +selectedImage);
                    Cursor cursor = getContentResolver().query(selectedImage,
                            filePathColumn, null, null, null);
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String picturePath = cursor.getString(columnIndex);
                    Log.d("Thisbuddy",picturePath);
                    photo = BitmapFactory.decodeFile(picturePath);
                    cursor.close();
                  imageV.setImageBitmap(BitmapFactory.decodeFile(picturePath));`

从图库中取出并保存在viewImage中后调用函数

`public void processImage(View view){
        String OCRresult = null;
        mTess.setImage(photo);
        OCRresult = mTess.getUTF8Text();
        TextView OCRTextView = (TextView) findViewById(R.id.OCRTextView);
        OCRTextView.setText(OCRresult);
    }
private void checkFile(File dir) {
        if (!dir.exists()&& dir.mkdirs()){
            copyFiles();
        }
        if(dir.exists()) {
            String datafilepath = datapath+ "/tessdata/eng.traineddata";
            File datafile = new File(datafilepath);
        if (!datafile.exists()) {
                copyFiles();
            }
        }
     }
private void copyFiles() {
        try {
            String filepath = datapath + "/tessdata/eng.traineddata";
            AssetManager assetManager = getAssets();
           InputStream instream = assetManager.open("tessdata/eng.traineddata");
            OutputStream outstream = new FileOutputStream(filepath);
            byte[] buffer = new byte[1024];
            int read;
            while ((read = instream.read(buffer)) != -1) {
                outstream.write(buffer, 0, read);
            }
            outstream.flush();
            outstream.close();
            instream.close();
            File file = new File(filepath);
            if (!file.exists()) {
                throw new FileNotFoundException();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }`

1 个答案:

答案 0 :(得分:0)

我来了一个解决方案, 我只是使用位图缩小图像

`  float ratio = Math.min(
                        (float) maxImageSize / photo.getWidth(),
                        (float) maxImageSize / photo.getHeight());
                int width = Math.round((float) ratio * photo.getWidth());
                int height = Math.round((float) ratio * photo.getHeight());

                Bitmap newBitmap = Bitmap.createScaledBitmap(photo, width,
                        height, filter);`

现在它对我来说很好。