Last time we implemented a plug-in that was supposed to merge Firefox’s GeckoView as an alternative to WebView, but it didn’t work out, and the dynamic library always failed to load. Strange only strange skill is not deep enough. Tesseract, the heart of Android’s text recognition program, will soon be built. A little trial is required, as noted below.

Use createPackageContext as a plugin

Github : Plugin101、Plugin Invoker 101

Share the Tesseract dynamic library

Chromium.googlesource.com/chromium/sr…

Introduce android Webview loading mechanism, did not understand, only understand “as long as the library APP class, you can load the dynamic library as usual”, so easy.

Github.com/adaptech-cz…

The Tesseract Android version by maven {url “https://jitpack.io”}, API ‘cz. Adaptech: tesseract4android: 4.4.1’ provides a pre-compiled library (can also be compiled), Directly referenced in the plug-in project, the test identifies the image file in the RAW directory. The caller does not need to integrate Tesseract’s Java/c++ code to have high OCR optical recognition capability.

public class TesseractPluginTest {...public static void Test(Context context) {
   //CMN.rt();
   TessBaseAPI tess = new TessBaseAPI();
   
   String dataPath = new File(Environment.getExternalStorageDirectory(), "tesseract").getAbsolutePath();
   
   tess.init(dataPath, "eng");
   
   tess.setImage(getBitmapFromAsset(context, R.raw.text));
   String text = tess.getUTF8Text(); / / plain text
   String textFormatted = tess.getHOCRText(0); // OCR tags HTML, separate each word, various information
   //CMN.pt("test_done", text); // The total time is less than one second, neither too fast nor too slow
   
   Toast.makeText(context, text, 1).show();
   
   tess.recycle();
}
Copy the code

The drawback is that the model files need to be downloaded and placed on the SD card, which means that the caller needs read and write access to the SD card, which can be avoided

Share files in private directories through contentProvider

Reference :(its code… Is the screen too small, or didn’t know there was a shortcut called Shift + Mouse Wheel?)

Access the data files section of the application through the ContentProvider

ContentProvider written in plugin:

public class PluginFileProvider extends ContentProvider {...

@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
   //CMN.Log("PluginFileProvider_openFile::", uri);
   File file = new File(getContext().getExternalFilesDir(null), uri.getPath());
   if (file.exists()) {
      //CMN.Log("PluginFileProvider_openFile::exists");
      return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
   }
   //CMN.Log("PluginFileProvider_openFile::not exist!!" );
   throw new FileNotFoundException(uri.getPath());
}
Copy the code

The authorities/name/getAuthority/URI is the path of the implementation class, and the authorities/URI is the getAuthority.

<application .

<provider
   android:name="com.knziha.plugin101.PluginFileProvider"
   android:authorities="com.knziha.plugin101"
   android:exported="true" >
</provider>
Copy the code

GetExternalFilesDir (/sdcard/Android/data/…) The data under the. TXT. The caller can access and read the contents of data.txt in the plug-in private directory without obtaining the read and write permission of the entire SD card. The Tesseract model files can be downloaded or unzipped to this directory and will be automatically deleted when the plug-in is uninstalled.

public class PrivatePathTest {...public static void Test(Context context) {
   try {
      //File dataFile = new File(context.getExternalFilesDir(null), "data.txt");
      //FileInputStream fint = new FileInputStream(dataFile); / / no
      //FileInputStream fin = context.openFileInput(dataFile.getName()); / / no
      
      //fin.getFD()
      
      Uri uri = Uri.parse("content://com.knziha.plugin101/data.txt");
      InputStream fin = context.getContentResolver().openInputStream(uri);
      
      byte[] data=new byte[512];
      int len=fin.read(data);
      fin.close();
      //CMN.Log("PrivatePathTest::", new String(data, 0, len));
   } catch (Exception e) {
      //CMN.Log(e);}}Copy the code

The next step is to implement the Tesseraction plug-in

To be continued…