Pro-Barcode Software Logo
flag german flag uk flag us Tip: Bookmark this page with Ctrl-D (Win) or Apple-D (Mac) MainProductsKnow How / FAQDownloadStoreContact

Manual - Macintosh Dynamic Library Datamatrix (C, Cocoa Samples)

Following is a walkthrough of two sample projects that demonstrate the use of the Datamatrix 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 have 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

    WSDY3unlock("LXXXXX-XXXXX","WSDY3-XXXX-XXXXXXXX");

    // Set data and moduleWidth / -Height

    WSDY3setDataToEncode("Datamatrix Test");
    WSDY3setModuleWidth(2.0f);

    // Create barcode with 300 dpi

    CGImageRef img = WSDY3createCode(300);

    // Save as PNG

    ExportCGImageToPNGFile(img, CFSTR("/tmp/sample-barcode.png"));

    return 0;
  }


All function names exported from the Datamatrix library start with WSDY3. See the reference for a complete list.

The library always starts in demo mode. To remove the DEMO text from the barcode, call the WSDY3unlock() 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 size 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 = WSDY3createCode(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 = WSDY3createCode(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 (for example, if you want to create an inverted code), the control exposes a function, bitpattern_datamatrix, that returns the actual bitpattern of the barcode. For every module the null-terminated array has a '1', for every space it has a '0'. Using the control's properties symbolRows and symbolCols you can then simply iterate through the array to paint the code.

Here's how to do it (simplified):

  ...

  char *pattern = WSDY3bitpattern_datamatrix("Datamatrix Test");

  for (int i = 0; i < WSDY3getSymbolRows(); i++)
  {
    for (int j = 0; j < WSDY3getSymbolCols(); j++)
    {
      if(pattern[i * WSDY3getSymbolCols() + j] == '1')
      {
        // Paint a little square
      }
    }
  }



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 from 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)
    {
      WSDY3unlock("LXXXXX-XXXXX","WSDY3-XXXX-XXXXXXXX");
    }
    return self;
   }

The unlock function returns a boolean that indicates if the unlock procedure was successful.