二維碼的原理
二維條形碼最早發明於日本,它是用某種特定的幾何圖形按一定規律在平面(二維方向上)分布的黑白相間的圖形記錄數據符號信息的,在代碼編制上巧妙地利用構成計算機內部邏輯基礎的「0」、「1」比特流的概念,使用若干個與二進位相對應的幾何形體來表示文字數值信息,通過圖象輸入設備或光電掃描設備自動識讀以實現信息自動處理。
Java的的的生成二維碼,解析二維碼,保存為圖片1.利用Google的ZXing工具包,生成和解析二維碼圖片,如果是maven項目則引入pom.xml中的依賴為<!-- 二維碼 -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
<!-- io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
2.根據內容,生成指定寬高,指定格式的二維碼圖片,這一步已經可以生成二維碼圖片了,可以掃面跳轉到制定連結了
/**
* 根據內容,生成指定寬高、指定格式的二維碼圖片
*
* @param text 內容
* @param width 寬
* @param height 高
* @param format 圖片格式
* @return 生成的二維碼圖片路徑
* @throws Exception
*/
public static String generateQRCode(String text, int width, int height, String format) throws Exception {
Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
String pathName = "D:/qrCode/QrCodeDemo.jpg";
File outputFile = new File(pathName);
if (!outputFile.exists()) {
outputFile.mkdirs();
}
MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
return pathName;
}
3.此方法為谷歌提供,用於生成二維碼
import com.google.zxing.common.BitMatrix;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
/**
* 用於二維碼的生成,由Google提供。
*
* Created by dengxincheng on 2017/10/20.
*/
public final class MatrixToImageWriter {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private MatrixToImageWriter() {}
public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}
//輸出為文件
public static void writeToFile(BitMatrix matrix, String format, File file)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}
//輸出為流
public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, stream)) {
throw new IOException("Could not write an image of format " + format);
}
}
}
4.解析指定路徑下的二維碼圖片內容
/**
* 解析指定路徑下的二維碼圖片
* @param filePath 二維碼圖片路徑
* @return
*/
private static String parseQRCode(String filePath) {
String content = "";
try {
File file = new File(filePath);
BufferedImage image = ImageIO.read(file);
LuminanceSource source = new BufferedImageLuminanceSource(image);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
Map<DecodeHintType, Object> hints = new HashMap<>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
MultiFormatReader formatReader = new MultiFormatReader();
Result result = formatReader.decode(binaryBitmap, hints);
System.out.println("result 為:" + result.toString());
System.out.println("resultFormat 為:" + result.getBarcodeFormat());
System.out.println("resultText 為:" + result.getText());
//設置返回值
content = result.getText();
} catch (Exception e) {
e.printStackTrace();
}
return content;
}
測試代碼:
public static void main(String[] args) {
String text = "https://www.csdn.net/";
System.out.println("二維碼的內容為:" + text);
int width = 300; //二維碼圖片的寬
int height = 300; //二維碼圖片的高
String format = "jpg"; //二維碼圖片的格式
try {
//生成二維碼圖片,並返回圖片路徑
String pathName = generateQRCode(text, width, height, format);
System.out.println("生成二維碼的圖片路徑:" + pathName);
String content = parseQRCode(pathName);
System.out.println("解析出二維碼的圖片的內容為:" + content);
} catch (Exception e) {
e.printStackTrace();
}
}
最後在你的文件目錄中就可以找到對應的二維碼了!