Two-dimensional code recognition of Android full compiler Zbar

Source: Internet
Author: User

Copyright NOTICE: Reprint must indicate this article transferred from Zhangjie's blog: http://blog.yanzhenjie.com

Probably just do Android development time has done two-dimensional code scanning, then know less things, found out the zxing and Zbar two libraries. Zxing is pure Java code implementation, for Android platform, Zbar is C implementation, can be used in many languages and platforms, such as Java, iOS platform, Android platform, Python and so on. Obviously Zbar recognition rate and speed are obviously faster than zxing, but helpless at that time will not compile Zbar, had to download zxing, but because of the lack of technical ability, for zxing custom Cut box also do not come out, had to download others compiled good zbar, Maybe because someone else changed the code or the compilation is not very complete, the late bug layer out, waste a good great strength to perfect.

Later had no chance to learn QR code scanning, until the last few days need to give our platform app plus QR code scanning function, I decided to use Zbar, so I compiled a complete, today to record this process, I hope to help the students need.

For example, the use of zxing, but I am sure that they have modified a lot of source code, and there are many places should be changed to JNI implementation, so the recognition rate and accuracy is quite high, but today I compiled the package is second seconds can be identified.

Because Zbar is based on LGPL-2.1 open source, so I'm based on the LGPL-2.1 protocol, I put a complete project source code and sample on GitHub, provide the function of directly calling Zbar identification byte[] data and calling the camera to recognize the QR code:
Https://github.com/yanzhenjie/android-zbar-sdk

Compiling Zbar

before formal compilation, be aware that compiling zbar requires compiling libiconv, compiling LIBICONV requires a Linux environment, and needs to use GCC. If you don't have a Linux environment, it doesn't matter, I've already provided the compiled Libiconv.

In fact, in Zbar's official website can also be downloaded to their compiled so and jar, but so file they only provide armeabi , armeabi-v7a , x86 platform:
Https://sourceforge.net/projects/zbar/files/?source=navbar

So I abandoned the provided compilation package, compiled by myself, the following is the step.

First download the Zbar source code on Zbar's Open source homepage:
Https://github.com/ZBar/ZBar

By the way on the Open Source home page Open Android folder, found that compiling Zbar need libiconv, next download Libiconv:
Http://www.gnu.org/software/libiconv

For Libiconv I was downloaded in 2017-02-02 when the latest version of 1.15 was released.

First, compile Libiconv

If you do not have a Linux environment to compile Libiconv, then you can download here I have compiled well libiconv1.15 :
http://download.csdn.net/detail/yanzhenjie1003/9833225, after a good file, you can skip this section directly, see below Zbar and Libiconv compiled together .

If you have a Linux environment where you can compile Libiconv, continue looking down.
After downloading the good libiconv, enter the Libiconv folder, if the report permission error can not go into sudo chmod 777 -R libiconv the execution on it:

Execute: ./configure If prompted without permission, execute: sudo chmod 777 configure , and then re /.configure -execute.

./configureafter execution, execute the make command to complete the compilation

The following errors may be encountered at compile time:
1, Configure:error:no acceptable C compiler found in $PATH
This means you do not have GCC installed, and after installing GCC, do not complete the command again.

Second, Zbar and Libiconv compile together

Libiconv compiled, then put Zbar and Libiconv together to compile the so file we need.

    1. Put the libiconv you just compiled into the JNI folder of our project.
    2. Unzip the Zbar that you just downloaded, first put the Zbar header file folder zbar/include into our project's JNI folder.
    3. Zbar the Java interface file zbarjni.c into the Jni folder of our project, ZBRJNI.C under the zbar/java folder.
    4. Place the folder where the Zbar core library files are located zbar/zbar under the Jni folder of our project.
    5. Copy the Zbar Android.mk Applicaiton.mk config.h from zbar\android\jni next to the Jni folder of our project.

At this point the Jni folder for our project is this:

Theoretically now you can start compiling, but because we have changed the folder structure of Zbar, so we want to Android.mk make changes, the main change is the folder path and file path, the modified Android.mk content is as follows:

My_local_path: =$ (call My-dir)# Libiconvinclude $(Clear_vars) Local_path: =$ (my_local_path) Local_module: = libiconvlocal_cflags: =  -wno-multichar  -d_android  -dlibdir= "C"  -dbuilding_libiconv  -dbuilding_libcharset  -din_librarylocal_src_fil ES: =  libiconv-1.15/lib/iconv.c  libiconv-1.15/libcharset/lib/localcharset.c  Libicon V-1.15/lib/relocatable.clocal_c_includes: =  $(Local_path)/libiconv-1.15/include    $ (local_path)/libiconv-1.15/libcharset  $(Local_path)/libiconv-1.15/libcharset/includeinclude$ (build_shared_library) Local_ldlibs: =-llog-lcharset# --------------------------------------------- --------# Libzbarinclude $(Clear_vars) Local_path: =$ (my_local_path) Local_module: = Zbarlocal_src_files: =Zbarjni.cZbar/img_scanner.cZbar/decoder.cZbar/image.cZbar/symbol.cZbar/convert.cZbar/config.cZbar/scanner.cZbar/error.cZbar/refcnt.cZbar/video.cZbar/video/null.cZbar/decoder/code128.cZbar/decoder/code39.cZbar/decoder/code93.cZbar/decoder/codabar.cZbar/decoder/databar.cZbar/decoder/ean.cZbar/decoder/i25.cZbar/decoder/qr_finder.cZbar/qrcode/bch15_5.cZbar/qrcode/binarize.cZbar/qrcode/isaac.cZbar/qrcode/qrdec.cZbar/qrcode/qrdectxt.cZbar/qrcode/rs.cZbar/qrcode/util.clocal_c_includes: =$(Local_path)/include            $ (local_path)/zbar  $(Local_path)/libiconv-1.15/includelocal_shared_libraries: = Libiconvinclude$ (build_shared_library)

Then fill in the Application.mk platform you want to compile, if you want to compile all:

APP_ABI := all

If you want to specify a number of platforms to compile, place the platform name in a space-delimited sequence:

APP_ABI := armeabi armeabi-v7a x86 x86_64 mips mips_64 arm64_v8a

At this point we use the command line to enter the project's JNI folder parent road, such as the general JNI in the case of the Jni folder is located ProjectName/ModuleName/src/main/jni , then we enter this main , and then execute ndk-build to compile.

If the prompt does not have ndk-build this command, then you need to download the NDK from http://developer.android.com and configure path on your computer.

ndk-buildafter execution will be libs generated under the so folder of all platforms, folders are required and so libiconv zbar file.

Troubleshooting errors encountered while compiling Zbar and Libiconv

The following errors may be found during compilation, as modified by the modification scheme.

1. libiconv-1.15/jni/libcharset/lib/localcharset.c:51:24:error:langinfo.h:no such file or directory
Open the Libiconv-1.15/libcharset/config.h file, search #define HAVE_LANGINFO_CODESET , about 14 lines, put this line commented:

#define HAVE_LANGINFO_CODESET 1 */

2 、... C undeclaired ...
Open libiconv-1.15/libcharset/lib/localcharset.c, search for the function get_charset_aliases() , about 124 lines.

About 195 rows or so, there's one int c; (you can search without int c; it), move the line to the get_charset_aliases() beginning:
Before moving:

After moving:

Zbar's jar Package

Now that the so file has, the rest is how to call the function in the so to identify the barcode/two-D code, first the zbar/java next in the net.sourceforge.zbar package and inside the Java file copied to your project java directory, the approximate structure is as follows:

Of course you also use the source code like this, you can also pack these classes into a jar package.

Call Zbar to identify the QR code

Now that all has been compiled and the jar file is available, we can call the encapsulated method in the jar to identify the QR code:

byte  [] ImageData = ...; image barcode = new  Image (size.width, Size.Height,  " Y800 ""); Barcode.setdata (ImageData); //specifies the area of the QR code in the picture, or it can not be specified to identify the full image.  //barcode.setcrop (StartX, starty, width, height);  String qrcodestring = null ; int  result = mimagescanner.scanimage (barcode); if  (Result! = 0 )    {Symbolset Symset = Mimagescanner.getresults (); for  (Symbol sym:symset) qrcodestring = Sym.getdata ();} if  (! Textutils.isempty (qrcodestring)) {//succeeded in identifying the QR code, qrcodestring is the data. }

How to use the camera in combination with other complex operations no longer said, a complete project I put on GitHub:
Https://github.com/yanzhenjie/android-zbar-sdk

Mountain high water far, Lake Lakes Goodbye!

Copyright NOTICE: Reprint must indicate this article transferred from Zhangjie's blog: http://blog.yanzhenjie.com

Two-dimensional code recognition of Android full compiler Zbar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.