matrix.boot.common.utils.BarCodeUtil Maven / Gradle / Ivy
package matrix.boot.common.utils;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import matrix.boot.common.exception.ServiceException;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
/**
* 条形码工具
* @author wangcheng
*/
public class BarCodeUtil {
private static final String FORMAT = "png";
/**
* 宽度
*/
private int width;
/**
* 高度
*/
private int height;
/**
* 文件路径
*/
private String filePath;
private BarCodeUtil() {}
private BarCodeUtil(int width, int height, String filePath) {
this.width = width;
this.height = height;
this.filePath = filePath;
}
/**
* 获取实例
* @param width 宽度
* @param height 高度
* @param filePath 文件路径
* @return 实例
*/
public static BarCodeUtil getInstance(int width, int height, String filePath) {
AssertUtil.notNullTip(filePath, "filePath");
return new BarCodeUtil(width, height, filePath);
}
/**
* 获取图片
* @param content 内容
* @return 图片缓存区
*/
public BufferedImage getBufferedImage(String content) {
int codeWidth = Math.max(3 + (7 * 6) + 5 + (7 * 6) + 3, width);
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.CODE_128, codeWidth, height, null);
return MatrixToImageWriter.toBufferedImage(bitMatrix);
} catch (Exception e) {
throw new ServiceException(e);
}
}
/**
* 写出流
* @param content 内容
* @param stream 输出流
*/
public void write(String content, OutputStream stream) {
int codeWidth = Math.max(3 + (7 * 6) + 5 + (7 * 6) + 3, width);
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.CODE_128, codeWidth, height, null);
MatrixToImageWriter.writeToStream(bitMatrix, FORMAT, stream);
} catch (Exception e) {
throw new ServiceException(e);
}
}
/**
* 获取图片的base64编码
* @param content 内容
* @return base64编码
*/
public String getImageBase64(String content) {
BufferedImage image = getBufferedImage(content);
return ImageUtil.getImageBase64(image);
}
/**
* 输出至文件(文件名随机生成)
* @param content 内容
* @return 随机的文件名
*/
public String writeToFile(String content) {
FileOutputStream fos = null;
try {
BufferedImage bufferedImage = this.getBufferedImage(content);
File file = new File(this.filePath, RandomUtil.getUUID() + "." + "png");
fos = new FileOutputStream(file);
ImageIO.write(bufferedImage, FORMAT, fos);
return file.getName();
} catch (Exception e) {
throw new ServiceException(e);
} finally {
BIOStreamUtil.closeStream(fos);
}
}
/**
* 解析条形码
* @param fileName 文件名
* @return 条形码内容
*/
public String parseFile(String fileName) {
AssertUtil.notNullTip(fileName, "BarCodeHelper:fileName");
try {
BufferedImage image = ImageIO.read(new File(filePath, fileName));
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result = new MultiFormatReader().decode(bitmap, null);
return result.getText();
} catch (Exception e) {
throw new ServiceException(e);
}
}
}