Java如何生成二维码
现在二维码已经到处普及,一般项目在个人中心这一块都会被要求放一个二维码。现在分享我在项目中一个小案例
首先我们需要在maven中引入谷歌的zxing包的依赖
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
控制层
@RequestMapping("/getReaderQRCode")
public void getReaderQRCode(HttpServletResponse response) {
ReaderEntity reader = ReaderSessionHelper.getInstance().getReader();
if (reader != null) {
String cardno = reader.getCardno(); //获取session中的reader信息来作为二维码保存信息
Map<String, String> jsonMap = new HashMap<>();
jsonMap.put("cardno", cardno);
jsonMap.put("token", String.valueOf(System.currentTimeMillis()));
String cardData = JSONObject.toJSONString(jsonMap);
/* String qrval = "QRC:" + new DESPlus().encrypt(cardData);*/
String qrval = new DESPlus().encrypt(cardData); //这里使用了desplus来进行加密
try (OutputStream outputStream = response.getOutputStream()) {
ZxingUtil.encode2DCode(outputStream, qrval); //调用zxing的方法,来生成二维码。后面会贴出该方法代码
} catch (IOException | WriterException e) {
logger.warn("生成读者二维码失败", e);
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
} else {
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
}
}
//二维码的工具类
public class ZxingUtil {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private static final int DEFAULT_HEIGHT = 300;// 二维码图片宽度
private static final int DEFAULT_WIDTH = 300;// 二维码图片高度
private static final String DEFAULT_FORMAT = "gif";// 二维码的图片格式
private static final int BARCODE_HEIGHT = 30;
private static final int BARCODE_WIDTH = 150;
private static final String BARCODE_FORMAT = "png";
/**
* 生成二维码 到指定 路径 、这个方法二维码会返回到你请求的位置,通过response响应,上面的控制层方法调用的是该方法,如果不用返回到请求的位置,那么就使用下面的方法,可以输出到你想要到的地方
*
* @param outputStream
* 到指定 路径 、FILE
* @param text
* 可以是网址 和 其他,网址就是跳转
* @throws WriterException
* @throws IOException
*/
public static void encode2DCode(OutputStream outputStream, String text)
throws WriterException, IOException {
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 内容所使用字符集编码
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(text,
BarcodeFormat.QR_CODE, DEFAULT_WIDTH, DEFAULT_HEIGHT, hints);
BufferedImage image = toBufferedImage(bitMatrix);
/*
* Graphics2D gs = image.createGraphics(); //载入logo Image img =
* ImageIO.read(new File(srcImagePath)); gs.drawImage(img, 125, 125,
* null); gs.dispose(); img.flush();
*/
if (!ImageIO.write(image, DEFAULT_FORMAT, outputStream)) {
throw new IOException("Could not write an image of format "
+ DEFAULT_FORMAT + " to stream");
}
}
}
/**
* 生成二维码 到指定 路径 、二维码会输出到某一个位置
*
* @param outputFile
* 到指定 路径 、FILE
* @param text
* 可以是网址 和 其他,网址就是跳转
* @throws WriterException
* @throws IOException
*/
public static void encode2DCode(File outputFile, String text)
throws WriterException, IOException {
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 内容所使用字符集编码
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(text,
BarcodeFormat.QR_CODE, DEFAULT_WIDTH, DEFAULT_HEIGHT, hints);
writeToFile(bitMatrix, DEFAULT_FORMAT, outputFile);
}
desplus加密方法网上很多,我就不贴出来。
好了,我已经最近遇到的事记录下来。