1, first need Qrcode.jar package
2, write a test class
Package com.lj.test;
Import Java.awt.Color;
Import Java.awt.Graphics2D;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import Javax.imageio.ImageIO;
Import Com.swetake.util.Qrcode; public class Test2 {/** * @param args */public static void main (string[] args) {String Imgpath = "e://luojie/1
. png ";
String content = "Hello";
Test2 handler = new Test2 ();
Handler.encoderqrcode (content, Imgpath);
SYSTEM.OUT.PRINTLN ("Encoder qrcode success");
public void Encoderqrcode (string content, String Imgpath) {try {qrcode Qrcodehandler = new QRCode ();
Qrcodehandler.setqrcodeerrorcorrect (' M ');
Qrcodehandler.setqrcodeencodemode (' B ');
Qrcodehandler.setqrcodeversion (7);
SYSTEM.OUT.PRINTLN (content);
byte[] Contentbytes = content.getbytes ("Utf-8");
BufferedImage bufimg = new BufferedImage (140, 140, BUFFEREDIMAGE.TYPE_INT_RGB);
graphics2d GS = Bufimg.creategraphics ();
Gs.setbackground (Color.White);
Gs.clearrect (0, 0, 140, 140); // Set Image Color > Black gs.setcolor (Color.Black);
Setting offset without setting may cause parsing error int pixoff = 2; Output > Two-dimensional code if (Contentbytes.length > 0 && contentbytes.length <) {boolean[][] codeout = QR
Codehandler.calqrcode (contentbytes); for (int i = 0; i < codeout.length. i++) {for (int j = 0; J < Codeout.length; J +) {if (codeout[j][i))
{Gs.fillrect (J * 3 + Pixoff, I * 3 + Pixoff, 3, 3); else {System.err.println ("qrcode content bytes Length = + contentbytes.length +" not in
[0,120]. ");
} gs.dispose ();
Bufimg.flush ();
File Imgfile = new file (Imgpath);
Generate two-dimensional Code qrcode picture Imageio.write (bufimg, "PNG", imgfile);
catch (Exception e) {e.printstacktrace ();
}
}
}
3, run this class, and the two-dimensional code will appear in the file directory you specified.