Java generates QR code image codes

Source: Internet
Author: User

First you need to import the Qrcode.jar package

Look at this Http://pan.baidu.com/s/1o6qRFqM.

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;

public class Qrcodeutil {
public static void Main (string[] args) {

Generate two-dimensional code
String content = "http://user.qzone.qq.com/913240046";
String Imgpath = "F:\\test.png";
String ccpath= "F:\\center.png";
Createqrcode (content, Imgpath,ccpath);

String Content2 = "http://user.qzone.qq.com/913240046";
String imgPath2 = "F:\\test2.png";
Qrcodeimg (CONTENT2,IMGPATH2);

}

/**
* @param content
* @param imgpath to generate the address of the image of the QR code
* @param ccbpath Two-dimensional code middle picture
* @return
*/
public static int Createqrcode (string content, String Imgpath, String ccbpath) {
try {
QRCode Qrcodehandler = new QRCode ();
Set the two-dimensional code error rate, optional L (7%), M (15%), Q (25%), H (30%), the higher the error rate can be stored less information, but the two-dimensional code clarity requirements of the smaller
Qrcodehandler.setqrcodeerrorcorrect (' Q ');
n represents the number, a means the character a-z,b represents the other character
Qrcodehandler.setqrcodeencodemode (' B ');
Set the two-dimensional code version, the value range of 1-40, the larger the size of the larger, the more information can be stored
int size = 12;
Qrcodehandler.setqrcodeversion (size);
int imgsize = + * (size-1);

byte[] Contentbytes = content.getbytes ("Utf-8");
BufferedImage bufimg = new BufferedImage (imgsize, imgsize, Bufferedimage.type_int_rgb);
graphics2d GS = Bufimg.creategraphics ();

Gs.setbackground (Color.White);
Gs.clearrect (0, 0, imgsize, imgsize);

Set Image Color > BLACK
Gs.setcolor (Color.Black);

Setting offset does not set may cause parsing errors
int pixoff = 2;
Output content > QR code
if (contentbytes.length > 0 && contentbytes.length <800) {
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,800].");
return-1;
}
Image img = imageio.read (new File (Ccbpath));//Instantiate an Image object.
Gs.drawimage (IMG,,, or null);//Set the location of the middle picture of the QR code


Gs.dispose ();
Bufimg.flush ();

Generate two-dimensional code QRCode pictures
File Imgfile = new file (Imgpath);
Imageio.write (bufimg, "PNG", imgfile);

} catch (Exception e)
{
E.printstacktrace ();
return-100;
}
return 0;
}

/**
* @param content QR code included
* @param imgpath Two-dimensional code generation and save Road strength
*/
public static void Qrcodeimg (String content,string imgpath) {
try {

QRCode QRCode = new QRCode ();
Qrcode.setqrcodeerrorcorrect (' M ');
Qrcode.setqrcodeencodemode (' B ');
Qrcode.setqrcodeversion (7);

int width = 140;
int height = 140;

BufferedImage bufimg = new BufferedImage (WIDTH,HEIGHT,BUFFEREDIMAGE.TYPE_INT_RGB);
graphics2d GS = Bufimg.creategraphics ();
Gs.setbackground (Color.White);
Gs.clearrect (0, 0, width, height);
Gs.setcolor (Color.Black);


byte[] Contentbytes = content.getbytes ("Utf-8");
int pixoff = 2;

Length limit
if (contentbytes.length>0 && contentbytes.length<120) {
boolean[][] Codeout = Qrcode.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.OUT.PRINTLN ("error");
}
Gs.dispose ();
Bufimg.flush ();

File Imgfile = new file (Imgpath);
try {
Imageio.write (bufimg, "PNG", imgfile);
} catch (IOException e) {
E.printstacktrace ();
}
} catch (Unsupportedencodingexception e) {
E.printstacktrace ();
}
}

}

Generated two-dimensional code picture

Java generates QR code image codes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.