·1生成带logo的二维码,并转base64
public static String generateBase64Img(String code_url,int width,int height,String format,String logopath) throws WriterException, IOException {
if(StringUtil.isEmpty(format)) {
format = "png";
}
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
/*
* 设置容错级别,默认为ErrorCorrectionLevel.L
* 因为中间加入logo所以建议你把容错级别调至H,否则可能会出现识别不了
*/
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//设置空白边距的宽度
hints.put(EncodeHintType.MARGIN, 1); //default is 4
//生成矩阵
BitMatrix bitMatrix = new MultiFormatWriter().encode(code_url, BarcodeFormat.QR_CODE, width, height, hints);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
//转图片
BufferedImage bufferedImage = toBufferedImage(bitMatrix,logopath);
ImageIO.write(bufferedImage, format, bos);
byte[] binary = bos.toByteArray();
return Base64.encodeBase64String(binary);
}
/**
* matrix转为二维码图片,如果有logo文件,则生成带logo的二维码
* @param matrix
* @return
* @author Mr G
* @throws IOException
*/
public static BufferedImage toBufferedImage(BitMatrix matrix,String logopath) throws IOException {
int width = matrix.getWidth();
int height = matrix.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, matrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
}
}
image.flush();
if(!StringUtil.isEmpty(logopath)) {
File logFile = new File(logopath);
if(logFile!=null && logFile.exists()) {
//读取logo图片
BufferedImage logo = ImageIO.read(logFile);
//绘制logo
BufferedImage logoMatrix = LogoMatrix(image, logo);
return logoMatrix;
}
}
return image;
}
/**
* 设置 logo
* @param matrixImage 源二维码图片
* @return 返回带有logo的二维码图片
* @throws IOException
* @author MR G
*/
public static BufferedImage LogoMatrix(BufferedImage matrixImage, BufferedImage logo) throws IOException{
/**
* 读取二维码图片,并构建绘图对象
*/
Graphics2D g2 = matrixImage.createGraphics();
int matrixWidth = matrixImage.getWidth();
int matrixHeigh = matrixImage.getHeight();
//开始绘制图片
g2.drawImage(logo,matrixWidth/5*2,matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5, null);//绘制
BasicStroke stroke = new BasicStroke(5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
g2.setStroke(stroke);// 设置笔画对象
//指定弧度的圆角矩形 绘制白边
RoundRectangle2D.Float round = new RoundRectangle2D.Float(matrixWidth/5*2, matrixHeigh/5*2, matrixWidth/5, matrixHeigh/5,0,0);
g2.setColor(Color.white);
g2.draw(round);// 绘制圆弧矩形
//设置logo 有一道灰色边框
BasicStroke stroke2 = new BasicStroke(1,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
g2.setStroke(stroke2);// 设置笔画对象
RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(matrixWidth/5*2+2, matrixHeigh/5*2+2, matrixWidth/5-4, matrixHeigh/5-4,20,20);
g2.setColor(new Color(128,128,128));
g2.draw(round2);// 绘制圆弧矩形*/
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
matrixImage.flush() ;
g2.dispose();
return matrixImage ;
}
2如果不带logo,那就简单了:
/**
* 生成图片
* @param code_url 二维码地址
* @param response
* @param width
* @param height
* @param format 图片格式eg: jpg、png
* @throws Exception
* @author Mr G
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static String generateBase64(String code_url, int width,int height,String format) throws Exception {
if(StringUtil.isEmpty(format)) {
format = "png";
}
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(code_url, BarcodeFormat.QR_CODE, width, height, hints);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
MatrixToImageWriter.writeToStream(bitMatrix, format, bos);
byte[] binary = bos.toByteArray();
return ImgUtil.toBase64(binary);
}