Using Python to invoke the Zxing library to generate two-dimensional code images

Source: Internet
Author: User

(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

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.