In fact, when writing this thing, some of the details of the configuration of the problem, I will no longer write in detail, only write the relevant main source code:
Package com.tz.util;
Import Java.awt.Color;
Import Java.awt.Graphics2D;
Import Java.awt.Image;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import java.io.IOException;
Import java.io.UnsupportedEncodingException;
Import Javax.imageio.ImageIO;
Import com.swetake.util.qrcode;//introduced the package to my own
/**
* Generate two-dimensional code
* @author Yanlong
* Content two-dimensional code contents
* Path of Imgpath QR code
* return void type returned
*
*/
public class Qrcodeimg {
A method of generating a QR code
public static void Getqrcodeimg (String content,string imgpath) {
Instantiating a QRCode object
QRCode qcqrcode=new QRCode ();
Coding
Qcqrcode.setqrcodeencodemode (' B ');
The size of the error rate 15%
Qcqrcode.setqrcodeerrorcorrect (' M ');
Version
Qcqrcode.setqrcodeversion (15);
int width=235;
int height=235;
Flower Board
BufferedImage image=new BufferedImage (WIDTH,HEIGHT,BUFFEREDIMAGE.TYPE_INT_BGR);
Drawing Tools
Graphics2D Gs=image.creategraphics ();
Start drawing
Background color
Gs.setbackground (Color.White);
Draw a rectangle
Gs.clearrect (0, 0, width, height);
Set the color of the content
Gs.setcolor (Color.Black);
Start working on our information
Byte[] Codeout;
try {
Codeout =content.getbytes ("Utf-8");
Returns an array of Boolean types through byte.
Boolean[][] Code=qcqrcode.calqrcode (codeout);
for (int i=0;i<code.length;i++) {
for (int j=0;j<code.length;j++) {
if (Code[j][i]) {
If it's true, paint it black.
Gs.fillrect (J*3+2, I*3+2, 3, 3);
}
}
}
Loading pictures
File File=new file ("C:/users/yanlong/desktop/123456.png");
Image srcimage=imageio.read (file);
int _width=srcimage.getwidth (NULL);
int _heigth=srcimage.getheight (NULL);
Gs.drawimage (Srcimage, (width-_width)/2, (height-_heigth)/2,_width,_heigth,null);
Freeing resources
Gs.dispose ();
Image.flush ();
Save------, write to the specified path
Imageio.write (Image, "PNG", New File (Imgpath));
System.out.println ("Two-dimensional code generation Success");
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
Main method
public static void Main (string[] args) {
Getqrcodeimg ("Although I believe that pay and harvest is not necessarily proportional, but I believe Tiandaochouqin!" ---Chen Yanlong "," c:/users/yanlong/desktop/3.png ");
}
}
————————————————————————————————————————————————————————————
Two-dimensional code
Java generates two-dimensional code