Delphi Android integrates ZXing Barcode
This way, the customer experience is not good enough. Can you integrate zx directly into the App? The answer is yes, but the actual operation will be a little cumbersome. Look at the process, which is divided into two parts: the first part introduces how to configure and compile zx source code, and the second part introduces how to integrate with App.
Part 1: Configuration and compilation of zx source code
1: Download the source code
I chose the 2.3.0 version, after downloading and decompressing it, as shown below:
We only need three folders, of which
core: core library files.
android-integration: The built-in integration library, which is used when compiling.
Android: Comes with sample scanner (zx).
2.Package the core library as a jar
Here I first package core and android-integratio into jars to use, you can also use the source code directly. Open eclipse, create a new Android Application Project, the name can be whatever you want, no need to create Activity, after completion, create a new src folder under the project, copy the entire com folder in core to src, right-click the project, select Export, and select in the window JAR file in Java:
Just select the src folder, and the export name here is ZXCore2.3.jar. After android-integratio does the same operation, you get ZXIntegration2.3.jar and keep it for later use.
Copy files to the Android folder (the name can be taken by yourself), open eclipse, create Android from existing code, select the above folder, after completion, there will be CaptureActivity (zx) in the project, and then import the two jar files packaged above, as follows The picture shows:
Build, if there is no error message, the environment has been configured successfully.
4.The source code is slightly modified so that delphi can call back normally
a. Open CaptureActivity.java, comment out the onCreateOptionsMenu event, temporarily hide the menu to make it look more like a module (you can also keep it, but the resource files of related functions must be released and the corresponding Activity needs to be added in AndroidManifest (if involved To)).
b. Locate the onResume event: Comment the following two pieces of code:
Now rebuild the project or run it once and wait for it to be integrated with the App.
The second part App integration zx
- Integrate resources
Create a new Firemonkey Mobile App and create the following folder in the root directory:
Copy the files in res in CaptureActivity to capture_assets (in this example, the capture_assets folder stores some help files, which can be deleted if they are not used).
2.Create a local callback function
Create NativeActivitySubclass.java to call back the onActivityResult event after zx completes the scan. This class inherits from delphi NativeActivity and registers a Native callback method in it. The detailed process of generating NativeActivitySubclass.java from NativeActivitySubclass.dex will not be repeated here. To talk about the general process, first restore the classes.dex that comes with delphi to embarcadero.jar, use javac to bring embarcadero.jar and android.jar environment to compile NativeActivitySubclass.java to get NativeActivitySubclass.class, and use jar cvf command to package NativeActivitySubclass.class Into a jar, and finally use dx to generate NativeActivitySubclass.dex.
Here you need to merge three files, Delphi’s own classes.dex, NativeActivitySubclass.dex, and a bin\classes.dex in the CaptureActivity project. Use DexMerger to merge the above three dex into one classes.dex, so far the functions have been integrated complete.
In delphi IDE> project> options> uses permission, add permissions as follows:
a: Change the default name to [package].[NativeActivitySubclass] (depending on your name);
b. Add CaptureActivity (only scan function is added here, others can be added as needed).
a. Project> deployment:
Pack Capture_res in the directory to the corresponding directory (you don’t have to pack all the files in, I deleted some multilingual and help files (if your local language is not simplified Chinese, you can add it yourself);
b. Replace the classes.dex that comes with delphi with the integrated classes.dex.
7.Build App, call zx
Now that zx has been integrated, the call is very simple, with only three lines of code (see Demo for detailed calls):
procedure CallInternalZXing; var LIntent: JIntent; begin LIntent := TJIntent.JavaClass.init; LIntent.setClassName(SharedActivityContext, StringToJString('com.google.zxing.client.android.CaptureActivity')); SharedActivity.startActivityForResult(LIntent, 0); end;
App running effect picture (note that the original “barcode scanner” has become our App name):