下面列出了com.google.zxing.client.j2se.MatrixToImageWriter#writeToPath ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* @param dir 生成二维码路径
* @param content 内容
* @param width 宽度
* @param height 高度
* @return
*/
public static String createQRCodeImage(String dir, String content, int width, int height) {
try {
File dirFile = null;
if (StringUtils.isNotBlank(dir)) {
dirFile = new File(dir);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
}
String qrcodeFormat = "png";
HashMap<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
File file = new File(dir, UUID.randomUUID().toString() + "." + qrcodeFormat);
MatrixToImageWriter.writeToPath(bitMatrix, qrcodeFormat, file.toPath());
return file.getAbsolutePath();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
private void doWrite(OutputStream os, Path path) throws IOException {
try {
Writer writer = new MultiFormatWriter();
Map<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, StandardCharsets.UTF_8.name());
hints.put(EncodeHintType.MARGIN, Integer.valueOf(margin));
hints.put(EncodeHintType.ERROR_CORRECTION, com.google.zxing.qrcode.decoder.ErrorCorrectionLevel.forBits(errorCorrectionLevel.getBits()));
BitMatrix matrix = writer.encode(uri.toUriString(), BarcodeFormat.QR_CODE, width, height, hints);
if (os != null) {
MatrixToImageWriter.writeToStream(matrix, imageFormatName, os);
}
else {
MatrixToImageWriter.writeToPath(matrix, imageFormatName, path);
}
} catch (WriterException e) {
throw new IOException(e);
}
}
/**
* 条形码编码
*
* @param contents
* @param width
* @param height
* @param imgPath
*/
public static void encode(String contents, int width, int height, String imgPath) {
int codeWidth = 3 + // start guard
(7 * 6) + // left bars
5 + // middle guard
(7 * 6) + // right bars
3; // end guard
codeWidth = Math.max(codeWidth, width);
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.EAN_13, codeWidth, height, null);
MatrixToImageWriter.writeToPath(bitMatrix, "png", new File(imgPath).toPath());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 二维码编码
*
* @param contents
* @param width
* @param height
* @param imgPath
*/
public static void encode2(String contents, int width, int height, String imgPath) {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
// 指定纠错等级
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
// 指定编码格式
hints.put(EncodeHintType.CHARACTER_SET, "GBK");
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
MatrixToImageWriter.writeToPath(bitMatrix, "png", new File(imgPath).toPath());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param content
* @param format
* @param height
* @return
*/
public static File saveBarCode(String content, BarcodeFormat format, int height) {
BitMatrix bitMatrix = createBarCode(content, format, height);
String fileName = String.format("BarCode-%d.png", System.currentTimeMillis());
File dest = SysConfiguration.getFileOfTemp(fileName);
try {
MatrixToImageWriter.writeToPath(bitMatrix, "png", dest.toPath());
return dest;
} catch (IOException ex) {
throw new RebuildException("Write BarCode failed : " + content, ex);
}
}
/**
* 生成二维码
*
* @param content 条码文本内容
* @param width 条码宽度
* @param height 条码高度
* @param fileType 文件类型,如png
* @param savePath 保存路径
*/
@SuppressWarnings({"rawtypes", "unchecked", "deprecation"})
public static void encode(String content, int width, int height, String fileType, String savePath) throws IOException, WriterException {
Charset charset = Charset.forName("UTF-8");
CharsetEncoder encoder = charset.newEncoder();
byte[] b = encoder.encode(CharBuffer.wrap(content)).array();
String data = new String(b, "iso8859-1");
Writer writer = new QRCodeWriter();
BitMatrix matrix = writer.encode(data, QR_CODE, width, height);
MatrixToImageWriter.writeToPath(matrix, fileType, Paths.get(savePath));
}
/**
* 根据bitMatrix生成二维码
* @param bitMatrix
* @param target
* @param imageType
*/
public static void genCode(BitMatrix bitMatrix, String imageType, String target){
try {
Path path = FileSystems.getDefault().getPath(target);
// 输出图像
MatrixToImageWriter.writeToPath(bitMatrix, imageType, path);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 写入二维码、以及将照片logo写入二维码中
*
* @param matrix 要写入的二维码
* @param format 二维码照片格式
* @param imagePath 二维码照片保存路径
* @param logoPath logo路径
* @throws IOException
*/
public static void writeToFile(BitMatrix matrix, String format, String imagePath, String logoPath) throws IOException {
MatrixToImageWriter.writeToPath(matrix, format, Paths.get(imagePath), new MatrixToImageConfig());
// 添加logo图片, 此处一定需要重新进行读取,而不能直接使用二维码的BufferedImage 对象
BufferedImage img = ImageIO.read(new File(imagePath));
MatrixToImageWriterEx.overlapImage(img, format, imagePath, logoPath, DEFAULT_CONFIG);
}
/**
* 写入二维码、以及将照片logo写入二维码中
*
* @param matrix 要写入的二维码
* @param format 二维码照片格式
* @param imagePath 二维码照片保存路径
* @param logoPath logo路径
* @param logoConfig logo配置对象
* @throws IOException
*/
public static void writeToFile(BitMatrix matrix, String format, String imagePath, String logoPath, MatrixToLogoImageConfig logoConfig) throws IOException {
MatrixToImageWriter.writeToPath(matrix, format, Paths.get(imagePath), new MatrixToImageConfig());
// 添加logo图片, 此处一定需要重新进行读取,而不能直接使用二维码的BufferedImage 对象
BufferedImage img = ImageIO.read(new File(imagePath));
MatrixToImageWriterEx.overlapImage(img, format, imagePath, logoPath, logoConfig);
}
/**
* 创建一个qrcode图片
*
* @param content 加密信息,建议使用json格式
* @param paramDTO qrcode 参数
* @throws WriterException
* @throws IOException
*/
public static void encode(String content, BarcodeParamDTO paramDTO) throws WriterException, IOException {
// 生成矩阵
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, paramDTO.getBarcodeFormat(), paramDTO.getWidth(),
paramDTO.getHeight(), paramDTO.getEncodeHints());
Path path = FileSystems.getDefault().getPath(paramDTO.getFilepath());
MatrixToImageWriter.writeToPath(bitMatrix, paramDTO.getImageFormat(), path);// 输出图像
}
/**
* 创建 QRCode 图片文件
*
* @param file 图片
* @param imageFormat 图片编码格式
* @param encodeContent QRCode 内容
* @param imageSize 图片大小
* @param foreGroundColor 前景色
* @param backGroundColor 背景色
* @throws WriterException
* @throws IOException
*/
public static void createQRCodeImageFile(Path file, ImageFormat imageFormat, String encodeContent, Dimension imageSize, Color foreGroundColor, Color backGroundColor) throws WriterException, IOException {
Assert.isTrue(FilenameUtils.getExtension(file.toString()).toUpperCase().equals(imageFormat.toString()), "文件扩展名和格式不一致");
QRCodeWriter qrWrite = new QRCodeWriter();
BitMatrix matrix = qrWrite.encode(encodeContent, BarcodeFormat.QR_CODE, imageSize.width, imageSize.height);
MatrixToImageWriter.writeToPath(matrix, imageFormat.toString(), file, new MatrixToImageConfig(foreGroundColor.getRGB(), backGroundColor.getRGB()));
}
/**
* 生成一个二维码图片
*
* @param inviteUrl
* @return
* @throws WriterException
* @throws IOException
*/
public static File createQRFile(String inviteUrl) throws WriterException, IOException {
BitMatrix bitMatrix = getBitMatrix(inviteUrl);
String tmpDir = System.getProperty("java.io.tmpdir");
Path path = FileSystems.getDefault().getPath(tmpDir, "qr_" + RandomUtils.generateRandomString(6) + ".png");
MatrixToImageWriter.writeToPath(bitMatrix, "png", path);// 输出图像
return path.toFile();
}
/**
* 生成二维码图片文件
*
* @param content 二维码内容
* @param path 文件保存路径
* @param width 宽
* @param height 高
*/
public static void createQRCode(String content, String path, int width, int height) throws WriterException, IOException {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
//toPath() 方法由 jdk1.7 及以上提供
MatrixToImageWriter.writeToPath(bitMatrix, FORMAT, new File(path).toPath());
}
/**
* @title 生成二维码图片
* @description 根据文本内容生成默认格式的二维图图片
* @param text 文本内容
* @param filePath 生成图片路径
* @throws WriterException
* @throws IOException
*/
public static void create(String text, String filePath) throws WriterException, IOException {
BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.QR_CODE, QRCODE_WIDTH, QRCODE_HEIGHT, ENCODE_HINTS);
Path path = FileSystems.getDefault().getPath(filePath);
MatrixToImageWriter.writeToPath(bitMatrix, FORMAT, path);
}
/**
* @title 生成二维码图片
* @description 根据文本内容,自定义宽度高度,生成所需要的二维码图片
* @param text 文本内容
* @param filePath 生成图片文件路径
* @param width 宽度
* @param height 高度
*/
public static void create(String text, String filePath, int width, int height) throws WriterException, IOException {
BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.QR_CODE, width, height, ENCODE_HINTS);
Path path = FileSystems.getDefault().getPath(filePath);
MatrixToImageWriter.writeToPath(bitMatrix, FORMAT, path);
}
/**
* @title 生成二维码图片
* @description 根据文本内容,自定义宽度高度,生成所需要的二维码图片
* @param text 文本内容
* @param filePath 生成图片文件路径
* @param width 宽度
* @param height 高度
* @param onColor 二维码颜色
* @param offColor 二维码背景颜色
*/
public static void create(String text, String filePath, int width, int height, Color onColor, Color offColor) throws WriterException, IOException {
BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.QR_CODE, width, height, ENCODE_HINTS);
Path path = FileSystems.getDefault().getPath(filePath);
MatrixToImageWriter.writeToPath(bitMatrix, FORMAT, path, new MatrixToImageConfig(onColor.getRGB(), offColor.getRGB()));
}
/**
* @title 生成二维码图片
* @description 根据文本内容,自定义宽度高度,自定义图片格式,生成所需要的二维码图片
* @param text 文本内容
* @param filePath 生成图片文件路径
* @param width 宽度
* @param height 高度
* @param format 图片格式
*/
public static void create(String text, String filePath, int width, int height, String format) throws WriterException, IOException {
BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.QR_CODE, width, height, ENCODE_HINTS);
Path path = FileSystems.getDefault().getPath(filePath);
MatrixToImageWriter.writeToPath(bitMatrix, format, path);
}
/**
* @title 生成二维码图片
* @description 根据文本内容,自定义宽度高度,自定义图片格式,自定义配置信息,生成所需要的二维码图片
* @param text 文本内容
* @param filePath 生成图片文件路径
* @param width 宽度
* @param height 高度
* @param format 图片格式
* @param hintTypes 配置信息
*/
public static void create(String text, String filePath, int width, int height, String format, Map<EncodeHintType, Object> hintTypes) throws WriterException, IOException {
BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.QR_CODE, width, height, hintTypes);
Path path = FileSystems.getDefault().getPath(filePath);
MatrixToImageWriter.writeToPath(bitMatrix, format, path);
}
/**
* 生成二维码图片文件
*
* @param content 二维码内容
* @param path 文件保存路径
*/
public static void createQRCodeFile(String content, String path) throws WriterException, IOException {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, HINTS);
MatrixToImageWriter.writeToPath(bitMatrix, FILE_FORMAT, new File(path).toPath());
}
/**
* 生成二维码图片文件
*
* @param content 二维码内容
* @param path 文件保存路径
* @param width 宽
* @param height 高
*/
public static void createQRCodeFile(String content, String path, int width, int height)
throws WriterException, IOException {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, HINTS);
MatrixToImageWriter.writeToPath(bitMatrix, FILE_FORMAT, new File(path).toPath());
}