Two-dimensional code: java generate two-dimensional code

1. The  two-dimensional code introduced

Forage analysis:

We are now commonly used in daily life generally refers to two-dimensional code QR code

QR Code is a two-dimensional code in the code system, invented by the Japanese in 1994 DENSO WAVE.

Dimensional bar code means an extended bar code readable another dimension, the pattern in black and white rectangles represent binary data, after the device is scanned to obtain information contained therein on the basis of the one-dimensional bar code. Width of one-dimensional bar recorded data, and its data length is not described. Dimensional bar code length, width data are recorded. There is a two-dimensional bar-dimensional bar code not "fix" and "fault tolerance." Fault tolerance even when there is no recognition to all of the bar code, bar code or that have defaced, you can correctly restore the information on the bar code. Many different types of two-dimensional bar code, the different institutions to develop a two-dimensional bar code has a different structure and writing, reading method.

 

In short, two-dimensional code is to use some of the information in accordance with certain principles box to record shows.

Two-dimensional code is divided into static code and live code:

Static code: is fixed information, using a mobile phone to a fixed scanning recognition will only get text information.

Live code: that is, to save a url , use the phone scanning recognition will jump to the url page or application corresponding to the information obtained can be greatly increased, and has considerable flexibility. So called live code.

2. The  two-dimensional code Principle

Specific principles, see here: https://www.cnblogs.com/alantu2018/p/8504373.html

3.  the Java generate two-dimensional code picture

To java produce two-dimensional code picture projects, generally a direct reference to jar package can be. Now there are two main options,

One is qrcode official website ( https://www.supershareware.com/qrcode-jar-free/ ) Download QrCode.jar ,

Another option is to download the song Valley ( http://repo1.maven.org/maven2/com/google/zxing/core/ ) of Zxing.jar

 

Personal experience Google's Zxing.jar than QrCode.jar relatively easy to use, but Zxing.jar can resolve features like bar codes. Therefore, it is recommended to download Zxing.jar use.

 

Here is a java a simple application generates a two-dimensional picture of the code:

 

  1 import java.io.ByteArrayOutputStream;
  2 
  3 import java.io.FileOutputStream;
  4 
  5 import java.io.IOException;
  6 
  7 import java.io.OutputStream;
  8 
  9 import java.util.HashMap;
 10 
 11 import java.util.Map;
 12 
 13  
 14 
 15 import com.google.zxing.BarcodeFormat;
 16 
 17 import com.google.zxing.EncodeHintType;
 18 
 19 import com.google.zxing.MultiFormatWriter;
 20 
 21 import com.google.zxing.WriterException;
 22 
 23 import com.google.zxing.client.j2se.MatrixToImageWriter;
 24 
 25 import com.google.zxing.common.BitMatrix;
 26 
 27 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
 28 
 29  
 30 
 31 /**
 32 
 33  * 二维码工具类
 34 
 35  * @author limingcheng
 36 
 37  *
 38 
 39  */
 40 
 41 public class QrCodeUtil {
 42 
43 is   
44 is  
45  / ** 
46 is  
47  * to generate a two-dimensional code image
 48  
49  * @param width
 50  
51 is  * @param height
 52 is  
53 is  * @param Content
 54 is  
55  * @return 
56 is  
57 is  * @throws WriterException
 58  
59  * @throws IOException
 60  
61 is   * / 
62 is  
63 is  public  static  byte [] createQRCode ( int width,int height, String Content) throws WriterException, IOException {
 64  
65  // dimensional code set the basic parameters 
66  
67 the Map <EncodeHintType, Object> = hints new new the HashMap <EncodeHintType, Object> ();
 68  
69 hints.put (EncodeHintType.CHARACTER_SET , "UTF-. 8"); // set-coded character set. 8 UTF 
70  
71 is hints.put (EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); // set the error correction level L / M / Q / H, the error correction level high recognition more difficult, the current level is the highest level set H 
72  
73 is hints.put (EncodeHintType.MARGIN, 0); // can be set in the range of 0-10, but only four changes 01 (2) 3 (456 ) 7 (8910)
 74  
75 // generates picture type is QRCode 
76  
77 BarcodeFormat the format = BarcodeFormat.QR_CODE;
 78  
79  // create a bitmap matrix objects 
80  
81 BitMatrix bitMatrix = new new MultiFormatWriter () encode (Content, the format, width, height, hints);.
 82  
83  / / set bit matrix transpose picture parameter
 84  
85  //         MatrixToImageConfig config = new new MatrixToImageConfig (Color.black.getRGB (), Color.white.getRGB ());
 86  
87  // bit stream objects object-matrix 
88  
89 ByteArrayOutputStream OS = new new ByteArrayOutputStream ();
 90  
91 is MatrixToImageWriter.writeToStream(bitMatrix, "png", os);
 92 
 93 return os.toByteArray();
 94 
 95 }
 96 
 97  
 98 
 99 public static void main(String[] args) throws WriterException, IOException {
100 
101 byte[] b = createQRCode(100, 100, "遇见最好的自己!");
102 
103 OutputStream os = new FileOutputStream("E:\\bestme.png");
104 
105 os.write(b);
106 
107 os.close();
108 
109 }
110 
111 }

 

 

Generate two-dimensional code picture is as follows:

 

 

Guess you like

Origin www.cnblogs.com/bestlmc/p/11846639.html