Created
December 18, 2015 03:21
-
-
Save velara3/e38fe3c486d1d3103bc3 to your computer and use it in GitHub Desktop.
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
package effects.supportClasses { | |
import com.flexcapacitor.effects.supportClasses.ActionEffectInstance; | |
import com.google.zxing.BarcodeFormat; | |
import com.google.zxing.MultiFormatWriter; | |
import com.google.zxing.common.ByteMatrix; | |
import com.google.zxing.common.flexdatatypes.HashTable; | |
import flash.display.BitmapData; | |
import flash.events.Event; | |
import effects.Encode; | |
/** | |
* @copy Encode | |
* */ | |
public class EncodeInstance extends ActionEffectInstance { | |
//-------------------------------------------------------------------------- | |
// | |
// Constructor | |
// | |
//-------------------------------------------------------------------------- | |
/** | |
* Constructor. | |
* */ | |
public function EncodeInstance(target:Object) { | |
super(target); | |
} | |
//-------------------------------------------------------------------------- | |
// | |
// Variables | |
// | |
//-------------------------------------------------------------------------- | |
//-------------------------------------------------------------------------- | |
// | |
// Properties | |
// | |
//-------------------------------------------------------------------------- | |
//-------------------------------------------------------------------------- | |
// | |
// Overridden methods | |
// | |
//-------------------------------------------------------------------------- | |
/** | |
* @private | |
* */ | |
override public function play():void { | |
super.play(); // Dispatch an effectStart event | |
var action:Encode = Encode(effect); | |
var multiFormatWriter:MultiFormatWriter; | |
var width:int = action.width; | |
var height:int = action.height; | |
var bitmapData:BitmapData = action.bitmapData; | |
var data:String; | |
var format:String = action.format; | |
var barcodeFormat:BarcodeFormat; | |
var result:Object; | |
var byteMatrix:ByteMatrix; | |
var transparentBackground:Boolean = action.transparent; | |
var fillColor:uint = action.fillColor; | |
/////////////////////////////////////////////////////////// | |
// Verify we have everything we need before going forward | |
/////////////////////////////////////////////////////////// | |
if (validate) { | |
if (width<0 || height<0) { | |
dispatchErrorEvent("Height and width cannot be set to zero"); | |
} | |
} | |
/////////////////////////////////////////////////////////// | |
// Continue with action | |
/////////////////////////////////////////////////////////// | |
// any images bound to this data throw errors | |
if (bitmapData!=null) { | |
//bitmapData.dispose(); | |
//bitmapData = null; | |
} | |
bitmapData = new BitmapData(width, height, transparentBackground, fillColor); | |
// get format | |
switch(format) { | |
case "QR_CODE": { | |
barcodeFormat = BarcodeFormat.QR_CODE; | |
break; | |
} | |
case "DATAMATRIX": { | |
barcodeFormat = BarcodeFormat.DATAMATRIX; | |
break; | |
} | |
case "UPC_E": { | |
barcodeFormat = BarcodeFormat.UPC_E; | |
break; | |
} | |
case "UPC_A": { | |
barcodeFormat = BarcodeFormat.UPC_A; | |
break; | |
} | |
case "EAN_8": { | |
barcodeFormat = BarcodeFormat.EAN_8; | |
break; | |
} | |
case "EAN_13": { | |
barcodeFormat = BarcodeFormat.EAN_13; | |
break; | |
} | |
case "CODE_128": { | |
barcodeFormat = BarcodeFormat.CODE_128; | |
break; | |
} | |
case "ITF": { | |
barcodeFormat = BarcodeFormat.ITF; | |
break; | |
} | |
case "PDF417": { | |
barcodeFormat = BarcodeFormat.PDF417; | |
break; | |
} | |
} | |
data = String(action.data); | |
multiFormatWriter = new MultiFormatWriter(); | |
// attempt encoding | |
try { | |
result = multiFormatWriter.encode(data, barcodeFormat, width, height, action.hints as HashTable); | |
} | |
catch (error:Error) { | |
// limit at 2953 ??? | |
action.errorMessage = error.message; | |
// handle invalid results | |
if (hasEventListener(Encode.INVALID)) { | |
dispatchEvent(new Event(Encode.INVALID)); | |
} | |
if (action.invalidEffect) { | |
action.invalidEffect.play(); | |
} | |
cancel(error.message); | |
return; | |
} | |
// QR_CODE, EAN_8 | |
if (format=="QR_CODE" || format=="EAN_8") { | |
byteMatrix = ByteMatrix(result); | |
// update bitmap data | |
for (var h:int=0; h < height; h++) { | |
for (var w:int=0; w < width; w++) { | |
if (byteMatrix._get(w, h) == 0) { | |
bitmapData.setPixel(w, h, 0); | |
} | |
else { | |
bitmapData.setPixel(w, h, 0xFFFFFF); | |
} | |
} | |
} | |
} | |
// says grey scale but the only values seem to be 0 and 255 | |
else if (format=="EAN_13") { | |
byteMatrix = ByteMatrix(result); | |
// update bitmap data | |
for (h=0; h < height; h++) { | |
for (w=0; w < width; w++) { | |
if (byteMatrix._get(w, h) == 0) { | |
bitmapData.setPixel(w, h, 0); | |
} | |
else { | |
bitmapData.setPixel(w, h, 0xFFFFFF); | |
} | |
} | |
} | |
} | |
action.bitmapData = null; | |
action.bitmapData = bitmapData; | |
// handle invalid results | |
if (hasEventListener(Encode.VALID)) { | |
dispatchEvent(new Event(Encode.VALID)); | |
} | |
if (action.validEffect) { | |
action.validEffect.play(); | |
} | |
/////////////////////////////////////////////////////////// | |
// Finish the effect | |
/////////////////////////////////////////////////////////// | |
finish(); | |
} | |
//-------------------------------------------------------------------------- | |
// | |
// Event handlers | |
// | |
//-------------------------------------------------------------------------- | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment