最近在做微信公众平台业务,其中遇到了获取微信二维码问题。
记录一次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/81223372https://blog.youkuaiyun.com/qq_26917447/article/details/81223372