package cn.edu.zafu;
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 QRCode {
public static void getQrcodeImgByContents(String contents, String imgPath) {
int width = 140;
int height = 140;
try {
Qrcode qrcode = new Qrcode();
// 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小
qrcode.setQrcodeErrorCorrect('M');
qrcode.setQrcodeEncodeMode('B');
// 设置设置二维码尺寸,取值范围1-40,值越大尺寸越大,可存储的信息越大
qrcode.setQrcodeVersion(7);
BufferedImage bufImg = new BufferedImage(width, height,
BufferedImage.TYPE_INT_BGR);
Graphics2D gs = bufImg.createGraphics();
gs.setBackground(Color.WHITE);
gs.clearRect(0, 0, width, height);
gs.setColor(Color.BLACK);
//偏移量
int pixoff = 2;
byte[] contentBytes = contents.getBytes("UTF-8");
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("大小超出限制");
}
Image img = ImageIO.read(new File("logo5.png"));//实例化一个Image对象。
gs.drawImage(img, 45, 45, 50,50, null);
gs.dispose();
bufImg.flush();
File imgFile = new File(imgPath);
ImageIO.write(bufImg, "png", imgFile);
System.out.println("二维码生成成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
StringBuffer str = new StringBuffer();
str.append("代码:290800000002273");
str.append("\n");
str.append("名称:HJJJKK");
String content= "29081933423445";
getQrcodeImgByContents(content, "1.png");
}
}
最近下载更多
zkjzkj123456 LV1
5月26日
訾政阳 LV1
2024年6月13日
zbjmml LV1
2024年5月7日
lilihehe LV1
2023年10月19日
zxsyaa LV1
2023年5月9日
2552655 LV1
2023年1月29日
June6666666666 LV1
2022年12月6日
微信网友_6224558473859072 LV1
2022年11月18日
OneTea LV1
2022年9月29日
老舰了你 LV1
2022年9月24日
最近浏览更多
zkjzkj123456 LV1
5月26日
微信网友_7351677866266624
1月22日
暂无贡献等级
zzkcll LV1
1月22日
edpwyg LV14
2024年7月22日
訾政阳 LV1
2024年6月13日
Protein
2024年5月27日
暂无贡献等级
zbjmml LV1
2024年5月7日
kingoneyang LV13
2023年11月30日
lilihehe LV1
2023年10月19日
qingye LV1
2023年6月16日

