First, the recent use of the generation of QR code technology, hereby recorded
This article is about the qrcode of Denso.
Second, knowledge preparation
2.1. Introduction to QRCode Basic properties
1. Symbol Specifications
From version 1 (21x21 module) to version (177x177 module), add 4 modules per side for each additional version.
2. Data type and capacity (refer to Maximum specification symbol version 40-L):
Numeric data: 7,089 character (s)
Letter data: 4,296 character (s)
8-bit byte data: 2,953 character (s)
Kanji data: 1,817 character (s)
3, Data representation method:
The Dark module represents the binary "1" and the light module represents the binary "0".
4, error correction ability (with the symbol specification, the higher the error correction ability, the lower the two-dimensional code capacity):
L-level: about 7% of the data code Word can be corrected
M-class: approx. 15% Data code Word Error
Q-level: About 25% of the data code Word can be corrected
H-Class: About 30% of the data code Word can be corrected
2.2. Maven Dependency
<dependency>
<groupId>qrcode</groupId>
<artifactId>qrcode</artifactId>
<version>1.0</version>
</dependency>
Third, generate two-dimensional code BufferedImage object
/*** @Description: Generate BufferedImage Object *@paramcontent * QR Code storage information *@paramErrorcorrect * Fault tolerance level * Indicates the length of the string: Fault tolerance (ECC) display encoding mode (ENCODEMODE) and versions (version) about the error-correcting level of two-dimensional code (error rate) , * Total four levels: Optional L (7%), M (15%), Q (25%), H (30%) (Max h). * Error correction information is also stored in the QR code, the higher the error correction level, the more space for error correction information, then the less useful information can be stored, * the smaller the requirements of the definition of QR code *@paramMode * Encoding mode: Numeric digit, alphanumeric English alphabet, binary binary, Kanji * Kanji (First capital letter) * /c0>@paramVersion * QR code versions number two-dimensional code version number: Also symbolizes the two-dimensional code of the information capacity; the QR code can be seen as a black-and-white grid matrix, different versions, * The total number of long-width squares of the matrix is different. 1-40 A total of 40 versions, version 1 is the 21*21 matrix, the version of each 1, the two-dimensional code two edge length of 4; * Version 2 is the 25x25 module, the highest version is 40, is the matrix of 177*177;@return * @throwsIOException*/ Public StaticBufferedImage Createqrcode (String content,CharErrorcorrect,CharModeintVersionthrowsIOException {bufferedimage image=NULL; if(NULL= = Content | | "". Equals (content)) { } Else{qrcode QRCode=NewQRCode (); Qrcode.setqrcodeerrorcorrect (Errorcorrect); Qrcode.setqrcodeencodemode (mode); Qrcode.setqrcodeversion (version); //gets the byte array of the content, sets the encoding format byte[] bytes = Content.getbytes ("UTF-8"); //The size of the picture, it will be larger according to version, and you need to calculate intImgsize = + * (version-1); Image=Newbufferedimage (imgsize, imgsize, bufferedimage.type_byte_binary); //Get brushesGraphics2D GS =Image.creategraphics (); //set the background color to whiteGs.setbackground (Color.White); Gs.clearrect (0, 0, Imgsize, imgsize); //set Image Color BlackGs.setcolor (Color.Black); //setting the offset, not setting may result in a two-dimensional code production error (Error parsing failed) intPixoff = 2; //Two-dimensional code output if(Bytes.length > 0 && bytes.length < 120) { Boolean[] s =Qrcode.calqrcode (bytes); for(inti = 0; i < s.length; i++) { for(intj = 0; J < S.length; J + +) { if(S[j][i]) {//Note that J * 3 + Pixoff, I * 3 + Pixoff, 3, 3, Pixoff and 3 also affect the two-dimensional code pixels, but the impact is not very large,//Two-dimensional code pixels are primarily affected by versionGs.fillrect (J * 3 + Pixoff, I * 3 + Pixoff, 3, 3);//Fill Rectangle area}}}} gs.dispose (); Image.flush (); } returnimage; }
Four, generate two-dimensional code image
/*** QR code output to file *@paramimage Two-dimensional code content *@paramformat picture formats such as JPG, GIF, etc. *@paramfile Output Files *@return * */ Public StaticFile WriteToFile (bufferedimage image,string format,file file)throwsioexception{if(NULL==file) {File=NewFile ("E:" +file.separator+ "xxx.xxx");//own default storage path} imageio.write (image, format, file); returnfile; }
V. Conversion into a stream
If you need to upload to a picture server, you need to convert bufferedimage into stream form
/*** @Description: convert bufferedimage-> output stream *@paramimage Two-dimensional code content *@paramformat picture formats such as JPG, GIF, etc. *@paramOutputStream Output Stream *@return * @throwsIOException*/ Public StaticOutputStream WriteToStream (bufferedimage image,string format,outputstream outputstream)throwsioexception{if(NULL==OutputStream) {OutputStream=NewBytearrayoutputstream (); } imageio.write (Image,format, OutputStream); returnOutputStream; }
Using QRCode to generate a QR code (JAVA)