【转】使用ZXing生成二维码 QR-Code

6 篇文章 0 订阅

直接贴上代码:

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/


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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值