Search code examples
androidtess-two

I am using tess-two for image to text conversion


i try to take photo from gallery and stored in viewImage and change to text, its loading for a long time and prompting not responding, if i tried with small size jpg file its converting.

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

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

for calling function after taking from gallery and kept in 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();
        }
    }`

Solution

  • i am came with a solution, i just shrink the image using bitmaps

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

    Now its working fine for me.