Java利用Qrcode生成二维码

1.jar包下载http://ishare.iask.sina.com.cn/download/explain.php?fileid=35428376

package ewm;


import java.io.*;


import java.util.Date;


import java.awt.*;


import java.awt.image.*;


import javax.imageio.*;


import com.swetake.util.Qrcode;


public class QRCodeEncoderTest


{


/** Creates a new instance of QRCodeEncoderTest */


public QRCodeEncoderTest()


{


}


public static void create_image(String name, String info) throws Exception {

try {

Qrcode testQrcode = new Qrcode();

testQrcode.setQrcodeErrorCorrect('M');

testQrcode.setQrcodeEncodeMode('B');

testQrcode.setQrcodeVersion(7);

byte[] d = info.getBytes("gbk");

System.out.println(d.length);

BufferedImage bi = new BufferedImage(98, 98,

BufferedImage.TYPE_INT_RGB);

//BufferedImage bi = new BufferedImage(98, 98,

//BufferedImage.TYPE_BYTE_BINARY);

Graphics2D g = bi.createGraphics();

g.setColor.WHITE);< /span>

g.clearRect(0, 0, 98, 98);

// 设定图像颜色:BLACK

g.setColor(Color.RED);

// 设置偏移量 不设置肯能导致解析出错

int pixoff = 3;

// 输出内容:二维码

// 限制最大字节数为120

if (d.length > 0 && d.length < 120) {

boolean[][] s = testQrcode.calQrcode(d);

for (int i = 0; i < s.length; i++) {


for (int j = 0; j < s.length; j++) {

if (s[j][i]) {

g.fillRect(j * 2 + pixoff, i * 2 + pixoff, 2, 2);

}

}

}

}

g.dispose();

bi.flush();

File f = new File("F:\\" + name + ".jpg");

if (!f.exists()) {

f.createNewFile();

}

// 创建图片

ImageIO.write(bi, "jpg", f);

} // end try

catch (Exception e) {


e.printStackTrace();


} // end catch


}


public static void main(String[] args) throws Exception {

QRCodeEncoderTest.create_image("ewm", "http://hao.360.cn/" + "");


} // end main


}


你可能感兴趣的:(QRCode)