java產生二維碼的幾種方式

來源:互聯網
上載者:User

標籤:writer   rom      機制   產生   tab   nload   計算   dea   

1: 使用SwetakeQRCode在Java項目中產生二維碼
http://swetake.com/qr/
或著http://sourceforge.jp/projects/qrcode/downloads/28391/qrcode.zip
這個是日本人寫的,產生的是我們常見的方形的二維碼
可以用中文

如:5677777ghjjjjj

2: 使用BarCode4j產生條碼和二維碼
BarCode4j網址:http://sourceforge.NET/projects/barcode4j/

barcode4j是使用datamatrix的二維碼產生演算法,為支援qr的演算法
datamatrix是歐美的標準,qr為日本的標準,
barcode4j一般產生出來是長方形的

如:88777alec000yan
這個部落格這方面說的挺清楚的:
http://baijinshan.iteye.com/blog/1004554

3:zxing
zxing 這個是google的

http://code.google.com/p/zxing/downloads/list

Java代碼:

 

import java.io.File;   import java.util.Hashtable;     import com.google.zxing.BarcodeFormat;   import com.google.zxing.EncodeHintType;   import com.google.zxing.MultiFormatWriter;   import com.google.zxing.client.j2se.MatrixToImageWriter;   import com.google.zxing.common.BitMatrix;   import com.google.zxing.qrcode.QRCodeWriter;         public class QRCodeEvents {              public static void main(String []args)throws Exception{           String text = "你好";           int width = 100;           int height = 100;           String format = "png";           Hashtable hints= new Hashtable();           hints.put(EncodeHintType.CHARACTER_SET, "utf-8");            BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height,hints);            File outputFile = new File("new.png");            MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);                   }   } 

 

4:google chart api就有實現二維碼的方法

    利用這個api,使用google appengine進行實現。

 

5:JS產生二維碼

使用jQuery-qrcode產生二維碼

先簡單說一下jquery-qrcode,這個開源的三方庫(可以從https://github.com/jeromeetienne/jquery-qrcode 擷取),

qrcode.js 是實現二維碼資料計算的核心類,

jquery.qrcode.js 是把它用jquery方式封裝起來的,用它來實現圖形渲染,其實就是畫圖(支援canvas和table兩種方式)


支援的功能主要有:

Js代碼:

  1. text     : "https://github.com/jeromeetienne/jquery-qrcode"  //設定二維碼內容  

     

Js代碼:

  1. render   : "canvas",//設定渲染方式  
  2. width       : 256,     //設定寬度  
  3. height      : 256,     //設定高度  
  4. typeNumber  : -1,      //計算模式  
  5. correctLevel    : QRErrorCorrectLevel.H,//錯誤修正等級  
  6. background      : "#ffffff",//背景顏色  
  7. foreground      : "#000000" //前景顏色  

使用方式非常簡單

 

Js代碼:

  1. jQuery(‘#output‘).qrcode({width:200,height:200,correctLevel:0,text:content});  

經過簡單實踐,

 

使用canvas方式渲染效能還是非常不錯的,但是如果用table方式,效能不太理想,特別是IE9以下的瀏覽器,所以需要自行最佳化一下渲染table的方式,這裡就不細述了。

其實上面的js有一個小小的缺點,就是預設不支援中文。

這跟js的機制有關係,jquery-qrcode這個庫是採用 charCodeAt() 這個方式進行編碼轉換的,

而這個方法預設會擷取它的 Unicode 編碼,一般的解碼器都是採用UTF-8, ISO-8859-1等方式,

英文是沒有問題,如果是中文,一般情況下Unicode是UTF-16實現,長度2位,而UTF-8編碼是3位,這樣二維碼的編解碼就不匹配了。

 

解決方式當然是,在二維碼編碼前把字串轉換成UTF-8,具體代碼如下:

 

function utf16to8(str) {       var out, i, len, c;       out = "";       len = str.length;       for(i = 0; i < len; i++) {       c = str.charCodeAt(i);       if ((c >= 0x0001) && (c <= 0x007F)) {           out += str.charAt(i);       } else if (c > 0x07FF) {           out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));           out += String.fromCharCode(0x80 | ((c >>  6) & 0x3F));           out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));       } else {           out += String.fromCharCode(0xC0 | ((c >>  6) & 0x1F));           out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));       }       }       return out;   }  

 

java產生二維碼的幾種方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.