一.什么是QRCode
QR Code码,是由Denso公司于1994年9月研制的一种矩阵二维码符号,它具有一维条码及其它二维条码所具有的信息容量大、可靠性高、可表示汉字及图象多种文字信息、保密防伪性强等优点。
二.生成各种风格二维码
导入jar包
com.google.zxing
core
3.1.0
1.生成不带logo的二维码图片
- 代码步骤:
/**
* 画制定logo和制定描述的二维码
*
* @author songyz
*/
public class PlayCode {
private static final int QRCOLOR = 0xFF000000;
private static final int BGWHITE = 0xFFFFFFFF;
private static final int WIDTH = 400;
private static final int HEIGHT = 400;
/**
* 用于设置QR二维码参数
*/
private static Map hints = new HashMap() {
private static final long serialVersionUID = 1L;
{
// 设置QR二维码的纠错级别(H为最高级别)具体级别信息
put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
// 设置编码方式
put(EncodeHintType.CHARACTER_SET, "utf-8");
put(EncodeHintType.MARGIN, 0);
}
};
/**
* 生成带logo的二维码图片
* 参数content:存入二维码的字符串
* 参数logoUrl:logo的地址
* 参数imgUrl:生成二维码的地址和名称
*/
public static void QRCode(String content, String imgUrl) {
//图片存放地址
try {
//二维码存放位置
File codeFile = new File(imgUrl);
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
// 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
BitMatrix bm = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
// 开始利用二维码数据创建Bitmap图片,分别设为黑(0xFFFFFFFF)白(0xFF000000)两色
for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++) {
image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE);
}
}
image.flush();
ImageIO.write(image, "png", codeFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 测试代码
@Test
public void test() {
QRCode("二维码内容", "D:\\img\\二维码.png");
}
-
生成的二维码
2.生成带logo的二维码图片
- 代码步骤:
/**
* 画制定logo和制定描述的二维码
*
* @author songyz
*/
public class PlayCode {
private static final int QRCOLOR = 0xFF000000;
private static final int BGWHITE = 0xFFFFFFFF;
private static final int WIDTH = 400;
private static final int HEIGHT = 400;
/**
* 用于设置QR二维码参数
*/
private static Map hints = new HashMap() {
private static final long serialVersionUID = 1L;
{
// 设置QR二维码的纠错级别(H为最高级别)具体级别信息
put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
// 设置编码方式
put(EncodeHintType.CHARACTER_SET, "utf-8");
put(EncodeHintType.MARGIN, 0);
}
};
/**
* 生成带logo的二维码图片
* 参数content:存入二维码的字符串
* 参数logoUrl:logo的地址
* 参数imgUrl:生成二维码的地址和名称
*/
public static void QRCodeLogo(String content, String logoUrl, String imgUrl) {
//图片存放地址
try {
//logo位置
File logoFile = new File(logoUrl);
//二维码存放位置
File codeFile = new File(imgUrl);
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
// 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
BitMatrix bm = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
// 开始利用二维码数据创建Bitmap图片,分别设为黑(0xFFFFFFFF)白(0xFF000000)两色
for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++) {
image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE);
}
}
int width = image.getWidth();
int height = image.getHeight();
if (Objects.nonNull(logoFile) && logoFile.exists()) {
// 构建绘图对象
Graphics2D g = image.createGraphics();
// 读取Logo图片
BufferedImage logo = ImageIO.read(logoFile);
// 开始绘制logo图片
g.drawImage(logo, width * 2 / 5, height * 2 / 5, width * 2 / 10, height * 2 / 10, null);
g.dispose();
logo.flush();
}
image.flush();
ImageIO.write(image, "png", codeFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 测试生成二维码
@Test
public void test() {
QRCodeLogo("二维码内容", "D:\\ba.jpg", "D:\\img\\二维码.png");
}
-
生成的二维码:
3.生成带下标的二维码
- 代码步骤:
/**
* 画制定logo和制定描述的二维码
*
* @author songyz
*/
public class PlayCode {
private static final int QRCOLOR = 0xFF000000;
private static final int BGWHITE = 0xFFFFFFFF;
private static final int WIDTH = 400;
private static final int HEIGHT = 400;
/**
* 用于设置QR二维码参数
*/
private static Map hints = new HashMap() {
private static final long serialVersionUID = 1L;
{
// 设置QR二维码的纠错级别(H为最高级别)具体级别信息
put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
// 设置编码方式
put(EncodeHintType.CHARACTER_SET, "utf-8");
put(EncodeHintType.MARGIN, 0);
}
};
/**
* 生成带logo的二维码图片
* 参数content:存入二维码的字符串
* 参数logoUrl:logo的地址
* 参数imgUrl:生成二维码的地址和名称
*/
public static void QRCodeLogo(String content, String logoUrl, String imgUrl,String productName) {
//图片存放地址
try {
//logo位置
File logoFile = new File(logoUrl);
//二维码存放位置
File codeFile = new File(imgUrl);
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
// 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
BitMatrix bm = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
// 开始利用二维码数据创建Bitmap图片,分别设为黑(0xFFFFFFFF)白(0xFF000000)两色
for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++) {
image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE);
}
}
int width = image.getWidth();
int height = image.getHeight();
if (Objects.nonNull(logoFile) && logoFile.exists()) {
// 构建绘图对象
Graphics2D g = image.createGraphics();
// 读取Logo图片
BufferedImage logo = ImageIO.read(logoFile);
// 开始绘制logo图片
g.drawImage(logo, width * 2 / 5, height * 2 / 5, width * 2 / 10, height * 2 / 10, null);
g.dispose();
logo.flush();
}
if (productName != null && !productName.equals("")) {
//新的图片,把带logo的二维码下面加上文字
BufferedImage outImage = new BufferedImage(400, 445, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D outg = outImage.createGraphics();
//画二维码到新的面板
outg.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
//画文字到新的面板
outg.setColor(Color.BLACK);
//字体、字型、字号
outg.setFont(new Font("宋体",Font.BOLD,30));
int strWidth = outg.getFontMetrics().stringWidth(productName);
if (strWidth > 399) {
//长度过长就换行
String productName1 = productName.substring(0, productName.length()/2);
String productName2 = productName.substring(productName.length()/2, productName.length());
int strWidth1 = outg.getFontMetrics().stringWidth(productName1);
int strWidth2 = outg.getFontMetrics().stringWidth(productName2);
outg.drawString(productName1, 200 - strWidth1/2, image.getHeight() + (outImage.getHeight() - image.getHeight())/2 + 12 );
BufferedImage outImage2 = new BufferedImage(400, 485, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D outg2 = outImage2.createGraphics();
outg2.drawImage(outImage, 0, 0, outImage.getWidth(), outImage.getHeight(), null);
outg2.setColor(Color.BLACK);
//字体、字型、字号
outg2.setFont(new Font("宋体",Font.BOLD,30));
outg2.drawString(productName2, 200 - strWidth2/2, outImage.getHeight() + (outImage2.getHeight() - outImage.getHeight())/2 + 5 );
outg2.dispose();
outImage2.flush();
outImage = outImage2;
}else {
//画文字
outg.drawString(productName, 200 - strWidth/2 , image.getHeight() + (outImage.getHeight() - image.getHeight())/2 + 12 );
}
outg.dispose();
outImage.flush();
image = outImage;
}
image.flush();
ImageIO.write(image, "png", codeFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 测试生成二维码
@Test
public void test() {
QRCodeLogo("二维码内容", "D:\\ba.jpg", "D:\\img\\二维码.png","付款二维码");
}
-
生成的二维码
所属文集:技术栈