Skip to content

Instantly share code, notes, and snippets.

@rynbyjn
Created May 12, 2011 21:56
Show Gist options
  • Save rynbyjn/969544 to your computer and use it in GitHub Desktop.
Save rynbyjn/969544 to your computer and use it in GitHub Desktop.
AS3 Code 128 Barcode Generation in Air
// I've only tested this with one code 128 font that I found here: http://freebarcodefonts.dobsonsw.com/
// it's a .ttf so it can be embedded in your app
public static function getBarcode( $rawData :String ) :String
{
var offset :Number = 32;
var highAscii :Number = 18;
var total :Number = 104;
var i :uint;
var len :uint;
var newCodeString :Vector.<Number> = new Vector.<Number>( $rawData.length + 3 );
newCodeString[ 0 ] = offset + highAscii + total;
len = $rawData.length;
for( i = 0; i < len; i++ )
{
var ASCIIValue :Number = $rawData.charCodeAt( i );
var checkDigit :Number = ( ( ASCIIValue - offset ) * ( i + 1 ) );
total += checkDigit;
newCodeString[ i + 1 ] = ASCIIValue;
}
var check :Number = total % 103;
var holder :Number = 0;
if( check + offset >= 127 ) holder = check + offset + highAscii;
else holder = check + offset;
newCodeString[ newCodeString.length - 2 ] = holder;
holder = 106 + offset + highAscii;
newCodeString[ newCodeString.length - 1 ] = holder;
len = newCodeString.length;
for( i = 0; i < len; i++ ) if( newCodeString[ i ] == 32 ) newCodeString[ i ] = 128;
return getBarcodeText( newCodeString );
}
private static function getBarcodeText( codeString :Vector.<Number> ) :String
{
var returnVal :String = '';
for(var i :uint = 0 ; i < codeString.length ; i++ )
{
returnVal += '&#' + codeString[ i ] + ';';
}
// flash can't seem to display all HTML entities needed for barcodes with just htmlText
// funky way to get the entities to show their values
// uses string object notation to avoid warnings in FDT
var html :HTMLLoader = new HTMLLoader();
var div :* = html.window[ 'document' ][ 'createElement' ]( 'div' );
div[ 'innerHTML' ] = returnVal;
var str :String = div[ 'innerHTML' ];
html = null;
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment