Hi Flex Coders
I want to share with you this flex component to generate barcodes.
At the moment it just generate type 128 barcodes. Maybe the next
version generate another barcode types...
Enjoy it :)
=====================================
Barcode.as
======================================
/**
* Class to generate barcodes
* At this version, just type 128 barcodes could be generated
*
* @author Antonio I. Aguilar J.
* @version 2006.01.25
*/
class Barcode extends mx.containers.Canvas {
// Variables
public var codeType:String="128";
public var showCode:Boolean=true;
// Methods
public function generate(code:String):Void {
if(code.length==0) throw new Error("Invalid code: "+code);
this.destroyAllChildren();
switch(this.codeType.toLowerCase()) {
case "128":
var C128:Array=new Array();
C128["0"]="11011001100";
C128["1"]="11001101100";
C128["2"]="11001100110";
C128["3"]="10010011000";
C128["4"]="10010001100";
C128["5"]="10001001100";
C128["6"]="10011001000";
C128["7"]="10011000100";
C128["8"]="10001100100";
C128["9"]="11001001000";
C128["10"]="11001000100";
C128["11"]="11000100100";
C128["12"]="10110011100";
C128["13"]="10011011100";
C128["14"]="10011001110";
C128["15"]="10111001100";
C128["16"]="10011101100";
C128["17"]="10011100110";
C128["18"]="11001110010";
C128["19"]="11001011100";
C128["20"]="11001001110";
C128["21"]="11011100100";
C128["22"]="11001110100";
C128["23"]="11101101110";
C128["24"]="11101001100";
C128["25"]="11100101100";
C128["26"]="11100100110";
C128["27"]="11101100100";
C128["28"]="11100110100";
C128["29"]="11100110010";
C128["30"]="11011011000";
C128["31"]="11011000110";
C128["32"]="11000110110";
C128["33"]="10100011000";
C128["34"]="10001011000";
C128["35"]="10001000110";
C128["36"]="10110001000";
C128["37"]="10001101000";
C128["38"]="10001100010";
C128["39"]="11010001000";
C128["40"]="11000101000";
C128["41"]="11000100010";
C128["42"]="10110111000";
C128["43"]="10110001110";
C128["44"]="10001101110";
C128["45"]="10111011000";
C128["46"]="10111000110";
C128["47"]="10001110110";
C128["48"]="11101110110";
C128["49"]="11010001110";
C128["50"]="11000101110";
C128["51"]="11011101000";
C128["52"]="11011100010";
C128["53"]="11011101110";
C128["54"]="11101011000";
C128["55"]="11101000110";
C128["56"]="11100010110";
C128["57"]="11101101000";
C128["58"]="11101100010";
C128["59"]="11100011010";
C128["60"]="11101111010";
C128["61"]="11001000010";
C128["62"]="11110001010";
C128["63"]="10100110000";
C128["64"]="10100001100";
C128["65"]="10010110000";
C128["66"]="10010000110";
C128["67"]="10000101100";
C128["68"]="10000100110";
C128["69"]="10110010000";
C128["70"]="10110000100";
C128["71"]="10011010000";
C128["72"]="10011000010";
C128["73"]="10000110100";
C128["74"]="10000110010";
C128["75"]="11000010010";
C128["76"]="11001010000";
C128["77"]="11110111010";
C128["78"]="11000010100";
C128["79"]="10001111010";
C128["80"]="10100111100";
C128["81"]="10010111100";
C128["82"]="10010011110";
C128["83"]="10111100100";
C128["84"]="10011110100";
C128["85"]="10011110010";
C128["86"]="11110100100";
C128["87"]="11110010100";
C128["88"]="11110010010";
C128["89"]="11011011110";
C128["90"]="11011110110";
C128["91"]="11110110110";
C128["92"]="10101111000";
C128["93"]="10100011110";
C128["94"]="10001011110";
C128["95"]="10111101000";
C128["96"]="10111100010";
C128["97"]="11110101000";
C128["98"]="11110100010";
C128["99"]="10111011110";
C128["100"]="10111101110";
C128["101"]="11101011110";
C128["102"]="11110101110";
C128["a"]="11101011110";
C128["b"]="10111101110";
C128["c"]="10111011110";
C128["A"]="11010000100";
C128["B"]="11010010000";
C128["C"]="11010011100";
C128["S"]="1100011101011";
var margin:Number=5;
// Calculate checksum
var codeLen:Number=code.length;
var codeString:String=C128["B"]; // Start
var checksum:Number=104;
var j:Number=1;
for(var i:Number=0;i<codeLen;i++) {
var tmp:Number=ord(code.charAt(i))-32;
checksum+=(j++*tmp);
codeString+=C128[tmp.toString()];
} // for
checksum%=103;
codeString+=C128[checksum.toString()];
codeString+=C128["S"]; // End
// Draw text
if(this.showCode==true) {
var txtFmt:TextFormat=new TextFormat();
txtFmt.font=this.getStyle("fontFamily");
var
labelMetrics:Object=txtFmt.getTextExtent(code);
var initObj:Object=new Object();
initObj.x=Math.floor(this.width/2)-Math.floor(labelMetrics.textFieldWidth/2);
initObj.y=this.height-(this.getStyle("fontSize")+margin);
initObj.text=code;
this.createChild(mx.controls.Label,"",initObj);
} // if
// Draw bars
var
barHeight:Number=(this.showCode==true)?(this.height-(this.getStyle("fontSize")+(margin*2))):(this.height-margin);
var barWidth:Number=1;
var x:Number=margin;
var y:Number=margin;
var codeStringLen:Number=codeString.length;
var
barsCanvas:mx.core.UIObject=this.createEmptyObject("",0);
for(var i:Number=0;i<codeStringLen;i++) {
var digit:String=codeString.substr(i,1);
if(digit=="1")
barsCanvas.fillRect(x+(i*barWidth),y,x+i+barWidth,barHeight,this.getStyle("color"),100);
} // for
break;
default:
throw new Error("Unsupported barcode type:
"+this.codeType);
break;
} // switch
} // generate
} // class
=====================================
testBarcode.mxml
======================================
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml"
xmlns:hz="*" creationComplete="barcode.generate('1234567890')">
<mx:Script>
<![CDATA[
private function printBarcode():Void {
var pj:PrintJob=new PrintJob();
if(pj.start()!=true) {
delete pj;
return;
} // if
// Add pages
pj.addPage(barcode);
pj.send();
// Delete print job.
delete pj;
} // printBarcode
]]>
</mx:Script>
<hz:Barcode id="barcode" width="155" height="100" color="#FF0000"
backgroundColor="#FFFF00" codeType="128" showCode="true"
vScrollPolicy="off" />
<mx:Button label="Print Barcode" click="printBarcode()" />
</mx:Application>
--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/flexcoders/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/