mirror of
https://github.com/zxing/zxing.git
synced 2025-02-21 02:55:27 -08:00
git-svn-id: https://zxing.googlecode.com/svn/trunk@2109 59b500cc-1b3d-0410-9834-0bbf25fbcc57
792 lines
32 KiB
XML
792 lines
32 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<mx:Application
|
|
xmlns:mx="http://www.adobe.com/2006/mxml"
|
|
layout="absolute" creationComplete="{this.init();}">
|
|
<mx:Script>
|
|
<![CDATA[
|
|
import com.google.zxing.client.testClass;
|
|
|
|
/*
|
|
Dear developer,
|
|
|
|
Thank you for your interest in the AS3 implementation of the zxing library.
|
|
This Actionscript (AS3) code needs Flash player version 10 to run.
|
|
This file is a very basic example how to use the zxing library to
|
|
scan various barcodes with a standard webcam.
|
|
This conversion is based on the zxing Java library, checked out from the SVN on August 7th 2009 (SVN revision ???)
|
|
All additions and bugfixes added after this date have not yet been implemented in this version.
|
|
This AS3 implementation has roughly the same recognition rate as revision ??? of Java implementation.
|
|
|
|
How to build this example in FlexBuilder:
|
|
- import this project (File->Import->Flex Project : select the 'client' folder)
|
|
- uncheck 'Use default location' and press Finish
|
|
- import the zxing library project (File->Import->FlexProject : select the 'core' folder)
|
|
- uncheck 'Use default location' and press Finish
|
|
- Right click the 'zxing client' project to select Properties
|
|
- goto the 'Flex Build Path' section and select the 'Source path' tabsheet
|
|
- click the 'Add Folder' button and select the 'src' directory in the 'core' project folder
|
|
Now the client will use the zxing source files directly to build the application.
|
|
Any updates in the library will then automatically be used by the client
|
|
An an alternative would be to generate a SWC library file for the zxing.
|
|
|
|
For more details, join the zxing project and please commit your bugfixes to the zxing repository.
|
|
http://zxing.google.com
|
|
|
|
Bas Vijfwinkel
|
|
|
|
Notes :
|
|
- This project should compile with Flexbuilder 3.0 in combination with Flash10.
|
|
If you do not need access to the local filesystem, Flash 9 should be sufficient to run this application.
|
|
- I have omitted some very specific settings in this example application (e.g. the extended mode for code39/128 barcodes).
|
|
The zxing library does handle these barcodes correctly if you set these modes correctly.
|
|
- Actionscript is not very good at handling character sets with various encodings (used in e.g. in QRCodes).
|
|
UTF8 encodings seem to work but I have not been able to test other character encodings.
|
|
Make sure to set the character encoding options.
|
|
- I have disabled the PDF417 writer because although the output is the same as the core library, the output does not
|
|
seem to be correct.
|
|
- Great performance improvements can be made but this needs quite some altering of the
|
|
current library.
|
|
In order to keep the library as much in line with the original library for future updates and bugfixes,
|
|
I have choosen not to implement those improvements here. I believe maintainability has a higher priority
|
|
than performance at this moment
|
|
I have introduced some extra classes in the
|
|
com/google/zxing/common/flexdatatypes folder that match some of the default Java classes.
|
|
- If you encounter bugs (and hopefully solutions) please join the zxing project and commit your fixes to
|
|
the repository. Zxing project members are always willing to assist you if you're not familiar with repositories.
|
|
- The Zxing project files are licensed under the Apache License, Version 2.0 (the "License");
|
|
You may not use this file except in compliance with the License. You may obtain a copy of the License at
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
- When using a webcam make sure you set the resolution to at least 300x300 with the setMode method of the Camera object.
|
|
The default 160x120 resolution of some webcams is too low for recognition of most barcodes.
|
|
|
|
*/
|
|
import mx.core.BitmapAsset;
|
|
import com.google.zxing.common.flexdatatypes.HashTable;
|
|
import flash.net.FileReference;
|
|
import flash.display.Bitmap;
|
|
import flash.display.*;
|
|
import flash.net.URLRequest;
|
|
import flash.events.Event;
|
|
import mx.controls.Alert;
|
|
import com.google.zxing.Writer;
|
|
import com.google.zxing.LuminanceSource;
|
|
import com.google.zxing.BufferedImageLuminanceSource;
|
|
import com.google.zxing.common.BitMatrix;
|
|
|
|
//import com.google.zxing.common.GlobalHistogramBinarizer;
|
|
import com.google.zxing.common.ByteMatrix;
|
|
import com.google.zxing.client.result.ParsedResult;
|
|
import com.google.zxing.client.result.ResultParser;
|
|
import com.google.zxing.DecodeHintType;
|
|
import com.google.zxing.BarcodeFormat;
|
|
import com.google.zxing.BinaryBitmap;
|
|
import com.google.zxing.MultiFormatReader;
|
|
import com.google.zxing.MultiFormatWriter;
|
|
import com.google.zxing.Result;
|
|
import com.google.zxing.oned.ITFWriter;
|
|
import com.google.zxing.oned.UPCAWriter;
|
|
import com.google.zxing.oned.Code128Writer;
|
|
import com.google.zxing.oned.Code39Writer;
|
|
import com.google.zxing.common.HybridBinarizer;
|
|
// for testing only
|
|
import flash.display.Loader;
|
|
import flash.events.Event;
|
|
import flash.net.URLRequest;
|
|
private var tc:testClass = new testClass();
|
|
private var pictLdr:Loader;
|
|
private const NEWLINE:String = '\n';
|
|
private var currentTestItem:Array = null;
|
|
// ------------------
|
|
private var fileRef:FileReference;
|
|
private var myReader:MultiFormatReader;
|
|
private var hints:HashTable;
|
|
private var myWriter:Writer;
|
|
|
|
|
|
|
|
public function init():void
|
|
{
|
|
// initialise the generic reader
|
|
myReader = new MultiFormatReader();
|
|
}
|
|
|
|
public function decodeImage(appendOutput:Boolean=false):void
|
|
{
|
|
// did the user select an image?
|
|
if (this.img.source != "")
|
|
{
|
|
// show a message we are decoding
|
|
|
|
if (!appendOutput) { this.resulttextarea.text = "*** decoding ***"; }
|
|
try
|
|
{
|
|
// is the image valid?
|
|
if (img.content != null)
|
|
{
|
|
// try to decode the image
|
|
this.decodeBitmapData((img.content as Bitmap).bitmapData,img.contentWidth, img.contentHeight,appendOutput);
|
|
}
|
|
else
|
|
{
|
|
// invalid image
|
|
if (!appendOutput)
|
|
{
|
|
this.resulttextarea.text = "no image selected";
|
|
}
|
|
else
|
|
{
|
|
this.resulttextarea.text += "no image selected";
|
|
}
|
|
}
|
|
}
|
|
catch(E:*)
|
|
{
|
|
this.resulttextarea.text = "*** Error in image data***"+E.message;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function decodeBitmapData(bmpd:BitmapData, width:int, height:int, appendOutput:Boolean=false):void
|
|
{
|
|
// create the container to store the image width and height in
|
|
var lsource:BufferedImageLuminanceSource = new BufferedImageLuminanceSource(bmpd);
|
|
// convert it to a binary bitmap
|
|
var bitmap:BinaryBitmap = new BinaryBitmap(new HybridBinarizer(lsource));
|
|
// get all the hints
|
|
var ht:HashTable = null;
|
|
ht = this.getAllHints()
|
|
var res:Result = null;
|
|
try
|
|
{
|
|
// try to decode the image
|
|
res = myReader.decode(bitmap,ht);
|
|
}
|
|
catch(e:*)
|
|
{
|
|
// failed
|
|
if (!appendOutput)
|
|
{
|
|
this.resulttextarea.text = e.message;
|
|
}
|
|
else
|
|
{
|
|
this.resulttextarea.text += e.message;
|
|
}
|
|
}
|
|
|
|
// did we find something?
|
|
if (res == null)
|
|
{
|
|
if (!appendOutput)
|
|
{
|
|
// no : we could not detect a valid barcode in the image
|
|
this.resulttextarea.text = "<<No decoder could read the barcode>>";
|
|
if (!this.bc_tryharder.selected)
|
|
{
|
|
this.resulttextarea.text = "<<No decoder could read the barcode : Retry with the 'Try Harder' setting>>";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
this.resulttextarea.text += "Could not decode image";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// yes : parse the result
|
|
var parsedResult:ParsedResult = ResultParser.parseResult(res);
|
|
// get a formatted string and display it in our textarea
|
|
if (!appendOutput)
|
|
{
|
|
this.resulttextarea.text = parsedResult.getDisplayResult();
|
|
}
|
|
else
|
|
{
|
|
this.resulttextarea.text += parsedResult.getDisplayResult();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public function getAllHints():HashTable
|
|
{
|
|
// get all hints from the user
|
|
var ht:HashTable = new HashTable();
|
|
if (this.bc_QR_CODE.selected) { ht.Add(DecodeHintType.POSSIBLE_FORMATS,BarcodeFormat.QR_CODE); }
|
|
if (this.bc_DATAMATRIX.selected) { ht.Add(DecodeHintType.POSSIBLE_FORMATS,BarcodeFormat.DATAMATRIX); }
|
|
if (this.bc_UPC_E.selected) { ht.Add(DecodeHintType.POSSIBLE_FORMATS,BarcodeFormat.UPC_E); }
|
|
if (this.bc_UPC_A.selected) { ht.Add(DecodeHintType.POSSIBLE_FORMATS,BarcodeFormat.UPC_A); }
|
|
if (this.bc_EAN_8.selected) { ht.Add(DecodeHintType.POSSIBLE_FORMATS,BarcodeFormat.EAN_8); }
|
|
if (this.bc_EAN_13.selected) { ht.Add(DecodeHintType.POSSIBLE_FORMATS,BarcodeFormat.EAN_13); }
|
|
if (this.bc_CODE_39.selected) { ht.Add(DecodeHintType.POSSIBLE_FORMATS,BarcodeFormat.CODE_39);}
|
|
if (this.bc_CODE_93.selected) { ht.Add(DecodeHintType.POSSIBLE_FORMATS,BarcodeFormat.CODE_93);}
|
|
if (this.bc_CODE_128.selected) { ht.Add(DecodeHintType.POSSIBLE_FORMATS,BarcodeFormat.CODE_128); }
|
|
if (this.bc_PDF417.selected) { ht.Add(DecodeHintType.POSSIBLE_FORMATS,BarcodeFormat.PDF417); }
|
|
if (this.bc_ITF.selected) { ht.Add(DecodeHintType.POSSIBLE_FORMATS,BarcodeFormat.ITF); }
|
|
if (this.bc_AZTEC.selected) { ht.Add(DecodeHintType.POSSIBLE_FORMATS,BarcodeFormat.AZTEC); }
|
|
if (this.bc_RSS14.selected) { ht.Add(DecodeHintType.POSSIBLE_FORMATS,BarcodeFormat.RSS_14); }
|
|
if (this.bc_RSS14Expanded.selected){ ht.Add(DecodeHintType.POSSIBLE_FORMATS,BarcodeFormat.RSS_EXPANDED); }
|
|
if (this.bc_CODABAR.selected) { ht.Add(DecodeHintType.POSSIBLE_FORMATS,BarcodeFormat.CODABAR); }
|
|
if (this.bc_tryharder.selected) { ht.Add(DecodeHintType.TRY_HARDER,true); }
|
|
return ht;
|
|
}
|
|
|
|
|
|
public function clearImage():void
|
|
{
|
|
// remove the image
|
|
this.img.source = null;
|
|
this.resulttextarea.text = "";
|
|
}
|
|
|
|
public function selectFile():void
|
|
{
|
|
// file open menu
|
|
fileRef = new FileReference();
|
|
var fileFilter4:FileFilter = new FileFilter('JPG','*.jpg');
|
|
var fileFilter1:FileFilter = new FileFilter('PNG','*.png');
|
|
var fileFilter2:FileFilter = new FileFilter('GIF','*.gif');
|
|
var fileFilter3:FileFilter = new FileFilter('All files','*.*');
|
|
fileRef.addEventListener(Event.SELECT, selectHandler);
|
|
fileRef.addEventListener(Event.COMPLETE,completeHandler);
|
|
fileRef.browse([fileFilter4,
|
|
fileFilter1,
|
|
fileFilter2,
|
|
fileFilter3]);
|
|
}
|
|
|
|
public function selectHandler(event:Event):void
|
|
{
|
|
// try to load the image
|
|
fileRef.removeEventListener(Event.SELECT, selectHandler);
|
|
fileRef.load();
|
|
}
|
|
|
|
public function completeHandler(event:Event):void
|
|
{
|
|
// image loaded succesfully
|
|
fileRef.removeEventListener(Event.COMPLETE,completeHandler);
|
|
img.load(fileRef.data);
|
|
}
|
|
|
|
public function selectNone():void
|
|
{
|
|
// remove all hints (except the try harder hint)
|
|
this.bc_QR_CODE.selected = false;
|
|
this.bc_DATAMATRIX.selected = false;
|
|
this.bc_UPC_E.selected = false;
|
|
this.bc_UPC_A.selected = false;
|
|
this.bc_EAN_8.selected = false;
|
|
this.bc_EAN_13.selected = false;
|
|
this.bc_CODE_128.selected = false;
|
|
this.bc_CODE_39.selected = false;
|
|
this.bc_CODE_93.selected = false;
|
|
this.bc_ITF.selected = false;
|
|
this.bc_AZTEC.selected = false;
|
|
this.bc_RSS14.selected = false;
|
|
this.bc_RSS14Expanded.selected = false;
|
|
this.bc_CODABAR.selected = false;
|
|
this.bc_PDF417.selected = false;
|
|
this.bc_tryharder.selected = false;
|
|
}
|
|
|
|
public function selectAll():void
|
|
{
|
|
// check all hints
|
|
this.bc_QR_CODE.selected = true;
|
|
this.bc_DATAMATRIX.selected = true;
|
|
this.bc_UPC_E.selected = true;
|
|
this.bc_UPC_A.selected = true;
|
|
this.bc_EAN_8.selected = true;
|
|
this.bc_EAN_13.selected = true;
|
|
this.bc_CODE_128.selected = true;
|
|
this.bc_CODE_39.selected = true;
|
|
this.bc_CODE_93.selected = true;
|
|
this.bc_ITF.selected = true;
|
|
this.bc_AZTEC.selected = true;
|
|
this.bc_RSS14.selected = true;
|
|
this.bc_RSS14Expanded.selected = true;
|
|
this.bc_CODABAR.selected = true;
|
|
this.bc_tryharder.selected = true;
|
|
this.bc_PDF417.selected = true;
|
|
}
|
|
|
|
private function videoDisplay_creationComplete():void
|
|
{
|
|
// try to attach the webcam to the videoDisplay
|
|
var camera:Camera = Camera.getCamera();
|
|
if (camera)
|
|
{
|
|
if ((camera.width == -1) && ( camera.height == -1))
|
|
{
|
|
// no webcam seems to be attached -> hide videoDisplay
|
|
videoDisplay.width = 0;
|
|
videoDisplay.height = 0;
|
|
this.videoDisplayLabel.visible = true;
|
|
this.buttonBox.visible = false;
|
|
}
|
|
else
|
|
{
|
|
// webcam detected
|
|
|
|
// change the default mode of the webcam
|
|
camera.setMode(350,350,5,true);
|
|
videoDisplay.width = camera.width;
|
|
videoDisplay.height = camera.height;
|
|
this.videoDisplayLabel.visible = false;
|
|
this.buttonBox.visible = true;
|
|
|
|
videoDisplay.attachCamera(camera);
|
|
}
|
|
} else {
|
|
Alert.show("You don't seem to have a webcam.");
|
|
}
|
|
}
|
|
|
|
private function decodeSnapshot():void
|
|
{
|
|
// try to decode the current snapshpt
|
|
var bmd:BitmapData = new BitmapData(this.videoDisplay.width, this.videoDisplay.height);
|
|
bmd.draw(videoDisplay);
|
|
this.decodeBitmapData(bmd, this.videoDisplay.width, this.videoDisplay.height);
|
|
|
|
}
|
|
|
|
public function generate(barcodeType:BarcodeFormat,image:Object,contents:String,checkFunction:Function=null):void
|
|
{
|
|
|
|
if (barcodeType == BarcodeFormat.PDF417)
|
|
{
|
|
Alert.show("The output is equal to the core Java code but it is not decoding correctly");
|
|
}
|
|
|
|
if (checkFunction != null)
|
|
{
|
|
contents = checkFunction(contents);
|
|
if (contents == null) { return;} // input check failed
|
|
}
|
|
myWriter = new MultiFormatWriter()
|
|
|
|
try
|
|
{
|
|
// try to generate the data for the barcode
|
|
var result:BitMatrix = myWriter.encode(contents,barcodeType,image.width-100,image.height-100) as BitMatrix;
|
|
}
|
|
catch (e:Error)
|
|
{
|
|
Alert.show("An error occured when making the barcode :"+e.message);
|
|
return;
|
|
}
|
|
|
|
var resultBits:Array = new Array(result.getWidth());
|
|
for (var i:int=0;i<result.getWidth();i++)
|
|
{
|
|
resultBits[i] = result._get(i,0);
|
|
}
|
|
|
|
// map the result onto the image
|
|
var bmpd:BitmapData = new BitmapData(image.width, image.height, false, 0x009900);
|
|
var bmp:Bitmap = new Bitmap(bmpd);
|
|
var whitepixel:Boolean;
|
|
|
|
for (var h:int=0;h<bmpd.height;h++)
|
|
{
|
|
for (var w:int=0;w<bmpd.width;w++)
|
|
{
|
|
|
|
if ((w<50||w >= (image.width-50)) || ((h<50) || (h>=(image.height-50))))
|
|
{
|
|
// make a 50px border around each barcode
|
|
bmpd.setPixel(w,h, 0xFFFFFF);
|
|
}
|
|
else
|
|
{
|
|
if ((barcodeType == BarcodeFormat.PDF417) ||
|
|
(barcodeType == BarcodeFormat.QR_CODE))
|
|
{
|
|
whitepixel = (result._get(w-50,h-50) == 0);
|
|
}
|
|
else
|
|
{
|
|
whitepixel = (resultBits[w-50] == 0);
|
|
}
|
|
|
|
if (whitepixel)
|
|
{
|
|
// white pixel
|
|
bmpd.setPixel(w,h, 0xFFFFFF);
|
|
}
|
|
else
|
|
{
|
|
// black pixel
|
|
bmpd.setPixel(w,h, 0x000000);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
image.source = bmp;
|
|
}
|
|
|
|
|
|
public function debugCreateRun():void
|
|
{
|
|
/*
|
|
var hints:HashTable = new HashTable();
|
|
hints.Add(DecodeHintType.POSSIBLE_FORMATS,BarcodeFormat.QR_CODE);
|
|
|
|
hints.Add(DecodeHintType.POSSIBLE_FORMATS,BarcodeFormat.UPC_A);
|
|
hints.Add(DecodeHintType.POSSIBLE_FORMATS,BarcodeFormat.EAN_8);
|
|
hints.Add(DecodeHintType.POSSIBLE_FORMATS,BarcodeFormat.EAN_13);
|
|
hints.Add(DecodeHintType.POSSIBLE_FORMATS,BarcodeFormat.CODE_39);
|
|
hints.Add(DecodeHintType.POSSIBLE_FORMATS,BarcodeFormat.CODE_128);
|
|
hints.Add(DecodeHintType.POSSIBLE_FORMATS,BarcodeFormat.PDF417);
|
|
hints.Add(DecodeHintType.POSSIBLE_FORMATS,BarcodeFormat.ITF);
|
|
|
|
|
|
this.tc.generateTestFiles(hints);
|
|
this.tc.currentTestItem = 0;
|
|
this.resulttextarea.text = "";
|
|
while (this.tc.currentTestItem<this.tc.testFilesCreate.length)
|
|
{
|
|
this.currentTestItem = this.tc.getNext(hints,true);
|
|
if (this.currentTestItem!=null)
|
|
{
|
|
var result:String = tryCreate(this.currentTestItem);
|
|
if (result == this.currentTestItem[1])
|
|
{
|
|
// OK
|
|
this.resulttextarea.text = this.resulttextarea.text + (this.currentTestItem[0]).toString()+ ":"+this.currentTestItem[2]+" : OK" + this.NEWLINE;
|
|
}
|
|
else
|
|
{ // failed
|
|
this.resulttextarea.text = this.resulttextarea.text + (this.currentTestItem[0]).toString()+ ":"+this.currentTestItem[2]+" : FAILED : found "+result+" instead of :" +this.currentTestItem[1] + this.NEWLINE;
|
|
}
|
|
}
|
|
// try next
|
|
this.tc.currentTestItem++;
|
|
}
|
|
*/
|
|
}
|
|
|
|
public function tryCreate(item:Object):String
|
|
{
|
|
|
|
// generate this barcode
|
|
this.generate(item[1] as BarcodeFormat,
|
|
this.img,
|
|
item[0] as String,
|
|
null);
|
|
|
|
// now try to decode this image
|
|
var ht:HashTable = new HashTable();
|
|
ht.Add(DecodeHintType.POSSIBLE_FORMATS,item[1] as BarcodeFormat);
|
|
var res:Result;
|
|
try
|
|
{
|
|
// try to decode the image we just created
|
|
var lsource:BufferedImageLuminanceSource = new BufferedImageLuminanceSource((img.content as Bitmap).bitmapData);
|
|
var bitmap:BinaryBitmap = new BinaryBitmap(new HybridBinarizer(lsource));
|
|
res = myReader.decode(bitmap,ht);
|
|
}
|
|
catch(e:*)
|
|
{
|
|
// failed
|
|
return "";
|
|
}
|
|
|
|
return res.getText();
|
|
}
|
|
|
|
public function debugRun():void
|
|
{
|
|
this.tc.currentTestItem = 0;
|
|
this.resulttextarea.text = "";
|
|
this.hints = getAllHints();
|
|
loadNextItem();
|
|
|
|
}
|
|
|
|
public function checkUPCA(text:String):String
|
|
{
|
|
while (text.length < 11) { text = "0" + text; }
|
|
if (text.length != 11)
|
|
{
|
|
Alert.show('UPC_A should be 11 digits');
|
|
return null;
|
|
}
|
|
|
|
var checksum_value:Number= 3*( (text.charCodeAt(1) - 48) +
|
|
(text.charCodeAt(3) - 48) +
|
|
(text.charCodeAt(5) - 48) +
|
|
(text.charCodeAt(7) - 48) +
|
|
(text.charCodeAt(9) - 48)) +
|
|
(text.charCodeAt(0) - 48) + (text.charCodeAt(2) - 48) +
|
|
(text.charCodeAt(4) - 48) + (text.charCodeAt(6) - 48) +
|
|
(text.charCodeAt(8) - 48) + (text.charCodeAt(10) - 48);
|
|
var checksum_digit:int = 10 - (checksum_value % 10);
|
|
if (checksum_digit == 10) { checksum_digit = 0; }
|
|
return text+checksum_digit.valueOf();
|
|
|
|
|
|
}
|
|
|
|
public function checkEAN13(text:String):String
|
|
{
|
|
if (text.length > 12)
|
|
{
|
|
Alert.show('EAN12 can only contain 13 digits (13th digit is checksum digit)');
|
|
return null;
|
|
}
|
|
var counter:int=0;
|
|
while(counter < text.length)
|
|
{
|
|
|
|
var cc:int = text.charCodeAt(counter);
|
|
if ((cc<0x30) || (cc>0x39))
|
|
{
|
|
Alert.show("EAN barcodes can only contain digits");
|
|
return null;
|
|
}
|
|
counter++;
|
|
}
|
|
while (text.length < 12) { text = "0" + text; }
|
|
// calculate checksum
|
|
var checksum_value:Number= 3*( (text.charCodeAt(1) - 48) +
|
|
(text.charCodeAt(3) - 48) +
|
|
(text.charCodeAt(5) - 48) +
|
|
(text.charCodeAt(7) - 48) +
|
|
(text.charCodeAt(9) - 48) +
|
|
(text.charCodeAt(11) - 48)) +
|
|
(text.charCodeAt(0) - 48) + (text.charCodeAt(2) - 48) +
|
|
(text.charCodeAt(4) - 48) + (text.charCodeAt(6) - 48) +
|
|
(text.charCodeAt(8) - 48) + (text.charCodeAt(10) - 48);
|
|
var checksum_digit:int = 10 - (checksum_value % 10);
|
|
if (checksum_digit == 10) { checksum_digit = 0; }
|
|
return text+checksum_digit.valueOf();
|
|
}
|
|
|
|
public function checkEAN8(text:String):String
|
|
{
|
|
if (text.length > 7)
|
|
{
|
|
Alert.show('EAN8 can only contain 7 digits (8th digit is used for checksum');
|
|
return null;
|
|
}
|
|
var counter:int=0;
|
|
while(counter < text.length)
|
|
{
|
|
|
|
var cc:int = text.charCodeAt(counter);
|
|
if ((cc<0x30) || (cc>0x39))
|
|
{
|
|
Alert.show("EAN barcodes can only contain digits");
|
|
return null;
|
|
}
|
|
counter++;
|
|
}
|
|
while (text.length < 7) { text = "0" + text; }
|
|
// calculate the checksum
|
|
var checksum_value:int = (text.charCodeAt(1) - 48) + (text.charCodeAt(3) - 48) + (text.charCodeAt(5) - 48) +
|
|
3 * ((text.charCodeAt(0) - 48) + (text.charCodeAt(2) - 48) + (text.charCodeAt(4) - 48) + (text.charCodeAt(6) - 48));
|
|
var checksum_digit:int = 10 - (checksum_value % 10);
|
|
if (checksum_digit == 10) { checksum_digit = 0; }
|
|
return text+checksum_digit.valueOf();
|
|
}
|
|
|
|
public function loadNextItem():void
|
|
{
|
|
while (this.tc.currentTestItem<this.tc.testFiles.length)
|
|
{
|
|
this.currentTestItem = this.tc.getNext(this.hints);
|
|
if (this.currentTestItem!=null)
|
|
{
|
|
if (this.currentTestItem[3]) // decode this entry (or skip)
|
|
{
|
|
loadAndDecode();
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
this.resulttextarea.text = this.resulttextarea.text + "IGNORED : " + this.currentTestItem[0] + this.NEWLINE;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// no more items ?
|
|
}
|
|
// try next
|
|
this.tc.currentTestItem++;
|
|
}
|
|
this.resulttextarea.text = this.resulttextarea.text + "*** FINISHED TEST RUN ***";
|
|
}
|
|
|
|
public function loadAndDecode():void
|
|
{
|
|
|
|
// load image
|
|
var imgsource:String = this.tc.baseDir + this.currentTestItem[0];
|
|
img.load(new URLRequest( imgsource ));
|
|
pictLdr = new Loader();
|
|
var pictURLReq:URLRequest = new URLRequest(imgsource);
|
|
pictLdr.load(pictURLReq);
|
|
pictLdr.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaded);
|
|
}
|
|
|
|
public function imgLoaded(event:Event):void
|
|
{
|
|
|
|
this.removeEventListener(Event.COMPLETE, imgLoaded);
|
|
this.img.data = Bitmap(pictLdr.content);
|
|
this.validateNow();
|
|
var lsource:BufferedImageLuminanceSource = new BufferedImageLuminanceSource((img.content as Bitmap).bitmapData);
|
|
var bitmap:BinaryBitmap = new BinaryBitmap(new HybridBinarizer(lsource));
|
|
try
|
|
{
|
|
var res:Result = myReader.decode(bitmap,this.hints);
|
|
}
|
|
catch (e:Error)
|
|
{
|
|
// something went wrong during decoding
|
|
res = null;
|
|
}
|
|
var source:String = this.currentTestItem[0];
|
|
var result:String = this.currentTestItem[2];
|
|
if (res == null)
|
|
{
|
|
this.resulttextarea.text = this.resulttextarea.text + "FAILED \t\t\t" + source + this.NEWLINE;
|
|
}
|
|
else
|
|
{
|
|
if (res.getText() == result)
|
|
{
|
|
this.resulttextarea.text = this.resulttextarea.text + "OK \t\t\t" + source + this.NEWLINE;
|
|
}
|
|
else
|
|
{
|
|
this.resulttextarea.text = this.resulttextarea.text + "MISMATCH \t\t\t " + source + " decoded : " + res.getText() + " expected : " + result + this.NEWLINE;
|
|
}
|
|
}
|
|
|
|
// next
|
|
this.tc.currentTestItem++;
|
|
loadNextItem();
|
|
|
|
}
|
|
|
|
|
|
]]>
|
|
</mx:Script>
|
|
<mx:VBox creationComplete="selectAll()">
|
|
<mx:Label text="Example for reading and creating barcodes with the ZXing library by Bas Vijfwinkel" fontWeight="bold" />
|
|
<mx:TabNavigator height="900" width="900">
|
|
<mx:HBox label="Read a barcode">
|
|
<mx:VBox width="450">
|
|
<mx:HBox>
|
|
<mx:Button label="Select image" click="selectFile()" x="10" y="318"/>
|
|
<mx:Button label="Decode!" click="decodeImage()" x="117" y="318"/>
|
|
<mx:Button label="Clear" click="clearImage()" x="198" y="318"/>
|
|
<mx:Button label="TestAll" click="debugRun()" x="250" y="318"/>
|
|
<!-- <mx:Button label="TestCreateAll" click="debugCreateRun()" x="250" y="368"/> -->
|
|
</mx:HBox>
|
|
<mx:Canvas width="300" height="300" backgroundColor="white">
|
|
<mx:Image id="img" width="300" height="300" />
|
|
</mx:Canvas>
|
|
<mx:Label text="Result :"/>
|
|
<mx:TextArea id="resulttextarea" width="300" height="300" />
|
|
</mx:VBox>
|
|
<mx:VBox>
|
|
<mx:VideoDisplay id="videoDisplay"
|
|
creationComplete="videoDisplay_creationComplete();"
|
|
width="300"
|
|
height="300" />
|
|
<mx:Label id="videoDisplayLabel" text="Camera not attached" visible="false"/>
|
|
<mx:HBox id="buttonBox" visible="true">
|
|
<mx:Button id="button"
|
|
label="Reload Camera"
|
|
click="videoDisplay_creationComplete();" />
|
|
<mx:Button id="snapshot"
|
|
label = "Decode Snapshot"
|
|
click ="decodeSnapshot();"
|
|
/>
|
|
</mx:HBox>
|
|
<mx:HBox x="320" y="10">
|
|
<mx:VBox>
|
|
<mx:CheckBox id="bc_QR_CODE" label="QR_CODE" />
|
|
<mx:CheckBox id="bc_DATAMATRIX" label="DATAMATRIX" />
|
|
<mx:CheckBox id="bc_UPC_E" label="UPC_E" />
|
|
<mx:CheckBox id="bc_UPC_A" label="UPC_A" />
|
|
<mx:CheckBox id="bc_EAN_8" label="EAN_8" />
|
|
<mx:CheckBox id="bc_EAN_13" label="EAN_13" />
|
|
<mx:CheckBox id="bc_CODABAR" label="CODABAR" />
|
|
<mx:CheckBox id="bc_ITF" label="ITF" />
|
|
<mx:Button label="Select All" click="selectAll()" />
|
|
</mx:VBox>
|
|
<mx:VBox>
|
|
<mx:CheckBox id="bc_CODE_128" label="CODE_128" />
|
|
<mx:CheckBox id="bc_CODE_39" label="CODE_39" />
|
|
<mx:CheckBox id="bc_CODE_93" label="CODE_93" />
|
|
<mx:CheckBox id="bc_PDF417" label="PDF417" />
|
|
<mx:CheckBox id="bc_AZTEC" label="AZTEC" />
|
|
<mx:CheckBox id="bc_RSS14" label="RSS14" />
|
|
<mx:CheckBox id="bc_RSS14Expanded" label="RSS14Expanded" />
|
|
<mx:CheckBox id="bc_tryharder" label="Try Harder" />
|
|
<mx:Button label="Select None" click="selectNone()" />
|
|
</mx:VBox>
|
|
</mx:HBox>
|
|
</mx:VBox>
|
|
</mx:HBox>
|
|
<mx:TabNavigator label="Create a barcode">
|
|
<mx:VBox label="EAN8">
|
|
<mx:Label text="Generate an EAN8 barcode"/>
|
|
<mx:TextInput id="EAN8INput" text="type the digits here"/>
|
|
<mx:Button label="Generate" buttonDown="{this.generate(BarcodeFormat.EAN_8,this.EAN8Image,this.EAN8INput.text,this.checkEAN8);}"/>
|
|
<mx:Image id="EAN8Image" width="600" height="600" />
|
|
</mx:VBox>
|
|
<mx:VBox label="EAN13">
|
|
<mx:Label text="Generate an EAN13 barcode"/>
|
|
<mx:TextInput id="EAN13INput" text="type the digits here"/>
|
|
<mx:Button label="Generate" buttonDown="{this.generate(BarcodeFormat.EAN_13,this.EAN13Image,this.EAN13INput.text,this.checkEAN13);}"/>
|
|
<mx:Image id="EAN13Image" width="600" height="600" />
|
|
</mx:VBox>
|
|
<mx:VBox label="QRCode">
|
|
<mx:Label text="Generate a QRCode"/>
|
|
<mx:TextInput id="QRINput" text="type text here"/>
|
|
<mx:Button label="Generate" buttonDown="{this.generate(BarcodeFormat.QR_CODE,this.QRImage,this.QRINput.text,null);}"/>
|
|
<mx:Image id="QRImage" width="600" height="600" />
|
|
</mx:VBox>
|
|
<mx:VBox label="UPC_A">
|
|
<mx:Label text="Generate an UPC_A barcode"/>
|
|
<mx:TextInput id="UPCAINput" text="type text here"/>
|
|
<mx:Button label="Generate" buttonDown="{this.generate(BarcodeFormat.UPC_A,this.UPCAImage,this.UPCAINput.text,this.checkUPCA);}"/>
|
|
<mx:Image id="UPCAImage" width="600" height="600" />
|
|
</mx:VBox>
|
|
<mx:VBox label="CODE39">
|
|
<mx:Label text="Generate a CODE39 barcode"/>
|
|
<mx:TextInput id="CODE39INput" text="type text here"/>
|
|
<mx:Button label="Generate" buttonDown="{this.generate(BarcodeFormat.CODE_39,this.CODE39Image,this.CODE39INput.text,null);}"/>
|
|
<mx:Image id="CODE39Image" width="600" height="600" />
|
|
</mx:VBox>
|
|
<mx:VBox label="CODE128">
|
|
<mx:Label text="Generate a CODE128 barcode"/>
|
|
<mx:TextInput id="CODE128INput" text="type text here"/>
|
|
<mx:Button label="Generate" buttonDown="{this.generate(BarcodeFormat.CODE_128,this.CODE128Image,this.CODE128INput.text,null);}"/>
|
|
<mx:Image id="CODE128Image" width="600" height="600" />
|
|
</mx:VBox>
|
|
<mx:VBox label="ITF">
|
|
<mx:Label text="Generate an ITF barcode"/>
|
|
<mx:TextInput id="ITFINput" text="type text here"/>
|
|
<mx:Button label="Generate" buttonDown="{this.generate(BarcodeFormat.ITF,this.ITFImage,this.ITFINput.text,null);}"/>
|
|
<mx:Image id="ITFImage" width="600" height="600" />
|
|
</mx:VBox>
|
|
<!-- <mx:VBox label="PDF417">
|
|
<mx:Label text="Generate a PDF417 barcode"/>
|
|
<mx:TextInput id="PDF417INput" text="type text here"/>
|
|
<mx:Button label="Generate" buttonDown="{this.generate(BarcodeFormat.PDF417,this.PDF417Image,this.PDF417INput.text,null);}"/>
|
|
<mx:Image id="PDF417Image" width="600" height="600" />
|
|
</mx:VBox>-->
|
|
</mx:TabNavigator>
|
|
</mx:TabNavigator>
|
|
</mx:VBox>
|
|
</mx:Application>
|