(1) Installation Jpype
Calling the jar package with Python requires the installation of the Jpype extension, which can be installed directly on Ubuntu using the apt-get jpype extension
$ sudo apt-get install Python-jpype
about how to invoke a jar package using Jpype. See http://blog.csdn.net/niuyisheng/article/details/9002926 (2) Get zxing jar package using zxing third-party library to generate two-dimensional code images, The introduction to Zxing can be seen on its GitHub address: https://github.com/zxing/zxing/. We are able to download the source code to compile the installation package. It is also possible to download jar packages directly online, and my jar packages are downloaded directly, such as the following:
$ wget http://repo1.maven.org/maven2/com/google/zxing/javase/2.2/javase-2.2.jar$ wget http://repo1.maven.org/ Maven2/com/google/zxing/core/2.2/core-2.2.jar
About the use of the Zxing library. Ability to view http://mygirl1314520.iteye.com/blog/1912109
(3) Calling the jar using Python
The following uses the Zxing library to generate a qr_code image of the QR code:
#!/usr/bin/python#-*-encoding:utf-8-*-from jpype import *# start JVMSTARTJVM (Getdefaultjvmpath (), "-ea", ("- djava.class.path=%s "%"./javase-2.2.jar:./core-2.2.jar ")) # Load the type you need to use Multiformatwriter = Jclass (" Com.google.zxing.MultiFormatWriter ") Barcodeformat = Jclass (" Com.google.zxing.BarcodeFormat ") Bitmatrix = Jclass (" Com.google.zxing.common.BitMatrix ") File = Jclass (" Java.io.File ") BufferedImage = Jclass (" Java.awt.image.BufferedImage ") ImageIO = Jclass (" Javax.imageio.ImageIO ") Bytearrayoutputstream = Jclass (" Java.io.ByteArrayOutputStream ") Matrixtoimagewriter = Jclass (" Com.google.zxing.client.j2se.MatrixToImageWriter ") Encodehinttype = Jclass ("Com.google.zxing.EncodeHintType") Hashtable = Jclass ("java.util.Hashtable") Strtoencode = " This is a testing string "# Setting Margin=0hints = Hashtable () hints.put (encodehinttype.margin, 0) matrix = multiformatwriter (). Encode (Strtoencode, Barcodeformat.qr_code, 260, 260, hints) image = Matrixtoimagewriter.tobufferedimage (matrix) Imageio.write (Image, "PNG", File ("tesT.png ")) # Close JVMSHUTDOWNJVM ()
(4) Implementation
The execution program gets pictures such as the following. can use QR Code scanning Tool to get the information stored in the QR code.
References:
How to use Http://blog.csdn.net/niuyisheng/article/details/9002926:jpype
How to use the Http://mygirl1314520.iteye.com/blog/1912109:zxing library
Using Python to invoke the Zxing library to generate two-dimensional code images