Adding a QR Code Reader in Flex on Android

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<br><br><?xml version="1.0" encoding="utf-8"?>
      xmlns:s="library://ns.adobe.com/flex/spark" title="HomeView">
   <fx:Declarations>
      <!-- Place non-visual elements (e.g., services, value objects) here -->
   </fx:Declarations>
   <fx:Script>
      <![CDATA[
         import com.google.zxing.BarcodeFormat;
         import com.google.zxing.BinaryBitmap;
         import com.google.zxing.BufferedImageLuminanceSource;
         import com.google.zxing.DecodeHintType;
         import com.google.zxing.Result;
         import com.google.zxing.client.result.ParsedResult;
         import com.google.zxing.client.result.ResultParser;
         import com.google.zxing.common.BitMatrix;
         import com.google.zxing.common.ByteMatrix;
         import com.google.zxing.common.GlobalHistogramBinarizer;
         import com.google.zxing.common.flexdatatypes.HashTable;
         import com.google.zxing.qrcode.QRCodeReader;
         import com.google.zxing.qrcode.detector.Detector;
          
         import spark.events.ViewNavigatorEvent;
          
         protected var camera:Camera;
         private var videoDisplay:Video=new Video(360, 360);
         private var qrReader:QRCodeReader;
         private var bmd:BitmapData;
         private var cameraStarted:Boolean = false;
          
         protected function button1_clickHandler(event:MouseEvent):void
         {
            if (!cameraStarted) {
               if (Camera.isSupported)
               {
                  camera=Camera.getCamera();
                  camera.setMode(360, 360, 24);
                   
                  videoDisplay.x = 360;
                  sv.addChild(videoDisplay);
                   
                  videoDisplay.attachCamera(camera);
                  videoDisplay.rotation=90;
                  qrReader=new QRCodeReader;
                  btn.label = "Scan Now";
                  lbl.text = "";
                  cameraStarted = true;
               }
               else {
                  lbl.text = "no camera found";
               }
            }
            else {
               decodeSnapshot();
            }
         }
          
         public function decodeSnapshot():void
         {
            lbl.text="checking...";
            bmd=new BitmapData(300, 300);
            bmd.draw(videoDisplay, null, null, null, null, true);
            videoDisplay.cacheAsBitmap=true;
            videoDisplay.cacheAsBitmapMatrix=new Matrix;
            decodeBitmapData(bmd, 300, 300);
            bmd.dispose();
            bmd=null;
            System.gc();
         }
          
         public function decodeBitmapData(bmpd:BitmapData, width:int, height:int):void
         {
            var lsource:BufferedImageLuminanceSource=new BufferedImageLuminanceSource(bmpd);
            var bitmap:BinaryBitmap=new BinaryBitmap(new GlobalHistogramBinarizer(lsource));
             
            var ht:HashTable=null;
            ht=this.getAllHints();
             
            var res:Result=null;
            try {
               res=qrReader.decode(bitmap, ht);
            }
            catch (event:Error) {
            }
             
            if (res == null) {
               videoDisplay.clear();
               lbl.text="nothing decoded";
            }
            else {
               var parsedResult:ParsedResult=ResultParser.parseResult(res);
               lbl.text=parsedResult.getDisplayResult();
               sv.removeChild(videoDisplay);
               cameraStarted = false;
               btn.label = "Start Camera";
            }
         }
          
         public function getAllHints():HashTable
         {
            var ht:HashTable=new HashTable;
            ht.Add(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE);
            return ht;
         }
          
      ]]>
   </fx:Script>
   <s:VGroup width="100%" horizontalAlign="center" id="vg">
      <s:SpriteVisualElement id="sv" width="360" height="400"/>
      <s:Label id="lbl" text="" />
      <s:Button id="btn" label="Start Camera" width="220" height="93" click="button1_clickHandler(event)"/>
   </s:VGroup>
</s:View>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public function decodeSnapshot():void
{
   lbl.text="checking...";
   bmd=new BitmapData(300, 300);
   bmd.draw(videoDisplay, null, null, null, null, true);
   videoDisplay.cacheAsBitmap=true;
   videoDisplay.cacheAsBitmapMatrix=new Matrix;
   decodeBitmapData(bmd, 300, 300);
   bmd.dispose();
   bmd=null;
   System.gc();
}
          
public function decodeBitmapData(bmpd:BitmapData, width:int, height:int):void
{
   var lsource:BufferedImageLuminanceSource=new BufferedImageLuminanceSource(bmpd);
   var bitmap:BinaryBitmap=new BinaryBitmap(new GlobalHistogramBinarizer(lsource));
 
   var ht:HashTable=null;
   ht=this.getAllHints();
             
   var res:Result=null;
   try {
      res=qrReader.decode(bitmap, ht);
   }
   catch (event:Error) {
   }
             
   if (res == null) {
      videoDisplay.clear();
      lbl.text="nothing decoded";
   }
   else {
      var parsedResult:ParsedResult=ResultParser.parseResult(res);
      lbl.text=parsedResult.getDisplayResult();
      sv.removeChild(videoDisplay);
      cameraStarted = false;
      btn.label = "Start Camera";
   }
}
          
public function getAllHints():HashTable
{
   var ht:HashTable=new HashTable;
   ht.Add(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE);
   return ht;
}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if (Camera.isSupported)
{
   camera=Camera.getCamera();
   camera.setMode(360, 360, 24);
                   
   videoDisplay.x = 360;
   sv.addChild(videoDisplay);
    
   videoDisplay.attachCamera(camera);
   videoDisplay.rotation=90;
   qrReader=new QRCodeReader;
   btn.label = "Scan Now";
   lbl.text = "";
   cameraStarted = true;
}
else {
   lbl.text = "no camera found";
}

Adding a QR Code Reader in Flex on Android,布布扣,bubuko.com

Adding a QR Code Reader in Flex on Android

上一篇:android手机两种方式获取IP地址


下一篇:IOS_画图 图片等比压缩 IOS_UIImage