<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.0.0</version>
</dependency>
1 import java.io.File; 2 import java.io.IOException; 3 import java.nio.file.Path; 4 import java.util.HashMap; 5 import com.google.zxing.BarcodeFormat; 6 import com.google.zxing.EncodeHintType; 7 import com.google.zxing.MultiFormatWriter; 8 import com.google.zxing.WriterException; 9 import com.google.zxing.client.j2se.MatrixToImageWriter; 10 import com.google.zxing.common.BitMatrix; 11 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; 12 13 public class Test { 14 15 public static void main(String[] args) { 16 //设置二维码像素 17 int width = 300; 18 int height = 300; 19 //二维码图片格式 20 String format = "png"; 21 //二维码当中要存储的信息 22 String content = "http://www.xiang5.com"; 23 HashMap<EncodeHintType, Comparable> hints = new HashMap<EncodeHintType, Comparable>(); 24 hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M); 25 26 //设置边距 27 try { 28 hints.put(EncodeHintType.MARGIN,2); 29 BitMatrix bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE,width,height,hints); 30 Path file = new File("D:/qrcode.png").toPath(); 31 MatrixToImageWriter.writeToPath(bitMatrix,format,file); 32 } catch (WriterException e) { 33 e.printStackTrace(); 34 } catch (IOException e) { 35 e.printStackTrace(); 36 } 37 } 38 }
所需要的架包:
下载地址: https://github.com/ZF520/QRCode/raw/master/zxing/bin/lib/zxing3.3.1.jar