1 File file=new File("E://zxd.jpg");//这个路径必须存在,不然回报路径不存在的nul异常
2 (Qrcode)使用了 qrcode-1.0.jar 第三方 包
(ImageIO)第二个jar就是jdK自带的 jre7\lib\rt.jar包
javax.imageio.ImageIO
2 (Qrcode)使用了 qrcode-1.0.jar 第三方 包
(ImageIO)第二个jar就是jdK自带的 jre7\lib\rt.jar包
javax.imageio.ImageIO
package com.zct.yfq.mgm.tools;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import com.swetake.util.Qrcode;
public class CordeUtil2 {
static int width = 90;
static int height = 90;
public static void main(String[] args) throws Exception {
File file=new File("E://zxd.jpg");
//encoderQRCoder("https://www.baidu.com/",file);
encoderQRCoder("小黄人,这个周出于什么原因不去练车",file);
}
public static void encoderQRCoder(String sms_info,File file) throws Exception {
try {
Qrcode testQrcode = new Qrcode();
testQrcode.setQrcodeErrorCorrect('M');
testQrcode.setQrcodeEncodeMode('B');
testQrcode.setQrcodeVersion(7);
String testString = sms_info;
byte[] d = testString.getBytes("utf-8");
BufferedImage bi = new BufferedImage(width, height,
BufferedImage.TYPE_BYTE_BINARY);
Graphics2D g = bi.createGraphics();
g.setBackground(Color.WHITE);
g.clearRect(0, 0, width, height);
g.setColor(Color.BLACK);
// 限制最大字节数为119
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, i * 2, 2, 2);
}
}
}
}
g.dispose();
bi.flush();
ImageIO.write(bi, "jpg", file);
} catch (Exception e) {
e.printStackTrace();
}
}
}