Barcode research-QR code

Source: Internet
Author: User

Introduction:

Now, I have a variety of QR codes, especially when I catch the subway to work every day. There are QR codes in the subway station, and there are QR codes in the bottom right corner of the train ticket. In fact, the QR code we often use here is a QR code, which was invented by the Japanese and a type of QR code.

Generally, the structure of a QR code is as follows. There are three positioning marks, which are verification marks in the lower right corner, and some timing marks, version information, and format information that cannot be identified by the naked eye, however, special devices can read data.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/12261521G-0.png "title =" 44.png" alt = "164007282.png"/>

For information about the QR code, see the following article:

Http://zh.wikipedia.org/wiki/QR%E7%A2%BC



Practice:

Here we use JAVA code to generate a QR code image and how to restore the original text content from the QR code image. We still use the ZXing Library:


Package com. charles. study. barcode. twodimensions; import java. awt. image. bufferedImage; import java. io. file; import java. util. hashMap; import java. util. hashtable; import java. util. map; import javax. imageio. imageIO; import com. google. zxing. barcodeFormat; import com. google. zxing. binaryBitmap; import com. google. zxing. decodeHintType; import com. google. zxing. encodeHintType; import com. google. zxing. luminanceSource; import com. google. zxing. multiFormatReader; import com. google. zxing. multiFormatWriter; import com. google. zxing. result; import com. google. zxing. client. j2se. bufferedImageLuminanceSource; import com. google. zxing. client. j2se. matrixToImageWriter; import com. google. zxing. common. bitMatrix; import com. google. zxing. common. hybridBinarizer; import com. google. zxing. qrcode. decoder. errorCorrectionLevel;/*** this tool class is used to process the encoding/decoding QR code * @ author charles. wang **/public class TwoDimensionCodeUtil {private TwoDimensionCodeUtil () {}/ *** generates a QR code based on a string with the specified width and height, and stored in the file at the specified location * @ param contents * @ param width * @ param height * @ param fileName * @ throws Exception */public static void generate2DBarCodeImage (String contents, int width, int height, String fileName) throws Exception {// here the input parameter check is omitted because it is not the focus // configure the information of the Q-R QR code Hashtable hints = new Hashtable (); // define the error correction level hints. put (EncodeHintType. ERROR_CORRECTION, ErrorCorrectionLevel. l); // specify the hints encoding rule for the QR code. put (EncodeHintType. CHARACTER_SET, "UTF-8"); // create the Bitmap matrix information corresponding to the QR code. BitMatrix bitMetrix = new MultiFormatWriter (). encode (contents, BarcodeFormat. QR_CODE, width, height, hints); // obtain the File Format String fileFormat = fileName from the input parameter. substring (fileName. lastIndexOf (". ") + 1, fileName. length (); // create the File object File outputFile = new File (fileName) from the input parameter; // write the Bitmap matrix to the MatrixToImageWriter in the specified image File. writeToFile (bitMetrix, fileFormat, outputFile);}/*** read a QR code image file, restore the original information * @ param imagePath * @ return */public static String resolve2DBarCodeImage (String imagePath) {BufferedImage image = null; try {// read the QR code image file image = ImageIO. read (new File (imagePath); if (image = null) {System. out. println ("the QR code image does not exist. ");} LuminanceSource source = new BufferedImageLuminanceSource (image); BinaryBitmap bitmap = new BinaryBitmap (new HybridBinarizer (source); // set the QR code decoding rule Map <DecodeHintType, string> hints = new HashMap <DecodeHintType, String> (); hints. put (DecodeHintType. CHARACTER_SET, "UTF-8"); // the Result obtained by decoding the image file based on the QR code result = new MultiFormatReader (). decode (bitmap, hints); // obtain the original text return result from the result. getText ();} catch (Exception e) {e. printStackTrace ();} return null;} public static void main (String [] args) {// QR code Demo // generate a QR code try {String qr_contents = "Charles QR code example "; int qr_width = 200; int qr_height = 200; generate2DBarCodeImage (qr_contents, qr_width, qr_height, "D:/Framework Study/BarCode/TwoDimensions/newQRcode.png");} catch (Exception ex) {System. err. print ("error generating QR code");} // parse the QR code String qr_original = resolve2DBarCodeImage ("D:/Framework Study/BarCode/TwoDimensions/newQRcode.png"); System. out. println ("restored information:" + qr_original );}}



In this experiment, when a QR code is generated, it will generate a QR code image file in D:/Framework Study/BarCode/TwoDimensions/newQRCode.png in the following format:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/122615Aa-1.png "title =" 45.png" alt = "164448883.png"/>

We can view the QR code image file. Obviously, the features mentioned in this document conform to the features of the QR code we introduced at the beginning:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/122615K35-2.png "title =" 46.png" alt = "1645420.3.png"/>


Then we run the following parsing process, and the console displays:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/1226155039-3.png "title =" 47.png" alt = "164628880.png"/>

It indicates that our code is completely correct.


I also used my colleague's mobile phone to scan the QR code I generated here. It will show the "Charles QR code sample" text on the mobile phone.


Benefits:

Here we provide an online website for customizing the QR code:

Http://cli.im/


This article from "parallel line cohesion" blog, please be sure to keep this source http://supercharles888.blog.51cto.com/609344/1341190

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.