Commit 86ae8468 by tc

Merge branch 'master' of…

Merge branch 'master' of ssh://begitlab.chubanyun.me:12122/rays/pcloud-common-parent into feature/listUtil
parents 67cd11ad 4d833466
......@@ -323,6 +323,32 @@ public class QrcodeUtils {
}
}
/**
* 创建带颜色的二维码
* @param url
* @param onColor
* @return
* @throws BizException
*/
public static String createColorWithMargin(String url, Integer onColor,Integer marginNum) throws BizException {
LOGGER.info("【二维码】创建带颜色的二维码,<START>.[url]=" + url+"[onClor]="+onColor);
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.ERROR_CORRECTION, level);
hints.put(EncodeHintType.MARGIN, null==marginNum ? margin : marginNum); // 设置白边
try {
// 生成矩阵
BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, ImageConstant.QRCODE_WITH,
ImageConstant.QRCODE_HEIGHT, hints);
BufferedImage bufferedImage = toBufferedImage(bitMatrix, onColor);
bufferedImage.flush();
return uploadImage(bufferedImage);
} catch (Exception e) {
LOGGER.error("【二维码】创建二维码失败,<ERROR>:" + e.getMessage(), e);
throw new FileException(FileException.FILE_UPLOAD_FAILURE, "创建二维码失败");
}
}
/**
* 创建渐变色二维码
......@@ -372,6 +398,53 @@ public class QrcodeUtils {
}
/**
* 创建渐变色二维码
*
* @param content
* 二维码响应地址
* @return
* @throws Exception
*/
public static String createGradientColorQr(String url, Color newColor, Color oldColor,Integer marginNum) throws BizException {
LOGGER.info("【二维码】创建渐变色二维码,<START>.[url]=" + url+"newColor="+newColor+"oldColor="+oldColor);
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.ERROR_CORRECTION, level);
hints.put(EncodeHintType.MARGIN, null==marginNum ? margin : marginNum); // 设置白边
try {
// 生成矩阵
BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, ImageConstant.QRCODE_WITH,
ImageConstant.QRCODE_HEIGHT, hints);
BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
bufferedImage.flush();
// 设置二维码颜色
int[] pixels = new int[ImageConstant.QRCODE_WITH * ImageConstant.QRCODE_HEIGHT];
for (int y = 0; y < bitMatrix.getHeight(); y++) {
for (int x = 0; x < bitMatrix.getWidth(); x++) {
// 二维码颜色
int num1 = (int) (oldColor.getRed()
- (oldColor.getRed() - newColor.getRed()) * 1.0 / bitMatrix.getHeight() * (y + 1));
int num2 = (int) (oldColor.getGreen()
- (oldColor.getGreen() - newColor.getGreen()) * 1.0 / bitMatrix.getHeight() * (y + 1));
int num3 = (int) (oldColor.getBlue()
- (oldColor.getBlue() - newColor.getBlue()) * 1.0 / bitMatrix.getHeight() * (y + 1));
Color color = new Color(num1, num2, num3);
int colorInt = color.getRGB();
// 此处可以修改二维码的颜色,可以分别制定二维码和背景的颜色;
pixels[y * ImageConstant.QRCODE_WITH + x] = bitMatrix.get(x, y) ? colorInt : 16777215;
}
}
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);
return uploadImage(image);
} catch (Exception e) {
LOGGER.error("【二维码】创建渐变色二维码失败,<ERROR>:" + e.getMessage(), e);
throw new FileException(FileException.FILE_UPLOAD_FAILURE, "创建渐变色二维码失败");
}
}
/**
* 创建带logo二维码
*
* @param content
......@@ -413,6 +486,49 @@ public class QrcodeUtils {
throw new FileException(FileException.FILE_UPLOAD_FAILURE, "创建带logo二维码失败");
}
}
/**
* 创建带logo二维码
*
* @param content
* 二维码响应地址
* @return
* @throws Exception
*/
public static String createLogoQr(String url, String logoPath, Integer onColor,Integer marginNum) throws BizException {
LOGGER.info("【二维码】创建带logo二维码,<START>.[url]=" + url+",logoPath="+logoPath+",onColor="+onColor);
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.ERROR_CORRECTION, level);
hints.put(EncodeHintType.MARGIN, null==marginNum ? margin : marginNum); // 设置白边
try {
// 生成矩阵
BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, ImageConstant.QRCODE_WITH,
ImageConstant.QRCODE_HEIGHT, 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);
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);
gs.setColor(Color.WHITE);
gs.draw(round);// 绘制圆弧矩形
gs.dispose();
img.flush();
return uploadImage(image);
} catch (Exception e) {
LOGGER.error("【二维码】创建带logo二维码失败,<ERROR>:" + e.getMessage(), e);
throw new FileException(FileException.FILE_UPLOAD_FAILURE, "创建带logo二维码失败");
}
}
/**
* 图片上传文件服务器返回url
......@@ -505,6 +621,77 @@ public class QrcodeUtils {
}
}
/**
* 创建渐变色带logo的二维码
* @param content
* @param srcImagePath
* @return
*/
public static String createGradientColorLogoQr(String content, String srcImagePath,Color newColor,Color oldColor,Integer marginNum) {
LOGGER.info("【二维码】 创建渐变色带logo的二维码,<START>.[content]=" + content+",srcImagePath="+srcImagePath+",newColor="+newColor+"oldColor="+oldColor);
try {
BufferedImage scaleImage = scale(srcImagePath, LOGO_WIDTH, LOGO_HEIGHT, true);
int[][] srcPixels = new int[LOGO_WIDTH][LOGO_HEIGHT];
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>();
hint.put(EncodeHintType.CHARACTER_SET, "utf-8");
hint.put(EncodeHintType.ERROR_CORRECTION, level);
hint.put(EncodeHintType.MARGIN, null==marginNum ? margin : marginNum); //设置白边
// 生成二维码
BitMatrix matrix = mutiWriter.encode(content, BarcodeFormat.QR_CODE, ImageConstant.QRCODE_WITH, ImageConstant.QRCODE_HEIGHT, hint);
deleteWhite(matrix);
// 二维矩阵转为一维像素数组
int halfW = matrix.getWidth() / 2;
int halfH = matrix.getHeight() / 2;
int[] pixels = new int[ImageConstant.QRCODE_WITH * ImageConstant.QRCODE_HEIGHT];
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;
// 在图片四周形成边框
} 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));
Color color = new Color(num1, num2, num3);
int colorInt = color.getRGB();
// 此处可以修改二维码的颜色,可以分别制定二维码和背景的颜色;
pixels[y * ImageConstant.QRCODE_WITH + 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);
return uploadImage(image);
} catch (BizException e) {
throw new BizException(e.getCode(), e.getMessage());
}catch (Exception e) {
LOGGER.error("【二维码】创建渐变色带logo的二维码失败,<ERROR>:" + e.getMessage(), e);
throw new FileException(FileException.FILE_UPLOAD_FAILURE, "创建渐变色带logo的二维码失败");
}
}
private static BufferedImage scale(String srcImageFile, int height,
......
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