zxing 生成二维码 带logo

·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);
	}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猩猩之火

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值