下面列出了怎么用com.google.zxing.client.j2se.MatrixToImageConfig的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 写入二维码、以及将照片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 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()));
}
/**
* 创建 QRCode 图片文件输出流,用于在线生成动态图片
*
* @param stream
* @param imageFormat
* @param encodeContent
* @param imageSize
* @param foreGroundColor
* @param backGroundColor
* @throws WriterException
* @throws IOException
*/
public static void createQRCodeImageStream(OutputStream stream, ImageFormat imageFormat, String encodeContent, Dimension imageSize, Color foreGroundColor, Color backGroundColor) throws WriterException, IOException {
QRCodeWriter qrWrite = new QRCodeWriter();
BitMatrix matrix = qrWrite.encode(encodeContent, BarcodeFormat.QR_CODE, imageSize.width, imageSize.height);
MatrixToImageWriter.writeToStream(matrix, imageFormat.toString(), stream, new MatrixToImageConfig(foreGroundColor.getRGB(), backGroundColor.getRGB()));
}
/**
* @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()));
}