引入依赖
com.google.zxing
core
3.2.1
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class QoUtil {
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(270, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width - 30; x++) {
for (int y = 0; y < height; y++) {
Image.setRGB(x, y, matrix.get(x, y) ? black : white);
}
}
// 控制二维码内容向左平移50,靠左展示,右侧可添加文字
Graphics2D graphics = Image.createGraphics();
// 平移位置
graphics.translate(-50, 0);
// 绘图
graphics.drawImage(Image, null, null);
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, 11);
//设置二维码的容错性
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//画二维码
BitMatrix BitMatrix = MultiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 300, 200, 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 = 200;
int starty = 80;
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");
}
public static void main(String[] args) throws Exception {
File qrcfile = new File("F:\\11", "13.jpg");
// 二维码内容设置链接,微信扫描后会自动跳转该链接;二维码内容设置文字,微信扫描后会展示文字;
// writetofile("https://www.baidu.com/", "jpg", qrcfile);
writetofile("hahahaahah", "jpg", qrcfile);
presstext("百度", qrcfile, 5, Color.red, 32);
}
}
运行生成带文字二维码示例
学海无涯苦作舟!!!