Commit 3759d134 authored by liqin's avatar liqin 💬

bug fixed

parent 229c4937
......@@ -69,13 +69,13 @@
<!-- General Mapper -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<!-- MySQL JDBC Driver -->
<dependency>
......@@ -242,18 +242,6 @@
<version>2.3</version>
<scope>provided</scope>
</dependency>
<!-- QR Generator -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.4.1</version>
</dependency>
<!-- Emoji Support -->
<dependency>
<groupId>com.vdurmont</groupId>
<artifactId>emoji-java</artifactId>
<version>5.1.1</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
......
package cn.wisenergy.chnmuseum.party.common.util;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.OutputStream;
import java.net.URL;
import java.util.Hashtable;
import java.util.Random;
public class QRCodeUtil {
private static final String CHARSET = "utf-8";
private static final String FORMAT_NAME = "JPG";
// 二维码尺寸
private static final int QRCODE_SIZE = 300;
// LOGO宽度
private static final int WIDTH = 60;
// LOGO高度
private static final int HEIGHT = 60;
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);
hints.put(EncodeHintType.MARGIN, 1);
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);
}
}
if (imgPath == null || "".equals(imgPath)) {
return image;
}
// 插入图片
QRCodeUtil.insertImage(image, imgPath, needCompress);
return image;
}
/**
* 插入LOGO
*
* @param source 二维码图片
* @param imgPath LOGO图片地址
* @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();
}
/**
* 生成二维码(内嵌LOGO)
*
* @param content 内容
* @param imgPath LOGO地址
* @param destPath 存放目录
* @param needCompress 是否压缩LOGO
* @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));
}
/**
* 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
*
* @param destPath 存放目录
* @author lanyuan
* Email: mmm333zzz520@163.com
* @date 2013-12-11 上午10:16:36
*/
public static 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 static void encode(String content, String imgPath, String destPath)
throws Exception {
QRCodeUtil.encode(content, imgPath, destPath, false);
}
/**
* 生成二维码
*
* @param content 内容
* @param destPath 存储地址
* @param needCompress 是否压缩LOGO
* @throws Exception
*/
public static void encode(String content, String destPath,
boolean needCompress) throws Exception {
QRCodeUtil.encode(content, null, destPath, needCompress);
}
/**
* 生成二维码
*
* @param content 内容
* @param destPath 存储地址
* @throws Exception
*/
public static void encode(String content, String destPath) throws Exception {
QRCodeUtil.encode(content, null, destPath, false);
}
/**
* 生成二维码(内嵌LOGO)
*
* @param content 内容
* @param imgPath LOGO地址
* @param output 输出流
* @param needCompress 是否压缩LOGO
* @throws Exception
*/
public static void encode(String content, String imgPath,
OutputStream output, boolean needCompress) throws Exception {
BufferedImage image = QRCodeUtil.createImage(content, imgPath,
needCompress);
ImageIO.write(image, FORMAT_NAME, output);
}
/**
* 生成二维码
*
* @param content 内容
* @param output 输出流
* @throws Exception
*/
public static void encode(String content, OutputStream output)
throws Exception {
QRCodeUtil.encode(content, null, output, false);
}
/**
* 解析二维码
*
* @param file 二维码图片
* @return
* @throws Exception
*/
public static String decode(File file) throws Exception {
BufferedImage image;
image = ImageIO.read(file);
if (image == null) {
return null;
}
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(
image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result;
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
result = new MultiFormatReader().decode(bitmap, hints);
String resultStr = result.getText();
return resultStr;
}
/**
* 解析二维码
*
* @param path 二维码图片地址
* @return
* @throws Exception
*/
public static String decode(String path) throws Exception {
return QRCodeUtil.decode(new File(path));
}
public static void main(String[] args) throws Exception {
// String text = "杨建龙" + "杨智平"; //这里设置自定义网站url
// String logoPath = "http://40.125.210.149/group1/M00/00/35/CgAMBFuvKQmAC3ynAABTy6wx-K4542.jpg";
// String destPath = "d:\\";
// Image logoPath = ImageIO.read(new URL("http://40.125.210.149/group1/M00/00/35/CgAMBFuvKQmAC3ynAABTy6wx-K4542.jpg"));
// QRCodeUtil.encode(text, destPath, true);
// QRCodeUtil.encode(text, logoPath, destPath,true);
// String imagePath = "d:\\95413982.jpg";
// String ret = QRCodeUtil.decode(imagePath);
// System.out.println(ret);
String text = "杨建龙" + "杨智平";
String logoPath = "http://40.125.210.149/group1/M00/00/35/CgAMBFuvKQmAC3ynAABTy6wx-K4542.jpg";
String destPath = "d:\\";
QRCodeUtil.encode(text, logoPath, destPath,true);
int = 2289;
}
}
\ No newline at end of file
package cn.wisenergy.chnmuseum.party.common.util;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
/**
* <p>
* 二维码工具类(使用ZXingjar包)
* </p>
*
* @author Tom
* @since 2018/9/29
*/
public class QRCodeUtils {
// 默认宽为300
private Integer width = 300;
// 默认高为300
private Integer height = 300;
// 默认二维码图片格式
private String imageFormat = "png";
// 默认二维码字符编码
private String charType = "utf-8";
// 默认二维码的容错级别
private ErrorCorrectionLevel corretionLevel = ErrorCorrectionLevel.H;
// 二维码与图片的边缘
private Integer margin = 1;
// 二维码参数
private Map<EncodeHintType, Object> encodeHits = new HashMap<>();
//main方法测试工具类
public static void main(String[] args) {
QRCodeUtils qrCode = new QRCodeUtils(300, 300);
//设置二维码的边缘为1
qrCode.setMargin(1);
//设置输出到桌面
String path = System.getProperty("user.home") + File.separator + "Desktop" + File.separator + "test.png";
//创建图片二维码
qrCode.createQrImage("https://www.baidu.com/", path);
//识别图片二维码的内容
System.out.println(qrCode.decodeQrImage(new File(path)));//https://www.baidu.com/
}
public QRCodeUtils(Integer width, Integer height, String imageFormat, String charType,
ErrorCorrectionLevel corretionLevel, Integer margin) {
if (width != null) {
this.width = width;
}
if (height != null) {
this.height = height;
}
if (imageFormat != null) {
this.imageFormat = imageFormat;
}
if (charType != null) {
this.charType = charType;
}
if (corretionLevel != null) {
this.corretionLevel = corretionLevel;
}
if (margin != null) {
this.margin = margin;
}
}
public QRCodeUtils(Integer width, Integer height, String imageFormat, String charType,
ErrorCorrectionLevel corretionLevel) {
this(width, height, imageFormat, charType, corretionLevel, null);
}
public QRCodeUtils(Integer width, Integer height, String imageFormat, String charType, Integer margin) {
this(width, height, imageFormat, charType, null, margin);
}
public QRCodeUtils(Integer width, Integer height, String imageFormat, String charType) {
this(width, height, imageFormat, charType, null, null);
}
public QRCodeUtils(Integer width, Integer height, String imageFormat) {
this(width, height, imageFormat, null, null, null);
}
public QRCodeUtils(Integer width, Integer height) {
this(width, height, null, null, null, null);
}
public QRCodeUtils() {
}
// 初始化二维码的参数
private void initialParamers() {
// 字符编码
encodeHits.put(EncodeHintType.CHARACTER_SET, this.charType);
// 容错等级 L、M、Q、H 其中 L 为最低, H 为最高
encodeHits.put(EncodeHintType.ERROR_CORRECTION, this.corretionLevel);
// 二维码与图片边距
encodeHits.put(EncodeHintType.MARGIN, margin);
}
// 得到二维码图片
public BufferedImage getBufferedImage(String content) {
initialParamers();
BufferedImage bufferedImage = null;
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, this.width,
this.height, this.encodeHits);
bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
} catch (WriterException e) {
e.printStackTrace();
return null;
}
return bufferedImage;
}
// 将二维码保存到输出流中
public void writeToStream(String content, OutputStream os) {
initialParamers();
try {
BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, this.width, this.height,
this.encodeHits);
MatrixToImageWriter.writeToStream(matrix, this.imageFormat, os);
} catch (Exception e) {
e.printStackTrace();
}
}
// 将二维码图片保存为文件
public void createQrImage(String content, File file) {
initialParamers();
try {
BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, this.width, this.height, this.encodeHits);
MatrixToImageWriter.writeToFile(matrix, this.imageFormat, file);
} catch (Exception e) {
e.printStackTrace();
}
}
// 将二维码图片保存到指定路径
public void createQrImage(String content, String path) {
initialParamers();
try {
BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, this.width, this.height, this.encodeHits);
MatrixToImageWriter.writeToPath(matrix, this.imageFormat, new File(path).toPath());
} catch (Exception e) {
e.printStackTrace();
}
}
//识别图片二维码
public String decodeQrImage(File file) {
String content = null;
try {
BufferedImage bufferedImage = ImageIO.read(new FileInputStream(file));
LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap image = new BinaryBitmap(binarizer);
Map<DecodeHintType, Object> decodeHits = new HashMap<>();
decodeHits.put(DecodeHintType.CHARACTER_SET, this.charType);
Result result = new MultiFormatReader().decode(image, decodeHits);
content = result.getText();
} catch (Exception e) {
e.printStackTrace();
}
return content;
}
public Integer getWidth() {
return width;
}
public void setWidth(Integer width) {
this.width = width;
}
public Integer getHeight() {
return height;
}
public void setHeight(Integer height) {
this.height = height;
}
public String getImageFormat() {
return imageFormat;
}
public void setImageFormat(String imageFormat) {
this.imageFormat = imageFormat;
}
public String getCharType() {
return charType;
}
public void setCharType(String charType) {
this.charType = charType;
}
public ErrorCorrectionLevel getCorretionLevel() {
return corretionLevel;
}
public void setCorretionLevel(ErrorCorrectionLevel corretionLevel) {
this.corretionLevel = corretionLevel;
}
public Integer getMargin() {
return margin;
}
public void setMargin(Integer margin) {
this.margin = margin;
}
public Map<EncodeHintType, Object> getHits() {
return encodeHits;
}
public void setHits(Map<EncodeHintType, Object> hits) {
this.encodeHits = hits;
}
}
package cn.wisenergy.chnmuseum.party.common.util;//package com.cmbchina.business.hall.common.util;
//
//
//
//import java.awt.Color;
//import java.awt.Graphics2D;
//import java.awt.image.BufferedImage;
//
//import javax.imageio.ImageIO;
//import javax.servlet.http.HttpServletResponse;
//
//import com.swetake.util.Qrcode;
//
///**
// * <p>
// *
// * </p>
// *
// * @author Tom
// * @since 2018/9/30
// */
//public class EncoderHandler {
//
// public void encoderQRCoder(String content, HttpServletResponse response) {
// try {
// Qrcode handler = new Qrcode();
// handler.setQrcodeErrorCorrect('M');
// handler.setQrcodeEncodeMode('B');
// handler.setQrcodeVersion(7);
//
// System.out.println(content);
// byte[] contentBytes = content.getBytes("UTF-8");
//
// BufferedImage bufImg = new BufferedImage(80, 80, BufferedImage.TYPE_INT_RGB);
//
// Graphics2D gs = bufImg.createGraphics();
//
// gs.setBackground(Color.WHITE);
// gs.clearRect(0, 0, 140, 140);
//
// //设定图像颜色:BLACK
// gs.setColor(Color.BLACK);
//
// //设置偏移量 不设置肯能导致解析出错
// int pixoff = 2;
// //输出内容:二维码
// if(contentBytes.length > 0 && contentBytes.length < 124) {
// boolean[][] codeOut = handler.calQrcode(contentBytes);
// for(int i = 0; i < codeOut.length; i++) {
// for(int j = 0; j < codeOut.length; j++) {
// if(codeOut[j][i]) {
// gs.fillRect(j * 3 + pixoff, i * 3 + pixoff,3, 3);
// }
// }
// }
// } else {
// System.err.println("QRCode content bytes length = " + contentBytes.length + " not in [ 0,120 ]. ");
// }
//
// gs.dispose();
// bufImg.flush();
//
//
//
// //生成二维码QRCode图片
// ImageIO.write(bufImg, "jpg", response.getOutputStream());
//
//
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
//}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment