Commit a84ed29e by caiyt

二维码格式由png改为svg

parent e032bca3
...@@ -134,7 +134,11 @@ ...@@ -134,7 +134,11 @@
<artifactId>javase</artifactId> <artifactId>javase</artifactId>
<version>3.3.3</version> <version>3.3.3</version>
</dependency> </dependency>
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jfreesvg</artifactId>
<version>3.4</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
\ No newline at end of file
...@@ -2,105 +2,67 @@ package com.soss.common.utils; ...@@ -2,105 +2,67 @@ package com.soss.common.utils;
import com.google.zxing.BarcodeFormat; import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType; import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix; import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.jfree.graphics2d.svg.SVGGraphics2D;
import org.jfree.graphics2d.svg.ViewBox;
import javax.imageio.ImageIO;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable; import java.util.Hashtable;
public class QRCodeUtil { public class QRCodeUtil {
private static int onColor = 0xFF000000; //前景色
private static int offColor = 0xFFFFFFFF; //背景色
private static int margin = 0; //白边大小,取值范围0~4
private static ErrorCorrectionLevel level = ErrorCorrectionLevel.L; //二维码容错率
/** public static void main(String[] args) {
* 生成二维码 String qrCodeSvg = getQRCodeSvg("www.google.com", 200, 200, false);
* System.out.println(qrCodeSvg);
* @param content //二维码内容 }
*/
public static void generateQRImage(String content, String format, ByteArrayOutputStream byteArrayOutputStream, int width, int height) { public static BufferedImage getQRCode(String targetUrl, int width, int height) {
Hashtable<EncodeHintType, Object> hints = new Hashtable<>(); Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
// 指定纠错等级 // 指定纠错等级
hints.put(EncodeHintType.ERROR_CORRECTION, level); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
// 指定编码格式 // 指定编码格式
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.MARGIN, margin); //设置白边 hints.put(EncodeHintType.MARGIN, 0); //设置白边
QRCodeWriter qrCodeWriter = new QRCodeWriter();
try { try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); BitMatrix byteMatrix = qrCodeWriter.encode(targetUrl, BarcodeFormat.QR_CODE, width, height, hints);
// bitMatrix = deleteWhite(bitMatrix); int crunchifyWidth = byteMatrix.getWidth();
BufferedImage image = toBufferedImage(bitMatrix); int crunchifyHeight = byteMatrix.getHeight();
// MatrixToImageWriter.writeToStream(bitMatrix, format, byteArrayOutputStream, config);
ImageIO.write(image, format, byteArrayOutputStream); //生成二维码图片
// ImageIO.write(image, format, new File("C:\\Users\\caiyo\\Desktop\\test.png")); //生成二维码图片
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
generateQRImage("test", "PNG", null, 151, 151);
}
/** BufferedImage image = new BufferedImage(crunchifyWidth, crunchifyHeight, BufferedImage.TYPE_INT_RGB);
* @param matrix 二维码矩阵相关 image.createGraphics();
* @param format 二维码图片格式
* @param byteArrayOutputStream 二维码图片文件字节流
* @param logoPath logo路径
* @throws IOException
*/
public static void writeToFile(BitMatrix matrix, String format, ByteArrayOutputStream byteArrayOutputStream, String logoPath) throws IOException {
BufferedImage image = toBufferedImage(matrix);
//载入logo
if (StringUtils.isNotEmpty(logoPath)) {
int ratioWidth = image.getWidth() * 2 / 10;
int ratioHeight = image.getHeight() * 2 / 10;
Image img = ImageIO.read(new File(logoPath));
int logoWidth = img.getWidth(null) > ratioWidth ? ratioWidth : img.getWidth(null);
int logoHeight = img.getHeight(null) > ratioHeight ? ratioHeight : img.getHeight(null);
int x = (image.getWidth() - logoWidth) / 2;
int y = (image.getHeight() - logoHeight) / 2;
Graphics2D gs = image.createGraphics(); Graphics2D graphics = (Graphics2D) image.getGraphics();
gs.drawImage(img, x, y, logoWidth, logoHeight, null); graphics.setColor(Color.WHITE);
gs.setColor(Color.black); graphics.fillRect(0, 0, crunchifyWidth, crunchifyHeight);
gs.setBackground(Color.WHITE); graphics.setColor(Color.BLACK);
gs.dispose();
img.flush();
}
ImageIO.write(image, format, byteArrayOutputStream); //生成二维码图片
}
public static BufferedImage toBufferedImage(BitMatrix matrix) { for (int i = 0; i < crunchifyWidth; i++) {
int width = matrix.getWidth(); for (int j = 0; j < crunchifyHeight; j++) {
int height = matrix.getHeight(); if (byteMatrix.get(i, j)) {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); graphics.fillRect(i, j, 1, 1);
for (int x = 0; x < width; x++) { }
for (int y = 0; y < height; y++) { }
image.setRGB(x, y, matrix.get(x, y) ? onColor : offColor);
} }
return image;
} catch (WriterException e) {
throw new RuntimeException("Error getting QR Code");
} }
return image;
} }
public static BitMatrix deleteWhite(BitMatrix matrix) { public static String getQRCodeSvg(String targetUrl, int width, int height, boolean withViewBox) {
int[] rec = matrix.getEnclosingRectangle(); SVGGraphics2D g2 = new SVGGraphics2D(width, height);
int resWidth = rec[2] + 1; BufferedImage qrCodeImage = getQRCode(targetUrl, width, height);
int resHeight = rec[3] + 1; g2.drawImage(qrCodeImage, 0, 0, width, height, null);
BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
resMatrix.clear(); ViewBox viewBox = null;
for (int i = 0; i < resWidth; i++) { if (withViewBox) {
for (int j = 0; j < resHeight; j++) { viewBox = new ViewBox(0, 0, width, height);
if (matrix.get(i + rec[0], j + rec[1]))
resMatrix.set(i, j);
}
} }
return resMatrix; return g2.getSVGElement(null, true, viewBox, null, null);
} }
} }
\ No newline at end of file
...@@ -12,25 +12,23 @@ import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult; ...@@ -12,25 +12,23 @@ import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult;
import com.github.binarywang.wxpay.exception.WxPayException; import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService; import com.github.binarywang.wxpay.service.WxPayService;
import com.github.wxpay.sdk.WXPayUtil; import com.github.wxpay.sdk.WXPayUtil;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.soss.common.core.domain.entity.SysUser; import com.soss.common.core.domain.entity.SysUser;
import com.soss.common.core.domain.model.LoginUser; import com.soss.common.core.domain.model.LoginUser;
import com.soss.common.enums.RefundState; import com.soss.common.enums.RefundState;
import com.soss.common.enums.ShopState; import com.soss.common.enums.ShopState;
import com.soss.common.exception.ServiceException; import com.soss.common.exception.ServiceException;
import com.soss.common.utils.QRCodeUtil;
import com.soss.common.utils.StringUtils; import com.soss.common.utils.StringUtils;
import com.soss.common.utils.ip.IpUtils; import com.soss.common.utils.ip.IpUtils;
import com.soss.common.utils.sign.Base64; import com.soss.common.utils.sign.Base64;
import com.soss.common.utils.sign.Md5Utils;
import com.soss.system.constants.OrderStatusConstant; import com.soss.system.constants.OrderStatusConstant;
import com.soss.system.domain.*; import com.soss.system.domain.*;
import com.soss.system.mapper.CustomerMapper; import com.soss.system.mapper.CustomerMapper;
import com.soss.system.mapper.OrderDetailMapper; import com.soss.system.mapper.OrderDetailMapper;
import com.soss.system.mapper.OrderRefundMapper; import com.soss.system.mapper.OrderRefundMapper;
import com.soss.system.mapper.ShopMapper; import com.soss.system.mapper.ShopMapper;
import com.soss.system.service.impl.AppServiceImpl;
import com.soss.system.service.impl.OrderOperationLogServiceImpl; import com.soss.system.service.impl.OrderOperationLogServiceImpl;
import com.soss.system.service.impl.OrderServiceImpl; import com.soss.system.service.impl.OrderServiceImpl;
import com.soss.system.utils.DistanceUtil; import com.soss.system.utils.DistanceUtil;
...@@ -58,8 +56,6 @@ import javax.crypto.spec.IvParameterSpec; ...@@ -58,8 +56,6 @@ import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.net.InetAddress; import java.net.InetAddress;
...@@ -514,7 +510,18 @@ public class WeixinServiceImpl { ...@@ -514,7 +510,18 @@ public class WeixinServiceImpl {
jsonObject.put("orderId", order.getId()); jsonObject.put("orderId", order.getId());
jsonObject.put("userId", order.getUserId()); jsonObject.put("userId", order.getUserId());
jsonObject.put("secret", uid); jsonObject.put("secret", uid);
return generateMiniQr(jsonObject.toJSONString() + "###");
String[] qrSizeArr = qrSize.split("\\*");
int width = Integer.parseInt(qrSizeArr[0]);
int height = Integer.parseInt(qrSizeArr[1]);
String content = jsonObject.toJSONString() + "###";
String qrCode = QRCodeUtil.getQRCodeSvg(content, width, height, false);
String qrCodeKey = Md5Utils.hash(qrCode);
stringRedisTemplate.opsForValue().set(qrCodeKey, qrCode);
stringRedisTemplate.expire(qrCodeKey, 1, TimeUnit.DAYS);
return qrCodeKey;
} }
/** /**
...@@ -625,9 +632,9 @@ public class WeixinServiceImpl { ...@@ -625,9 +632,9 @@ public class WeixinServiceImpl {
return WxPayNotifyResponse.fail("回调有误!"); return WxPayNotifyResponse.fail("回调有误!");
} }
public List<JSONObject> getArea(String lng, String lat, Boolean testFlag) throws ParseException { public List<JSONObject> getArea(String lng, String lat, Boolean testFlag) {
String provinceString ="[value ='%s'][0].label"; String provinceString = "[value ='%s'][0].label";
String cityString ="[value='%s'][0].children[value='%s'][0].label"; String cityString = "[value='%s'][0].children[value='%s'][0].label";
String zoneString = "[value='%s'][0].children[value='%s'][0].children[value='%s'][0].label"; String zoneString = "[value='%s'][0].children[value='%s'][0].children[value='%s'][0].label";
Shop shop = new Shop(); Shop shop = new Shop();
if (BooleanUtils.isTrue(testFlag)) { if (BooleanUtils.isTrue(testFlag)) {
...@@ -643,10 +650,8 @@ public class WeixinServiceImpl { ...@@ -643,10 +650,8 @@ public class WeixinServiceImpl {
double realDistance = DistanceUtil.getRealDistance(Double.parseDouble(lng), Double.parseDouble(lat), Double.parseDouble(shop1.getLng()), Double.parseDouble(shop1.getLat())); double realDistance = DistanceUtil.getRealDistance(Double.parseDouble(lng), Double.parseDouble(lat), Double.parseDouble(shop1.getLng()), Double.parseDouble(shop1.getLat()));
shop1.setRealDistance(realDistance); shop1.setRealDistance(realDistance);
shop1.setDistance(DistanceUtil.getDistanceDesc(realDistance)); shop1.setDistance(DistanceUtil.getDistanceDesc(realDistance));
appService.perfectOrderState(shop1, lng, lat);
} else { } else {
shop1.setDistance("-1"); shop1.setDistance("-1");
shop1.setOrderState(1);
} }
String province = shop1.getProvince(); String province = shop1.getProvince();
if(proString.contains(province)){ if(proString.contains(province)){
...@@ -802,51 +807,4 @@ public class WeixinServiceImpl { ...@@ -802,51 +807,4 @@ public class WeixinServiceImpl {
throw new ServiceException("微信登录异常,请重新登录"); throw new ServiceException("微信登录异常,请重新登录");
} }
} }
public String generateMiniQr(String content) {
String fileName = System.currentTimeMillis() + ".png";
try {
String[] qrSizeArr = qrSize.split("\\*");
int width = Integer.parseInt(qrSizeArr[0]);
int height = Integer.parseInt(qrSizeArr[1]);
log.info("qr with: {} height: {}", width, height);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
QRCodeUtil.generateQRImage(content, "PNG", byteArrayOutputStream, width, height);
// MatrixToImageWriter.writeToStream(bitMatrix, "PNG", byteArrayOutputStream);
return ossFileService.uploadFile(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()), fileName);
} catch (Exception e) {
log.error("生成二维码失败:{}", e.getMessage(), e);
}
return null;
}
/**
* 图片放大缩小
*/
public static BufferedImage zoomInImage(BufferedImage originalImage, int width, int height) {
BufferedImage newImage = new BufferedImage(width, height, originalImage.getType());
Graphics g = newImage.getGraphics();
g.drawImage(originalImage, 0, 0, width, height, null);
g.dispose();
return newImage;
}
/**
* 删除白边
*/
private BitMatrix deleteWhite(BitMatrix matrix) {
int[] rec = matrix.getEnclosingRectangle();
int resWidth = rec[2] + 1;
int resHeight = rec[3] + 1;
BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
resMatrix.clear();
for (int i = 0; i < resWidth; i++) {
for (int j = 0; j < resHeight; j++) {
if (matrix.get(i + rec[0], j + rec[1])) {
resMatrix.set(i, j);
}
}
}
return resMatrix;
}
} }
...@@ -76,6 +76,8 @@ public class OrderServiceImpl implements IOrderService { ...@@ -76,6 +76,8 @@ public class OrderServiceImpl implements IOrderService {
private OrderTakingServiceImpl orderTakingService; private OrderTakingServiceImpl orderTakingService;
@Autowired @Autowired
private SysConfigServiceImpl sysConfigService; private SysConfigServiceImpl sysConfigService;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired @Autowired
SendSmsUtil sendSmsUtil; SendSmsUtil sendSmsUtil;
...@@ -90,6 +92,8 @@ public class OrderServiceImpl implements IOrderService { ...@@ -90,6 +92,8 @@ public class OrderServiceImpl implements IOrderService {
public Order selectOrderById(Long id) { public Order selectOrderById(Long id) {
Order order = orderMapper.selectOrderById(id); Order order = orderMapper.selectOrderById(id);
order.setShop(shopMapper.selectShopById(order.getShopId())); order.setShop(shopMapper.selectShopById(order.getShopId()));
String pickCode = stringRedisTemplate.opsForValue().get(order.getPickCode());
order.setPickCode(pickCode);
OrderSnapshot orderSnapshot = orderSnapshotService.selectOrderSnapshotByOrderId(order.getId()); OrderSnapshot orderSnapshot = orderSnapshotService.selectOrderSnapshotByOrderId(order.getId());
String snapshot = orderSnapshot.getSnapshot(); String snapshot = orderSnapshot.getSnapshot();
List<OrderDetail> orderDetails = JSONObject.parseArray(snapshot, OrderDetail.class); List<OrderDetail> orderDetails = JSONObject.parseArray(snapshot, OrderDetail.class);
...@@ -611,7 +615,8 @@ public class OrderServiceImpl implements IOrderService { ...@@ -611,7 +615,8 @@ public class OrderServiceImpl implements IOrderService {
String waitTime = getWaitTimeByOrderId(order.getId()); String waitTime = getWaitTimeByOrderId(order.getId());
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
map.put("waitTime", waitTime); map.put("waitTime", waitTime);
map.put("pickCode", order.getPickCode()); String pickCode = stringRedisTemplate.opsForValue().get(order.getPickCode());
map.put("pickCode", pickCode);
map.put("orderNum", order.getOrderNum()); map.put("orderNum", order.getOrderNum());
map.put("state", order.getState()); map.put("state", order.getState());
return map; return map;
......
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