java使字符串转二维码

        最近在做微信公众平台业务,其中遇到了获取微信二维码问题。

记录一次url转为二维码的问题        

String qrCodeUrl = HttpUtil.post(
		String.format(properties.getCreate_qrCode_url(),
			redisconfig.get(Constants.RedisPrefix.WX_ACCESS_TOKEN)),
		new Gson().toJson(new QrCodeRequest(QRCODE_EXPIRES_IN)));
OutputStream stream = null;
	try
	{
	    String qrCode_url = (String) parseObject.get("url");

	    // 载入图像
	    BitMatrix byteMatrix = new MultiFormatWriter().encode(new String(qrCode_url.getBytes(), "iso-8859-1"),
		    BarcodeFormat.QR_CODE, null == sizeMap.get("width") ? 200 : sizeMap.get("width"), 200);

	    BufferedImage image = toBufferedImage(byteMatrix);
	    // 将四位数字的验证码保存到Session中。
	    // 禁止图像缓存。
	    response.setHeader("Pragma", "no-cache");
	    response.setHeader("Cache-Control", "no-cache");
	    response.setDateHeader("Expires", 0);
	    response.setContentType("image/jpeg");
	    // 将图像输出到Servlet输出流中。
	    stream = response.getOutputStream();
	    ImageIO.write(image, "jpeg", stream);

	    stream.flush();
	} catch (IOException e)
	{
	    e.printStackTrace();
	} catch (WriterException e)
	{
	    e.printStackTrace();
	} finally
	{
	    if (null != stream)
	    {
		try
		{
		    stream.close();
		} catch (IOException e)
		{
		    e.printStackTrace();
		}
	    }

	}
    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;

    private BufferedImage toBufferedImage(BitMatrix matrix)
    {
	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) ? BLACK : WHITE);
	    }
	}
	return image;
    }
<dependency>
     <groupId>com.google.zxing</groupId>
     <artifactId>core</artifactId>
     <version>3.3.0</version>
</dependency>

转载:https://blog.youkuaiyun.com/qq_26917447/article/details/81223372

icon-default.png?t=LA92https://blog.youkuaiyun.com/qq_26917447/article/details/81223372

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值