利用Java生成带有文字的二维码
作者:daisy 时间:2022-05-21 15:01:38
介绍
主要使用了goole的zxing
包,下面给出了示例代码,很方便大家的理解和学习,代码都属于初步框架,功能有了,需要根据实际使用情况完善优化。
第一步、maven导入zxing
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.2.1</version>
</dependency>
第二步、开始生成二维码:
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
/**
* 把生成的二维码存入到图片中
* @param matrix zxing包下的二维码类
* @return
*/
public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}
/**
* 生成二维码并写入文件
* @param content 扫描二维码的内容
* @param format 图片格式 jpg
* @param file 文件
* @throws Exception
*/
public static void writeToFile(String content, String format, File file)
throws Exception {
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
@SuppressWarnings("rawtypes")
Map hints = new HashMap();
//设置UTF-8, 防止中文乱码
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
//设置二维码四周白色区域的大小
hints.put(EncodeHintType.MARGIN,1);
//设置二维码的容错性
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//画二维码
BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
BufferedImage image = toBufferedImage(bitMatrix);
if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}
第三步、把文字写入二维码图片中:
/**
* 给二维码图片加上文字
* @param pressText 文字
* @param qrFile 二维码文件
* @param fontStyle
* @param color
* @param fontSize
*/
public static void pressText(String pressText, File qrFile, int fontStyle, Color color, int fontSize) throws Exception {
pressText = new String(pressText.getBytes(), "utf-8");
Image src = ImageIO.read(qrFile);
int imageW = src.getWidth(null);
int imageH = src.getHeight(null);
BufferedImage image = new BufferedImage(imageW, imageH, BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(src, 0, 0, imageW, imageH, null);
//设置画笔的颜色
g.setColor(color);
//设置字体
Font font = new Font("宋体", fontStyle, fontSize);
FontMetrics metrics = g.getFontMetrics(font);
//文字在图片中的坐标 这里设置在中间
int startX = (WIDTH - metrics.stringWidth(pressText)) / 2;
int startY = HEIGHT/2;
g.setFont(font);
g.drawString(pressText, startX, startY);
g.dispose();
FileOutputStream out = new FileOutputStream(qrFile);
ImageIO.write(image, "JPEG", out);
out.close();
System.out.println("image press success");
}
第四步、在main方法中测试一下,一个中间带文字的二维码就生成了
public static void main(String[] args) throws Exception {
File qrcFile = new File("/Users/deweixu/","google.jpg");
writeToFile("www.google.com.hk", "jpg", qrcFile);
pressText("谷歌", qrcFile, 5, Color.RED, 32);
}
总结
标签:java,文字,二维码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring注解之@Lazy注解使用解析
2023-08-28 23:12:23
Maven配置多仓库无效的解决
2023-11-29 04:37:10
![](https://img.aspxhome.com/file/2023/1/60681_0s.jpg)
java源码解析之String类的compareTo(String otherString)方法
2023-11-11 23:10:00
Android按钮单击事件的四种常用写法总结
2023-07-15 09:05:18
Java与Node.js利用AES加密解密出相同结果的方法示例
2021-09-11 21:06:58
阿里开源Java诊断工具神器使用及场景详解
2023-11-06 17:24:21
Springboot 在普通类型注入Service或mapper
2023-11-29 15:26:21
详解java Collections.sort的两种用法
2023-11-28 09:30:31
![](https://img.aspxhome.com/file/2023/3/60473_0s.png)
使用webmagic实现爬虫程序示例分享
2021-10-08 22:10:26
Java中的break和continue关键字的使用方法总结
2022-07-13 11:50:46
Java递归来实现汉诺塔游戏,注释详细
2023-07-05 10:33:52
![](https://img.aspxhome.com/file/2023/2/64412_0s.jpg)
Springboot与vue实现数据导出方法具体介绍
2023-11-06 21:00:34
Flutter Widgets MediaQuery控件屏幕信息适配
2023-06-29 04:48:21
![](https://img.aspxhome.com/file/2023/2/120092_0s.jpg)
Java利用POI读取、写入Excel的方法指南
2023-11-23 15:39:23
![](https://img.aspxhome.com/file/2023/9/59899_0s.png)
通过实例解析JMM和Volatile底层原理
2023-05-20 19:10:48
![](https://img.aspxhome.com/file/2023/1/62201_0s.png)
Maven中央仓库发布的实现方法
2023-12-09 07:15:06
![](https://img.aspxhome.com/file/2023/1/64521_0s.jpg)
java二维数组实现推箱子小游戏
2022-08-31 10:18:40
SpringBoot整合mybatis-plus进阶详细教程
2023-11-27 05:13:12
![](https://img.aspxhome.com/file/2023/6/61836_0s.png)
Java 多线程并发编程提高数据处理效率的详细过程
2021-06-29 04:19:39
![](https://img.aspxhome.com/file/2023/6/65766_0s.jpg)
SpringBoot 如何从配置文件读取值到对象中
2023-10-13 15:43:15
![](https://img.aspxhome.com/file/2023/3/58533_0s.png)