Package com. liuxingyu;
Import java. awt. Color;
Import java. awt. Graphics2D;
Import java. awt. Image;
Import java. awt. image. BufferedImage;
Import java. io. File;
Import javax. imageio. ImageIO;
Import com. swetake. util. Qrcode;
Public class Dimensional {
/**
* Generate a QR code Image
* @ Param content the content of the QR code Image
* @ Param imgPath: generate the complete path of the QR code Image
* @ Param ccbpath the logo path in the middle of the QR code Image
*/
Public static int createQRCode (String content, String imgPath, String ccbPath ){
Try {
Qrcode qrcodeHandler = new Qrcode ();
QrcodeHandler. setQrcodeErrorCorrect ('M ');
QrcodeHandler. setQrcodeEncodeMode ('B ');
QrcodeHandler. setQrcodeVersion (7 );
// System. out. println (content );
Byte [] contentBytes = content. getBytes ("gb2312 ");
BufferedImage bufImg = new BufferedImage (140,140, BufferedImage. TYPE_INT_RGB );
Graphics2D gs = bufImg. createGraphics ();
Gs. setColor. WHITE );
Gs. clearRect (0, 0,140,140 );
// Set the image color> BLACK
Gs. setColor (Color. BLUE );
// Parsing error may occur if the offset is not set.
Int pixoff = 2;
// Output content> QR code
If (contentBytes. length> 0 & contentBytes. length <120 ){
Boolean [] [] codeOut = qrcodeHandler. 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].");
Return-1;
}
Image img = ImageIO. read (new File (ccbPath); // instantiate an Image object.
Gs. drawImage (img, 40, 40, null); // set the position of the Avatar in the QR code. We recommend that you set the image size to a specific value.
Gs. dispose ();
BufImg. flush ();
// Generate a QR code QRCode Image
File imgFile = new File (imgPath );
ImageIO. write (bufImg, "png", imgFile );
} Catch (Exception e ){
E. printStackTrace ();
Return-100;
}
Return 0;
}
Public static void main (String [] args ){
String imgPath = "D:/Workspaces/2013/Dimensional_code/Web root/WEB-INF/img/wab.png ";
String content = "I'm liuxingyu. Where are you? ";
String ccbPath = "D:/Workspaces/2013/dimensional_codes/WebRoot/WEB-INF/img/cc.png ";
CreateQRCode (content, imgPath, ccbPath );
System. out. println ("encode success ");
}
}
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/150I16432-0.jpg "width =" 140 "alt =" a2cc7cd98d1001e9c3435e3dba0e7bec54e79781 "/>