Java Two-dimensional code

Source: Internet
Author: User

When HTTP://WWW.OSTOOLS.NET/QR saw a tool to generate a QR code, an idea was created:

Why don't you do a two-dimensional code generation and parsing tools? Spent a lot of time, hey, do it ...

Take a look at it first:

CODE_QR:

code_128:

pdf_417:

Two-dimensional code means:

Here are the steps to proceed:

One: Download the zxing package:

Can be downloaded here: http://code.google.com/p/zxing/downloads/list

zxing-2.1.zip:http://code.google.com/p/zxing/downloads/detail?name=zxing-2.1.zip&can=2&q=

Get:

Zxing-2.1\core\core.jar

Zxing-2.1\javase\javase.jar

Two: Add him to the inside of your project:

/qrcodes/src/com/b510/qrcode/qrcode.java

  1/** 2 * 3 */4 package com.b510.qrcode;  5 6 Import Java.awt.image.BufferedImage;  7 Import Java.io.File;  8 Import java.io.IOException; 9 Import java.util.Hashtable; Ten import Java.util.Map; Import Javax.imageio.ImageIO; Import Com.google.zxing.BarcodeFormat; Com.google.zxing.BinaryBitmap import; Com.google.zxing.DecodeHintType import; + Import Com.google.zxing.EncodeHintType; Import Com.google.zxing.LuminanceSource; Import Com.google.zxing.MultiFormatReader; Import Com.google.zxing.MultiFormatWriter; Com.google.zxing.ReaderException import; Import Com.google.zxing.Result; Com.google.zxing.Writer import; Import com.google.zxing.WriterException; Import Com.google.zxing.client.j2se.BufferedImageLuminanceSource; Com.google.zxing.common.BitMatrix import; Com.google.zxing.common.HybridBinarizer import; Com.google.zxing.oned.CodaBarWriter import; Import Com.google.zxing.oned.Code128Writer; Import com.google.zxing.oned.Code39writer; Import Com.google.zxing.oned.EAN13Writer; Import Com.google.zxing.oned.EAN8Writer; Com.google.zxing.oned.ITFWriter import; Com.google.zxing.oned.UPCAWriter import; Com.google.zxing.pdf417.encoder.PDF417Writer import; Com.google.zxing.qrcode.QRCodeWriter import; 37 38/** 39 * Use zxing Open Source tool to generate QR code qrcode * * * @date 2012-10-26 * @author XHW * * * * * * */public class Qrco De {The private static final int BLACK = 0xff000000; a private static final int white = 0xFFFFFFFF; 48 49/* * @param args * * * public static void Main (string[] args) {qrcode test = new QRCode (); The file File = new file ("C://test.png"); 55/** 56 * In the Com.google.zxing.MultiFormatWriter class, we define some codes that we don't know, and the QR code is just one of them. <br> * Public Bitmatrix Encode (String contents, barcodeformat format, int Widt h, int height, Map<EncodeHintType,?> hints) throws Writerexception {for writer writer; Case ean_8:64 writer = new Ean8writer (); break; ean_13:67 writer = new Ean13writer (); ; upc_a:70 writer = new Upcawriter (); ; Qr_code case://Here is the two-dimensional code of the writer = new Qrcodewriter (); a break; code_39:76 writer = new Code39writer (); a break; code_128 Case://This can be generated by the writer = new Code128writer (); break; Bayi Case itf:82 writer = new Itfwriter (); The break; Case pdf_417://This can generate the Pdf417writer writer = new (); The break; Case CODabar:88 writer = new Codabarwriter (); ; default:91 throw new IllegalArgumentException ("No encoder available for format" + forma T); Writer.encode} (contents, format, width, height, hints); 94} * * * Test.encode ("Helloworld,i ' m hongten.welcome to my zone:http://www.cnblogs.com /hongten ", File, Barcodeformat.qr_code, $, (+), NULL); 98 Test.decode (file); 99}100 101/**102 * Generate qrcode QR code <br> 103 * When encoding requires &L in Com.google.zxing.qrcode.encoder.Encoder.java t;br>104 * Static final String default_byte_mode_encoding = "Iso8859-1"; <br>105 * modified to UTF-8, otherwise Chinese post-compilation parsing is not <br>106 */107 public void encode (String contents, file file, barcodeformat format, int width, int height, Map<encodehinttype,?> hints) {108 try {
//Eliminate garbled characters
contents = new String (contents.getbytes ("UTF-8"), "iso-8859-1");109 Bitmatrix Bitmatrix = new Multiformatwriter (). Encode (contents, format, width, height); Etofile (Bitmatrix, "PNG", file), 111} catch (Exception e) {e.printstacktrace (); 113}114 }115 116/**117 * Generate two-dimensional code picture <br>118 * 119 * @param matrix120 * @param format121 * Picture Format 122 * @param file123 * generate QR Code picture location 124 * @throws IOException125 */126 public Stat IC void WriteToFile (Bitmatrix matrix, String format, file file) throws IOException {127 BufferedImage image = Tobu Fferedimage (matrix); Imageio.write (image, format, file), 129}130 131/**132 * Generate two-dimensional code content <br>133 * 134 * @param matrix135 * @return136 */137 public static BufferedImage Tobufferedimage (Bitmatrix MATRIX) {138 int width = matrix.getwidth (); 139 int height = matrix.getheight (); bufferedimage Image = New BufferedimagE (width, height, bufferedimage.type_int_argb); 141 for (int x = 0; x < width; + +) {142 for (int y = 0; Y < height; y++) {143 Image.setrgb (x, Y, Matrix.get (x, y) = = true? Black:white); 144}145}146 return image;147}148 149/**150 * Parse qrcode QR code 151 */152 @SuppressWarnings ("unchecked") 153 public void decode (file file) {154 try {155 Buffere Dimage image;156 try {157 image = Imageio.read (file); 158 if (image = = null) {1 System.out.println ("Could not decode image");}161 Luminancesour Ce Source = new Bufferedimageluminancesource (image); 162 Binarybitmap bitmap = new Binarybitmap (New HYBRIDB Inarizer (source)) 163 Result result;164 @SuppressWarnings ("Rawtypes") 165 H ashtable hints = new Hashtable (); 166//Decoding settings are encoded as: utf-8167 hints.put (Decodehinttype.character_set, "Utf-8"); 168 result = new Multi Formatreader (). Decode (bitmap, hints); 169 String resultstr = Result.gettext (); System.ou T.PRINTLN ("Parsed content:" + resultstr); 171} catch (IOException IoE) {172 System.out.println (ioe.tost Ring ()); 173} catch (Readerexception re) {174 System.out.println (re.tostring ()); 175 }176} catch (Exception ex) {177 System.out.println (ex.tostring ()); 178}179}180}

is not very simple .....

SOURCE Download: Http://files.cnblogs.com/hongten/QRCodes.rar

Java Two-dimensional code

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.