二維條碼也有許多不一樣的碼制,就碼制的編碼原理而言,一般分爲三種類型。javascript
編碼原理:
創建在一維條碼基礎之上,按須要堆積成兩行或多行。html
最經常使用編碼,原理: 在一個矩形空間經過黑白像素在矩陣中的不一樣分佈進行編碼。在矩陣相應的位置上,用點(方點、圓點或其它形狀)的出現表示二進制「1」,點的不出現表示二進制的「0」java
經過不一樣長度的條進行編碼,主要用於郵政編碼。android
如今最經常使用的就是這種,我們如今主要介紹的也是這種。爲啥這種使用二維碼那麼受反應呢?主要QR Code這種二維碼有以下優勢:git
- 識讀速度快
- 數據密度大
- 佔用空間小
你們能夠了解下二維碼的結構,知道大概就好了,若是想了解詳細信息的話能夠自行百度,國家有詳細的二維碼規範。github
這裏介紹以下兩種實現方式:api
因爲github上沒有相關的jar包,因此須要咱們本身生成一下,上面有好多關於android相關的,咱們只須要選取核心包和javase這兩部分代碼。既下圖矩形框內容: 生成方式我大體說下:首先在ecplise裏新建一個java項目zxing,將剛纔畫框代碼拷貝進去,而後導出jar包便可。若是你不想生成也能夠在個人github上自行下載。
package cn.rivamed.zxing; import java.io.File; import java.nio.file.Path; import java.util.HashMap; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; public class CreateQRCode { public static void main(String[] args) { int width=300; int height=300; String format="png"; //這裏若是你想自動跳轉的話,須要加上https:// String content="https://github.com/hbbliyong/QRCode.git"; HashMap hits=new HashMap(); hits.put(EncodeHintType.CHARACTER_SET, "utf-8");//編碼 //糾錯等級,糾錯等級越高存儲信息越少 hits.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); //邊距 hits.put(EncodeHintType.MARGIN, 2); try { BitMatrix bitMatrix=new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hits); //若是作網頁版輸出能夠用輸出到流 //MatrixToImageWriter.writeToStream(matrix, format, stream); Path path=new File("D:/zxingQRCode.png").toPath(); MatrixToImageWriter.writeToPath(bitMatrix, format, path); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("that is all"); } }
生成的結果以下:
package cn.rivamed.zxing; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.HashMap; import javax.imageio.ImageIO; import com.google.zxing.Binarizer; import com.google.zxing.BinaryBitmap; import com.google.zxing.EncodeHintType; import com.google.zxing.LuminanceSource; import com.google.zxing.MultiFormatReader; import com.google.zxing.MultiFormatWriter; import com.google.zxing.NotFoundException; import com.google.zxing.Result; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.common.BitArray; import com.google.zxing.common.BitMatrix; import com.google.zxing.common.HybridBinarizer; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; public class ReadQRCode { public static void main(String[] args) { try { MultiFormatReader formatReader=new MultiFormatReader(); File file=new File("D:/zxingQRCode.png"); BufferedImage image=ImageIO.read(file); BinaryBitmap binaryBitmap=new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image))); HashMap hints=new HashMap(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8");//編碼 Result result=formatReader.decode(binaryBitmap, hints); System.out.println("解析結果:"+result.toString()); System.out.println("二維碼格式類型:"+result.getBarcodeFormat()); System.out.println("二維碼文本"+result.getText()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package cn.rivamed.qrcode; import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import javax.imageio.ImageIO; import com.swetake.util.Qrcode; public class CreateQRCode { public static void main(String[] args) throws IOException { Qrcode x=new Qrcode(); int version=7; x.setQrcodeErrorCorrect('M');//糾錯等級 x.setQrcodeEncodeMode('B');//N表明數字,A表明a-Z,B表明其它(中文等) x.setQrcodeVersion(version);//版本號 String qrData="https://github.com/hbbliyong/QRCode.git"; //int width=300; int width=67+12*(version-1); //int height=300; int height=67+12*(version-1); BufferedImage bufferedImage=new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); Graphics2D gs=bufferedImage.createGraphics(); gs.setBackground(Color.WHITE); gs.setColor(Color.BLACK); gs.clearRect(0, 0, width, height); int pixoff=2;//偏移量,若是不加有可能會致使識別不許確 //若是有漢字須要加上編碼 byte[] d=qrData.getBytes("gb2312"); //byte[] d=qrData.getBytes(); if(d.length>0&&d.length<120){ boolean[][] s=x.calQrcode(d); for(int i=0;i<s.length;i++){ for(int j=0;j<s.length;j++){ if(s[j][i]){ gs.fillRect(j*3+pixoff, i*3+pixoff, 3, 3); } } } } gs.dispose(); bufferedImage.flush(); ImageIO.write(bufferedImage, "png", new File("D:/qrcode.png")); } }
生成的結果以下:
這裏須要注意的是,二維碼長寬不能想zxing之直接定義,須要跟進這個公式生成67+12*(version-1)。好比我直接定義二維碼的長寬爲300.就會變成以下樣子。 這上面空白看的不是太清,你把圖片下載下載下來看就比較明顯了。
package cn.rivamed.qrcode; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import jp.sourceforge.qrcode.QRCodeDecoder; import jp.sourceforge.qrcode.data.QRCodeImage; public class ReadQRCode { public static void main(String[] args) throws IOException { File file=new File("D:/qrcode.png"); BufferedImage bufferedImage=ImageIO.read(file); QRCodeDecoder codeDecoder=new QRCodeDecoder(); String result=new String(codeDecoder.decode(new QRCodeImage() { @Override public int getWidth() { // TODO Auto-generated method stub return bufferedImage.getWidth(); } @Override public int getPixel(int arg0, int arg1) { // TODO Auto-generated method stub return bufferedImage.getRGB(arg0, arg1); } @Override public int getHeight() { // TODO Auto-generated method stub return bufferedImage.getHeight(); } }),"gb2312"); System.out.println(result); } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>二維碼生成</title> <script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery.min.js"></script> <script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery.qrcode.min.js"></script> </head> <body> 生成的二維碼以下:<br> <dir id="qrcode"></dir> <script type="text/javascript"> jQuery('#qrcode').qrcode('https://github.com/hbbliyong/QRCode.git'); </script> </body> </html>