
![]() ![]() ![]() | Tip: Bookmark this page with Ctrl-D (Win) or Apple-D (Mac) | Main • Products • Know How / FAQ • Download • Store • Contact |
Manual - Macintosh Dynamic Library Code 128 / 128 GS1 (C, Cocoa Samples)
Following is a walkthrough of a two sample projects that demonstrates the use of the Code 128 library with C and Objective-C. The projects are located in the C-Sample and Cocoa-Sample folders in the archive.Note
In order for the samples to compile independently of the actual installation location, copies of the library has been put into the project folder of the respective project. For new projects you should reference the library from the actual installation location (usually /usr/local/lib).
Using the library in a C command line tool
This very simple example demonstrates how to create a barcode and save it to a file. It's a good starting point if you have to batch create barcodes. You could also use AppleScript to call the command line tool to create barcodes via Automator.
int main(int argc, const char * argv[])
{
// Unlock to remove demo restrictions
WSDY1unlock("LXXXXX-XXXXX","WSDY1-XXXX-XXXXXXXX");
// Set data and moduleWidth / -Height
WSDY1setDataToEncode("12345678");
WSDY1setModuleWidth(1.0f);
WSDY1setModuleHeight(30.0f);
// Create barcode with 300 dpi
CGImageRef img = WSDY1createCode(300);
// Save as PNG
ExportCGImageToPNGFile(img, CFSTR("/tmp/sample-barcode.png"));
return 0;
}
All function names exported from the Code 128 library start with WSDY1. See the reference for a complete list.
The library always starts in demo mode. To remove the DEMO text from the barcode, call the WSDY1unlock() function with your user name and key before creating the first barcode.
Using the library in a Cocoa application
Creating barcodes in Cocoa is equally straight forward. The provided sample has a custom view, BarcodeView, that creates and displays a barcode. The view also handles the controls for module width, height etc.
The actual barcode creation takes place in the view's drawRect method:
- (void)drawRect:(NSRect)rect
{
[[NSColor whiteColor] setFill];
NSBezierPath *p = [NSBezierPath bezierPathWithRect:rect];
[p fill];
CGImageRef img = WSDY1createCode(72);
NSImage *barcode = [self imageFromCGImageRef:img];
[barcode drawAtPoint: NSMakePoint(0,rect.size.height - [barcode size].height)
fromRect:NSMakeRect(0,0,[barcode size].width,[barcode size].height)
operation: NSCompositeCopy
fraction:1.0f];
[barcode release];
CGImageRelease(img);
}
Saving the barcode
Saving the code works the same way as above:
- (IBAction)save:(id)sender
{
NSSavePanel *panel = [NSSavePanel savePanel];
[panel setRequiredFileType:@"png"];
int runResult = [panel runModalForDirectory:nil file:@""];
if (runResult == NSOKButton)
{
CGImageRef img = WSDY1createCode(300);
NSImage *barcode = [self imageFromCGImageRef:img];
NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:[barcode TIFFRepresentation]];
NSData *data = [rep representationUsingType: NSPNGFileType properties: nil];
[data writeToFile: [panel filename] atomically: NO];
[barcode release];
CGImageRelease(img);
}
}
Using the bitpattern functions
If you want to do your own drawing, the library exposes two functions, bitpattern_c128 and bitpattern_c128gs1 that return the actual bitpattern of the barcode as a null-terminated string. For every bar the array has a '1', for every space it has a '0'. You can then simply iterate through the array to paint the code.
Here's how to do it (simplified):
...
char *pattern = WSDY1bitpattern_c128("12345");
for (int i = 0; i < strlen(pattern); i++)
{
if(pattern[i] == '1')
{
// Paint a bar
}
}
...
The bitpattern routines do not use the library's properties; you'll have to provide the data to encode in the function call.
Note: The bitpattern routines return an empty array if you are using the demo.
Unlocking the library (removing the DEMO text)
To unlock the control (and remove the DEMO text in the barcode), you'll need the license information you received after completing your purchase in the store.
Simply call the unlock function of the library and provide your license info. With Cocoa you could do this in the respective view's initWithFrame method, alternatively use the applicationDidFinishLaunching delegate method:
- (id)initWithFrame:(NSRect)frameRect
{
if((self = [super initWithFrame:frameRect]) != nil)
{
WSDY1unlock("LXXXXX-XXXXX","WSDY1-XXXX-XXXXXXXX");
}
return self;
}
The unlock function returns a boolean that indicates if the unlock procedure was successful.