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

I am using tess-two for image to text conversion

我尝试从图库中拍摄照片并存储在 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();
        }
    }`

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

`  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);`

现在它对我来说工作正常。