1、最近使用到了生成二維碼的技術,特此記錄數組
本文介紹的是Denso的QRCODE。服務器
2、知識準備maven
2.一、QRCODE基本屬性簡介編碼
一、符號規格 spa
從版本1(21×21模塊)到版本40(177×177 模塊),每提升一個版本,每邊增長4個模塊。code
二、數據類型與容量(參照最大規格符號版本40-L級):orm
數字數據:7,089個字符對象
字母數據: 4,296個字符blog
8位字節數據: 2,953個字符圖片
漢字數據:1,817個字符
三、數據表示方法:
深色模塊表示二進制"1",淺色模塊表示二進制"0"。
四、糾錯能力(同符號規格下,糾錯能力越高,二維碼容量越低):
L級:約可糾錯7%的數據碼字
M級:約可糾錯15%的數據碼字
Q級:約可糾錯25%的數據碼字
H級:約可糾錯30%的數據碼字
2.二、maven依賴
<dependency>
<groupId>qrcode</groupId>
<artifactId>qrcode</artifactId>
<version>1.0</version>
</dependency>
3、生成二維碼BufferedImage對象
/** * @Description: 生成BufferedImage對象 * @param content * 二維碼存放的信息 * @param errorCorrect * 容錯級別 * 表示的字符串長度: 容錯率(ECC) 顯示編碼模式(EncodeMode)及版本(Version)有關二維碼的糾錯級別(排錯率), * 共有四級:可選L(7%)、M(15%)、Q(25%)、H(30%)(最高H)。 * 糾錯信息一樣存儲在二維碼中,糾錯級別越高,糾錯信息佔用的空間越多,那麼能存儲的有用信息就越少, * 對二維碼清晰度的要求越小 * @param mode * 編碼模式 編碼模式:Numeric 數字, Alphanumeric 英文字母,Binary 二進制,Kanji * 漢字(第一個大寫字母表示) * @param version * 二維碼的版本號 二維碼的版本號:也象徵着二維碼的信息容量;二維碼能夠當作一個黑白方格矩陣,版本不一樣, * 矩陣長寬方向方格的總數量分別不一樣。 1-40總共40個版本,版本1爲21*21矩陣,版本每增1,二維碼的兩個邊長都增4; * 版本2 爲25x25模塊,最高版本爲是40,是177*177的矩陣; * @return * @throws IOException */ public static BufferedImage createQrcode(String content, char errorCorrect, char mode, int version) throws IOException { BufferedImage image =null; if (null == content || "".equals(content)) { } else { Qrcode qrcode = new Qrcode(); qrcode.setQrcodeErrorCorrect(errorCorrect); qrcode.setQrcodeEncodeMode(mode); qrcode.setQrcodeVersion(version); // 獲取內容的字節數組,設置編碼格式 byte[] bytes = content.getBytes("UTF-8"); // 圖片尺寸,會根據version的變大,而變大,本身須要計算 int imgSize = 67 + 12 * (version - 1); image = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_BYTE_BINARY); // 獲取畫筆 Graphics2D gs = image.createGraphics(); // 設置背景色 白色 gs.setBackground(Color.WHITE); gs.clearRect(0, 0, imgSize, imgSize); // 設定圖像顏色 黑色 gs.setColor(Color.BLACK); // 設置偏移量,不設置可能致使二維碼生產錯誤(解析失敗出錯) int pixoff = 2; // 二維碼輸出 if (bytes.length > 0 && bytes.length < 120) { boolean[][] s = qrcode.calQrcode(bytes); for (int i = 0; i < s.length; i++) { for (int j = 0; j < s.length; j++) { if (s[j][i]) { //注意j * 3 + pixoff, i * 3 + pixoff, 3, 3中的pixoff和3也會影響二維碼像素,可是影響並不會很大, //二維碼像素主要受version影響 gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);//填充矩形區域 } } } } gs.dispose(); image.flush(); } return image; }
4、生成二維碼圖片
/** * 二維碼輸出到文件 * @param image 二維碼內容 * @param format 圖片格式 例如jpg、gif等 * @param file 輸出文件 * @return * */ public static File writeToFile(BufferedImage image,String format,File file) throws IOException{ if(null == file){ file = new File("E:"+File.separator+"xxx.xxx");//本身的默認存儲路徑 } ImageIO.write(image, format, file); return file; }
5、轉換成流
若是須要上傳到圖片服務器,須要將BufferedImage轉換成流形式
/** * @Description: 轉換BufferedImage->輸出流 * @param image 二維碼內容 * @param format 圖片格式 例如jpg、gif等 * @param outPutStream 輸出流 * @return * @throws IOException */ public static OutputStream writeToStream(BufferedImage image,String format,OutputStream outPutStream) throws IOException{ if(null == outPutStream){ outPutStream = new ByteArrayOutputStream(); } ImageIO.write(image,format, outPutStream); return outPutStream; }