content: package com.example.androidocr;
  
  import android.content.Intent;
  import android.content.res.AssetManager;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.provider.ContactsContract;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.ImageView;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import com.googlecode.tesseract.android.TessBaseAPI;
  
  import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.util.regex.Matcher;
  import java.util.regex.Pattern;
  
  public class MainActivity extends AppCompatActivity {
  
      Bitmap image;
      private TessBaseAPI mTess;
      String datapath = "";
  
      ImageView testImage;
      TextView runOCR;
      TextView displayText;
      TextView displayEmail;
      TextView displayPhone;
      TextView displayName;
      TextView openContacts;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          runOCR = (TextView) findViewById(R.id.textView);
          openContacts = (TextView) findViewById(R.id.textView6);
  
          displayText = (TextView) findViewById(R.id.textView2);
          displayName = (TextView) findViewById(R.id.textView5);
          displayPhone = (TextView) findViewById(R.id.textView4);
          displayEmail = (TextView) findViewById(R.id.textView3);
  
  
          //init image
          image = BitmapFactory.decodeResource(getResources(), R.drawable.test_image3);
  
          //initialize Tesseract API
          String language = "eng";
          datapath = getFilesDir()+ "/tesseract/";
          mTess = new TessBaseAPI();
  
          checkFile(new File(datapath + "tessdata/"));
  
          mTess.init(datapath, language);
  
          //run the OCR on the test_image...
          runOCR.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  processImage();
              }
          });
  
          //Add the extracted info from Business Card to the phone's contacts...
          openContacts.setOnClickListener(new View.OnClickListener(){
              @Override
              public void onClick(View v) {
                  addToContacts();
              }
          });
      }
  
      public void processImage(){
          String OCRresult = null;
          mTess.setImage(image);
          OCRresult = mTess.getUTF8Text();
          //displayText.setText(OCRresult);
          extractName(OCRresult);
          extractEmail(OCRresult);
          extractPhone(OCRresult);
      }
  
      public void extractName(String str){
          System.out.println("Getting the Name");
          final String NAME_REGEX = "^([A-Z]([a-z]*|\\.) *){1,2}([A-Z][a-z]+-?)+$";
          Pattern p = Pattern.compile(NAME_REGEX, Pattern.MULTILINE);
          Matcher m =  p.matcher(str);
          if(m.find()){
              System.out.println(m.group());
              displayName.setText(m.group());
          }
      }
  
      public void extractEmail(String str) {
          System.out.println("Getting the email");
          final String EMAIL_REGEX = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";
          Pattern p = Pattern.compile(EMAIL_REGEX, Pattern.MULTILINE);
          Matcher m = p.matcher(str);   // get a matcher object
          if(m.find()){
              System.out.println(m.group());
              displayEmail.setText(m.group());
          }
      }
  
      public void extractPhone(String str){
          System.out.println("Getting Phone Number");
          final String PHONE_REGEX="(?:^|\\D)(\\d{3})[)\\-. ]*?(\\d{3})[\\-. ]*?(\\d{4})(?:$|\\D)";
          Pattern p = Pattern.compile(PHONE_REGEX, Pattern.MULTILINE);
          Matcher m = p.matcher(str);   // get a matcher object
          if(m.find()){
              System.out.println(m.group());
              displayPhone.setText(m.group());
          }
      }
  
      private void checkFile(File dir) {
          //directory does not exist, but we can successfully create it
          if (!dir.exists()&& dir.mkdirs()){
              copyFiles();
          }
          //The directory exists, but there is no data file in it
          if(dir.exists()) {
              String datafilepath = datapath+ "/tessdata/eng.traineddata";
              File datafile = new File(datafilepath);
              if (!datafile.exists()) {
                  copyFiles();
              }
          }
      }
  
      private void copyFiles() {
          try {
              //location we want the file to be at
              String filepath = datapath + "/tessdata/eng.traineddata";
  
              //get access to AssetManager
              AssetManager assetManager = getAssets();
  
              //open byte streams for reading/writing
              InputStream instream = assetManager.open("tessdata/eng.traineddata");
              OutputStream outstream = new FileOutputStream(filepath);
  
              //copy the file to the location specified by 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();
  
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
      private void addToContacts(){
  
          // Creates a new Intent to insert a contact
          Intent intent = new Intent(ContactsContract.Intents.Insert.ACTION);
           // Sets the MIME type to match the Contacts Provider
          intent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
  
          //Checks if we have the name, email and phone number...
          if(displayName.getText().length() > 0 && ( displayPhone.getText().length() > 0 || displayEmail.getText().length() > 0 )){
              //Adds the name...
              intent.putExtra(ContactsContract.Intents.Insert.NAME, displayName.getText());
  
              //Adds the email...
              intent.putExtra(ContactsContract.Intents.Insert.EMAIL, displayEmail.getText());
              //Adds the email as Work Email
              intent .putExtra(ContactsContract.Intents.Insert.EMAIL_TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK);
  
              //Adds the phone number...
              intent.putExtra(ContactsContract.Intents.Insert.PHONE, displayPhone.getText());
              //Adds the phone number as Work Phone
              intent.putExtra(ContactsContract.Intents.Insert.PHONE_TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK);
  
              //starting the activity...
              startActivity(intent);
          }else{
              Toast.makeText(getApplicationContext(), "No information to add to contacts!", Toast.LENGTH_LONG).show();
          }
  
  
      }
  
  
  }
  