首先如今C#Qrcode的源碼 http://www.codeproject.com/Articles/20574/Open-Source-QRCode-Libraryhtml
而後須要修改一些代碼用於支持中文this
修改代碼的時候會遇到可能遇到的問題spa
問題一(這個問題我遇到了):調試
調試Winform平臺的例子時出現以下提 示:Error'ThoughtWorks.QRCode.Properties.Resources' does not contain a definition for 'GetResource'。code
只要把這些出錯的地方改爲:MemoryStream memoryStream = new MemoryStream((byte[])Resources.ResourceManager.GetObject(fileName)); 就能夠了。orm
問題二:htm
調試WM版的示例程序在Encode時,出現IndexOutOfRangeException in rsTemp 的超出索引最大值異常,它在rsTemp[rsBlockNumber][j] = codewords[i2];這裏拋出異常~
本來我覺得是程序邏輯出錯,後來通過對比桌面版和WM版的類庫,發現沒有錯啊,而後終於找到一個比較「笨」的解決方案:以下:
我注意到,在QRCodeMobileLib項目下的QRCodeEncoder.cs類中,定義了個: internal static String DATA_PATH = "qrcode_data";
此行代碼在桌面版被註釋掉了,我猜是否是要把資源文件拷貝到WM程序的這個目錄下呢?好吧,因而動手,在QRCodeWindowsMobile5項目下創建一個新文件夾,名字是qrcode_data,而後再拷貝那些資源文件到此目錄。
而後設置這些資源文件的生成動做爲(複製,內容)好了,而後再從新調試一下,Encode~經過!blog
修改支持中文:索引
修改庫QRCodeLib中的類QRCodeEncoder.cs資源
public virtual Bitmap Encode(String content) { if (QRCodeUtility.IsUniCode(content)) { return Encode(content, Encoding.Unicode); } else { return Encode(content, Encoding.ASCII); } }
爲
public virtual Bitmap Encode(String content) { if (QRCodeUtility.IsUniCode(content)) { return Encode(content, Encoding.GetEncoding("gb2312")); } else { return Encode(content, Encoding.ASCII); } }
對應的,QRCodeDecoder.cs類中的相應代碼也要改。這樣就中英文支持了。
擴展 1.修改 QRCodeEncoder.cs 跟 QRCodeDecoder.cs Search一下,將 Encoding.Unicode 替換成 Encoding.UTF8
2.修改 QRCodeUtility.cs 將 UnicodeEncoding encoding = new UnicodeEncoding(); 改爲 UTF8Encoding encoding = new UTF8Encoding();
參考自:http://www.cnblogs.com/shihao/archive/2012/01/05/2312879.html