二維碼的生成與解析。有多種途徑。我選擇用大品牌,google老大的zxing。java
gitHub連接是(我用的3.0.0,已是nio了)git
https://github.com/zxing/zxing/tree/zxing-3.0.0github
-
- MultiFormatReader
-
- MultiFormatWriter
-
-
- MatrixToImageWriter
pom.xml中的配置爲apache
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <groupId>com.shihy</groupId>
- <artifactId>qrcode</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
-
- <name>qrcode</name>
- <url>http://maven.apache.org</url>
-
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>com.google.zxing</groupId>
- <artifactId>core</artifactId>
- <version>3.0.0</version>
- </dependency>
- <dependency>
- <groupId>com.google.zxing</groupId>
- <artifactId>javase</artifactId>
- <version>3.0.0</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.10</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>fastjson</artifactId>
- <version>1.1.29</version>
- </dependency>
- </dependencies>
- </project>
生成圖像與解析圖像的測試工具類:json
- package com.polysaas.edu.qrcode;
-
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.nio.file.FileSystems;
- import java.nio.file.Path;
- import java.util.HashMap;
- import java.util.Map;
-
- import javax.imageio.ImageIO;
-
- import org.junit.Test;
-
- import com.alibaba.fastjson.JSONObject;
- import com.google.zxing.BarcodeFormat;
- import com.google.zxing.Binarizer;
- import com.google.zxing.BinaryBitmap;
- import com.google.zxing.DecodeHintType;
- import com.google.zxing.EncodeHintType;
- import com.google.zxing.LuminanceSource;
- import com.google.zxing.MultiFormatReader;
- import com.google.zxing.MultiFormatWriter;
- import com.google.zxing.NotFoundException;
- import com.google.zxing.Result;
- import com.google.zxing.WriterException;
- import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
- import com.google.zxing.client.j2se.MatrixToImageWriter;
- import com.google.zxing.common.BitMatrix;
- import com.google.zxing.common.HybridBinarizer;
-
- public class QRCodeTest {
-
-
- @Test
- public void testEncode() throws WriterException, IOException {
- String filePath = "D://";
- String fileName = "zxing.png";
- JSONObject json = new JSONObject();
- json.put(
- "zxing",
- "https://github.com/zxing/zxing/tree/zxing-3.0.0/javase/src/main/java/com/google/zxing");
- json.put("author", "shihy");
- String content = json.toJSONString();
- int width = 200;
- int height = 200;
- String format = "png";
- Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
- hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
- BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
- BarcodeFormat.QR_CODE, width, height, hints);
- Path path = FileSystems.getDefault().getPath(filePath, fileName);
- MatrixToImageWriter.writeToPath(bitMatrix, format, path);
- System.out.println("輸出成功.");
- }
-
-
- @Test
- public void testDecode() {
- String filePath = "D://zxing.png";
- BufferedImage image;
- try {
- image = ImageIO.read(new File(filePath));
- LuminanceSource source = new BufferedImageLuminanceSource(image);
- Binarizer binarizer = new HybridBinarizer(source);
- BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
- Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
- hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
- Result result = new MultiFormatReader().decode(binaryBitmap, hints);
- JSONObject content = JSONObject.parseObject(result.getText());
- System.out.println("圖片中內容: ");
- System.out.println("author: " + content.getString("author"));
- System.out.println("zxing: " + content.getString("zxing"));
- System.out.println("圖片中格式: ");
- System.out.println("encode: " + result.getBarcodeFormat());
- } catch (IOException e) {
- e.printStackTrace();
- } catch (NotFoundException e) {
- e.printStackTrace();
- }
- }
- }
測試生成圖像爲:maven
![](http://static.javashuo.com/static/loading.gif)