// // Barcode.m // Barcode // // Created by Simon Urbanek on 11/18/08. // Copyright 2008 __MyCompanyName__. All rights reserved. // #import "Barcode.h" #include "../barcode/barcode.h" @implementation Barcode - (id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { // Initialization code } return self; } - (void) setBarcode: (NSString*) cont type: (int) codeType { struct Barcode_Item *b; b = Barcode_Create((char*)[cont UTF8String]); Barcode_Encode(b, codeType); const char *e = b->partial, *c = e; printf("encoded: '%s'\n", e); unsigned int len = 0, i = 0; ws = strlen(e); w = (int*) calloc(sizeof(int), ws); while (*c) { if (*c >= '0' && *c <= '9') len += w[i++] = *c - '0'; else if (*c >= 'a' && *c <= 'z') len += w[i++] = *c - 'a' + 1; else if (*c >= 'A' && *c <= 'Z') len += w[i++] = *c - 'A' + 1; c++; } ws = i; i = 0; while (i < ws) printf("%d ", w[i++]); printf("\n Total length: %d\n", len); scale = 1; while (len * (scale + 1) < 320) scale++; } - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0); CGContextFillRect(context, rect); CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1.0); [[NSString stringWithFormat:@"Code segments=%d", ws] drawInRect:rect withFont:[UIFont systemFontOfSize:10.0]]; int x = 0, i = 0; while (i < ws) { int k = w[i] * scale; if ((i&1) == 1) { CGRect r = { x, 20, k, 100 }; CGContextFillRect(context, r); } x += k; i++; } } - (void)dealloc { [super dealloc]; } @end