Python zxing Library Parsing (barcode Two-dimensional code recognition)

Source: Internet
Author: User

Various scan code software

Recently to do a two-dimensional code recognition of the project, find two-dimensional code to identify a lot of open source of non-open source software

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

Zbar

First try to Zbar,python load Zbar when a variety of error. The possible reason is that the Zbar DLL file is 32 bits, and my system is 64 bits, so it can't run. Can only take the source code to compile a 64-bit out, for my hand this party to compile their own source code is difficult to accept, so give up. later, the article said that Zbar could not identify the skewed barcode, and could not locate the barcode area.

Zxing

Zbar is not OK, just try zxing. It's easy to find the Zxing page on GitHub.

https://github.com/zxing/zxing/

And then discover that zxing also has a Python version:

A burst of excitement, point in (https://github.com/oostendo/python-zxing), found the author's description of this Python package is:A quick and dirty wrapper for the ZXing Barcode Library . (a fast and simple zxing shell)

The Zxing folder and several other files are downloaded, the following began to toss this shabby package.

Build a Python zxing project

Create a new Pydev project in Eclipse and copy the Zxing folder in.

Because the file contains __init__.py, the folder is automatically recognized as a package.

Tests.py is a routine that calls zxing to identify barcodes. A Test_codereader () function is called in the main function to identify the barcode in the next picture sample.png of the current directory.

and Test_codereader () invokes the Decode () function in the zxing package developed for Python.

ZX = Barcodereader ()

Barcode = Zx.decode (testimage)

To open the __init__.py, there is barcodereader () the implementation code of this class.

Python Call jar Package

In the Decode function, the operation to invoke the jar package is implemented with the Popen in the subprocess package, similar to the pipeline communication in VB,VC.

(stdout, stderr) = subprocess. Popen (cmd, stdout=subprocess. PIPE, universal_newlines=true). Communicate ()

The above sentence is the core code that calls the jar package, and CMD is the command that is sent to the jar package. The type of CMD is list, each element is a parameter, and the command that is finally entered into the 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 on how to use subprocess online, and the main problem here is the command itself that calls the jar package.

The problems and solutions that arise

Since it is a routine, the hands of the hand of course directly run, but stuck in the subprocess. Popen () Here, the reported error is that the Java main class cannot be loaded.

1. Error: The main class could not be found or could not be loaded Com.google.zxing.client.j2se.CommandLineRunnercmd

Then the cmd command output, this command is JAVA–CP packet path/jar registration. jar Java class name.

The first problem is that there are no two jar packages (Javase.jar;core.jar) in my directory that are specified in the program, and the path is not correct. So from the web search to the address of the two packages download down (of course, you can also download zxing source manual compilation).

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

Download zxing two Jar pack articles: http://blog.csdn.net/rongbo_j/article/details/47055795

"Zxing jar package we can get from Maven repository, open maven repository http://www.mvnrepository.com/, search for zxing"

After downloading the jar package, change the name to Core.jar and Javase.jar (in order to get rid of the following string of numbers with the code), copy to the Zxing folder in the project just now.

Note that in the original program, the jar package was added to the directory libs = ["Javase/javase.jar", "Core/core.jar"]

We need to change this sentence to libs = ["Javase.jar", "Core.jar"], is so simple and rude.

Try again, or not, the output of CMD is:

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

The path to the jar package becomes : /javase.jar, .... /core.jar, this slash and Dot Plus, the directory is wrong.

Then find the code to add a slash to the path:
libraries = [Self.location + "/" + L for L in Self.libs]

Remove the slash and change it to

libraries = [L for L in Self.libs]

This removes the two points and slashes in the path and runs again to meet the new problem.

2, Noclassdeffounderror:com/beust/jcommander/jcommander

Specific information is 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

Can not find the Jcommander class, although still have problems, but the main class finally found ...

Check it out, the original Jcommander is a Java command-line parsing class, not included in the Zxing jar package. It needs to be downloaded separately and placed in the project directory.

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

Links in the 5 floor of the response compared to the force, because given the Jcommander: Http://central.maven.org/maven2/com/beust/jcommander/1.48/jcommander-1.48.jar

Put the jar package in the Zxing file and add the package to Lib , i.e.:

Libs = ["Javase.jar", "Core.jar", "Jcommander-1.48.jar"]

Now the cmd command has become:

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

The program runs successfully, but the barcode information is not output, and we can manually output it when debugging. The information returned is stored in the file_results and is output.


It can be seen that the QR code corresponds to a URL, you can use the mobile phone sweep to verify the right.

Barcodes can also be

Found a barcode of the picture, put in the directory to try, can also be identified.

This article source

The source code for this article (including several jar files) has been uploaded to Csdn:

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

Python zxing Library Parsing (barcode Two-dimensional code recognition)

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.