之前大概了解了一下二维码的生成过程,分享给大家:
1.如果实现二维码功能需要导入核心jar包,我导入的是core-3.2.1.jar;
2.首先是创建一张带有参数内容的图片,方法如下:
private static BufferedImage createImage(String content, String imgPath, boolean needCompress) throws Exception {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//容错率:容错率越高,二维码的有效像素点就越多.
hints.put(EncodeHintType.CHARACTER_SET, CHARSET); //使用小写的编码,大写会出现]Q2\000026开头内容
hints.put(EncodeHintType.MARGIN, 1); //margin 边框设置
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);//二维码矩阵
int width = bitMatrix.getWidth();
int height = bitMatrix.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, bitMatrix.get(x, y) ? 0xFF000000
: 0xFFFFFFFF); //填充,可设置颜色 颜色的取值为后6位
}
}
if (imgPath == null || "".equals(imgPath)) {
return image;
}
// 插入图片
QrcodeUtil.insertImage(image, imgPath, needCompress);
return image;
}
/**
* 在生成的二维码中插入图片
* @param source
* @param imgPath
* @param needCompress
* @throws Exception
*/
private static void insertImage(BufferedImage source, String imgPath, boolean needCompress) throws Exception {
// File file = new File(imgPath);
// if (!file.exists()) {
// System.err.println("" + imgPath + " 该文件不存在!");
// return;
// }
Image src = ImageIO.read(new URL(imgPath)); //后续需要从本地读取再补充
int width = src.getWidth(null);
int height = src.getHeight(null);
if (needCompress) { // 压缩LOGO
if (width > WIDTH) {
width = WIDTH;
}
if (height > HEIGHT) {
height = HEIGHT;
}
Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 绘制缩小后的图
g.dispose();
src = image;
}
// 插入LOGO
Graphics2D graph = source.createGraphics();
int x = (QRCODE_SIZE - width) / 2;
int y = (QRCODE_SIZE - height) / 2;
graph.drawImage(src, x, y, width, height, null);
Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
graph.setStroke(new BasicStroke(3f));
graph.draw(shape);
graph.dispose();
}
3.调用此方法
/**
* 生成带logo二维码,并保存到磁盘
* @param content
* @param imgPath logo图片
* @param destPath
* @param needCompress
* @throws Exception
*/
public static void encode(String content, String imgPath, String destPath, boolean needCompress) throws Exception {
BufferedImage image = QrcodeUtil.createImage(content, imgPath, needCompress);
mkdirs(destPath);
String file = new Random().nextInt(99999999) + ".jpg";//生成随机文件名
ImageIO.write(image, FORMAT_NAME, new File(destPath + "/" + file));
}
public static byte[] encodeAndReQrcode(String content, String imgPath, boolean needCompress) throws Exception {
BufferedImage image = QrcodeUtil.createImage(content, imgPath, needCompress);
// mkdirs(destPath);
// String file = new Random().nextInt(99999999) + ".jpg";//生成随机文件名
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(image, FORMAT_NAME, out);
byte[] qrcode = out.toByteArray();
// String qrcodePath = destPath + "/" + file;
return qrcode;
}
public static void mkdirs(String destPath) {
File file = new File(destPath);
// 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir。(mkdir如果父目录不存在则会抛出异常)
if (!file.exists() && !file.isDirectory()) {
file.mkdirs();
}
}
4.如果不要logo只要在调用时将logo的路径传为null就好。