使用ZXing生成二维码 QR-Code
转自:http://hi.baidu.com/mmforever/blog/item/1d910b551be513dab645aed8.html
直接贴上代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package 2 { 3 import flash.display.Bitmap; 4 import flash.display.BitmapData; 5 import flash.display.Sprite; 6 import flash.events.Event; 7 import flash.events.MouseEvent; 8 import com.google.zxing.BarcodeFormat; 9 import com.google.zxing.MultiFormatWriter; 10 import com.google.zxing.common.ByteMatrix; 11 12 public class QR extends Sprite 13 { 14 private var qrImg:Bitmap; 15 16 public function QR() 17 { 18 if (stage) 19 { 20 init(); 21 } 22 else 23 { 24 this.addEventListener(Event.ADDED_TO_STAGE, init); 25 } 26 } 27 28 private function init(e:Event = null):void 29 { 30 this.removeEventListener(Event.ADDED_TO_STAGE, init); 31 btn.addEventListener(MouseEvent.CLICK, refreshCode); 32 } 33 34 private final function refreshCode(e:MouseEvent):void 35 { 36 if (qrImg != null) 37 { 38 mc.removeChild(qrImg); 39 qrImg = null; 40 } 41 42 var textString:String = txt.text; 43 var matrix:ByteMatrix; 44 var qrEncoder:MultiFormatWriter = new MultiFormatWriter(); 45 try 46 { 47 matrix = (qrEncoder.encode(textString,BarcodeFormat.QR_CODE,150,150)) as ByteMatrix; 48 } 49 catch (e:Error) 50 { 51 trace('err'); 52 return; 53 } 54 var bmd:BitmapData = new BitmapData(150,150,false,0x808080); 55 56 for (var h:int = 0; h < 150; h++) 57 { 58 for (var w:int = 0; w < 150; w++) 59 { 60 if (matrix._get(w,h) == 0) 61 { 62 bmd.setPixel(w, h, 0x000000); 63 } 64 else 65 { 66 bmd.setPixel(w, h, 0xFFFFFF); 67 } 68 } 69 } 70 qrImg = new Bitmap(bmd); 71 mc.addChild(qrImg); 72 } 73 } 74 }
ZXing 下载地址:
http://code.google.com/p/zxing/
ZXing 类库 注意点:
源代码中有两处UTF-8的问题,会导致乱码,
其一:com.google.zxing.qrcode.encoder.encoder类中的
internal const System.String DEFAULT_BYTE_MODE_ENCODING = "ISO-8859-1";
此处,将ISO-8859-1改为UTF-8
其二:com.google.zxing.qrcode.decoder.DecodedBitStreamParser类的成员
private const System.String UTF8 = "UTF8";
应将UTF8改为UTF-8
本人发现,搜索网上资料,按上述修改后,生成或者读取中文的二维码仍然会出错,经调试,须修改以下位置才行。
1、生成二维码
在com.google.zxing.qrcode.encoder.encoder类中
除了下面的
public static var DEFAULT_BYTE_MODE_ENCODING:String = "ISO-8859-1";
修改成
public static var DEFAULT_BYTE_MODE_ENCODING:String = "UTF-8";
外。
下面的函数须补上蓝色显示的内容
public static function append8BitBytes(content:String , bits:BitVector , encoding:String ):void
{
var bytes:ByteArray = new ByteArray();
try {
//bytes = content.getBytes(encoding);
if ((encoding == "Shift_JIS") || (encoding == "SJIS")) { bytes.writeMultiByte(content, "shift-jis");}
else if (encoding == "Cp437") { bytes.writeMultiByte(content, "IBM437"); }
else if (encoding == "ISO8859_2") { bytes.writeMultiByte(content, "iso-8859-2"); }
else if (encoding == "ISO8859_3") { bytes.writeMultiByte(content, "iso-8859-3"); }
else if (encoding == "ISO8859_4") { bytes.writeMultiByte(content, "iso-8859-4"); }
else if (encoding == "ISO8859_5") { bytes.writeMultiByte(content, "iso-8859-5"); }
else if (encoding == "ISO8859_6") { bytes.writeMultiByte(content, "iso-8859-6"); }
else if (encoding == "ISO8859_7") { bytes.writeMultiByte(content, "iso-8859-7"); }
else if (encoding == "ISO8859_8") { bytes.writeMultiByte(content, "iso-8859-8"); }
else if (encoding == "ISO8859_9") { bytes.writeMultiByte(content, "iso-8859-9"); }
else if (encoding == "ISO8859_11"){ bytes.writeMultiByte(content, "iso-8859-11"); }
else if (encoding == "ISO8859_15"){ bytes.writeMultiByte(content, "iso-8859-15"); }
else if ((encoding == "ISO-8859-1") || (encoding == "ISO8859-1")) { bytes.writeMultiByte(content, "iso-8859-1"); }
else if (encoding == "UTF-8"){ bytes.writeMultiByte(content, "utf-8"); }
else
{
//other encodings not supported
throw new Error("Encoding "+ encoding + " not supported");
}
bytes.position = 0;
} catch (uee:Error) {
throw new WriterException(uee.toString());
}
for (var i:int = 0; i < bytes.length; ++i) {
bits.appendBits(bytes[i], 8);
}
}
2、读取二维码
打开com.google.zxing.qrcode.decoder.DecodedBitStreamParser类
查看代码,不知为何,默认了使用Shift-JIS,日文编码。
以下改成默认UTF-8编码。
1)增加定义:
private static var ASSUME_UTF_8:Boolean=true;
2)返回UTF-8,修改guessEncoding,在函数第一行增加以下内容
private static function guessEncoding(bytes:Array):String
{
if(ASSUME_UTF_8){
return UTF8;
}
………………
………………
}
另外,
ZXing类库在发布错误时出现的
import mx.controls.Image 和 import mx.controls.List 错误提示
直接注释掉代码行就行。
参考资料:
http://hi.baidu.com/baid/blog/item/fd446e06d199a77702088102.html
http://blog.csdn.net/NickWar/article/details/5684134
http://www.remotesynthesis.com/post.cfm/adding-a-qr-code-reader-in-flex-on-android
http://blog.everythingflex.com/2011/03/15/flex-qr-code-sample/