前言: 這周有個項目需要生成二維碼,研究了一下使用Google的zxing生成二維碼,發現效果還可以,在這裡記錄下。如果需要更加定製化的二維碼,也可接通第三方API服務生成二維碼。 二維碼的生成 : ...
前言: 這周有個項目需要生成二維碼,研究了一下使用Google的zxing生成二維碼,發現效果還可以,在這裡記錄下。如果需要更加定製化的二維碼,也可接通第三方API服務生成二維碼。
一、生成二維碼 :
@Component("qrcodeHandler") public class QrcodeHandlerImpl implements QrcodeHandler { private static final Logger logger = LoggerFactory.getLogger(QrcodeHandlerImpl.class); /** 二維碼顏色 */ private static final int FRONT_COLOR = 0xFF000000; // 前景色黑色 private static final int BACK_COLOR = 0xFFFFFFFF; // 背景色白色 /** 編碼格式 */ private static final String CHARACTER_SET = "UTF-8"; /** 圖片類型 */ private static final String FORMAT = "png"; /** 二維碼尺寸 */ private static final int QRCODE_SIZE = 300; /** logo信息 */ private static final int LOGO_SIZE = 60; private static final HashMap<EncodeHintType, Object> hints; static { hints = new HashMap<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, CHARACTER_SET); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); hints.put(EncodeHintType.MARGIN, 1); } /** * 生成簡單的二維碼 * * @param contents 二維碼實際內容,包括網址、文本、文件等, 如http://www.cnblogs.com/ark-blog/ * @param destFile 生成的二維碼文件 */ public void generateSimpleQrcode(String contents, File destFile) { try { // 1. 生成位矩陣 BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints); // 2. 保存圖片 MatrixToImageWriter.writeToPath(bitMatrix, FORMAT, destFile.toPath()); } catch (Exception e) { logger.error("#generateSimpleQrcode() - create qrcode failed, exception=[{}]", e); throw BusinessException.newInstance("500", "create qrcode failed"); } } /** * 生成帶logo的二維碼 * * @param contents 二維碼實際內容,包括網址、文本、文件等, 如http://www.cnblogs.com/ark-blog/ * @param logoFile logo * @param destFile 生成的二維碼文件 */ public void generateLogoQrcode(String contents, File logoFile, File destFile) { try { // 1. 創建位矩陣圖 MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); BitMatrix bitMatrix = multiFormatWriter.encode(contents, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints); BufferedImage qrcodeImage = new BufferedImage(QRCODE_SIZE, QRCODE_SIZE, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < QRCODE_SIZE; x++) { for (int y = 0; y < QRCODE_SIZE; y++) { qrcodeImage.setRGB(x, y, bitMatrix.get(x, y) ? FRONT_COLOR : BACK_COLOR); } } // 2. 在矩陣圖上繪製logo Image logoImage = ImageIO.read(logoFile); BufferedImage logoBufferedImage = new BufferedImage(LOGO_SIZE, LOGO_SIZE, BufferedImage.TYPE_INT_RGB); logoBufferedImage.getGraphics().drawImage(logoImage, 0, 0, null); int position = (QRCODE_SIZE - LOGO_SIZE) / 2; Graphics2D graphics2d = qrcodeImage.createGraphics(); graphics2d.drawImage(logoBufferedImage, position, position, null); graphics2d.dispose(); // 3. 保存圖片 qrcodeImage.flush(); ImageIO.write(qrcodeImage, FORMAT, destFile); } catch (Exception e) { logger.error("#generateLogoQrcode() - create qrcode failed, exception=[{}]", e); throw BusinessException.newInstance("500", "create qrcode failed"); } } }
二、解析二維碼
@Component("qrcodeHandler") public class QrcodeHandlerImpl implements QrcodeHandler { private static final Logger logger = LoggerFactory.getLogger(QrcodeHandlerImpl.class); /** * 解析二維碼 * * @param qrcode 待解析二維碼文件 * @return 解析結果 */ public Result decode(File qrcode) { try { // 1. 得到qrcode的bigmap數據 BufferedImage bufferedImage = ImageIO.read(qrcode); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage))); // 2. 解碼配置 Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>(); hints.put(DecodeHintType.CHARACTER_SET, "UTF-8"); // 3. 解析二維碼 MultiFormatReader reader = new MultiFormatReader(); Result result = reader.decode(bitmap, hints); logger.trace("#decode() - result=[{}], qrcode-barcodeFormat=[{}], qrcode-content=[{}]", result.toString(), result.getBarcodeFormat(), result.getText()); return result; } catch (Exception e) { logger.error("#decode() - decode qrcode failed, exception=[{}]", e); throw BusinessException.newInstance("500", "Decode qrcode failed"); } } }