Browse Source

add:新增二维码工具类

yonge 5 years ago
parent
commit
cab1a27984
3 changed files with 98 additions and 0 deletions
  1. 10 0
      mec-util/pom.xml
  2. 75 0
      mec-util/src/main/java/com/ym/mec/util/qrcode/QRCodeUtils.java
  3. 13 0
      pom.xml

+ 10 - 0
mec-util/pom.xml

@@ -24,5 +24,15 @@
 			<groupId>commons-beanutils</groupId>
 			<artifactId>commons-beanutils</artifactId>
 		</dependency>
+
+			<dependency>
+				<groupId>com.google.zxing</groupId>
+				<artifactId>core</artifactId>
+			</dependency>
+
+			<dependency>
+				<groupId>com.google.zxing</groupId>
+				<artifactId>javase</artifactId>
+			</dependency>
 	</dependencies>
 </project>

+ 75 - 0
mec-util/src/main/java/com/ym/mec/util/qrcode/QRCodeUtils.java

@@ -0,0 +1,75 @@
+package com.ym.mec.util.qrcode;
+
+import java.awt.image.BufferedImage;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Hashtable;
+
+import javax.imageio.ImageIO;
+
+import com.google.zxing.BarcodeFormat;
+import com.google.zxing.BinaryBitmap;
+import com.google.zxing.EncodeHintType;
+import com.google.zxing.LuminanceSource;
+import com.google.zxing.ReaderException;
+import com.google.zxing.Result;
+import com.google.zxing.WriterException;
+import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
+import com.google.zxing.common.BitMatrix;
+import com.google.zxing.common.HybridBinarizer;
+import com.google.zxing.qrcode.QRCodeReader;
+import com.google.zxing.qrcode.QRCodeWriter;
+import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
+
+public class QRCodeUtils {
+
+	/**
+	 * 生成包含字符串信息的二维码图片
+	 * @param outputStream 文件输出流路径
+	 * @param content 二维码携带信息
+	 * @param qrCodeSize 二维码图片大小
+	 * @param imageFormat 二维码的格式
+	 * @throws WriterException 
+	 * @throws IOException 
+	 */
+	public static boolean createQrCode(OutputStream outputStream, String content, int qrCodeSize, String imageFormat) throws WriterException, IOException {
+		// 设置二维码纠错级别MAP
+		Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
+		hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 矫错级别
+		QRCodeWriter qrCodeWriter = new QRCodeWriter();
+		// 创建比特矩阵(位矩阵)的QR码编码的字符串
+		BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);
+		// 使BufferedImage勾画QRCode (matrixWidth 是行二维码像素点)
+		int matrixWidth = bitMatrix.getWidth();
+		int matrixHight = bitMatrix.getHeight();
+		BufferedImage image = new BufferedImage(matrixWidth, matrixHight, BufferedImage.TYPE_INT_RGB);
+
+		for (int x = 0; x < matrixWidth; x++) {
+			for (int y = 0; y < matrixHight; y++) {
+				image.setRGB(x, y, (bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF));
+			}
+		}
+		return ImageIO.write(image, imageFormat, outputStream);
+	}
+
+	/**
+	 * 读二维码并输出携带的信息
+	 */
+	public static String readQrCode(InputStream inputStream) throws IOException {
+		// 从输入流中获取字符串信息
+		BufferedImage image = ImageIO.read(inputStream);
+		// 将图像转换为二进制位图源
+		LuminanceSource source = new BufferedImageLuminanceSource(image);
+		BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
+		QRCodeReader reader = new QRCodeReader();
+		Result result = null;
+		try {
+			result = reader.decode(bitmap);
+		} catch (ReaderException e) {
+			e.printStackTrace();
+		}
+		return result.getText();
+	}
+
+}

+ 13 - 0
pom.xml

@@ -18,6 +18,7 @@
 		<spring-boot.version>2.1.6.RELEASE</spring-boot.version>
 		<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
 		<zipkin.version>2.12.2</zipkin.version>
+		<google.zxing.version>3.4.0</google.zxing.version>
 	</properties>
 
 	<dependencyManagement>
@@ -133,6 +134,18 @@
 				<version>1.9.2</version>
 			</dependency>
 
+			<dependency>
+				<groupId>com.google.zxing</groupId>
+				<artifactId>core</artifactId>
+				<version>${google.zxing.version}</version>
+			</dependency>
+
+			<dependency>
+				<groupId>com.google.zxing</groupId>
+				<artifactId>javase</artifactId>
+				<version>${google.zxing.version}</version>
+			</dependency>
+
 		</dependencies>
 	</dependencyManagement>