Java two-dimensional code generation and resolution sample code _java

Source: Internet
Author: User
Tags getmessage int size set background

Two-dimensional code, is a black-and-white plane geometry through the corresponding coding algorithm to record text, pictures, URLs and other information of the barcode picture. The following figure

Characteristics of two-dimensional codes:

1. High density coding, large information capacity

Can accommodate up to 1850 uppercase letters or 2,710 digits or 1108 bytes, or more than 500 Chinese characters, more than ordinary bar code information capacity about dozens of times times higher.

2. Wide range of Codes

The bar code can be the picture, sound, text, signature, fingerprint, such as digital information can be encoded, with bar code to express; can represent multiple languages, can represent image data.

3. Strong fault-tolerant ability, with error correction function

This makes two-dimensional barcode due to perforation, defaced and other causes of local damage, can be correctly read, damage area of up to 50% can still recover information.

4. High decoding reliability

It is much lower than the common bar code decoding error rate two out of 10,000, the BER does not exceed one out of 10,000.

5. Encryption measures may be introduced

Confidentiality, good security.

6. Low cost, easy production, durable

Because of these characteristics, two-dimensional code is now more and more popular, application is more and more widely (see Baidu Encyclopedia, Introduction is not the focus), so master how to develop two-dimensional code is very good knowledge reserves, so this article will explain how to generate, parse two-dimensional code.

One, Java

Jar Package Required: Qrcode.jar

http://sourceforge.jp/projects/qrcode/

Twodimensioncode class: Two-dimensional code operation core class

Package qrcode;
Import Java.awt.Color;
Import Java.awt.Graphics2D;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import java.io.IOException;
Import Java.io.InputStream;

Import Java.io.OutputStream;

Import Javax.imageio.ImageIO;
Import Jp.sourceforge.qrcode.QRCodeDecoder;

Import jp.sourceforge.qrcode.exception.DecodingFailedException;

Import Com.swetake.util.Qrcode; public class Twodimensioncode {/** * Generate two-dimensional code (qrcode) picture * @param Content Store Contents * @param imgpath picture path/public void E
 Ncoderqrcode (string content, String Imgpath) {This.encoderqrcode (content, Imgpath, "PNG", 7); /** * Generate a two-dimensional code (qrcode) picture * @param content Storage Contents * @param output stream/public void Encoderqrcode (String content, out
 Putstream output) {this.encoderqrcode (content, Output, "PNG", 7); /** * Generate two-dimensional code (qrcode) picture * @param Content Store Contents * @param imgpath Picture path * @param imgtype Picture Type * * public void ENCODERQ Rcode (string content, String Imgpath, String imgtype) {This.encoderqrcode (contenT, Imgpath, Imgtype, 7); /** * Generate two-dimensional code (qrcode) picture * @param content storage * @param output stream * @param imgtype Picture Type * * public void ENCODERQRC
 Ode (string content, OutputStream output, string imgtype) {this.encoderqrcode (content, output, imgtype, 7);
 /** * Generate two-dimensional code (qrcode) picture * @param content storage * @param imgpath picture path * @param imgtype Picture type * @param size two-dimensional code dimensions * * public void Encoderqrcode (string content, String Imgpath, string imgtype, int size) {try {bufferedimage bufimg = this.
 
 Qrcodecommon (content, imgtype, size);
 File Imgfile = new file (Imgpath);
 Generate two-dimensional Code qrcode picture Imageio.write (bufimg, Imgtype, imgfile);
 catch (Exception e) {e.printstacktrace ();
 /** * Generate two-dimensional code (qrcode) picture * @param content storage * @param output stream * @param imgtype Picture type * @param size two-dimensional code dimensions * *  public void Encoderqrcode (string content, OutputStream output, string imgtype, int size) {try {bufferedimage bufimg =
 This.qrcodecommon (content, imgtype, size); Generate two-dimensional code qrcode picture ImageIO.Write (bufimg, imgtype, Output);
 catch (Exception e) {e.printstacktrace (); /** * Generate a two-dimensional code (qrcode) Picture of the public method * @param content Storage * @param imgtype Picture type * @param size two-dimensional code size * @return/pri
 Vate bufferedimage Qrcodecommon (string content, string imgtype, int size) {BufferedImage bufimg = null;
 try {qrcode Qrcodehandler = new QRCode ();
 Set the two-dimensional code error rate, optional L (7%), M (15%), Q (25%), H (30%), the higher the rate of error, the less information can be stored, but the requirements for two-dimensional code clarity of the smaller qrcodehandler.setqrcodeerrorcorrect (' M ');
 Qrcodehandler.setqrcodeencodemode (' B ');
 Set up the two-dimensional code size, the value range 1-40, the larger the value of the larger size, the more information can be stored qrcodehandler.setqrcodeversion (size);
 Gets the byte array of the content, sets the encoding format byte[] contentbytes = content.getbytes ("Utf-8");
 Picture size int imgsize = + (size-1);
 bufimg = new BufferedImage (imgsize, imgsize, Bufferedimage.type_int_rgb);
 graphics2d GS = Bufimg.creategraphics ();
 Set Background color gs.setbackground (color.white);

 Gs.clearrect (0, 0, imgsize, imgsize);
 Set Image Color > Black gs.setcolor (Color.Black);
 Set offset, no setting may cause parsing error int pixoff = 2; Output Content &GT Two-dimensional code if (Contentbytes.length > 0 && contentbytes.length <) {boolean[][] codeout = QRCODEHANDLER.CALQRC
 Ode (contentbytes); for (int i = 0; i < codeout.length. i++) {for (int j = 0; J < Codeout.length; J +) {if (Codeout[j][i]) {Gs.fil
 Lrect (J * 3 + Pixoff, I * 3 + Pixoff, 3, 3);
 else {throw new Exception ("qrcode content bytes Length =" + Contentbytes.length + "not in [0, 800].");
 Gs.dispose ();
 Bufimg.flush ();
 catch (Exception e) {e.printstacktrace ();
 return bufimg; /** * Parse two-dimensional code (QRCODE) * @param imgpath Picture Path * @return */public String Decoderqrcode (String imgpath) {//QRCode
 Two-dimensional code picture file ImageFile = new files (imgpath);
 BufferedImage bufimg = null;
 String content = null;
 try {bufimg = Imageio.read (ImageFile);
 Qrcodedecoder decoder = new Qrcodedecoder (); 
 Content = new String (Decoder.decode (New Twodimensioncodeimage (bufimg)), "Utf-8"); catch (IOException e) {System.out.println ("Error:" + e.getmessage ());
 E.printstacktrace ();
 catch (Decodingfailedexception DfE) {System.out.println ("Error:" + dfe.getmessage ());
 Dfe.printstacktrace ();
 } return content; /** * Parse two-dimensional code (QRCODE) * @param input stream * @return/public String Decoderqrcode (InputStream input) {Bufferedim
 Age bufimg = null;
 String content = null;
 try {bufimg = Imageio.read (input);
 Qrcodedecoder decoder = new Qrcodedecoder (); 
 Content = new String (Decoder.decode (New Twodimensioncodeimage (bufimg)), "Utf-8");
 catch (IOException e) {System.out.println ("Error:" + e.getmessage ());
 E.printstacktrace ();
 catch (Decodingfailedexception DfE) {System.out.println ("Error:" + dfe.getmessage ());
 Dfe.printstacktrace ();
 } return content;
 public static void Main (string[] args) {String Imgpath = "G:/tddownload/michael_qrcode.png"; String encodercontent = "Hello big, small, Welcome to qrcode!" + "\nmyblog [http://sjsky.iteye.com]" + "\nemail [Sjsky007@gma
 Il.com] "; Twodimensioncode handler = new TwodiMensioncode ();
Handler.encoderqrcode (Encodercontent, Imgpath, "PNG"); try {//OutputStream output = new FileOutputStream (imgpath);//Handler.encoderqrcode (content, output);//} catch (Ex
 
 
 Ception e) {//E.printstacktrace ();//} System.out.println ("========encoder success");
 String decodercontent = Handler.decoderqrcode (Imgpath);
 System.out.println ("The analytic result is as follows:");
 System.out.println (decodercontent);
 System.out.println ("========decoder success!!!"); }
}

twodimensioncodeimage class: Two-dimensional code Picture Object

Package qrcode;

Import Java.awt.image.BufferedImage;

Import Jp.sourceforge.qrcode.data.QRCodeImage;

public class Twodimensioncodeimage implements Qrcodeimage {

 bufferedimage bufimg;
 
 Public Twodimensioncodeimage (BufferedImage bufimg) {
 this.bufimg = bufimg;
 }
 
 @Override public
 int getheight () {return
 bufimg.getheight ();
 }

 @Override public
 int getpixel (int x, int y) {return
 Bufimg.getrgb (x, y);
 }

 @Override public
 int getwidth () {return
 bufimg.getwidth ();
 }

}

The above is the Java two-dimensional code generation and analysis of data collation, follow-up continue to add, thank you for your support of this site!

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.