B4A Library OCR - MobileVisionText

mczubel

Member
Licensed User
Longtime User
No, I can't.
7.30,7.01, 6.8, diferent devices.
no work, and is an excellent work!!
 

wes58

Active Member
Licensed User
Longtime User
I have tried your library and it works perfect. The only issue I have (not your fault) is that when I wanted to scan text in portrait, I didn't get any text. And the reason was that the saved picture is rotated (saved in landscape mode).
To fix my problem easily (not perfect solution, I think) I compiled your library java code with the following changes:
1. check the screen orientation
2. rotate the bitmap
B4X:
    public static Bitmap rotate(Bitmap bitmap, int degree) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();

        Matrix mtx = new Matrix();
        mtx.postRotate(degree);
        return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
    }   

    private Bitmap decodeBitmapUri(Context ctx, Uri uri) throws FileNotFoundException {
 
          int targetW = 600;
        int targetH = 600;
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(ctx.getContentResolver().openInputStream(uri), null, bmOptions);
        int photoW = bmOptions.outWidth;
        int photoH = bmOptions.outHeight;
        int scaleFactor = Math.min(photoW / targetW, photoH / targetH);
        bmOptions.inJustDecodeBounds = false;
        bmOptions.inSampleSize = scaleFactor;
        Bitmap bm = BitmapFactory.decodeStream(ctx.getContentResolver().openInputStream(uri), null, bmOptions);
        int rotation = this.getWindowManager().getDefaultDisplay().getRotation();
        if(rotation == 0){    //portrait
            rotation = 90;
        }
        else if(rotation == 1){        //landscape
            rotation = 0;
        }
        else if(rotation == 2){   
            rotation = 270;
        }
        else if(rotation == 3){
            rotation = 180;
        }
        bm = rotate(bm, rotation);
        return bm;
    }
I hope that it may be useful to someone. There are probably better solution to detect the orientation of the taken photo, but for me on Galaxy S7 it works fine.
 

Johan Schoeman

Expert
Licensed User
Longtime User
I have no problem should you want to post your updated library for other members to use. Thanks for the update/changes/sharing your mods
 
Last edited:

jarinashameem

Member
Licensed User
Longtime User
Hi,
When used the sample code, there is a error as mentioned below.

B4A ERROR = Could not set up the detector!

Please provide the solution
 

Johan Schoeman

Expert
Licensed User
Longtime User
If you don't initially have an active internet connection the dependencies won't be downloaded and then the detector won't work. I guess that is perhaps the problem that you have at present i.e the dependencies have not been installed on your device (it should happen automatically when you start the app with an active internet connection)
 

jarinashameem

Member
Licensed User
Longtime User
Sorry, i tried using both Data Connection and WiFi Connected Internet. I am getting this error on the log

Logger connected to: LENOVO Lenovo A7010a48
--------- beginning of system
--------- beginning of main
sending message to waiting queue (mvt_error_result)
sending message to waiting queue (OnActivityResult)
running waiting messages (2)
B4A ERROR = Could not set up the detector!
** Activity (main) Resume **
 

Johan Schoeman

Expert
Licensed User
Longtime User
For some reason your detector is not active. I guess it is because the dependencies have not downloaded and installed successfully.


B4X:
                if (detector.isOperational() && bitmap != null) {
                    Frame frame = new Frame.Builder().setBitmap(bitmap).build();
                    SparseArray<TextBlock> textBlocks = detector.detect(frame);
                    String blocks = "";
                    String lines = "";
                    String words = "";
                    for (int index = 0; index < textBlocks.size(); index++) {
                        //extract scanned text blocks here
                        TextBlock tBlock = textBlocks.valueAt(index);
                        blocks = blocks + tBlock.getValue() + "\n" + "\n";
                        for (Text line : tBlock.getComponents()) {
                            //extract scanned text lines here
                            lines = lines + line.getValue() + "\n";
                            for (Text element : line.getComponents()) {
                                //extract scanned text words here
                                words = words + element.getValue() + ", ";
                            }
                        }
                    }
                    if (textBlocks.size() == 0) {
                        scanResults.setText("Scan Failed: Found nothing to scan");
                    } else {
                        scanResults.setText(scanResults.getText() + "Blocks: " + "\n");
                        scanResults.setText(scanResults.getText() + blocks + "\n");
                        scanResults.setText(scanResults.getText() + "---------" + "\n");
                        scanResults.setText(scanResults.getText() + "Lines: " + "\n");
                        scanResults.setText(scanResults.getText() + lines + "\n");
                        scanResults.setText(scanResults.getText() + "---------" + "\n");
                        scanResults.setText(scanResults.getText() + "Words: " + "\n");
                        scanResults.setText(scanResults.getText() + words + "\n");
                        scanResults.setText(scanResults.getText() + "---------" + "\n");
                    }
                } else {
                    scanResults.setText("Could not set up the detector!");
                }
 

supriono

Member
Licensed User
Longtime User
i have this error
can same one help me how to fixe this?

B4A Version: 8.00
Parsing code. (0.01s)
Compiling code. (6.03s)
Compiling layouts code. (0.01s)
Organizing libraries. (18.52s)
Generating R file. (14.44s)
Compiling generated Java code. (32.45s)
Convert byte code - optimized dex. Error
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/truiton/mobile/vision/ocr/MainActivity;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/truiton/mobile/vision/ocr/MainActivity$1;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/truiton/mobile/vision/ocr/R;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/truiton/mobile/vision/ocr/R$id;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/truiton/mobile/vision/ocr/R$layout;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lmobilevisiontextwrapper/mobilevisiontextWrapper;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lmobilevisiontextwrapper/mobilevisiontextWrapper$1;
7 errors; aborting
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…