我們都知道,最近2年移動支付在中國堪稱新四大發明之一。
二維碼無處不在,特別是最近的支付寶掃碼領紅包,微信,qq,到處在發,阿里有點攻占騰訊移動支付市場的勢頭啊~博主忽然就對二維碼是怎么畫的有了點好奇,然后自己就整了一下,整體看下來比較簡單,好了廢話不多說,直接上碼~
1,新建一個工程,如下圖,命名為QRcode,然后新建package 命名為code
2,新建一個類QRCode,代碼如下:
1 package code; 2 3 import java.awt.Color; 4 import java.awt.Graphics2D; 5 import java.awt.image.BufferedImage; 6 import java.io.File; 7 import java.io.IOException; 8 import java.io.InputStream; 9 import java.io.OutputStream; 10 11 import javax.imageio.ImageIO; 12 13 import jp.sourceforge.qrcode.QRCodeDecoder; 14 import jp.sourceforge.qrcode.exception.DecodingFailedException; 15 16 import com.swetake.util.Qrcode; 17 18 public class QRCode { 19 20 /** 21 * 生成二維碼(QRCode)圖片 22 * @param content 存儲內容 23 * @param imgPath 圖片路徑 24 */ 25 public void encoderQRCode(String content, String imgPath) { 26 this.encoderQRCode(content, imgPath, "png", 10); 27 } 28 29 /** 30 * 生成二維碼(QRCode)圖片 31 * @param content 存儲內容 32 * @param output 輸出流 33 */ 34 public void encoderQRCode(String content, OutputStream output) { 35 this.encoderQRCode(content, output, "png", 10); 36 } 37 38 /** 39 * 生成二維碼(QRCode)圖片 40 * @param content 存儲內容 41 * @param imgPath 圖片路徑 42 * @param imgType 圖片類型 43 */ 44 public void encoderQRCode(String content, String imgPath, String imgType) { 45 this.encoderQRCode(content, imgPath, imgType, 10); 46 } 47 48 /** 49 * 生成二維碼(QRCode)圖片 50 * @param content 存儲內容 51 * @param output 輸出流 52 * @param imgType 圖片類型 53 */ 54 public void encoderQRCode(String content, OutputStream output, String imgType) { 55 this.encoderQRCode(content, output, imgType, 10); 56 } 57 58 /** 59 * 生成二維碼(QRCode)圖片 60 * @param content 存儲內容 61 * @param imgPath 圖片路徑 62 * @param imgType 圖片類型 63 * @param size 二維碼尺寸 64 */ 65 public void encoderQRCode(String content, String imgPath, String imgType, int size) { 66 try { 67 BufferedImage bufImg = this.qRCodeCommon(content, imgType, size); 68 69 File imgFile = new File(imgPath); 70 // 生成二維碼QRCode圖片 71 ImageIO.write(bufImg, imgType, imgFile); 72 } catch (Exception e) { 73 e.printStackTrace(); 74 } 75 } 76 77 /** 78 * 生成二維碼(QRCode)圖片 79 * @param content 存儲內容 80 * @param output 輸出流 81 * @param imgType 圖片類型 82 * @param size 二維碼尺寸 83 */ 84 public void encoderQRCode(String content, OutputStream output, String imgType, int size) { 85 try { 86 BufferedImage bufImg = this.qRCodeCommon(content, imgType, size); 87 // 生成二維碼QRCode圖片 88 ImageIO.write(bufImg, imgType, output); 89 } catch (Exception e) { 90 e.printStackTrace(); 91 } 92 } 93 94 /** 95 * 生成二維碼(QRCode)圖片的公共方法 96 * @param content 存儲內容 97 * @param imgType 圖片類型 98 * @param size 二維碼尺寸 99 * @return 100 */ 101 private BufferedImage qRCodeCommon(String content, String imgType, int size) { 102 BufferedImage bufImg = null; 103 try { 104 Qrcode qrcodeHandler = new Qrcode(); 105 // 設置二維碼排錯率,可選L(7%)、M(15%)、Q(25%)、H(30%),排錯率越高可存儲的信息越少,但對二維碼清晰度的要求越小 106 qrcodeHandler.setQrcodeErrorCorrect('M'); 107 qrcodeHandler.setQrcodeEncodeMode('B'); 108 // 設置設置二維碼尺寸,取值范圍1-40,值越大尺寸越大,可存儲的信息越大 109 qrcodeHandler.setQrcodeVersion(size); 110 // 獲得內容的字節數組,設置編碼格式 111 byte[] contentBytes = content.getBytes("utf-8"); 112 // 圖片尺寸 113 int imgSize = 67 + 12 * (size - 1); 114 bufImg = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_RGB); 115 Graphics2D gs = bufImg.createGraphics(); 116 // 設置背景顏色 117 gs.setBackground(Color.WHITE); 118 gs.clearRect(0, 0, imgSize, imgSize); 119 120 // 設定圖像顏色> BLACK 121 gs.setColor(Color.BLACK); 122 // 設置偏移量,不設置可能導致解析出錯 123 int pixoff = 2; 124 // 輸出內容> 二維碼 125 if (contentBytes.length > 0 && contentBytes.length < 1600) { 126 boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes); 127 for (int i = 0; i < codeOut.length; i++) { 128 for (int j = 0; j < codeOut.length; j++) { 129 if (codeOut[j][i]) { 130 gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3); 131 } 132 } 133 } 134 } else { 135 throw new Exception("QRCode content bytes length = " + contentBytes.length + " not in [0, 800]."); 136 } 137 gs.dispose(); 138 bufImg.flush(); 139 } catch (Exception e) { 140 e.printStackTrace(); 141 } 142 return bufImg; 143 } 144 145 /** 146 * 解析二維碼(QRCode) 147 * @param imgPath 圖片路徑 148 * @return 149 */ 150 public String decoderQRCode(String imgPath) { 151 // QRCode 二維碼圖片的文件 152 File imageFile = new File(imgPath); 153 BufferedImage bufImg = null; 154 String content = null; 155 try { 156 bufImg = ImageIO.read(imageFile); 157 QRCodeDecoder decoder = new QRCodeDecoder(); 158 content = new String(decoder.decode(new TwoDimensionCodeImage(bufImg)), "utf-8"); 159 } catch (IOException e) { 160 System.out.println("Error: " + e.getMessage()); 161 e.printStackTrace(); 162 } catch (DecodingFailedException dfe) { 163 System.out.println("Error: " + dfe.getMessage()); 164 dfe.printStackTrace(); 165 } 166 return content; 167 } 168 169 /** 170 * 解析二維碼(QRCode) 171 * @param input 輸入流 172 * @return 173 */ 174 public String decoderQRCode(InputStream input) { 175 BufferedImage bufImg = null; 176 String content = null; 177 try { 178 bufImg = ImageIO.read(input); 179 QRCodeDecoder decoder = new QRCodeDecoder(); 180 content = new String(decoder.decode(new TwoDimensionCodeImage(bufImg)), "utf-8"); 181 } catch (IOException e) { 182 System.out.println("Error: " + e.getMessage()); 183 e.printStackTrace(); 184 } catch (DecodingFailedException dfe) { 185 System.out.println("Error: " + dfe.getMessage()); 186 dfe.printStackTrace(); 187 } 188 return content; 189 } 190 }
3,再新建一個圖片對象類TwoDimensionCodeImage ,代碼如下:
1 package code; 2 3 import java.awt.image.BufferedImage; 4 5 import jp.sourceforge.qrcode.data.QRCodeImage; 6 7 public class TwoDimensionCodeImage implements QRCodeImage { 8 9 BufferedImage bufImg; 10 11 public TwoDimensionCodeImage(BufferedImage bufImg) { 12 this.bufImg = bufImg; 13 } 14 15 @Override 16 public int getHeight() { 17 return bufImg.getHeight(); 18 } 19 20 @Override 21 public int getPixel(int x, int y) { 22 return bufImg.getRGB(x, y); 23 } 24 25 @Override 26 public int getWidth() { 27 return bufImg.getWidth(); 28 } 29 30 }
4,最重要的部分來了,引入QRCode.jar包,如下圖,(注,jar包地址就不寫了,可自行百度下載~包也不大)
5,整個項目調試沒有報錯,即可。
6,我們來寫一段測試代碼試試,代碼如下:
1 public static void main(String[] args) { 2 String imgPath = "C:\\Users\\JJJ\\Desktop\\11.jpg"; 3 String encoderContent = "曾經\r\n 有人說我帥我不承認\r\n 然后他們就打我\r\n 現在\r\n 那些說我帥的人 \r\n都用手機掃到了這張圖\r\n 他們會不會又想打我~\r\n →_→ Merry Christmas~JJJ"; 4 QRCode handler = new QRCode(); 5 handler.encoderQRCode(encoderContent, imgPath, "png"); 6 System.out.println("========encoder success"); 7 String decoderContent = handler.decoderQRCode(imgPath); 8 System.out.println("解析結果如下:"); 9 System.out.println(decoderContent); 10 System.out.println("========decoder success!!!"); 11 }
7,測試結果截圖如下:
擼完收功~
整個還是相對來說很簡單的,因為主要的畫圖部分功能,都是由QRcode.jar包完成的,另外要說明的是,我們的字符串長度是有限制的,目前代碼中是10*10的矩陣,如果要顯示更多的字符就需要將矩陣調大。最大是44*44貌似,最小是7*7,如果沒記錯的話~
寫在最后
忽然發現我們以后去相親的話,自己制作一張二維碼,寫上工作,姓名,年齡,愛好,等等~互相掃一掃,是不是也是很方便啊~還有就是可以自己制作一張二維碼發給朋友或者對象,調戲一下,也是不錯的~作為一名程序員,是不是可以盡顯我們的逼格~不然總以為我們只會修電腦~~