Original v:http://www.cnblogs.com/bigroc/p/7496995.html#3797682
Topic: Java uses Qrcode.jar to generate and parse two-dimensional code demo welcome Novice encouragement, the great God supervision
# do not know QRCode please visit the wiki, self-understanding, there is not much to explain
Working ******** before creating a QR code
Go to the address given below to download the Qrcode.jar package, which already includes the build and parse.
The jar package downloaded to the official website is not resolved.
Https://files.cnblogs.com/files/bigroc/QRCode.zip
Create your test class and get the jar package started. * * * The first part: Generate two-dimensional code
Package Com.bigroc.qrcode;import Com.swetake.util.qrcode;import Javax.imageio.imageio;import java.awt.*;import Java.awt.image.bufferedimage;import Java.io.file;import Java.io.ioexception;import java.io.unsupportedencodingexception;/** * Created by Bigroc on 2017/9/8. */public class Createqrcode {public static void main (string[] args) throws IOException {//api document specifies how to calculate the height of a picture, 7 is The version number of this test is int width = 67+12* (7-1); int height = 67+12* (7-1); QRCode x = new QRCode (); X.setqrcodeerrorcorrect (' M ');//Error correction level X.setqrcodeencodemode (' B ');//(N: Number) (A:a~z,a~z) (B: Other) X.setqrcodeversio N (7);//version number 1-40 String qrdata = "www.bigroc.xyz";//content information byte[] D = qrdata.getbytes ("Utf-8");//man to format need to throw an exception Buffer BufferedImage bufferedimage = new BufferedImage (width, height, bufferedimage.type_int_bgr); Plot graphics2d GS = Bufferedimage.creategraphics (); Gs.setbackground (Color.White); Gs.setcolor (Color.Black); Gs.clearrect (0, 0, width, height); offset int pixoff = 2; /** * Easy to step on the pit * Note the order of the i,j inside the For Loop, * S[j][i] The order of the j,i of the two-dimensional array to be with the Gs.fillrect in this method (J*3+pixoff,i *3+pixoff, 3, 3); * Order matching, otherwise the parsing picture is a string of numbers */if (d.length > 0 && d.length <) {Boolean[][] s = x.c Alqrcode (d); for (int i = 0, i < s.length; i++) {for (int j = 0; J < S.length; J + +) {if (S[j] [i]) {Gs.fillrect (J * 3 + Pixoff, I * 3 + Pixoff, 3, 3); }}}} gs.dispose (); Bufferedimage.flush (); Imageio.write (BufferedImage, "PNG", New File ("D:/qrcode.png")); System.out.println ("Two-dimensional code generation completed"); }}
The second part: parsing the QR code (note: It is necessary to implement the Qrcodeimage interface)
Package Com.bigroc.qrcode;import Jp.sourceforge.qrcode.qrcodedecoder;import Javax.imageio.imageio;import Java.awt.image.bufferedimage;import java.io.file;import java.io.ioexception;/** * Created by BigRoc on 2017/9/8. */public class Readqrcode {public static void Main (string[] args) throws IOException { //Picture path File File = new File ("D:/qrcode.png"); Read picture to buffer bufferedimage bufferedimage = imageio.read (file); QRCode decoder Qrcodedecoder codedecoder = new Qrcodedecoder (); /** *codedecoder.decode (New Myqrcodeimage ()) * Here to implement the Qrcodeimage interface, the last piece of code //////by parsing the QR code to obtain information string result = new String (Codedecoder.decode (new Myqrcodeimage (BufferedImage)), "Utf-8"); SYSTEM.OUT.PRINTLN (result);} }
Part III: Implementing the Qrcodeimage Interface
Package Com.bigroc.qrcode;import Jp.sourceforge.qrcode.data.qrcodeimage;import java.awt.image.bufferedimage;/** * Implement the Qrcodeimage interface, * Set the decoded picture information * Tell Codedecoder.decode () the type of picture to be parsed * Created by Bigroc on 2017/9/9. */public class Myqrcodeimage implements qrcodeimage{ bufferedimage bufferedimage; Public Myqrcodeimage (BufferedImage bufferedimage) { this.bufferedimage=bufferedimage; } Wide @Override public int getwidth () { return bufferedimage.getwidth (); } High @Override public int getheight () { return bufferedimage.getheight (); } Pixel or color @Override public int getPixel (int i, int j) { return Bufferedimage.getrgb (I,J); }}
Java uses Qrcode.jar to generate and parse two-dimensional code