Commit eff16e15 by guiq

feat: [none] 【优化】带logo二维码放大后不清晰问题优化

parent 4e732fe2
...@@ -482,36 +482,38 @@ public class QrcodeUtils { ...@@ -482,36 +482,38 @@ public class QrcodeUtils {
/** /**
* 创建带logo二维码 * 创建带logo二维码
* *
* @param content * @param content 二维码响应地址
* 二维码响应地址
* @return * @return
* @throws Exception * @throws Exception
*/ */
public static String createLogoQr(String url, String logoPath, Integer onColor) throws BizException { public static String createLogoQr(String url, String logoPath, Integer onColor) throws BizException {
LOGGER.info("【二维码】创建带logo二维码,<START>.[url]=" + url+",logoPath="+logoPath+",onColor="+onColor); LOGGER.info("【二维码】创建带logo二维码,<START>.[url]=" + url + ",logoPath=" + logoPath + ",onColor=" + onColor);
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); return createLogoQr(url, logoPath, onColor, ImageConstant.QRCODE_HEIGHT, ImageConstant.QRCODE_WITH, LOGO_HEIGHT, LOGO_WIDTH);
}
public static String createLogoQr(String url, String logoPath, Integer onColor, Integer qrH, Integer qrW, Integer logoH, Integer logoW) throws BizException {
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.ERROR_CORRECTION, level); hints.put(EncodeHintType.ERROR_CORRECTION, level);
hints.put(EncodeHintType.MARGIN, margin); // 设置白边 hints.put(EncodeHintType.MARGIN, margin); // 设置白边
try { try {
// 生成矩阵 // 生成矩阵
BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, ImageConstant.QRCODE_WITH, BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, qrW, qrH, hints);
ImageConstant.QRCODE_HEIGHT, hints);
BufferedImage image = toBufferedImage(bitMatrix, onColor); BufferedImage image = toBufferedImage(bitMatrix, onColor);
Graphics2D gs = image.createGraphics(); Graphics2D gs = image.createGraphics();
// 载入logo // 载入logo
byte[] file = FileUtils.downloadByteFromUrl(logoPath); byte[] file = FileUtils.downloadByteFromUrl(logoPath);
ByteArrayInputStream in = new ByteArrayInputStream(file); ByteArrayInputStream in = new ByteArrayInputStream(file);
BufferedImage srcImage = ImageIO.read(in); BufferedImage srcImage = ImageIO.read(in);
Image img = srcImage.getScaledInstance(LOGO_WIDTH, LOGO_HEIGHT, BufferedImage.SCALE_SMOOTH); Image img = srcImage.getScaledInstance(logoW, logoH, BufferedImage.SCALE_SMOOTH);
int x = (image.getWidth() - LOGO_WIDTH) / 2; int x = (image.getWidth() - logoW) / 2;
int y = (image.getHeight() - LOGO_HEIGHT) / 2; int y = (image.getHeight() - logoH) / 2;
gs.drawImage(img, x, y, LOGO_WIDTH, LOGO_HEIGHT, null); gs.drawImage(img, x, y, logoW, logoH, null);
BasicStroke stroke = new BasicStroke(5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); BasicStroke stroke = new BasicStroke(5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
gs.setStroke(stroke);// 设置笔画对象 gs.setStroke(stroke);// 设置笔画对象
// 指定弧度的圆角矩形 // 指定弧度的圆角矩形
RoundRectangle2D.Float round = new RoundRectangle2D.Float(x, y, LOGO_WIDTH, LOGO_HEIGHT, 20, 20); RoundRectangle2D.Float round = new RoundRectangle2D.Float(x, y, logoW, logoH, 20, 20);
gs.setColor(Color.WHITE); gs.setColor(Color.WHITE);
gs.draw(round);// 绘制圆弧矩形 gs.draw(round);// 绘制圆弧矩形
gs.dispose(); gs.dispose();
...@@ -587,75 +589,80 @@ public class QrcodeUtils { ...@@ -587,75 +589,80 @@ public class QrcodeUtils {
} }
} }
/** public static String createGradientColorLogoQr(String content, String srcImagePath, Color newColor, Color oldColor, Integer qrH, Integer qrW, Integer logoH, Integer logoW) {
* 创建渐变色带logo的二维码
* @param content
* @param srcImagePath
* @return
*/
public static String createGradientColorLogoQr(String content, String srcImagePath,Color newColor,Color oldColor) {
LOGGER.info("【二维码】 创建渐变色带logo的二维码,<START>.[content]=" + content+",srcImagePath="+srcImagePath+",newColor="+newColor+"oldColor="+oldColor);
try { try {
BufferedImage scaleImage = scale(srcImagePath, LOGO_WIDTH, LOGO_HEIGHT, true); BufferedImage scaleImage = scale(srcImagePath, logoW, logoH, true);
int[][] srcPixels = new int[LOGO_WIDTH][LOGO_HEIGHT]; int[][] srcPixels = new int[logoW][logoH];
for (int i = 0; i < scaleImage.getWidth(); i++) { for (int i = 0; i < scaleImage.getWidth(); i++) {
for (int j = 0; j < scaleImage.getHeight(); j++) { for (int j = 0; j < scaleImage.getHeight(); j++) {
srcPixels[i][j] = scaleImage.getRGB(i, j); srcPixels[i][j] = scaleImage.getRGB(i, j);
} }
} }
Map<EncodeHintType, Object> hint = new HashMap<EncodeHintType, Object>(); Map<EncodeHintType, Object> hint = new HashMap<>();
hint.put(EncodeHintType.CHARACTER_SET, "utf-8"); hint.put(EncodeHintType.CHARACTER_SET, "utf-8");
hint.put(EncodeHintType.ERROR_CORRECTION, level); hint.put(EncodeHintType.ERROR_CORRECTION, level);
hint.put(EncodeHintType.MARGIN, margin); //设置白边 hint.put(EncodeHintType.MARGIN, margin); //设置白边
// 生成二维码 // 生成二维码
BitMatrix matrix = mutiWriter.encode(content, BarcodeFormat.QR_CODE, ImageConstant.QRCODE_WITH, ImageConstant.QRCODE_HEIGHT, hint); BitMatrix matrix = mutiWriter.encode(content, BarcodeFormat.QR_CODE, qrW, qrH, hint);
deleteWhite(matrix); deleteWhite(matrix);
// 二维矩阵转为一维像素数组 // 二维矩阵转为一维像素数组
int halfW = matrix.getWidth() / 2; int halfW = matrix.getWidth() / 2;
int halfH = matrix.getHeight() / 2; int halfH = matrix.getHeight() / 2;
int[] pixels = new int[ImageConstant.QRCODE_WITH * ImageConstant.QRCODE_HEIGHT]; int[] pixels = new int[qrW * qrH];
int imhHalfW = logoW / 2;
for (int y = 0; y < matrix.getHeight(); y++) { for (int y = 0; y < matrix.getHeight(); y++) {
for (int x = 0; x < matrix.getWidth(); x++) { for (int x = 0; x < matrix.getWidth(); x++) {
// 左上角颜色,根据自己需要调整颜色范围和颜色 // 左上角颜色,根据自己需要调整颜色范围和颜色
if (x > halfW - IMAGE_HALF_WIDTH && x < halfW + IMAGE_HALF_WIDTH && y > halfH - IMAGE_HALF_WIDTH if (x > halfW - imhHalfW && x < halfW + imhHalfW && y > halfH - imhHalfW
&& y < halfH + IMAGE_HALF_WIDTH) { && y < halfH + imhHalfW) {
pixels[y * ImageConstant.QRCODE_WITH + x] = srcPixels[x - halfW + IMAGE_HALF_WIDTH][y - halfH + IMAGE_HALF_WIDTH]; pixels[y * qrW + x] = srcPixels[x - halfW + imhHalfW][y - halfH + imhHalfW];
} else if ((x > halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH && x < halfW - IMAGE_HALF_WIDTH + FRAME_WIDTH } else if ((x > halfW - imhHalfW - FRAME_WIDTH && x < halfW - imhHalfW + FRAME_WIDTH
&& y > halfH - IMAGE_HALF_WIDTH - FRAME_WIDTH && y < halfH + IMAGE_HALF_WIDTH + FRAME_WIDTH) && y > halfH - imhHalfW - FRAME_WIDTH && y < halfH + imhHalfW + FRAME_WIDTH)
|| (x > halfW + IMAGE_HALF_WIDTH - FRAME_WIDTH && x < halfW + IMAGE_HALF_WIDTH + FRAME_WIDTH || (x > halfW + imhHalfW - FRAME_WIDTH && x < halfW + imhHalfW + FRAME_WIDTH
&& y > halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH && y > halfW - imhHalfW - FRAME_WIDTH
&& y < halfH + IMAGE_HALF_WIDTH + FRAME_WIDTH) && y < halfH + imhHalfW + FRAME_WIDTH)
|| (x > halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH && x < halfW + IMAGE_HALF_WIDTH + FRAME_WIDTH || (x > halfW - imhHalfW - FRAME_WIDTH && x < halfW + imhHalfW + FRAME_WIDTH
&& y > halfH - IMAGE_HALF_WIDTH - FRAME_WIDTH && y > halfH - imhHalfW - FRAME_WIDTH
&& y < halfH - IMAGE_HALF_WIDTH + FRAME_WIDTH) && y < halfH - imhHalfW + FRAME_WIDTH)
|| (x > halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH && x < halfW + IMAGE_HALF_WIDTH + FRAME_WIDTH || (x > halfW - imhHalfW - FRAME_WIDTH && x < halfW + imhHalfW + FRAME_WIDTH
&& y > halfH + IMAGE_HALF_WIDTH - FRAME_WIDTH && y > halfH + imhHalfW - FRAME_WIDTH
&& y < halfH + IMAGE_HALF_WIDTH + FRAME_WIDTH)) { && y < halfH + imhHalfW + FRAME_WIDTH)) {
pixels[y * ImageConstant.QRCODE_WITH + x] = 0xfffffff; pixels[y * qrW + x] = 0xfffffff;
// 在图片四周形成边框 // 在图片四周形成边框
} else { } else {
// 二维码颜色 13 72 107 50 165 162 // 二维码颜色 13 72 107 50 165 162
int num1 = (int) (oldColor.getRed() - (oldColor.getRed() - newColor.getRed())*1.0 / matrix.getHeight() * (y + 1)); int num1 = (int) (oldColor.getRed() - (oldColor.getRed() - newColor.getRed()) * 1.0 / matrix.getHeight() * (y + 1));
int num2 = (int) (oldColor.getGreen() - (oldColor.getGreen() - newColor.getGreen())*1.0 / matrix.getHeight() * (y + 1)); int num2 = (int) (oldColor.getGreen() - (oldColor.getGreen() - newColor.getGreen()) * 1.0 / matrix.getHeight() * (y + 1));
int num3 = (int) (oldColor.getBlue() - (oldColor.getBlue() - newColor.getBlue()) *1.0/ matrix.getHeight() * (y + 1)); int num3 = (int) (oldColor.getBlue() - (oldColor.getBlue() - newColor.getBlue()) * 1.0 / matrix.getHeight() * (y + 1));
Color color = new Color(num1, num2, num3); Color color = new Color(num1, num2, num3);
int colorInt = color.getRGB(); int colorInt = color.getRGB();
// 此处可以修改二维码的颜色,可以分别制定二维码和背景的颜色; // 此处可以修改二维码的颜色,可以分别制定二维码和背景的颜色;
pixels[y * ImageConstant.QRCODE_WITH + x] = matrix.get(x, y) ? colorInt : 16777215; pixels[y * qrW + x] = matrix.get(x, y) ? colorInt : 16777215;
// 0x000000:0xffffff // 0x000000:0xffffff
} }
} }
} }
BufferedImage image = new BufferedImage(ImageConstant.QRCODE_WITH, ImageConstant.QRCODE_HEIGHT, BufferedImage.TYPE_INT_RGB); BufferedImage image = new BufferedImage(qrW, qrH, BufferedImage.TYPE_INT_RGB);
image.getRaster().setDataElements(0, 0, ImageConstant.QRCODE_WITH, ImageConstant.QRCODE_HEIGHT, pixels); image.getRaster().setDataElements(0, 0, qrW, qrH, pixels);
return uploadImage(image); return uploadImage(image);
} catch (BizException e) { } catch (BizException e) {
throw new BizException(e.getCode(), e.getMessage()); throw new BizException(e.getCode(), e.getMessage());
}catch (Exception e) { } catch (Exception e) {
LOGGER.error("【二维码】创建渐变色带logo的二维码失败,<ERROR>:" + e.getMessage(), e); LOGGER.error("【二维码】创建渐变色带logo的二维码失败,<ERROR>:" + e.getMessage(), e);
throw new FileException(FileException.FILE_UPLOAD_FAILURE, "创建渐变色带logo的二维码失败"); throw new FileException(FileException.FILE_UPLOAD_FAILURE, "创建渐变色带logo的二维码失败");
} }
}
/**
* 创建渐变色带logo的二维码
*
* @param content
* @param srcImagePath
* @return
*/
public static String createGradientColorLogoQr(String content, String srcImagePath, Color newColor, Color oldColor) {
LOGGER.info("【二维码】 创建渐变色带logo的二维码,<START>.[content]=" + content + ",srcImagePath=" + srcImagePath + ",newColor=" + newColor + "oldColor=" + oldColor);
return createGradientColorLogoQr(content, srcImagePath, newColor, oldColor, ImageConstant.QRCODE_HEIGHT, ImageConstant.QRCODE_WITH, LOGO_HEIGHT, LOGO_WIDTH);
} }
/** /**
......
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