促使草人寫這一系列(將會是)文章的原因是二維碼現在很流行,很容易接觸到,而且二維碼又是那么容易就生成——就不說有很多在線的生成器,許多應用軟件也都有生成二維碼的功能,比如Firefox瀏覽器、QQ等。
最初的時候,草人看到QQ生成的花哨的二維碼就想自己寫出生成自己喜歡的二維碼的程序。

當我開始去了解二維碼(后來主要是QR code)的時候,我知道QR code(QR Code碼,是由Denso公司于1994年9月研制的一種矩陣二維碼符號,它具有一維條碼及其它二維條碼所具有的信息容量大、可靠性高、可表示漢字及圖象多種文字信息、保密防偽性強等優點。——來自百度百科)并不是那么容易的實現,為什么不容易盜用一張高格逼的圖就能解釋了

要是盯著這些圖看,都要被嚇住了,整個很牛的樣子(沒說不牛啊)。所以決定先惡搞一番,就是先弄出一張看似二維碼的圖。

1 package spoofQRcode; 2 3 import java.awt.Color; 4 import java.awt.Graphics2D; 5 import java.awt.image.BufferedImage; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 import javax.imageio.ImageIO; 9 10 public class CreatQRImage2 { 11 12 public static String m_content = null; 13 PRivate static String[] m_numCoding = {"0000","0001","0010","0011","0100","0101","0110","0111","1000", 14 "1001"}; 15 private static String m_strCoding = null; 16 17 public CreatQRImage2(String content){ 18 this.m_content = content; 19 numToStrCoding(); 20 } 21 public static void numToStrCoding(){ 22 for(int n = 0;n < m_content.length();n++) 23 {for (int i = 0; i < m_numCoding.length ;i++){ 24 //System.out.println(content.charAt(n)+1); 25 if(m_content.charAt(n)-48 == i) 26 { 27 //System.out.println("ok"); 28 //System.out.println(numCoding[i]); 29 m_strCoding += m_numCoding[i]; 30 break; 31 } 32 else continue; 33 } 34 } 35 } 36 public static void creat(int imgSize,String imageFormat,String toPath)throws IOException{ 37 FileOutputStream fos = null; 38 BufferedImage buffImg = null; 39 try{ 40 41 buffImg = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_RGB); 42 Graphics2D gs = buffImg.createGraphics(); 43 gs.setBackground(Color.WHITE); 44 gs.clearRect(0, 0, imgSize, imgSize); 45 gs.setColor(Color.BLACK); 46 for(int i = 0;i <= 6;i++) 47 for(int j = 0;j <= 6;j++) 48 if(i==0||j==0||i==6||j==6) 49 {gs.fillRect(i*8,j*8,8,8);} 50 for(int i = 14;i <= 20;i++) 51 for(int j = 0;j <= 6;j++) 52 if(i==14||j==0||i==20||j==6) 53 {gs.fillRect(i*8,j*8,8,8);} 54 for(int i = 0;i <= 6;i++) 55 for(int j = 14;j <= 20;j++) 56 if(j==14||i==0||j==20||i==6) 57 {gs.fillRect(i*8,j*8,8,8); 58 59 } 60 gs.fillRect(2*8,2*8,24,24); 61 gs.fillRect(16*8,2*8,24,24); 62 gs.fillRect(2*8,16*8,24,24); 63 //gs.setColor(Color.blue); 64 int strCodingIndex = 4; 65 int x = 0; 66 int y = 0; 67 for(int k = strCodingIndex;k < m_strCoding.length();k++){ 68 //System.out.println("ok?"); 69 //System.out.println(strCoding.charAt(k)); 70 if(x > 20) 71 { 72 y += 1; 73 x -= 20; 74 } 75 if(!(x<7&&y<7)&&!(13<x&&y<7)&&!(13<y&&x<7)) 76 gs.fillRect(x*8,y*8, 77 (m_strCoding.charAt(k)-48)*8,(m_strCoding.charAt(k)-48)*8); 78 x++; 79 if(x==21&&y==20&&k<m_strCoding.length()){ 80 System.out.println("Oversize!"); 81 strCodingIndex = k; 82 break; 83 } 84 if(x<21&&y<=20&&k==m_strCoding.length()-1){ 85 System.out.println("Small Data!"); 86 strCodingIndex = k; 87 break; 88 } 89 90 } 91 92 gs.dispose(); 93 buffImg.flush(); 94 fos=new FileOutputStream(toPath); 95 ImageIO.write(buffImg, imageFormat, fos); 96 } 97 98 catch (Exception e) { 99 e.printStackTrace();100 }101 finally{102 if(fos!=null){103 fos.close();104 }}105 }106 107 public static void main(String[] args) throws IOException{108 int imgSize = 168;109 String imageFormat = "png";110 String toPath = "F:/z27.png";111 String content = "15162100138093948324385427385237923464085535342427"112 + "524378523234233343425673143325889543465754602394784752937307";113 CreatQRImage2 obj = new CreatQRImage2(content);114 obj.creat(imgSize,imageFormat,toPath);115 116 }117 }View Code
這樣的話可以得到這樣的結果——

我知道你會說“瞎了我的狗眼了”,這都是啥,首先不說代碼(不能直視,我只是圖個方便),這二維碼也不能掃啊。先消消氣,我且慢慢道來……
1.你看圖是不是已經有個“二維碼”的外貌了(別仔細看)?OK,那么也就是說至少某些地方對了。
2.再說說我的想法,生成二維碼圖,就是在一張“白布”上寫上一些由黑白(深淺)的塊,“黑塊”(深色)代表“1”,“白塊”(淺色)代表“0”,這樣就可以存儲數據了。

buffImg = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_RGB); Graphics2D gs = buffImg.createGraphics(); gs.setBackground(Color.WHITE); gs.clearRect(0, 0, imgSize, imgSize);“白布”

gs.setColor(Color.BLACK); gs.fillRect(x,y,width,height);"黑塊"
3.至于最大的問題就是,”白布“上”黑塊“(深色)、”白塊“(淺色)的規則和算法。要能夠被其他方提供的掃描器識別,格式就得標準化。要使生成的二維碼能”quick response"等就需要算法。
如果草人這樣瞎扯你沒法忍了,當然也可以是對二維碼產生興趣了,那先看看http://wenku.baidu.com/link?url=0BOpyLC5YOUTmdip7PlHIWOUihTQKQJJyVE_0Em9lEZda94FgGHROYrMtCoTN1oozw5gc-YPrgtAYabWgh1QbbYJAdu3YXb8wdug_YPuFWG(QRCode 編碼解碼標準)。這樣的話,下一篇應該就是實現了。
特別聲明:轉載請注明原始鏈接
新聞熱點
疑難解答