/** * Zxing图形码生成工具 * * @param contents * 内容 * @param barcodeFormat * BarcodeFormat对象 * @param format * 图片格式,可选[png,jpg,bmp] * @param width * 宽 * @param height * 高 * @param margin * 边框间距px * @param saveImgFilePath * 存储图片的完整位置,包含文件名 * @return {boolean} */ public static boolean encode(String contents, BarcodeFormat barcodeFormat, Integer margin, ErrorCorrectionLevel errorLevel, String format, int width, int height, String saveImgFilePath) { Boolean bool = false; BufferedImage bufImg; Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); // 指定纠错等级 hints.put(EncodeHintType.ERROR_CORRECTION, errorLevel); hints.put(EncodeHintType.MARGIN, margin); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); try { // contents = new String(contents.getBytes("UTF-8"), "ISO-8859-1"); BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, barcodeFormat, width, height, hints); MatrixToImageConfig config = new MatrixToImageConfig(0xFF000001, 0xFFFFFFFF); bufImg = MatrixToImageWriter.toBufferedImage(bitMatrix, config); bool = writeToFile(bufImg, format, saveImgFilePath); } catch (Exception e) { e.printStackTrace(); } return bool; }
/** * Zxing图形码生成工具 * * @param contents 内容 * @param barcodeFormat BarcodeFormat对象 * @param format 图片格式,可选[png,jpg,bmp] * @param width 宽 * @param height 高 * @param margin 边框间距px * @param saveImgFilePath 存储图片的完整位置,包含文件名 * @return */ public Boolean encode(String contents, BarcodeFormat barcodeFormat, Integer margin, ErrorCorrectionLevel errorLevel, String format, int width, int height, String saveImgFilePath) { Boolean bool; BufferedImage bufImg; Map<EncodeHintType, Object> hints = new HashMap<>(); // 指定纠错等级 hints.put(EncodeHintType.ERROR_CORRECTION, errorLevel); hints.put(EncodeHintType.MARGIN, margin); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); try { // contents = new String(contents.getBytes("UTF-8"), "ISO-8859-1"); BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, barcodeFormat, width, height, hints); MatrixToImageConfig config = new MatrixToImageConfig(0xFF000001, 0xFFFFFFFF); bufImg = MatrixToImageWriter.toBufferedImage(bitMatrix, config); bool = this.writeToFile(bufImg, format, saveImgFilePath); } catch (Throwable t) { log.error("encode-ex:{}", t); throw Throwables.propagate(t); } return bool; }
/** * Testing utility that converts BitmapImage to BufferedImage type. */ protected static BufferedImage toBufferedImage(BitmapImage matrix) { MatrixToImageConfig config = new MatrixToImageConfig(); int width = matrix.getWidth(); int height = matrix.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY); int onColor = config.getPixelOnColor(); int offColor = config.getPixelOffColor(); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, matrix.get(x, y) ? onColor : offColor); } } return image; }
private void writeToStream(OutputStream stream) throws IOException, WriterException { MatrixToImageWriter.writeToStream(this.createMatrix(), this.imageType, stream, new MatrixToImageConfig( this.onColor, this.offColor)); }
/** * 创建 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())); }