Hi!

I got the Barcode component from Antonio I. Aguilar J. and ported it to
Flex2.
The component is able to draw type 128C barcodes.

I add three files here, for Barcode generation (Barcode.as -- the
drawing component itself, BarcodeGenerator.as -- the generator component
which invokes Barcode.as with some parameters, BarcodeExample.mxml --
just a example application, which shows, how to create Barcodes).

Barcode.as

package {
     /**
      * Barcode.as
      * Class to generate barcodes
      * At this version, just type 128 barcodes could be generated
      *
      * @author Antonio I. Aguilar J., Friedrich Dimmel
([EMAIL PROTECTED])
      * @version 2007.02.22
      */

     import flash.display.Shape;
     import flash.display.Sprite;

     public class Barcode extends Sprite {
         private var whiteColor:uint = 0xFFFFFF;
         private var blackColor:uint = 0x000000;
         private var i:Number;
         private var barWidth:Number=1;
         private var codeString:String = "";

         public var barHeight:Number = 50;
         public var code:String = "0";

         public function generateBarcode():Shape {

             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";

             // Calculate checksum
             var codeLen:Number=code.length;
             codeString=C128["B"]; // Start
             var checksum:Number=104;
             var j:Number=1;
             for(i=0;i<codeLen;i++) {
                 var tmp:Number=code.charCodeAt(i)-32;
                 checksum+=(j++*tmp);
                 codeString+=C128[tmp.toString()];
             } // for
             checksum%=103;
             codeString+=C128[checksum.toString()];
             codeString+=C128["S"]; // End

             var codeStringLen:Number=codeString.length;

             // child is the shape, in which the barcode should be
rendered
             var child:Shape = new Shape();
             child.graphics.beginFill(whiteColor);
             child.graphics.drawRect(0, 0, codeStringLen, barHeight);
             child.graphics.endFill();

             for(i=0; i<codeStringLen; i++) {
                 var digit:String = codeString.substr(i, 1);
                     if(digit == "1") {
                     child.graphics.beginFill(blackColor);
                     child.graphics.drawRect(i*barWidth, 0, i+barWidth,
barHeight);
                     child.graphics.endFill();
                 } else {
                     child.graphics.beginFill(whiteColor);
                     child.graphics.drawRect(i*barWidth, 0, i+barWidth,
barHeight);
                     child.graphics.endFill();
                 }
             }
             return child;
         }
     }
}

BarcodeGenerator.as

package {
/**
      * BarcodeGenerator.as
      * Barcode-Generator Wrapper class
      *
      * @author Friedrich Dimmel ([EMAIL PROTECTED])
      * @version 2007.02.22
      */

     import mx.controls.Image;
     import flash.display.BitmapData;
     import flash.display.DisplayObject;
     import flash.geom.Rectangle;
     import flash.display.Bitmap;
     import flash.display.Shape;
     import mx.core.UIComponent;
     import mx.containers.Canvas;
     import mx.controls.Label;

     public class BarcodeGenerator {

         public static function generateBarcode(code:String="0",
height:Number=60, showText:Boolean=true,
backgroundColor:uint=0xffffff):Canvas {
             var can:Canvas = new Canvas();
             can.horizontalScrollPolicy = "off";
             can.verticalScrollPolicy = "off";
             can.height = height;
             can.setStyle("backgroundColor", backgroundColor);

             var barcode:Barcode = new Barcode();
             barcode.code = code;
             barcode.barHeight = height;
             if(showText) {
                 barcode.barHeight -= 15;
             }

             var sBarcode:Shape = barcode.generateBarcode();
             var rect:Rectangle = new Rectangle(0, 0, (sBarcode.width /
2) + 1, barcode.barHeight);
             var bd:BitmapData = getBitmapData(sBarcode, rect);

             var img:Image = new Image();
             var bitmap:Bitmap = new Bitmap(bd);
             img.source = bitmap;
             can.addChild(img);

             if(showText) {
                 var textLabel:Label = new Label();
                 textLabel.text = code;
                 textLabel.y = barcode.barHeight;
                 textLabel.percentWidth = 100;
                 textLabel.setStyle("textAlign", "center");
                 textLabel.setStyle("fontWeight", "bold");
                 can.addChild(textLabel);
             }
             return can;
         }

         private static function
getBitmapData(displayObject:DisplayObject,
clipRect:Rectangle=null):BitmapData {
             var theWidth:Number = displayObject.width;
             var theHeight:Number = displayObject.height;
             if(clipRect != null) {
                 theWidth = clipRect.width;
                 theHeight = clipRect.height;
             }
             var bd:BitmapData = new BitmapData(theWidth, theHeight);
             bd.draw(displayObject, null, null, null, clipRect);
             return bd;
         }
     }
}

BarcodeExample.mxml

<?xml version="1.0" encoding="utf-8"?>
<!--
     BarcodeExample.mxml
     Example file, that shows, how to create Barcodes (128C) with Flex2
on the fly

     @author:     Fritz Dimmel ([EMAIL PROTECTED])
     @version:    1.0

     release notes:
         Barcode.as component is based on the component of Antonio I.
Aguilar J.
         ported to Flex2 by Fritz Dimmel
-->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";
layout="absolute" initialize="init()" backgroundColor="0xffffff">
     <mx:Script>
         <![CDATA[
             internal function init():void {
                
vbox.addChild(BarcodeGenerator.generateBarcode("01234BARCODE56789"));
                
vbox.addChild(BarcodeGenerator.generateBarcode("helloWorld", 120));
                 vbox.addChild(BarcodeGenerator.generateBarcode("123456",
60, false));
                
vbox.addChild(BarcodeGenerator.generateBarcode("007JamesBond", 60, true,
0xffff00));
             }
         ]]>
     </mx:Script>

     <mx:VBox id="vbox" />
</mx:Application>


I hope someone could use it!

Bye,
Fritz Dimmel


Reply via email to