一丶添加依赖
<!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.3</version>
</dependency>
二丶二维码生成方式
- MultiFormatWriter(生成二维码)
- AztecWriter(生成 Aztec码)
- DataMatrixWriter(DataMatrix二维码)
- OneDimensionalCodeWriter
- PDF417Writer(生成条形码)
- QRCodeWriter(生成二维码)
- UPCAWriter(生成UPCA码)
三丶生成二维码实现Dome
package com.zpf.homeworkBy18.utils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.aztec.AztecWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.datamatrix.DataMatrixWriter;
import com.google.zxing.oned.OneDimensionalCodeWriter;
import com.google.zxing.oned.UPCAWriter;
import com.google.zxing.pdf417.PDF417Writer;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
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;
public class TwoImgUtil {
private static final String QR_CODE_IMAGE_PATH = "static\\images\\two.jpg";
private static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException {
MultiFormatWriter qrCodeWriter = new MultiFormatWriter();
HashMap hints = new HashMap(16);
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height,hints);
Path path = FileSystems.getDefault().getPath(filePath);
System.out.println(path.getFileName().toString());
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
MatrixToImageWriter.writeToPath(bitMatrix, "PNG",path );
}
public static void main(String[] args) {
try {
generateQRCodeImage("小黑同学!", 350, 350, QR_CODE_IMAGE_PATH);
} catch (WriterException e) {
System.out.println("Could not generate QR Code, WriterException :: " + e.getMessage());
} catch (IOException e) {
System.out.println("Could not generate QR Code, IOException :: " + e.getMessage());
}
}
}
四丶“扫描”二维码
public static String decode(String path) throws Exception {
File file = new File(path);
BufferedImage image;
image = ImageIO.read(file);
if (image == null) {
return null;
}
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result;
Hashtable hints = new Hashtable();
hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
result = new MultiFormatReader().decode(bitmap, hints);
String resultStr = result.getText();
return resultStr;
}
总结:用java生成二维码比较简单,熟悉API即可。码的实现其实就是加解密的一个过程。码其实就是加密对象留下的线索。在解码的时候通过线索解码即可。