The internet is mostly eclipse projects, because I use as, so say how to configure.
ID card image information from Baidu.
First, import the relevant documents
In fact, we do not have to compile their own, can be other projects compiled by the jar and so library directly to use. Because the recognition of the effect is only related to the font, to identify what fonts to choose their own library on the line.
1. New Libs folder, import jar Package
2. Create a new Jnilibs folder under the main folder and copy the armxxxx two files to the past.
Note that so library can not be misplaced, otherwise there will be Java.lang.UnsatisfiedLinkError exception.
3. I have provided resources for so library and jar packages on GitHub and can be downloaded and used directly.
Well-configured path:
Second, the example code
1, to realize the recognition function, the mobile phone to put a Tessdata folder, this is our font. Downloads are available on my GitHub project, both in English and Chinese.
2, call the API directly in the code, pass in the font path and font name
3, incoming pictures, get results
4, the recognition takes a little time, in order not to let the app turn on the card to die, I used the handler
5, because to read the root directory of the Tessdata file, so the app should be added to read the SD card permissions, or will be error
public class Mainactivity extends Appcompatactivity {private Bitmap srcbitmap; EditText EditText; String result; Handler Handler; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Handler = new Handler () {@Override public void Handlemessage (Message msg) {Super.han Dlemessage (msg); Edittext.settext (result); } }; EditText = (editText) Findviewbyid (R.id.edittext); Srcbitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.test); New Thread (New Runnable () {@Override public void run () {result = DOOCR (Srcbitmap, "/ Chi_sim "); Message msg = new Message (); Handler.sendmessage (msg); }}). Start (); }/** * for image recognition * * @param bitmap * image to be identified * @param LanguAge * Recognition Language * @return Recognition result String */public string DOOCR (Bitmap Bitmap, String language) {Tes Sbaseapi Baseapi = new Tessbaseapi (); Baseapi.init (Getsdpath (), language); This line must be added, Tess-two requires that BMP must be configured for this bitmap = Bitmap.copy (Bitmap.Config.ARGB_8888, true); Baseapi.setimage (bitmap); String text = Baseapi.getutf8text (); Baseapi.clear (); Baseapi.end (); return text; }/** * Get the path of the SD card * * @return Path of the string */public static string Getsdpath () {File sddir = null; Boolean sdcardexist = Environment.getexternalstoragestate (). Equals (Android.os.Environment.MEDIA_MOU NTED); Determine if the SD card exists if (sdcardexist) {sddir = Environment.getexternalstoragedirectory ();//Get External Memory directory} return sddir.tostring (); }}
Layout file:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: context= "Com.example.binbin.tesseracttest.MainActivity" > <imageview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:background= "@drawable/test" Android:layout_centerhorizonta L= "true" android:layout_margintop= "103DP" android:id= "@+id/imageview"/> <edittext android:l Ayout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "recognition result" android:id= "@+id/ EditText "android:layout_below=" @+id/imageview "Android:layout_centerhorizontal= "true" android:layout_margintop= "99DP"/></relativelayout>
Permissions:
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android: Name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
Three
Finally, the project address on GitHub:
Https://github.com/wzb19960208/TesseractSample
Android Studio Inside Configuration tesseract