QR code 乱谈(一)

  • 缘由  

  促使草人写这一系列(将会是)文章的原因是二维码现在很流行,很容易接触到,而且二维码又是那么容易就生成——就不说有很多在线的生成器,许多应用软件也都有生成二维码的功能,比如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 编码解码标准)。这样的话,下一篇应该就是实现了。

特别声明:转载请注明原始链接

作者:半透明的稻草人 出处:http://www.cnblogs.com/zhry/
原文地址:https://www.cnblogs.com/zhry/p/4720315.html