- 积分
- 136401
- 注册时间
- 2014-12-27
- 最后登录
- 2026-1-23
- 在线时间
- 605 小时
- 威望
- 562
- 贡献
- 29
- 金币
- 52903
- 钢镚
- 1422
- 交易凭证
- 1
- 分享
- 0
- 精华
- 33
- 帖子
- 2094
- 主题
- 1742
TA的每日心情 | 擦汗 2018-4-10 15:18 |
|---|
签到天数: 447 天 [LV.9]以坛为家II
超级版主
    
- 威望
- 562
- 贡献
- 29
- 金币
- 52903
- 钢镚
- 1422
 
|
本帖最后由 破晓 于 2015-8-17 11:22 编辑
直接贴上代码:
- import flash.display.Bitmap;
- import flash.display.BitmapData;
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.events.MouseEvent;
- import com.google.zxing.BarcodeFormat;
- import com.google.zxing.MultiFormatWriter;
- import com.google.zxing.common.ByteMatrix;
-
- public class QR extends Sprite {
- private var qrImg:Bitmap;
-
- public function QR() {
- if (stage) init();
- else this.addEventListener(Event.ADDED_TO_STAGE, init);
- }
-
- private function init(e:Event = null):void
- {
- this.removeEventListener(Event.ADDED_TO_STAGE, init);
- btn.addEventListener(MouseEvent.CLICK, refreshCode);
- }
- private final function refreshCode(e:MouseEvent):void
- {
- if(qrImg != null)
- {
- mc.removeChild(qrImg);
- qrImg = null;
- }
- var textString:String = txt.text;
- var matrix:ByteMatrix;
- var qrEncoder:MultiFormatWriter = new MultiFormatWriter();
- try
- {
- matrix = (qrEncoder.encode(textString,BarcodeFormat.QR_CODE,250,250)) as ByteMatrix;
- }
- catch (e:Error)
- {
- trace('err');
- return;
- }
- var bmd:BitmapData = new BitmapData(250, 250, false, 0x808080);
- for (var h:int = 0; h < 250; h++)
- {
- for (var w:int = 0; w < 250; w++)
- {
- if (matrix._get(w, h) == 0)
- {
- bmd.setPixel(w, h, 0x000000);
- }
- else
- {
- bmd.setPixel(w, h, 0xFFFFFF);
- }
- }
- }
- qrImg = new Bitmap(bmd);
- mc.addChild(qrImg);
- }
- }
复制代码 ZXing 下载地址:
http://code.google.com/p/zxing/
https://github.com/zxing/zxing
ZXing 类库注意点:
源代码中有两处UTF-8的问题,会导致乱码,
其一:com.google.zxing.qrcode.encoder.encoder类中的
internal const System.StringDEFAULT_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/p ... -in-flex-on-android
http://blog.everythingflex.com/2011/03/15/flex-qr-code-sample/
本文来自:http://blog.csdn.net/linguifa/article/details/41076131
|
|