Commit 04bcf57e by 李传峰

Merge branch 'feature/1007659' into 'release'

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

See merge request rays/pcloud-common-parent!292
parents 171c7594 eff16e15
......@@ -483,35 +483,37 @@ public class QrcodeUtils {
/**
* 创建带logo二维码
*
* @param content
* 二维码响应地址
* @param content 二维码响应地址
* @return
* @throws Exception
*/
public static String createLogoQr(String url, String logoPath, Integer onColor) throws BizException {
LOGGER.info("【二维码】创建带logo二维码,<START>.[url]=" + url+",logoPath="+logoPath+",onColor="+onColor);
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
LOGGER.info("【二维码】创建带logo二维码,<START>.[url]=" + url + ",logoPath=" + logoPath + ",onColor=" + onColor);
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.ERROR_CORRECTION, level);
hints.put(EncodeHintType.MARGIN, margin); // 设置白边
try {
// 生成矩阵
BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, ImageConstant.QRCODE_WITH,
ImageConstant.QRCODE_HEIGHT, hints);
BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, qrW, qrH, hints);
BufferedImage image = toBufferedImage(bitMatrix, onColor);
Graphics2D gs = image.createGraphics();
// 载入logo
byte[] file = FileUtils.downloadByteFromUrl(logoPath);
ByteArrayInputStream in = new ByteArrayInputStream(file);
BufferedImage srcImage = ImageIO.read(in);
Image img = srcImage.getScaledInstance(LOGO_WIDTH, LOGO_HEIGHT, BufferedImage.SCALE_SMOOTH);
int x = (image.getWidth() - LOGO_WIDTH) / 2;
int y = (image.getHeight() - LOGO_HEIGHT) / 2;
gs.drawImage(img, x, y, LOGO_WIDTH, LOGO_HEIGHT, null);
Image img = srcImage.getScaledInstance(logoW, logoH, BufferedImage.SCALE_SMOOTH);
int x = (image.getWidth() - logoW) / 2;
int y = (image.getHeight() - logoH) / 2;
gs.drawImage(img, x, y, logoW, logoH, null);
BasicStroke stroke = new BasicStroke(5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
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.draw(round);// 绘制圆弧矩形
gs.dispose();
......@@ -587,75 +589,80 @@ public class QrcodeUtils {
}
}
/**
* 创建渐变色带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);
public static String createGradientColorLogoQr(String content, String srcImagePath, Color newColor, Color oldColor, Integer qrH, Integer qrW, Integer logoH, Integer logoW) {
try {
BufferedImage scaleImage = scale(srcImagePath, LOGO_WIDTH, LOGO_HEIGHT, true);
int[][] srcPixels = new int[LOGO_WIDTH][LOGO_HEIGHT];
BufferedImage scaleImage = scale(srcImagePath, logoW, logoH, true);
int[][] srcPixels = new int[logoW][logoH];
for (int i = 0; i < scaleImage.getWidth(); i++) {
for (int j = 0; j < scaleImage.getHeight(); 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.ERROR_CORRECTION, level);
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);
// 二维矩阵转为一维像素数组
int halfW = matrix.getWidth() / 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 x = 0; x < matrix.getWidth(); x++) {
// 左上角颜色,根据自己需要调整颜色范围和颜色
if (x > halfW - IMAGE_HALF_WIDTH && x < halfW + IMAGE_HALF_WIDTH && y > halfH - IMAGE_HALF_WIDTH
&& y < halfH + IMAGE_HALF_WIDTH) {
pixels[y * ImageConstant.QRCODE_WITH + x] = srcPixels[x - halfW + IMAGE_HALF_WIDTH][y - halfH + IMAGE_HALF_WIDTH];
} else if ((x > halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH && x < halfW - IMAGE_HALF_WIDTH + FRAME_WIDTH
&& y > halfH - IMAGE_HALF_WIDTH - FRAME_WIDTH && y < halfH + IMAGE_HALF_WIDTH + FRAME_WIDTH)
|| (x > halfW + IMAGE_HALF_WIDTH - FRAME_WIDTH && x < halfW + IMAGE_HALF_WIDTH + FRAME_WIDTH
&& y > halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH
&& y < halfH + IMAGE_HALF_WIDTH + FRAME_WIDTH)
|| (x > halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH && x < halfW + IMAGE_HALF_WIDTH + FRAME_WIDTH
&& y > halfH - IMAGE_HALF_WIDTH - FRAME_WIDTH
&& y < halfH - IMAGE_HALF_WIDTH + FRAME_WIDTH)
|| (x > halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH && x < halfW + IMAGE_HALF_WIDTH + FRAME_WIDTH
&& y > halfH + IMAGE_HALF_WIDTH - FRAME_WIDTH
&& y < halfH + IMAGE_HALF_WIDTH + FRAME_WIDTH)) {
pixels[y * ImageConstant.QRCODE_WITH + x] = 0xfffffff;
if (x > halfW - imhHalfW && x < halfW + imhHalfW && y > halfH - imhHalfW
&& y < halfH + imhHalfW) {
pixels[y * qrW + x] = srcPixels[x - halfW + imhHalfW][y - halfH + imhHalfW];
} else if ((x > halfW - imhHalfW - FRAME_WIDTH && x < halfW - imhHalfW + FRAME_WIDTH
&& y > halfH - imhHalfW - FRAME_WIDTH && y < halfH + imhHalfW + FRAME_WIDTH)
|| (x > halfW + imhHalfW - FRAME_WIDTH && x < halfW + imhHalfW + FRAME_WIDTH
&& y > halfW - imhHalfW - FRAME_WIDTH
&& y < halfH + imhHalfW + FRAME_WIDTH)
|| (x > halfW - imhHalfW - FRAME_WIDTH && x < halfW + imhHalfW + FRAME_WIDTH
&& y > halfH - imhHalfW - FRAME_WIDTH
&& y < halfH - imhHalfW + FRAME_WIDTH)
|| (x > halfW - imhHalfW - FRAME_WIDTH && x < halfW + imhHalfW + FRAME_WIDTH
&& y > halfH + imhHalfW - FRAME_WIDTH
&& y < halfH + imhHalfW + FRAME_WIDTH)) {
pixels[y * qrW + x] = 0xfffffff;
// 在图片四周形成边框
} else {
// 二维码颜色 13 72 107 50 165 162
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 num3 = (int) (oldColor.getBlue() - (oldColor.getBlue() - newColor.getBlue()) *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 num3 = (int) (oldColor.getBlue() - (oldColor.getBlue() - newColor.getBlue()) * 1.0 / matrix.getHeight() * (y + 1));
Color color = new Color(num1, num2, num3);
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
}
}
}
BufferedImage image = new BufferedImage(ImageConstant.QRCODE_WITH, ImageConstant.QRCODE_HEIGHT, BufferedImage.TYPE_INT_RGB);
image.getRaster().setDataElements(0, 0, ImageConstant.QRCODE_WITH, ImageConstant.QRCODE_HEIGHT, pixels);
BufferedImage image = new BufferedImage(qrW, qrH, BufferedImage.TYPE_INT_RGB);
image.getRaster().setDataElements(0, 0, qrW, qrH, pixels);
return uploadImage(image);
} catch (BizException e) {
throw new BizException(e.getCode(), e.getMessage());
}catch (Exception e) {
} catch (Exception e) {
LOGGER.error("【二维码】创建渐变色带logo的二维码失败,<ERROR>:" + e.getMessage(), e);
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