一. maven依赖
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.1.0</version>
</dependency>
二. 开始码代码
package QRCode;
import com.google.zxing.*;
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;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
* @Author Li Kun
* @date 12/15 0015 8:27
*/
public class ZxingQRCode {
public static void createQrCode(int width, int height, String content){
//设置参数
Map hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
hints.put(EncodeHintType.MARGIN,1);
//生成二维码
try{
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE,width,height,hints);
Path path2 = Paths.get("D:/"+ UUID.randomUUID()+".jpg");
MatrixToImageWriter.writeToPath(bitMatrix,"jpg",path2);
}catch (Exception e) {
e.printStackTrace();
}
}
public static void createLogoQrCode(int width, int height, String content){
Map hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.MARGIN,1);
try{
BitMatrix bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE,width,height);
File qrFile = new File("D:/QRCode/"+UUID.randomUUID()+".png");
File logoFile = new File("D:/QRCode/jack.li.png");
MatrixToImageWriter.writeToPath(bitMatrix,"png", qrFile.toPath());
addLogo(qrFile, logoFile);
}catch (Exception e){
e.printStackTrace();
}
}
public static void addLogo(File qrFile, File logoFile){
if(!qrFile.isFile() || !logoFile.isFile()){
System.out.println("File is not exist");
return;
}
try{
BufferedImage image = ImageIO.read(qrFile);
Graphics2D graphics2D = image.createGraphics();
BufferedImage logo = ImageIO.read(logoFile);
int widthLogo = image.getWidth() / 6;
int heightLogo = image.getHeight() / 6;
int x = (image.getWidth() - widthLogo) / 2;
int y = (image.getHeight() - heightLogo) / 2;
graphics2D.drawImage(logo, x, y, widthLogo, heightLogo, null);
graphics2D.drawRoundRect(x, y, widthLogo, heightLogo, 10, 10);
graphics2D.setStroke(new BasicStroke(2));
graphics2D.setColor(Color.BLUE);
graphics2D.drawRect(x, y, widthLogo, heightLogo);
graphics2D.dispose();
ImageIO.write(image, "png", new File("D:/QRCode/newPic.png"));
}catch (Exception e){
e.printStackTrace();
}
}
public static Result readQrCode(String filePath){
Result result = null;
try{
MultiFormatReader formatReader = new MultiFormatReader();
File qrCode = new File(filePath);
BufferedImage image = ImageIO.read(qrCode);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
HashMap hints = new HashMap();
hints.put(DecodeHintType.CHARACTER_SET,"utf-8");
result = formatReader.decode(bitmap, hints);
}catch (Exception e){
e.printStackTrace();
}
return result;
}
public static void main(String[] args){
//createQrCode(500,500,"Hello Jack");
//createLogoQrCode(500,500,"Hello Jack");
//createLogoQrCode(300,300,"https://www.baidu.com/");
readQrCode("D:/QRCode/newPic.png");
}
}