java實現二維碼的生成和解析:QRCode、zxing 兩種方式


第一種:QRCode.jar,使用QRCode生成和解析二維碼

1.導入jar包

 

 2.代碼

(1)QRCodeUtil .java

  1 import com.swetake.util.Qrcode;
  2 import jp.sourceforge.qrcode.QRCodeDecoder;
  3 
  4 import javax.imageio.ImageIO;
  5 import java.awt.*;
  6 import java.awt.image.BufferedImage;
  7 import java.io.File;
  8 
  9 public class QRCodeUtil {
 10     /**
 11      *加密:  文字信息 ->二維碼.png
 12      * @param content 文字信息
 13      * @param imgPath 二維碼路徑
 14      * @param imgType 二維碼類型:png
 15      * @param size    二維碼尺寸
 16      * @throws Exception
 17      */
 18     public void encoderQRCode(String content,String imgPath,String imgType,int size)  throws Exception{
 19         //BufferedImage :內存中的一張圖片
 20         BufferedImage bufImg =   qRcodeCommon(content,imgType,size);
 21 
 22         File file = new File(imgPath);// "src/二維碼.png" --> 二維碼.png
 23 
 24         //生成圖片
 25         ImageIO.write(bufImg, imgType, file);
 26     }
 27 
 28     /**
 29      *產生一個二維碼的BufferedImage
 30      * @param content 二維碼隱藏信息
 31      * @param imgType 圖片格式
 32      * @param size    圖片大小
 33      * @return
 34      */
 35     private BufferedImage qRcodeCommon(String content, String imgType, int size) throws Exception {
 36         BufferedImage bufImg =null;
 37         //Qrcode對象:字符串->boolean[][]
 38         Qrcode qrCodeHandler = new Qrcode();
 39         //設置二維碼的排錯率:7% L<M<Q<H30%  :排錯率越高,可存儲的信息越少;但是對二維碼清晰對要求越小
 40         qrCodeHandler.setQrcodeErrorCorrect('M');
 41         //可存放的信息類型:N:數字、  A:數字+A-Z  B:所有
 42         qrCodeHandler.setQrcodeEncodeMode('B');
 43         //尺寸:取值范圍:1-40
 44         qrCodeHandler.setQrcodeVersion(size);
 45 
 46         //"Hello world" -> byte[]"Hello world" -->boolean[][]
 47         byte[] contentBytes = content.getBytes("UTF-8");
 48         boolean[][] codeOut = qrCodeHandler.calQrcode(contentBytes);
 49 
 50         int imgSize =  67 + 12*(size -1) ;
 51 
 52         //BufferedImage:內存中的圖片
 53         bufImg = new BufferedImage(imgSize,imgSize,BufferedImage.TYPE_INT_RGB );//red green blue
 54 
 55         //創建一個畫板
 56         Graphics2D gs = bufImg.createGraphics();
 57         gs.setBackground(Color.WHITE);//將畫板的背景色設置為白色
 58         gs.clearRect( 0,0, imgSize,imgSize); //初始化
 59         gs.setColor(Color.BLACK);//設置 畫板上 圖像的顏色(二維碼的顏色)
 60 
 61         int pixoff = 2 ;
 62 
 63         for(int j=0;j<codeOut.length;j++) {
 64             for(int i=0;i<codeOut.length;i++) {
 65                 if(codeOut[j][i]) {
 66                     gs.fillRect(j*3+pixoff , i*3+pixoff, 3, 3);
 67                 }
 68             }
 69         }
 70         //增加LOGO
 71         //將硬盤中的src/logo.png  加載為一個Image對象
 72         Image logo =  ImageIO.read(new File("/src/logo.png")  ) ;
 73         int maxHeight = bufImg.getHeight();
 74         int maxWdith = bufImg.getWidth();
 75 
 76         //在已生成的二維碼上 畫logo
 77         gs.drawImage(logo,imgSize/5*2,imgSize/5*2, maxWdith/5,maxHeight/5 ,null) ;
 78 
 79         gs.dispose(); //釋放空間
 80         bufImg.flush(); //清理
 81         return bufImg ;
 82     }
 83 
 84     //解密:  二維碼(圖片路徑) -> 文字信息
 85     public String decoderQRCode(String imgPath) throws Exception{
 86 
 87         //BufferedImage內存中的圖片  :硬盤中的imgPath圖片 ->內存BufferedImage
 88         BufferedImage bufImg =  ImageIO.read( new File(imgPath) ) ;
 89         //解密
 90         QRCodeDecoder decoder = new QRCodeDecoder() ;
 91 
 92         TwoDimensionCodeImage tdcImage = new TwoDimensionCodeImage(bufImg);
 93         byte[] bs = decoder.decode(tdcImage) ;    //bufImg
 94         //byte[] -->String
 95         String content    = new String(bs,"UTF-8");
 96         return content;
 97     }
 98 
 99 
100 }

 (2)TwoDimensionCodeImage .java

 1 import jp.sourceforge.qrcode.data.QRCodeImage;
 2 
 3 import java.awt.image.BufferedImage;
 4 
 5 public class TwoDimensionCodeImage implements QRCodeImage {
 6     BufferedImage bufImg;  //將圖片加載到內存中
 7     public TwoDimensionCodeImage(BufferedImage bufImg) {
 8         this.bufImg = bufImg;
 9     }
10     public int getWidth() {
11         return bufImg.getWidth();
12     }
13 
14     public int getHeight() {
15         return bufImg.getHeight();
16     }
17 
18     public int getPixel(int x, int y) {
19         return bufImg.getRGB(x,y);
20     }
21 }

 (3)Test .java

 1 public class Test {
 2     public static void main(String[] args)  throws Exception{
 3         //生成二維碼
 4         /*
 5          * 生成圖片的路徑        src/二維碼.png
 6          * 文字信息、網址信息 :  "helloworld"
 7          */
 8         String imgPath = "e:\\二維碼2.png";
 9         String content =  "http://baidu.com";  //掃描二維碼后,網頁跳轉
10 
11         //生成二維碼
12         /*
13          * 加密:  文字信息 ->二維碼
14          * 解密:  二維碼 -> 文字信息
15          */
16         QRCodeUtil qrUtil = new QRCodeUtil();
17         //加密:  文字信息 ->二維碼
18         qrUtil.encoderQRCode(content, imgPath, "png", 17);
19 
20 //           TwoDimensionCode handler = new TwoDimensionCode();
21 //           handler.encoderQRCode(content, imgPath, "png", 7);
22 
23 
24 //        //解密:  二維碼 -> 文字信息
25         String imgContent = qrUtil.decoderQRCode(imgPath) ;
26         System.out.println(imgContent);
27 
28 
29     }
30 }

第二種:借助Google提供的ZXing Core工具包

1.maven依賴

 1  <dependency>
 2             <groupId>com.google.zxing</groupId>
 3             <artifactId>core</artifactId>
 4             <version>3.4.0</version>
 5         </dependency>
 6         <dependency>
 7             <groupId>com.google.zxing</groupId>
 8             <artifactId>javase</artifactId>
 9             <version>3.3.1</version>
10         </dependency>

2.代碼

(1)ZXingUtil .java

 1 import com.google.zxing.*;
 2 import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
 3 import com.google.zxing.common.BitMatrix;
 4 import com.google.zxing.common.HybridBinarizer;
 5 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
 6 import jp.sourceforge.qrcode.util.Color;
 7 
 8 import javax.imageio.ImageIO;
 9 import java.awt.image.BufferedImage;
10 import java.io.File;
11 import java.util.HashMap;
12 import java.util.Hashtable;
13 import java.util.Map;
14 
15 public class ZXingUtil {
16     public static void encodeImg(String imgPath,String format,String content,int width,int height,String logo) throws Exception {
17         Hashtable<EncodeHintType,Object > hints = new Hashtable<EncodeHintType,Object>() ;
18         //排錯率  L<M<Q<H
19         hints.put( EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H) ;
20         //編碼
21         hints.put( EncodeHintType.CHARACTER_SET, "utf-8") ;
22         //外邊距:margin
23         hints.put( EncodeHintType.MARGIN, 1) ;
24         /*
25          * content : 需要加密的 文字
26          * BarcodeFormat.QR_CODE:要解析的類型(二維碼)
27          * hints:加密涉及的一些參數:編碼、排錯率
28          */
29         BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE,width,height, hints);
30         //內存中的一張圖片:此時需要的圖片 是二維碼-> 需要一個boolean[][] ->BitMatrix
31         BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
32 
33         for(int x=0;x<width;x++) {
34             for(int y=0;y<height;y++) {
35                 image.setRGB(x, y,     (bitMatrix.get(x,y)? Color.BLACK:Color.WHITE)  );
36             }
37         }
38         //畫logo
39         image = LogoUtil.logoMatrix(image, logo) ;
40         //string->file
41         File file = new File(imgPath);
42         //生成圖片
43         ImageIO.write(image, format, file);
44     }
45 
46     //解密:二維碼->文字
47     public static void decodeImg(File file) throws Exception {
48         if(!file.exists()) return ;
49         //file->內存中的一張圖片
50         BufferedImage imge = ImageIO.read(file)  ;
51 
52         MultiFormatReader formatReader = new MultiFormatReader() ;
53         LuminanceSource source = new BufferedImageLuminanceSource(imge);
54         Binarizer binarizer = new HybridBinarizer(source);
55         BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
56         //圖片 ->result
57         Map map = new HashMap();
58         map.put(EncodeHintType.CHARACTER_SET, "utf-8") ;
59         Result result = formatReader.decode(binaryBitmap  ,map ) ;
60         System.out.println("解析結果:"+ result.toString());
61     }
62 
63 }

(2)LogoUtil .java

 1 import javax.imageio.ImageIO;
 2 import java.awt.*;
 3 import java.awt.geom.RoundRectangle2D;
 4 import java.awt.image.BufferedImage;
 5 import java.io.File;
 6 import java.io.IOException;
 7 
 8 public class LogoUtil {
 9     //傳入logo、二維碼 ->帶logo的二維碼
10     public  static BufferedImage  logoMatrix( BufferedImage matrixImage,String logo ) throws IOException {
11         //在二維碼上畫logo:產生一個  二維碼畫板
12         Graphics2D g2 = matrixImage.createGraphics();
13 
14         //畫logo: String->BufferedImage(內存)
15         BufferedImage logoImg = ImageIO.read(new File(logo));
16         int height = matrixImage.getHeight();
17         int width = matrixImage.getWidth();
18         //純logo圖片
19         g2.drawImage(logoImg, width * 2 / 5, height * 2 / 5, width * 1 / 5, height * 1 / 5, null);
20 
21         //產生一個 畫 白色圓角正方形的 畫筆
22         BasicStroke stroke = new BasicStroke(5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
23         //將畫板-畫筆 關聯
24         g2.setStroke(stroke);
25         //創建一個正方形
26         RoundRectangle2D.Float round = new RoundRectangle2D.Float(width * 2 / 5, height * 2 / 5, width * 1 / 5, height * 1 / 5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
27         g2.setColor(Color.WHITE);
28         g2.draw(round);
29 
30         //灰色邊框
31         BasicStroke stroke2 = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
32         g2.setStroke(stroke2);
33         //創建一個正方形
34         RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(width * 2 / 5 + 2, height * 2 / 5 + 2, width * 1 / 5 - 4, height * 1 / 5 - 4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
35 //        Color color = new Color(128,128,128) ;
36         g2.setColor(Color.GRAY);
37         g2.draw(round2);
38 
39         g2.dispose();
40         matrixImage.flush();
41 
42         return matrixImage;
43     }
44 }

(3)test.java

 1 import java.io.File;
 2 
 3 public class test {
 4     public static void main(String[] args) throws Exception {
 5         String imgPath = "src/二維碼12.png";
 6         String content = "helloworld你好";
 7         String logo = "src/logo.png";
 8         //加密:文字信息->二維碼
 9         ZXingUtil.encodeImg(imgPath, "png",content, 430, 430,logo);
10         //解密:二維碼  ->文字信息
11         ZXingUtil.decodeImg(new File(imgPath));
12     }
13 }

(效果圖)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM