故不积跬步,无以至千里;不积小流,无以成江海。 ——荀子《劝学篇》
每一篇优秀的文章都非一日写成,定期写点东西,有所积累,并不断完善。
今天借助网上各位前辈的优秀文章,尝试自己封装了一个图片的工具类,分享给大家。
话不多说,代码如下↓
package com.lm.util;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
/**
*
* @package com.lm.util
* @file QRcodeUtil.java 创建时间:2019年11月29日下午8:53:33
* @title 二维码工具类
* @description
* @author mr.bone
* @version 1.0
*
*/
public class ImageUtil {
/**
* zxing 方式生成二维码操作对象(BitMatrix)
*/
private static BitMatrix createQRcode(String content,Map<String,Object> param) throws WriterException{
final int width = (int) param.get("width");
final int height = (int) param.get("height");
final Map<EncodeHintType,Object> hints = new HashMap<EncodeHintType,Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
return new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hints);
}
/**
* 生成二维码并放入输出流中
*/
public static void createQRcodeToOutputStream(String content,Map<String,Object> param,OutputStream out) throws WriterException, IOException {
final BitMatrix bitMatrix = createQRcode(content,param);
//将二维码操作对象(BitMatrix)放入内存中图片缓存区并生成BufferedImage
BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix, new MatrixToImageConfig(Color.BLACK.getRGB(), Color.WHITE.getRGB()));
final String format = (String) param.get("format");
//将BufferedImage=>byte[]=>OutputStream
ImageIO.write(bufferedImage,format,out);
}
}
欲探其祥,api如下↓
接口:com.google.zxing.Writer
方法名 | 返回值类型 |
---|---|
encode(String contents, BarcodeFormat format, int width, int height) | BitMatrix |
encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType,?> hints) | BitMatrix |
参数类型 | 参数名 | 描述 |
---|---|---|
String | contents | 编码的内容(想要二维码不同,编码内容必须不同可通过随机数实现例如(String content = "http://aaaa.bbb.ccc?t="+Math.Random(); ) |
BarcodeFormat | format | 编码的方式(BarcodeFormat.QR_CODE :二维码、条形码…) |
int | width | 首选的宽度 |
int | height | 首选的高度 |
Map<EncodeHintType,?> | hints | 编码时的额外参数 |
额外参数
参数 | 说明 |
---|---|
ERROR_CORRECTION | 容错率,指定容错等级,例如二维码中使用的ErrorCorrectionLevel , Aztec使用Integer (分为四个等级:L/M/Q/H, 等级越高,容错率越高,识别速度降低。例如一个角被损坏,容错率高的也许能够识别出来。通常为H) |
CHARACTER_SET | 编码集(编码集,通常有中文,设置为 utf-8) |
DATA_MATRIX_SHAPE | 指定生成的数据矩阵的形状,类型为SymbolShapeHint |
MARGIN | 生成条码的时候使用,指定边距,单位像素,受格式的影响。类型Integer , 或String 代表的数字类型 |
PDF417_COMPACT | 指定是否使用PDF417紧凑模式(具体含义不懂)类型Boolean |
PDF417_COMPACTION | 指定PDF417的紧凑类型 |
PDF417_DIMENSIONS | 指定PDF417的最大最小行列数 |
AZTEC_LAYERS | aztec编码相关,不理解 |
QR_VERSION | 指定二维码版本,版本越高越复杂,反而不容易解析 |
方法:ImageIO.write(BufferedImage image,String format,OutputStream out);
参数类型 | 参数名 | 描述 |
---|---|---|
BufferedImage | image | BufferedImage 对象 |
BarcodeFormat | format | 图片格式,gif,png等 |
OutputStream | out | 输出流 |
参考链接:
https://www.jianshu.com/p/6607e69b1121
https://blog.csdn.net/jiachunchun/article/details/89670721
随时补充,下期再见~~