forked from zxing/zxing
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
git-svn-id: https://zxing.googlecode.com/svn/trunk@1842 59b500cc-1b3d-0410-9834-0bbf25fbcc57
- Loading branch information
srowen
committed
Jun 29, 2011
1 parent
776e9b6
commit f6525fc
Showing
5 changed files
with
362 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
actionscript/core/test/com/google/zxing/EncodeDecodeEAN8Test.as
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
actionscript/core/test/com/google/zxing/IntegrationTestsSuite.as
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() { | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
actionscript/core/test/com/google/zxing/testrunner/ZXingTestsRunner.mxml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
* 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. | ||
--> | ||
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" | ||
xmlns:flexUnitUIRunner="http://www.adobe.com/2009/flexUnitUIRunner" | ||
creationComplete="runTests()" | ||
width="1024" height="768"> | ||
|
||
<mx:Script> | ||
<![CDATA[ | ||
import com.google.zxing.IntegrationTestsSuite; | ||
import org.flexunit.runner.FlexUnitCore; | ||
public function runTests():void { | ||
var core:FlexUnitCore = new FlexUnitCore(); | ||
core.addListener(uiListener); | ||
core.run(IntegrationTestsSuite); | ||
} | ||
]]> | ||
</mx:Script> | ||
<flexUnitUIRunner:TestRunnerBase id="uiListener" | ||
width="100%" height="100%" /> | ||
</mx:Application> |
Oops, something went wrong.