|
|
@@ -0,0 +1,303 @@
|
|
|
+package com.aoyang.tms.util;
|
|
|
+
|
|
|
+import com.google.zxing.BarcodeFormat;
|
|
|
+import com.google.zxing.EncodeHintType;
|
|
|
+import com.google.zxing.MultiFormatWriter;
|
|
|
+import com.google.zxing.WriterException;
|
|
|
+import com.google.zxing.client.j2se.MatrixToImageConfig;
|
|
|
+import com.google.zxing.client.j2se.MatrixToImageWriter;
|
|
|
+import com.google.zxing.common.BitMatrix;
|
|
|
+import com.google.zxing.qrcode.QRCodeWriter;
|
|
|
+import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import sun.font.FontDesignMetrics;
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import java.awt.*;
|
|
|
+import java.awt.geom.RoundRectangle2D;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Hashtable;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Random;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @description:
|
|
|
+ * @author: xumingrui
|
|
|
+ * @create: 2020 -01 -21 -15:02
|
|
|
+ */
|
|
|
+public class QRCodeUtils {
|
|
|
+ private String CHARSET = "utf-8";
|
|
|
+ private String FORMAT_NAME = "JPG";
|
|
|
+ // 二维码尺寸
|
|
|
+ private int QRCODE_WIDTH = 648;
|
|
|
+ // 二维码尺寸
|
|
|
+ private int QRCODE_HEIGHT = 648;
|
|
|
+ // LOGO宽度
|
|
|
+ private int WIDTH = 60;
|
|
|
+ // LOGO高度
|
|
|
+ private int HEIGHT = 60;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 司机端调用,返回bufferedImage
|
|
|
+ *
|
|
|
+ * @param content
|
|
|
+ * @return
|
|
|
+ * @throws WriterException
|
|
|
+ */
|
|
|
+ public BufferedImage qrCode(String content) throws WriterException {
|
|
|
+ //1. 生成二维码
|
|
|
+ BitMatrix bitMatrix = encode(content);
|
|
|
+ //2.将BitMatrix 转换为BufferedImage
|
|
|
+ BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix, new MatrixToImageConfig(Color.BLACK.getRGB(), Color.WHITE.getRGB()));
|
|
|
+ return bufferedImage;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成二维码
|
|
|
+ *
|
|
|
+ * @param contents 二维码内容
|
|
|
+ * @return 二维码的描述对象 BitMatrix
|
|
|
+ * @throws WriterException 编码时出错
|
|
|
+ */
|
|
|
+ private BitMatrix encode(String contents) throws WriterException {
|
|
|
+ final Map<EncodeHintType, Object> hints = new HashMap<>();
|
|
|
+ hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
|
|
|
+ hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
|
|
|
+ return new QRCodeWriter().encode(contents, BarcodeFormat.QR_CODE, QRCODE_WIDTH, QRCODE_HEIGHT, hints);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private 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);
|
|
|
+ hints.put(EncodeHintType.MARGIN, 1);
|
|
|
+ BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
|
|
|
+ BarcodeFormat.QR_CODE, QRCODE_WIDTH, QRCODE_HEIGHT, 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (imgPath == null || "".equals(imgPath)) {
|
|
|
+ return image;
|
|
|
+ }
|
|
|
+ // 插入图片
|
|
|
+ insertImage(image, imgPath, needCompress);
|
|
|
+ return image;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 插入LOGO
|
|
|
+ *
|
|
|
+ * @param source 二维码图片
|
|
|
+ * @param imgPath LOGO图片地址
|
|
|
+ * @param needCompress 是否压缩
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ private 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 File(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_WIDTH - width) / 2;
|
|
|
+ int y = (QRCODE_HEIGHT - 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();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成二维码(内嵌LOGO)
|
|
|
+ *
|
|
|
+ * @param content 内容
|
|
|
+ * @param imgPath LOGO地址
|
|
|
+ * @param destPath 存放目录
|
|
|
+ * @param needCompress 是否压缩LOGO
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public String encode(String content, String imgPath, String destPath,
|
|
|
+ boolean needCompress) throws Exception {
|
|
|
+ BufferedImage image = createImage(content, imgPath,
|
|
|
+ needCompress);
|
|
|
+ mkdirs(destPath);
|
|
|
+ String file = new Random().nextInt(99999999) + ".jpg";
|
|
|
+ ImageIO.write(image, FORMAT_NAME, new File(destPath + "/" + file));
|
|
|
+ return file;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
|
|
|
+ *
|
|
|
+ * @param destPath 存放目录
|
|
|
+ * @date 2013-12-11 上午10:16:36
|
|
|
+ */
|
|
|
+ public void mkdirs(String destPath) {
|
|
|
+ File file = new File(destPath);
|
|
|
+ //当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
|
|
|
+ if (!file.exists() && !file.isDirectory()) {
|
|
|
+ file.mkdirs();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成二维码(内嵌LOGO)
|
|
|
+ *
|
|
|
+ * @param content 内容
|
|
|
+ * @param imgPath LOGO地址
|
|
|
+ * @param destPath 存储地址
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public void encode(String content, String imgPath, String destPath)
|
|
|
+ throws Exception {
|
|
|
+ encode(content, imgPath, destPath, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成二维码
|
|
|
+ *
|
|
|
+ * @param content 内容
|
|
|
+ * @param destPath 存储地址
|
|
|
+ * @param needCompress 是否压缩LOGO
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public void encode(String content, String destPath,
|
|
|
+ boolean needCompress) throws Exception {
|
|
|
+ encode(content, null, destPath, needCompress);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成二维码
|
|
|
+ *
|
|
|
+ * @param content 内容
|
|
|
+ * @param destPath 存储地址
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public void encode(String content, String destPath) throws Exception {
|
|
|
+ encode(content, null, destPath, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成二维码(内嵌LOGO)
|
|
|
+ *
|
|
|
+ * @param content 内容
|
|
|
+ * @param imgPath LOGO地址
|
|
|
+ * @param output 输出流
|
|
|
+ * @param needCompress 是否压缩LOGO
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public void encode(String content, String imgPath,
|
|
|
+ OutputStream output, boolean needCompress) throws Exception {
|
|
|
+ BufferedImage image = createImage(content, imgPath,
|
|
|
+ needCompress);
|
|
|
+ ImageIO.write(image, FORMAT_NAME, output);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成二维码
|
|
|
+ *
|
|
|
+ * @param content 内容
|
|
|
+ * @param output 输出流
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public void encode(String content, OutputStream output)
|
|
|
+ throws Exception {
|
|
|
+ encode(content, null, output, false);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 在背景图上插入二维码图片 -- 收银员二维码使用
|
|
|
+ */
|
|
|
+ public BufferedImage backAddQr(BufferedImage backBufferedImage, BufferedImage qrImage, String content) throws IOException {
|
|
|
+ // BufferedImage backBufferedImage = ImageIO.read ( backImage );
|
|
|
+ int width = backBufferedImage.getWidth(null);
|
|
|
+ int height = backBufferedImage.getHeight(null);
|
|
|
+ // 插入LOGO
|
|
|
+ Graphics2D graph = backBufferedImage.createGraphics();
|
|
|
+ int x = (width - QRCODE_WIDTH) / 2;
|
|
|
+ int y = 540;
|
|
|
+ graph.drawImage(qrImage, x, y, QRCODE_WIDTH, QRCODE_WIDTH, null);
|
|
|
+
|
|
|
+ if (StringUtils.isNotBlank(content)) {
|
|
|
+ int contentSize = content.length();
|
|
|
+ Font font = new Font("微软雅黑", Font.PLAIN, 65);
|
|
|
+ graph.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);//设置抗锯齿
|
|
|
+ graph.setPaint(new Color(0, 0, 0, 0));//阴影颜色
|
|
|
+ graph.setFont(font);
|
|
|
+ graph.setColor(Color.white);
|
|
|
+ //加气站名字大于15个字,二维码上换行显示
|
|
|
+ if (contentSize > 15) {
|
|
|
+ int contentLength = getWordWidth(font, content.substring(0, 15));
|
|
|
+ graph.drawString(content.substring(0, 15), (width - contentLength) / 2, 1300);
|
|
|
+ contentLength = getWordWidth(font, content.substring(15, contentSize));
|
|
|
+ graph.drawString(content.substring(15, contentSize), (width - contentLength) / 2, 1400);
|
|
|
+ } else {
|
|
|
+ int contentLength = getWordWidth(font, content);
|
|
|
+ graph.drawString(content, (width - contentLength) / 2, 1300);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Shape shape = new RoundRectangle2D.Float(x, y, QRCODE_WIDTH, QRCODE_WIDTH, 6, 6);
|
|
|
+ graph.setStroke(new BasicStroke(3f));
|
|
|
+ graph.draw(shape);
|
|
|
+ graph.dispose();
|
|
|
+ return backBufferedImage;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static int getWordWidth(Font font, String content) {
|
|
|
+ FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
|
|
|
+ int width = 0;
|
|
|
+ for (int i = 0; i < content.length(); i++) {
|
|
|
+ width += metrics.charWidth(content.charAt(i));
|
|
|
+ }
|
|
|
+ return width;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+ QRCodeUtils utils = new QRCodeUtils();
|
|
|
+ BufferedImage qrBuffered = utils.qrCode("内容");
|
|
|
+ System.out.println(qrBuffered.getWidth() + "," + qrBuffered.getHeight());
|
|
|
+ // BufferedImage finalImage = utils.backAddQr("/Users/xumingrui/Desktop/sback.png",qrBuffered,"");
|
|
|
+ File file = new File("/Users/xumingrui/Desktop/1.jpg");
|
|
|
+ //ImageIO.write(finalImage, "jpg", file);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|