Small ape search, homework to help similar effects. It is implemented based on Google tesseract-ocr. Since it is developed based on C++, it cannot be directly used in Android. Therefore, this project uses Tess -two as a branch of Android.

The preparatory work

Android Studio gradle dependencies

// The compiled SO library and jar package
compile 'com. Rmtheis: Tess - two: 6.1.1'
// Image clipping
compile 'com. Edmodo: cropper: 1.0.1'Copy the code

In fact, you can also download github.com/rmtheis/tes… The source code is compiled in a Linux environment.

2) The second way to compile the source code (!!)

  • Install the VMware VM
  • Install Ubuntu for Linux
  • Install necessary Tools
sudo apt-get update
sudo apt-get install gitCopy the code
  • Configure JDK, NDK, SDK environment (step 10,000 holes)
  • Download the Tess – Two code
git clone https://github.com/rmtheis/tess-two.git tessCopy the code
  • Begin to compile
cd /tess/tess-two/jni

ndk-buildCopy the code
  • Copy the compiled Tess – Two directory to your project’s libraries and associate the app with the project’s libraries.

Tess – two use

  • Download the data dictionary, create an Assets directory in app/main, and go to github.com/tesseract-o… Download the dictionary library for the corresponding language.

  • Mainactivity. Java basically writes dictionary files from assets to the phone’s file directory

/** * Copy files from assets to **@param path
 */
public void deepFile(String path) {
    String newPath = getExternalFilesDir(null) + "/";
    try {
        String str[] = getAssets().list(path);
        if (str.length > 0) {// If it is a directory
            File file = new File(newPath + path);
            file.mkdirs();
            for (String string : str) {
                path = path + "/" + string;
                deepFile(path);
                path = path.substring(0, path.lastIndexOf('/'));// Return to the original path}}else {// If it is a file
            InputStream is = getAssets().open(path);
            FileOutputStream fos = new FileOutputStream(new File(newPath + path));
            byte[] buffer = new byte[1024];
            int count = 0;
            while (true) {
                count++;
                int len = is.read(buffer);
                if (len == -1) {
                    break;
                }
                fos.write(buffer, 0, len); } is.close(); fos.close(); }}catch(IOException e) { e.printStackTrace(); }}Copy the code

Click to jump to the photo taking interface

public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_camera:
                Intent intent = new Intent(this, TakePhoteActivity.class);
                startActivity(intent);
                break; }}Copy the code

  • Takephoteactivity. Java mainly performs photo taking and cropping operations.

The following is the back interface after a successful photo taking, and the clipping interface is displayed after a successful photo taking

 /** * Callback * store the picture and display the screenshot interface **@param data
     */
    @Override
    public void onCameraStopped(byte[] data) {
        Log.i("TAG"."==onCameraStopped==");
        // Create an image
        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        // System time
        long dateTaken = System.currentTimeMillis();
        // Image name
        String filename = DateFormat.format("yyyy-MM-dd kk.mm.ss", dateTaken).toString() + ".jpg";
        // Store images (PATH directory)
        Uri source = insertImage(getContentResolver(), filename, dateTaken, PATH, filename, bitmap, data);
        // Prepare screenshots
        try {
            mCropImageView.setImageBitmap(MediaStore.Images.Media.getBitmap(this.getContentResolver(), source));
        } catch (IOException e) {
            Log.e(TAG, e.getMessage());
        }
        showCropperLayout();
    }Copy the code

Clipping operation, jump to the recognition interface after completion

private View.OnClickListener cropcper = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_closecropper:
                    showTakePhotoLayout();
                    break;
                case R.id.btn_startcropper:
                    // Get the screenshot and rotate it 90 degrees
                    Bitmap cropperBitmap = mCropImageView.getCroppedImage();
                    Bitmap bitmap = Utils.rotate(cropperBitmap, -90);

                    // System time
                    long dateTaken = System.currentTimeMillis();
                    // Image name
                    String filename = DateFormat.format("yyyy-MM-dd kk.mm.ss", dateTaken).toString() + ".jpg";
                    Uri uri = insertImage(getContentResolver(), filename, dateTaken, PATH, filename, bitmap, null);

                    Intent intent = new Intent(TakePhoteActivity.this, ShowCropperedActivity.class);
                    intent.setData(uri);
                    intent.putExtra("path", PATH + filename);
                    intent.putExtra("width", bitmap.getWidth());
                    intent.putExtra("height", bitmap.getHeight());
// intent.putExtra("cropperImage", bitmap);
                    startActivity(intent);
                    bitmap.recycle();
                    finish();
                    break; }}};Copy the code
  • ShowCropperedActivity. Java is mainly operating

Initialize the recognizer

/ / sd card path
private static String LANGUAGE_PATH = "";
// Recognize the language
private static final String LANGUAGE = "chi_sim";//chi_sim | eng
private TessBaseAPI baseApi = new TessBaseAPI();

@Override
protected void onCreate(Bundle savedInstanceState) {
    LANGUAGE_PATH = getExternalFilesDir("") + "/";

    baseApi.init(LANGUAGE_PATH, LANGUAGE);
    // Set the mode
    baseApi.setPageSegMode(TessBaseAPI.PageSegMode.PSM_AUTO);
}Copy the code

Gray image processing to remove noise can improve accuracy

/** * grayscale processing **@param bitmap3
 * @return* /
public Bitmap convertGray(Bitmap bitmap3) {
    colorMatrix = new ColorMatrix();
    colorMatrix.setSaturation(0);
    ColorMatrixColorFilter filter = new ColorMatrixColorFilter(colorMatrix);

    Paint paint = new Paint();
    paint.setColorFilter(filter);
    Bitmap result = Bitmap.createBitmap(bitmap3.getWidth(), bitmap3.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(result);

    canvas.drawBitmap(bitmap3, 0.0, paint);
    return result;
}Copy the code

Begin to identify

// Pass in the image
baseApi.setImage(bitmap);
// Get the result of recognition
String result = baseApi.getUTF8Text();
// End the identification
baseApi.end();Copy the code

The results of

Demo has been uploaded to Github. If necessary, download github.com/wangtaoT/An…