Java – Build a business card reader using android vision text OCR

Build a business card reader using android vision text OCR… here is a solution to the problem.

Build a business card reader using android vision text OCR

I’m building an android app using Google’s android mobile visual OCR text to enter a business card as a contact in my phone.

So far, I’ve been able to recognize any Latin-generated text and have been able to apply regular expressions on code blocks

What I did was create a Contacts bean class for the five variables name, email, compnayname, website, adrs, phnno
After applying regular expressions on the generated live data, I filter the results and save them in an object of type bean class
I pass the object to the Activity and extract the data stored in the object and display it in My TextView.

OCR graphics class detection methods >>>

List<? extends Text> textComponents = text.getComponents();
        for(final  Text currentText : textComponents) {
            float left = translateX(currentText.getBoundingBox().left);
            float bottom = translateY(currentText.getBoundingBox().bottom);
            canvas.drawText(currentText.getValue(), left, bottom, sTextPaint);
            if (currentText != null && currentText.getValue() != null) {
                stringList.add(currentText.getValue());

Log.e("OCrGraphic", "Text detected! " + currentText.getValue());

if (isCompany== false && currentText.getValue().matches(".[ A-Z]. [^@$#/-<>!] +")) {
                    Log.e("currentTextcompanyName", currentText.getValue());
                    companyName = "";
                    companyName = currentText.getValue();
                    isCompany = true;
                    contactsBeans.setCompanyName(companyName);
                }

if (isEmail == false && currentText.getValue().matches("^[_A-Za-z0-9-\\\\+]+(\\\\.[ _A-Za-z0-9-]+)*@\"\n" +
                        "\t\t+ \"[A-Za-z0-9-]+(\\\\.[ A-Za-z0-9]+)*(\\\\.[ A-Za-z]{2,})$") || currentText.getValue().contains("@")) {
                    Log.e("currentTextemail", currentText.getValue());
                    email = "";
                    email = currentText.getValue();
                    isEmail = true;
                    contactsBeans.setEmail(email);

}
                Patterns.WEB_URL.matcher(currentText.getValue()).matches();
                if (isWebsite == false && currentText.getValue().matches("^(https?| ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.; ]*[-a-zA-Z0-9+&@#/%=~_|]") || currentText.getValue().startsWith("www") || currentText.getValue().contains("Website") || currentText.getValue().contains("www")) {
                    Log.e("currentTextWebsite", currentText.getValue());
                    website = "";
                    website = currentText.getValue();
                    isWebsite = true;
                    contactsBeans.setWebsite(website);

}
                if (isName== false && currentText.getValue().matches("[a-zA-z]+([ '-][a-zA-Z]+)*")) {
                    Log.e("name", currentText.getValue());
                    name = "";
                    name = currentText.getValue();
                    isName = true;
                    contactsBeans.setName(name);
                }

if (isPhone == false && !currentText.getValue().contains("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") && currentText.getValue().startsWith("+") || currentText.getValue().startsWith("0") && currentText.getValue().contains("+-0123456789/-#") ) {
                    Log.e("currentTextphone", currentText.getValue());
                    phone = "";
                    phone = currentText.getValue();
                    isPhone = true;
                    contactsBeans.setPhone(phone);
                }

if (isAdrs == false &&currentText.getValue().matches("[a-zA-z]+([ '-][a-zA-Z]+)*") && currentText.getValue().contains("Address") || currentText.getValue(). contains("Office") || currentText.getValue().contains("Floor") || currentText.getValue().contains("Plaza") || currentText.getValue().contains("office") || currentText.getValue().contains("Floor")|| currentText.getValue().contains("Floors")|| currentText.getValue().contains("floors")|| currentText.getValue().contains("floor")|| currentText.getValue().contains("Street")|| currentText.getValue().contains("Road")) {
                    address = "";
                    address = currentText.getValue();
                    isAdrs = true;
                    contactsBeans.setAddress(address);
                    Log.e("currentTextaddress", currentText.getValue());
                }

timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        context = ApplicationController.getContext();
                        Intent intent = new Intent(context,ContactsEditActivity.class);
                 /*       Log.e("CBname",contactsBeans.getName());
                        Log.e("CBemail",contactsBeans.getEmail());
                        Log.e("CBadrs",contactsBeans.getAddress());
                        Log.e("CBwebsite",contactsBeans.getWebsite());
                        Log.e("CBcomp",contactsBeans.getCompanyName());
                        Log.e("CBphone",contactsBeans.getPhone()); */
                        intent.putExtra("contactsList",contactsBeans);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NEW_TASK);
                         intent.putStringArrayListExtra("contactsList",stringList);
                        context.startActivity(intent);
                    }
                },6000,6000);

/*

*/
} 

Contacs beans resolve classes

public class ContactsBeans implements Parcelable {
    String name;
    String phone; String email; String companyName;
    String address; String website;
    public List<ContactsBeans> selectedContactsAttribute;

public ContactsBeans() {
    }

public ContactsBeans(List<ContactsBeans> selectedContactsAttribute) {
        this.selectedContactsAttribute = selectedContactsAttribute;
    }

public ContactsBeans(String name, String phone, String email, String companyName, String address, String website) {

this.name = name;
        this.phone = phone;
        this.email = email;
        this.companyName = companyName;
        this.address = address;
        this.website = website;
    }

protected ContactsBeans(Parcel in) {
        name = in.readString();
        phone = in.readString();
        email = in.readString();
        companyName = in.readString();
        address = in.readString();
        website = in.readString();
        selectedContactsAttribute = in.createTypedArrayList(ContactsBeans.CREATOR);
    }

public static final Creator<ContactsBeans> CREATOR = new Creator<ContactsBeans>() {
        @Override
        public ContactsBeans createFromParcel(Parcel in) {
            return new ContactsBeans(in);
        }

@Override
        public ContactsBeans[] newArray(int size) {
            return new ContactsBeans[size];
        }
    };

public String getName() {
        return name;
    }

public void setName(String name) {
        this.name = name;
    }

public String getPhone() {
        return phone;
    }

public void setPhone(String phone) {
        this.phone = phone;
    }

public String getEmail() {
        return email;
    }

public void setEmail(String email) {
        this.email = email;
    }

public String getCompanyName() {
        return companyName;
    }

public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

public String getAddress() {
        return address;
    }

public void setAddress(String address) {
        this.address = address;
    }

public String getWebsite() {
        return website;
    }

public void setWebsite(String website) {
        this.website = website;
    }

public List<ContactsBeans> getSelectedContactsAttribute() {
        return selectedContactsAttribute;
    }

public void setSelectedContactsAttribute(List<ContactsBeans> selectedContactsAttribute) {
        this.selectedContactsAttribute = selectedContactsAttribute;
    }

@Override
    public int describeContents() {
        return 0;
    }

@Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeString(phone);
        dest.writeString(email);
        dest.writeString(companyName);
        dest.writeString(address);
        dest.writeString(website);
        dest.writeTypedList(selectedContactsAttribute);
    }
}

https://developers.google.com/android/reference/com/google/android/gms/vision/text/Text

https://codelabs.developers.google.com/codelabs/mobile-vision-ocr/#6

I have done it by following the tutorial above
I have the following questions

a-) How can I use a line of text instead of a text block?

b-) How do I use the Timer Task in the Graphic class to kill it when it’s done, or should I use a different method?

c-) Are there any apps that I haven’t found using visual OCR to enter business cards, although they say yes?

d-) Any suggestions for my regex exp to be properly tested in a separate Java IDE?

e-) I’m using intents extra to fetch the data stored in the contact bean object and display it in the activity, and although I put flags in my IF, it’s like a snowball strong > declaration that never stops.

f-) At some point, we can prevent the OCR library from detecting any further text after all flags are true. Or just any way?

g-) Does the condition keep overriding my variables regardless of whether the condition is true or not?

All help is highly valued.
Thanks for the assignment.

Solution

I can help with some of them.

a-) How to use Text Lines instead of text blocks?

List<Line> lines = (List<Line>) textBlock.getComponents();

You may need to iterate through the TextBlock SparseArray to get each block line. In addition, this approach is also suitable for getting each element from each row. The getComponents() method is located in the Text interface implemented by all text items.

b-) I am using Timer Task in the Graphic class how to kill it when it’s done or should i use some other approach?

You can count the number of detections received in OcrDetectorProcessor and terminate the set number when it is received.

f-) Can at some point,we can stop the OCR library from detecting any further text after all the flags have gone true. or just any way?

You can stop the instrumentation pipeline by stopping CameraSource. In the CodeLabs example of OcrCaptureActivity, this is done in onPause and onDestroy. By stopping and releasing mPreview, the application stops and cleans up the camera’s Hooks.

Hope this helps.

Related Problems and Solutions