diff --git a/actionscript/core/build.xml b/actionscript/core/build.xml index f166e7d2a..cbff999bc 100644 --- a/actionscript/core/build.xml +++ b/actionscript/core/build.xml @@ -14,17 +14,21 @@ * See the License for the specific language governing permissions and * limitations under the License. --> - + - + - - + + + + + + + - @@ -32,6 +36,8 @@ + @@ -44,19 +50,46 @@ - + - - + - + + + + + + + + + + + + + + + + + + true + true + + + + + + + + diff --git a/actionscript/core/test/com/google/zxing/EncodeDecodeEAN8Test.as b/actionscript/core/test/com/google/zxing/EncodeDecodeEAN8Test.as new file mode 100644 index 000000000..8c6981769 --- /dev/null +++ b/actionscript/core/test/com/google/zxing/EncodeDecodeEAN8Test.as @@ -0,0 +1,180 @@ +/* + * Copyright 2011 ZXing authors + * + * 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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.zxing { + + import com.adobe.images.PNGEncoder; + import com.google.zxing.client.result.ParsedResult; + import com.google.zxing.client.result.ResultParser; + import com.google.zxing.common.ByteMatrix; + import com.google.zxing.common.GlobalHistogramBinarizer; + import com.google.zxing.common.flexdatatypes.HashTable; + + import flash.display.Bitmap; + import flash.display.BitmapData; + import flash.events.Event; + import flash.net.FileReference; + import flash.utils.ByteArray; + + import mx.core.BitmapAsset; + + import org.flexunit.asserts.assertEquals; + import org.flexunit.asserts.assertTrue; + import org.flexunit.asserts.fail; + + public class EncodeDecodeEAN8Test { + + [Embed(source="/blackbox/ean8-1/1.gif")] + private var Ean8_48512343:Class; + + [Embed(source="/data/ean8/ean8_48512343.png")] + private var ZXingGeneratedEan8_48512343:Class; + + private var imageWidth:int = 338; + private var imageHeight:int = 323; + + [Test(description="Read EAN8 and validate decoded number.")] + public function testDecodedEAN8ShouldBeEqualToEncodedNumber():void { + var ean8image:BitmapAsset = new Ean8_48512343() as BitmapAsset; + var decodedNumber:Number = decodeEAN8BarcodeImage(ean8image.bitmapData); + assertEquals(48512343, decodedNumber); + } + + [Test(async, + description="Ensure an encoded EAN8 image equals to expected.")] + public function testEncodedEAN8ImageShouldBeEqualToTarget():void { + var testCode:Number = 48512343; + var expectedEan8image:BitmapAsset = + new ZXingGeneratedEan8_48512343() as BitmapAsset; + var bitmap:Bitmap = generateEANBarcodeImage(testCode, + expectedEan8image.width, + expectedEan8image.height); + var diffObject:Object = + expectedEan8image.bitmapData.compare(bitmap.bitmapData); + assertTrue("diffObject should be int", + diffObject is int); + assertEquals(0, diffObject as int); + } + + [Test(description="Encode a number, decode it, and compare.")] + public function encodedEAN8shouldBeDecodedSuccessfully():void { + var testCode:Number = 48512343; + var decodedNumber:Number = encodeAndDecode(testCode); + assertEquals(testCode, decodedNumber); + } + + [Test(description="Bug: Endoding and decoding 1 fails")] + public function shouldEncodeAndDecodeAnyValidEAN8number():void { + var testCode:Number = 12345678; + var decodedNumber:Number = encodeAndDecode(testCode); + assertEquals(testCode, decodedNumber); + } + + private function encodeAndDecode(testCode:Number):Number { + var bitmap:Bitmap = generateEANBarcodeImage(testCode, imageWidth, + imageHeight); + var decodedNumber:Number = decodeEAN8BarcodeImage(bitmap.bitmapData); + return decodedNumber; + } + + public function saveBitmapToImage(bitmapData:BitmapData, fileName:String, + onFileSaveComplete:Function):void { + var imageBytes:ByteArray = PNGEncoder.encode(bitmapData); + var file:FileReference = new FileReference(); + file.addEventListener(Event.COMPLETE, onFileSaveComplete); + file.save(imageBytes, fileName); + } + + public function generateEANBarcodeImage(number:Number, imageWidth:Number, + imageHeight:Number):Bitmap { + var contents:String = formatEAN8String(number); + var resultBits:Array = encodeEAN8(contents); + var bitmap:Bitmap = generateImage(resultBits, imageWidth, imageHeight); + return bitmap; + } + + public function decodeEAN8BarcodeImage(imageBitmapData:BitmapData):Number { + var resultNumber:Number; + var source:BufferedImageLuminanceSource = + new BufferedImageLuminanceSource(imageBitmapData); + var luminance:BinaryBitmap = + new BinaryBitmap(new GlobalHistogramBinarizer(source)); + var hints:HashTable = new HashTable(); + hints.Add(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.EAN_8); + var reader:MultiFormatReader = new MultiFormatReader(); + var result:Result = reader.decode(luminance, hints); + if (result != null) { + var parsedResult:ParsedResult = ResultParser.parseResult(result); + var resultText:String = parsedResult.getDisplayResult(); + resultNumber = Number(resultText); + } + return resultNumber; + } + + private function generateImage(resultBits:Array, imageWidth:Number, + imageHeight:Number):Bitmap { + // generate an image + var bitmapData:BitmapData = new BitmapData(imageWidth, imageHeight, false, + 0xFFFFFF); + var bitmap:Bitmap = new Bitmap(bitmapData); + for (var h:int = 0; h < bitmapData.height; h++) { + var bmpdwidth:int = bitmapData.width; + var padding:int = bmpdwidth * 0.12; + var barsWidth:int = bmpdwidth - padding; + for (var w:int = 0; w < barsWidth; w++) { + if (resultBits[Math.round(w * (resultBits.length / barsWidth))] + == 0) { + bitmapData.setPixel(w + padding / 2, h, 0); + } + else { + bitmapData.setPixel(w + padding / 2, h, 0xFFFFFF); + } + } + } + return bitmap; + } + + private function encodeEAN8(contents:String):Array { + var barcodeType:BarcodeFormat = BarcodeFormat.EAN_8; + var myWriter:MultiFormatWriter = new MultiFormatWriter(); + try { + var result:ByteMatrix = myWriter.encode(contents, barcodeType) + as ByteMatrix; + } catch (e:Error) { + fail("An error occured when making the barcode: " + e); + } + var resultBits:Array = new Array(result.height()); + for (var i:int = 0; i < result.height(); i++) { + resultBits[i] = result._get(i, 0); + } + return resultBits; + } + + private function formatEAN8String(number:Number):String { + var inputlength:Number = 8; + var barCodeFormat:String = "EAN8"; + + var contents:String = String(number); + if (contents.length > inputlength) { + fail(barCodeFormat + ' can only contain ' + inputlength + ' digits'); + return null; + } + while (contents.length < inputlength) { + contents = "0" + contents; + } + return contents; + } + } +} diff --git a/actionscript/core/test/com/google/zxing/IntegrationTestsSuite.as b/actionscript/core/test/com/google/zxing/IntegrationTestsSuite.as new file mode 100644 index 000000000..b8da589bb --- /dev/null +++ b/actionscript/core/test/com/google/zxing/IntegrationTestsSuite.as @@ -0,0 +1,26 @@ +/* + * Copyright 2011 ZXing authors + * + * 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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.zxing { + + [Suite] + [RunWith("org.flexunit.runners.Suite")] + public class IntegrationTestsSuite { + public var encodeDecodeTest:EncodeDecodeEAN8Test; + + public function IntegrationTestsSuite() { + } + } +} diff --git a/actionscript/core/test/com/google/zxing/testrunner/ZXingTestsRunner.mxml b/actionscript/core/test/com/google/zxing/testrunner/ZXingTestsRunner.mxml new file mode 100644 index 000000000..faa070c7d --- /dev/null +++ b/actionscript/core/test/com/google/zxing/testrunner/ZXingTestsRunner.mxml @@ -0,0 +1,36 @@ + + + + + + + + + diff --git a/actionscript/readme.txt b/actionscript/readme.txt index 54c4c0ee2..312a51246 100644 --- a/actionscript/readme.txt +++ b/actionscript/readme.txt @@ -7,4 +7,81 @@ Many improvements can be made and not all patches and updates have been applied If you would like the help, feel free to join the zxing project and submit your improvements and bugfixes. If you have questions, please send them to the zxing newsgroup. -Bas Vijfwinkel \ No newline at end of file +Bas Vijfwinkel + + +Build Instructions +------------------ + +Building ZXing AS3 code requires: + +* Flex4 SDK + http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4 + +* FlexUnit + http://flexunit.org/releases/flexunit-4.1.0-8-4.1.0.16076.zip + +* AS3Corelib + https://github.com/downloads/mikechambers/as3corelib/as3corelib-.93.zip + +* Apache Ant + http://ant.apache.org/ + + +Setup +----- + +1. Please download and unzip Flex4 SDK. +2. Define FLEX_HOME environment variable with a full path to Flex4 SDK folder. + +On Mac or Linux: + +$ export FLEX_HOME=/specify-full-path-to/flex4_sdk + +on Win: + +$ set FLEX_HOME=/specify-full-path-to/flex4_sdk + +3. Download and unzip FlexUnit and AS3Corelib to core/libs folder. Make sure +that all swc and jar files are located directly under + +./zxing-read-only/actionscript/core/libs: + +$ ls -l libs +-rw-r----- 1 john smith 193727 Jun 28 14:18 as3corelib.swc +-rw-r--r-- 1 john smith 597386 Apr 13 16:57 flexUnitTasks-4.1.0-8.jar +-rw-r--r-- 1 john smith 192629 Apr 13 16:57 flexunit-4.1.0-8-as3_4.1.0.16076.swc +-rw-r--r-- 1 john smith 203196 Apr 13 16:57 flexunit-4.1.0-8-flex_4.1.0.16076.swc +-rw-r--r-- 1 john smith 2379 Apr 13 17:01 flexunit-aircilistener-4.1.0-8-4.1.0.16076.swc +-rw-r--r-- 1 john smith 11993 Apr 13 16:58 flexunit-cilistener-4.1.0-8-4.1.0.16076.swc +-rw-r--r-- 1 john smith 9686 Apr 13 16:58 flexunit-flexcoverlistener-4.1.0-8-4.1.0.16076.swc +-rw-r--r-- 1 john smith 227292 Apr 13 17:01 flexunit-uilistener-4.1.0-8-4.1.0.16076.swc +-rw-r--r-- 1 john smith 6332 Apr 13 16:58 fluint-extensions-4.1.0-8-4.1.0.16076.swc + +4. Download and unzip Apache Ant. Add ant tool to the PATH. + + +Building ZXing SWC +------------------ + +To build zxing.swc, run ant: + +$ cd zxing-read-only/actionscript/core +$ ant + +The build should create zxing-1.6.swc in actionscript/core/bin folder. + + +Building and Running ZXing Tests +-------------------------------- + +To build tests, execute: + +$ ant compile.tests + +To run the tests, open bin/output/ZXingTestsRunner.swf in a Flash Player or a +web browser. Here is an example of running the tests with the standalone +Flash Player debugger: + +$ flashplayerdebugger bin/output/ZXingTestsRunner.swf +