本文是學習慕課網課程《Java生成二維碼》(http://www.imooc.com/learn/531)的筆記。html
1、二維碼的分類java
線性堆疊式二維碼、矩陣式二維碼、郵政碼。git
2、二維碼的優缺點github
優勢:1. 高密度編碼,信息容量大;2.編碼範圍廣;3.容錯能力強;4.譯碼可靠性高;5.可引入加密措施;6.成本低,易製做,持久耐用。ide
缺點:1.二維碼技術成爲手機病毒、釣魚網站傳播的新渠道;2.信息容易泄露。學習
3、三大國際標準網站
1.PDF417:不支持中文;this
2.DM:專利未公開,須要支付專利費用;google
3.QR Code:專利公開,支持中文。編碼
其中,QR Code具備識讀速度快、數據密度大、佔用空間小的優點。
4、糾錯能力
L級:約可糾錯7%的數據碼字
M級:約可糾錯15%的數據碼字
Q級:約可糾錯25%的數據碼字
H級:約可糾錯30%的數據碼字
5、ZXing生成/讀取二維碼
首先,下載ZXing源文件。下載地址:https://github.com/zxing/zxing/releases;
再次,建立一個Java項目。將ZXing源文件中的core/src/main/java/com和javase/src/main/java/com兩個文件複製到項目中,編譯成jar文件;
最後,在之後的開發中就可使用該jar文件。
ZXing生成二維碼的代碼以下:
1 package com.aston.qrcode.zxing; 2 3 import java.io.File; 4 import java.io.OutputStream; 5 import java.io.OutputStreamWriter; 6 import java.nio.file.Path; 7 import java.util.HashMap; 8 9 import javax.sound.midi.Patch; 10 11 import com.google.zxing.BarcodeFormat; 12 import com.google.zxing.EncodeHintType; 13 import com.google.zxing.MultiFormatWriter; 14 import com.google.zxing.client.j2se.MatrixToImageWriter; 15 import com.google.zxing.common.BitMatrix; 16 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; 17 18 //生成二維碼 19 public class CreateQRCode { 20 public static void main(String[] args){ 21 final int width = 300; 22 final int height = 300; 23 final String format = "png"; 24 final String content = "我愛你,中國"; 25 26 //定義二維碼的參數 27 HashMap hints = new HashMap(); 28 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 29 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); 30 hints.put(EncodeHintType.MARGIN, 2); 31 32 //生成二維碼 33 try{ 34 //OutputStream stream = new OutputStreamWriter(); 35 BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); 36 Path file = new File("F:/img.png").toPath(); 37 MatrixToImageWriter.writeToPath(bitMatrix, format, file); 38 //MatrixToImageWriter.writeToStream(bitMatrix, format, stream); 39 }catch(Exception e){ 40 41 } 42 43 } 44 45 }
ZXing讀取二維碼信息代碼以下:
1 package com.aston.qrcode.zxing; 2 3 import java.awt.image.BufferedImage; 4 import java.io.File; 5 import java.util.HashMap; 6 7 import javax.imageio.ImageIO; 8 9 import com.google.zxing.BinaryBitmap; 10 import com.google.zxing.EncodeHintType; 11 import com.google.zxing.MultiFormatReader; 12 import com.google.zxing.Result; 13 import com.google.zxing.client.j2se.BufferedImageLuminanceSource; 14 import com.google.zxing.common.HybridBinarizer; 15 16 //讀取二維碼信息 17 public class ReadQRCode { 18 19 public static void main(String[] args) throws Exception { 20 MultiFormatReader formatReader = new MultiFormatReader(); 21 File file = new File("F:/img.png"); 22 23 BufferedImage image = ImageIO.read(file); 24 BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image))); 25 26 //定義二維碼的參數 27 HashMap hints = new HashMap(); 28 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 29 30 Result result = formatReader.decode(binaryBitmap, hints); 31 32 System.out.println("二維碼解析結果:" + result.toString()); 33 System.out.println("二維碼的格式:" + result.getBarcodeFormat()); 34 System.out.println("二維碼的文本內容:" + result.getText()); 35 } 36 37 }
6、QRCode生成/讀取二維碼
QRCode生成和讀取二維碼的jar是分開的,下載網址以下:
QRCode生成二維碼網址:http://swetake.com/qrcode/index-e.html
QRCode讀取二維碼網址:https://osdn.jp/projects/qrcode
QRCode生成二維碼代碼以下:
1 package com.aston.qrcode.qrcode; 2 3 import java.awt.Color; 4 import java.awt.Graphics2D; 5 import java.awt.image.BufferedImage; 6 import java.io.File; 7 8 import javax.imageio.ImageIO; 9 10 import com.swetake.util.Qrcode; 11 12 public class CreateQRCode { 13 14 public static void main(String[] args) throws Exception { 15 Qrcode x=new Qrcode(); 16 x.setQrcodeErrorCorrect('M');//糾錯等級 17 x.setQrcodeEncodeMode('B');//N 表明數據; A 表明a-A; B 表明其餘字符 18 x.setQrcodeVersion(7);//版本 19 20 String qrData = "我愛你,中國"; 21 22 int width = 67 + 12*(7-1); 23 int height = 67 + 12*(7-1); 24 //int width = 300; 25 //int height = 300; 26 int pixoff = 2;//偏移量 27 28 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 29 30 Graphics2D gs = bufferedImage.createGraphics(); 31 gs.setBackground(Color.WHITE); 32 gs.setColor(Color.BLACK); 33 gs.clearRect(0, 0, width, height); 34 35 byte[] d =qrData.getBytes("utf-8"); 36 if (d.length>0 && d.length <120){ 37 boolean[][] s = x.calQrcode(d); 38 39 for (int i=0;i<s.length;i++){ 40 for (int j=0;j<s.length;j++){ 41 if (s[j][i]) { 42 gs.fillRect(j*3+pixoff,i*3+pixoff,3,3); 43 } 44 } 45 } 46 } 47 48 gs.dispose(); 49 bufferedImage.flush(); 50 51 ImageIO.write(bufferedImage, "png", new File("F:/qrcode.png")); 52 } 53 54 }
QRCode讀取二維碼信息代碼以下:
1 package com.aston.qrcode.qrcode; 2 3 import java.awt.image.BufferedImage; 4 import java.io.File; 5 import java.io.IOException; 6 7 import javax.imageio.ImageIO; 8 9 import jp.sourceforge.qrcode.QRCodeDecoder; 10 11 public class ReadQRCode { 12 public static void main(String[] args) throws Exception { 13 File file = new File("F:/qrcode.png"); 14 15 BufferedImage bufferedImage = ImageIO.read(file); 16 17 QRCodeDecoder qrCodeDecoder = new QRCodeDecoder(); 18 19 String result = new String(qrCodeDecoder.decode(new MyQRCodeImage(bufferedImage)),"utf-8"); 20 21 System.out.println("二維碼解析結果:" + result); 22 } 23 }
MyQRCodeImage.java代碼以下:
1 package com.aston.qrcode.qrcode; 2 3 import java.awt.image.BufferedImage; 4 5 import jp.sourceforge.qrcode.data.QRCodeImage; 6 7 public class MyQRCodeImage implements QRCodeImage { 8 private BufferedImage bufferedImage; 9 10 public MyQRCodeImage(BufferedImage bufferedImage){ 11 this.bufferedImage = bufferedImage; 12 } 13 14 @Override 15 public int getHeight() { 16 // TODO Auto-generated method stub 17 return bufferedImage.getHeight(); 18 } 19 20 @Override 21 public int getPixel(int arg0, int arg1) { 22 // TODO Auto-generated method stub 23 return bufferedImage.getRGB(arg0, arg1); 24 } 25 26 @Override 27 public int getWidth() { 28 // TODO Auto-generated method stub 29 return bufferedImage.getWidth(); 30 } 31 32 }
7、聲明
以上代碼均出自慕課網教程,且通過本人驗證可成功執行。轉載時能夠不註明本文網址,但請註明慕課網教程網址。