二维码扫描已是时下非常常用的分享、推广的方式,Google的zxing是一个很好用的二维码工具包,本文分享如何使用zxing生成自定义的二维码。
tips:开发工具IntelliJ IDEA
- 新建MAVEN工程
- pom.xml 新增zxing和junit依赖
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
3.实现二维码功能逻辑
/**
* 生成二维码图片
* @param content 二维码内容
* @param width 宽
* @param height 高
* @param margin 二维码外边距,0到4
* @param imgPath 二维码图片存放路径
*/
public void createQRCode(String content,int width,int height,int margin,String imgPath) throws WriterException, IOException {
/**二维码参数设定*/
Hashtable<EncodeHintType,Object> hintTypes = new Hashtable<EncodeHintType, Object>();
hintTypes.put(EncodeHintType.CHARACTER_SET,CharacterSetECI.UTF8);
hintTypes.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//容错级别,共4级,详细问度娘
hintTypes.put(EncodeHintType.MARGIN,margin);//二维码外边距
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE,width,height,hintTypes);//创建二维码,本质为二位数组
/**将二维码转为图片*/
BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
bufferedImage.setRGB(i,j,bitMatrix.get(i,j)? Color.BLACK.getRGB():Color.WHITE.getRGB());
}
}
File imageFile = new File(imgPath+"myQRCode.png");
if (!imageFile.getParentFile().exists()){
imageFile.getParentFile().mkdirs();
}
ImageIO.write(bufferedImage,"png",imageFile);
}
/**
* 生成二维码图片字节数组,可将字节数组写入文件、经base64编码后写入FreeMarker模版等操作
* @param content 二维码内容
* @param width 宽
* @param height 高
* @param margin 外边距
* @return 二维码对应的字节数组
* @throws Exception
*/
public byte[] getQRCodeBytes(String content,int width,int height,int margin) throws Exception{
Hashtable<EncodeHintType,Object> hintTypes = new Hashtable<EncodeHintType, Object>();
ByteArrayOutputStream outputStream = null;
hintTypes.put(EncodeHintType.CHARACTER_SET,CharacterSetECI.UTF8);//设置编码
hintTypes.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//设置容错级别
hintTypes.put(EncodeHintType.MARGIN,margin);//设置外边距
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE,width,height,hintTypes);//获取二维码
//将二维码写入图片
BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
bufferedImage.setRGB(i,j,bitMatrix.get(i,j)? Color.RED.getRGB():Color.WHITE.getRGB());
}
}
//将图片转为字节数组
outputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage,"png",outputStream);
return outputStream.toByteArray();
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
if (outputStream != null){
outputStream.close();
}
}
}
4.好了,代码码完了,测试一波
@Test
public void testCreateDefaultQRCode() throws IOException, WriterException {
QRCodeService service = new QRCodeService();
service.createQRCode("hello world",300,300,"./");
}
@Test
public void testGetQRCodeBytes() throws Exception {
QRCodeService service = new QRCodeService();
byte[] bytes = service.getQRCodeBytes("https://www.runoob.com",500,500,1);
FileUtils.writeByteArrayToFile(new File("我的二维码.png"),bytes,true);
}
- FileUtils为Apache的开源工具包
- 以上源码可在GitHub上查看下载,点击查看