Python zxing library parsing (barcode QR code identification), pythonzxing

Source: Internet
Author: User

Python zxing library parsing (barcode QR code identification), pythonzxing
Various code scanning software

I recently want to build a QR code recognition project. I found that there are many open-source and non-open-source software for QR code recognition.

Http://www.oschina.net/project/tag/238/

Zbar

First, I tried Zbar. Various errors are reported when python loads ZBar. The possible cause is that the dll file of zbar is 32-bit, and my system is 64-bit, so it cannot run. I can only compile a 64-bit source code by myself. It is unacceptable for me to compile the source code by myself, so I gave up.Later, I saw the article saying that Zbar cannot identify the skewed bar code and cannot locate the bar code area.

Zxing

Zbar is not feasible. Just try zxing. It is easy to find the zxing page on github.

Https://github.com/zxing/zxing/

Then we found that zxing has python versions:

A burst of excitement, click in (https://github.com/oostendo/python-zxing), found the author's description of this python package is:A quick and dirty wrapper forZXing barcode library. (A fast and simple zxing shell)

Download the zxing folder and several other files. The following is a simple package.

Create a Python Zxing Project

Create a PyDev project in Eclipse and copy the zxing folder.

Because the file _ init _. py is included, the folder is automatically recognized as a package.

Tests. py is a routine that calls zxing to identify the bar code. The main function calls the test_codereader () function to identify the bar code in the sample.png image in the current directory.

Test_codereader () calls the decode () function in the zxing package developed for python.

Zx = BarCodeReader ()

Barcode = zx. decode (testimage)

Turn on _ init _. py, which contains the implementation code of the BarCodeReader () class.

Python calls the jar package

In the decode function, the Popen In the subprocess package is used to call the jar package, which is similar to the pipeline communication in VB and VC.

(Stdout, stderr) = subprocess. Popen (Cmd, Stdout = subprocess. PIPE, universal_newlines = True). communicate ()

The above statement is the core code used to call the jar package. cmd is the command sent to the jar package. The type of cmd is list, and each element is a parameter. The command format entered to jar is as follows.

['Java', '-cp ','.; javase. jar; core. jar; jcommander-1.48.jar ', 'com. google. zxing. client. j2se. commandLineRunner ', 'sample.png']

There are a lot of tutorials online on how to use subprocess. The main problem here is that the command to call the jar package itself.

Problems and Solutions

Since it is a routine, it runs directly, but is stuck in subprocess. Popen (), the error is that the java main class cannot be loaded.

1, Error: unable to find or load main class com. google. zxing. client. j2se. CommandLineRunnercmd

So I output the cmd command, which is the java-cp package path/jar registration. jar java class name.

The first problem I found is that there are no jar packages specified in the program in my directory (Javase. jar; core. jar), And the path is incorrect. So I found the addresses of the two packages on the Internet to download them (of course, you can also download the source code of zxing for manual compilation ).

About how to compile zxing: http://blog.sina.com.cn/s/blog_76adb0160102uxqb.html

Download articles for two zxing jar packages: http://blog.csdn.net/rongbo_j/article/details/47055795

"We can obtain the zxing jar package from the maven resource library. Open the maven resource library http://www.mvnrepository.com/and search for zxing"

After downloading the jar package, change the name to core. jar and javase. jar (to work with the code, remove the following number) and copy it to the zxing folder in the project just now.

Note that the directory is added to the jar package in the original program.Libs = ["javase/javase. jar", "core/core. jar"]

We need to change this sentenceLibs = ["javase. jar", "core. jar"],It's so simple and rude.

Try again. The output cmd is:

['Java', '-cp ','.. /javase. jar ;.. /core. jar ', 'com. google. zxing. client. j2se. commandLineRunner ', 'sample.png']

The path of the jar package in is changed../Javase. jar; ../core. jar,This slash and Dot are added, and the directory is incorrect.

Then find the code for adding a slash to the path:
Libraries = [self. location + "/" + l for l in self. libs]

Delete the slash and change it

Libraries = [l for l in self. libs]

In this way, the dot and slash in the path are removed and run again to meet new problems.

2. NoClassDefFoundError: com/beust/jcommander/JCommander

The details are as follows:

Exception in thread "main" java. lang. NoClassDefFoundError: com/beust/jcommander/JCommander
At com. google. zxing. client. j2se. CommandLineRunner. main (CommandLineRunner. java: 52)
Caused by: java. lang. ClassNotFoundException: com. beust. jcommander. JCommander
At java.net. URLClassLoader. findClass (Unknown Source)
At java. lang. ClassLoader. loadClass (Unknown Source)
At sun. misc. Launcher $ AppClassLoader. loadClass (Unknown Source)
At java. lang. ClassLoader. loadClass (Unknown Source)
... 1 more

The JCommander class cannot be found. Although there is still a problem, the main class is finally found...

Check that JCommander is a Java command line parsing class and is not included in the zxing jar package. You need to download it separately and put it in the project directory.

Reference: http://stackoverflow.com/questions/30435688/getting-java-lang-noclassdeffounderror-com-beust-jcommander-parameterexception

The reply on the fifth floor in the link is more powerful, because the JCommander: http://central.maven.org/maven2/com/beust/jcommander/1.48/jcommander-1.48.jar is given.

Put the jar package in the zxing file and add the package to lib., That is:

Libs = ["javase. jar", "core. jar", "jcommander-1.48.jar"]

Now the cmd command is changed:

['Java', '-cp', 'javase. jar; core. jar; jcommander-1.48.jar', 'com. google. zxing. client. j2se. CommandLineRunner ', 'sample.png']

The program runs successfully, but the bar code information is not output. You can manually output it during debugging. The returned information is stored in file_results and output.


We can see that this QR Code corresponds to a Web site. You can scan it on your mobile phone to verify if it is correct.

The bar code can also be used.

I found a bar code image, put it in the directory, and tried it. It can also be identified.

Source code

The source code of this project (including the jar files) has been uploaded to CSDN:

Http://download.csdn.net/detail/sheep7777777/9733896

Related Article

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.