#include #ifndef WIN32 #include #endif #include // for strlen #include // for atoi #include "barcode.h" #ifdef WIN32 extern int getopt( int, char **, char * ); extern int opterr; extern int optind; extern char *optarg; #define strncasecmp _st_strncasecmp #endif char *file ; FILE *output_fp ; static int bReadCODABAR = 0 ; static int bReadCODE39 = 0 ; static int bReadCODE128 = 0 ; static int bReadCODE25 = 0 ; static int bReadEAN13 = 0 ; static int bReadEAN8 = 0 ; static int bReadUPCA = 0 ; static int bReadUPCE = 0 ; static int bShowPageNo = 0 ; void set_barcode_types(char *arg) ; int flag(void *hBarcode, int argc, char *argv[]) ; int usage(void *hBarcode, char *name) ; int main(int argc, char *argv[]) { char **bar_codes ; char **bar_codes_type ; int bar_count ; uint16 i ; void *hBarcode ; hBarcode = STCreateBarCodeSession() ; output_fp = stdout ; flag(hBarcode, argc, argv) ; i = 0 ; STSetParameter(hBarcode, ST_READ_CODE39, &i) ; STSetParameter(hBarcode, ST_READ_CODE128, &i) ; STSetParameter(hBarcode, ST_READ_CODE25, &i) ; STSetParameter(hBarcode, ST_READ_EAN13, &i) ; STSetParameter(hBarcode, ST_READ_EAN8, &i) ; STSetParameter(hBarcode, ST_READ_UPCA, &i) ; STSetParameter(hBarcode, ST_READ_UPCE, &i) ; STSetParameter(hBarcode, ST_READ_CODABAR, &i) ; i = 1 ; if (bReadCODE39) STSetParameter(hBarcode, ST_READ_CODE39, &i) ; if (bReadCODE128) STSetParameter(hBarcode, ST_READ_CODE128, &i) ; if (bReadCODE25) STSetParameter(hBarcode, ST_READ_CODE25, &i) ; if (bReadEAN13) STSetParameter(hBarcode, ST_READ_EAN13, &i) ; if (bReadEAN8) STSetParameter(hBarcode, ST_READ_EAN8, &i) ; if (bReadUPCA) STSetParameter(hBarcode, ST_READ_UPCA, &i) ; if (bReadUPCE) STSetParameter(hBarcode, ST_READ_UPCE, &i) ; if (bReadCODABAR) STSetParameter(hBarcode, ST_READ_CODABAR, &i) ; if (!bReadCODE39 && !bReadCODE128 && !bReadCODE25 && !bReadEAN13 && !bReadEAN8 && !bReadUPCA && !bReadUPCE && !bReadCODABAR) { fprintf (stderr, "Please user the -t flag to specify the types f barcode to read\n") ; fprintf (stderr, "e.g. -t \"code39|code128\"\n") ; exit (1) ; } bar_count = STReadBarCode(hBarcode, file, 0, &bar_codes, &bar_codes_type) ; if (bar_count < 0) { switch(bar_count) { case ST_ERROR_FILE_OPEN: fprintf (stderr, "Cannot open %s as a TIF File\n", file) ; break ; case ST_ERROR_MULTI_PLANE: case ST_ERROR_BITS_PER_SAMPLE: fprintf (stderr, "TIF File format error\n") ; break ; } exit (- bar_count) ; } for (i = 0; i < bar_count; i++) { uint32 TopLeftX, TopLeftY, BotRightX, BotRightY ; int nPage ; nPage = STGetBarCodePos(hBarcode, i, &TopLeftX, &TopLeftY, &BotRightX, &BotRightY) ; if (bShowPageNo) fprintf (output_fp, "%d:%s\n", nPage, bar_codes[i]) ; else fprintf (output_fp, "%s\n", bar_codes[i]) ; } exit (0) ; } int flag(void *hBarcode, int argc, char *argv[]) { char ch ; float val ; uint16 ival ; uint32 lval ; file = 0 ; while ((ch = getopt (argc, argv, "-f:F:T:k:R:A:c:C:u:i:l:j:o:OL:p:t:r:d:q:S:awmPsn:N:")) != EOF) { switch(ch) { case 1: file = optarg ; break ; case 'F': if (! (output_fp = fopen (optarg, "w"))) { fprintf (stderr, "Cannot open file %s for output\n", optarg) ; exit (1) ; } break ; case 'R': STSetParameter(hBarcode, ST_REGEX_MATCH, optarg) ; break ; case 'T': ival = atoi (optarg) ; STSetParameter(hBarcode, ST_CONTRAST, &ival) ; break ; case 't': set_barcode_types(optarg) ; break ; case 'k': ival = atoi (optarg) ; STSetParameter(hBarcode, ST_SKEW_SETTING, &ival) ; break ; case 'a': ival = 1 ; STSetParameter(hBarcode, ST_CODE39_NEEDS_ASTERISKS, &ival) ; break ; case 'A': ival = atoi (optarg); STSetParameter(hBarcode, ST_SKEW_SETTING, &ival) ; break ; case 'c': ival = atoi (optarg); STSetParameter(hBarcode, ST_MIN_LEN, &ival) ; break ; case 'C': ival = atoi (optarg); STSetParameter(hBarcode, ST_MAX_LEN, &ival) ; break ; case 'S': STSetParameter(hBarcode, ST_TIFF_SPLIT, optarg) ; break ; case 'f': file = optarg ; break ; case 'u': if (sscanf(optarg, "%f", &val) == 1) STSetParameter(hBarcode, ST_UPPER_RATIO, &val) ; break ; case 'l': if (sscanf(optarg, "%f", &val) == 1) STSetParameter(hBarcode, ST_LOWER_RATIO, &val) ; break ; case 'q': ival = atoi(optarg) ; STSetParameter(hBarcode, ST_QUIET_SIZE, &ival) ; break ; case 'o': ival = atoi(optarg) ; STSetParameter(hBarcode, ST_MIN_OCCURRENCE, &ival) ; break ; case 'p': ival = atoi(optarg) ; STSetParameter(hBarcode, ST_PREF_OCCURRENCE, &ival) ; break ; case 'd': ival = atoi(optarg) ; STSetParameter(hBarcode, ST_ORIENTATION_MASK, &ival) ; break ; case 'j': lval = atoi(optarg) ; STSetParameter(hBarcode, ST_LINE_JUMP, &lval) ; break ; case 'v': _st_version(hBarcode) ; break ; case 'w': ival = 1 ; STSetParameter(hBarcode, ST_TIF_WARNINGS, &ival) ; break ; case 'O': ival = 1 ; STSetParameter(hBarcode, ST_USE_OVERSAMPLING, &ival) ; break ; case 'L': ival = atoi(optarg) ; STSetParameter(hBarcode, ST_OVERSAMPLING_SEP, &ival) ; break ; case 'm': ival = 1 ; STSetParameter(hBarcode, ST_MULTIPLE_READ, &ival) ; break ; case 's': ival = 1 ; STSetParameter(hBarcode, ST_DESPECKLE, &ival) ; break ; case 'n': ival = atoi(optarg) ; STSetParameter(hBarcode, ST_NOISEREDUCTION, &ival) ; break ; case 'N': ival = atoi(optarg) ; STSetParameter(hBarcode, ST_NOISEREDUCTION, &ival) ; ival = 1 ; STSetParameter(hBarcode, ST_DESPECKLE, &ival) ; break ; case 'i': ival = atoi(optarg) ; STSetParameter(hBarcode, ST_PAGE_NO, &ival) ; break ; case 'P': bShowPageNo = 1 ; break ; default: usage(hBarcode, argv[0]) ; break ; } } if (! file) usage(hBarcode, argv[0]) ; return 0 ; } int usage(void *hBarcode, char *name) { _st_copyright(hBarcode) ; _st_version(hBarcode) ; printf ("\n") ; printf ("Usage: %s -f image_file -t barcode_types [options]\n", name) ; printf ("\n") ; printf ("-f image_file image_file can be of type TIF or JPG. JPG files must use\n") ; printf (" the extension \".jpg\" or \".jpeg\"\n") ; printf ("\n") ; printf ("-t barcode_types Specifies the types of barcodes to read, barcode_types is\n") ; printf (" one or more of \"code39\", \"code128\", \"upca\", \"upce\", \"ean8\",\n") ; printf (" \"code25\", \"ean13\" and \"codabar\" joined together with the | character.\n") ; printf ("-t any Searches for a barcode of any type\n") ; printf (" e.g \"code39|code128\"\n") ; printf ("\n") ; printf ("Other options:\n\n") ; printf ("-i page_index Page number of image indexed from 1\n") ; printf ("\n") ; printf ("-j jump Frequency at which scan lines are sampled, default is 1\n") ; printf ("\n") ; printf ("-k skew tolerance Skew tolerance 0 = off to 5 = any angle\n") ; printf ("\n") ; printf ("-m Scan for multiple barcodes\n") ; printf ("\n") ; printf ("-n noise_reduction Noise Reduction level (0 = off, typical value is 20)\n") ; printf ("\n") ; printf ("-N noise_reduction Advanced Noise Reduction (0 = off, typical value is 20)\n") ; printf ("\n") ; printf ("-o min_occurs Minimum number of matching scan lines, default 2\n") ; printf ("\n") ; printf ("-O Sample the scan lines in blocks of 3 with each line\n") ; printf (" separated by oversample_sep lines (see below).\n") ; printf ("\n") ; printf ("-L oversample_sep Distance between the line samples when over-sampling is\n") ; printf (" used. Default value is 3.\n") ; printf ("\n") ; printf ("-p pref_occurs Preferred number of matching scan lines, default 5\n") ; printf ("\n") ; printf ("-P Show page numbers\n") ; printf ("\n") ; printf ("-q quiet_size Number of pixels wide for the quiet zone around bar code\n") ; printf ("\n") ; printf ("-d scan_direction Scan direction mask 1 = Left to Right, 2 = Top to Bottom\n") ; printf (" 4 = Right to Left and 8 = Bottom to Top. default is 15.\n") ; printf (" The scan direction mask controls the directions in which.\n") ; printf (" the barcode reader will look for a barcode. With this\n") ; printf (" version it it now possible to scan for barcodes in\n") ; printf (" any rotation with one call to the reader.\n") ; printf ("\n") ; printf ("-S file_template: Split a multi-page TIF file into smaller TIF files.\n") ; printf (" The first page of each new file will contain a barcode\n") ; printf (" The template must include %%d to index the files\n") ; printf (" e.g. file%%d.tif. Must be used with -m flag.\n") ; printf ("\n") ; printf ("-T color_threshold Set the color threshold\n") ; printf ("-w Print TIF Warnings to STDERR\n") ; printf ("\n") ; printf ("-u upper_ratio Upper limit of ratio between narrow and wide lines\n") ; printf (" , default is 4.0 (Code 39 only)\n") ; printf ("\n") ; printf ("-c min_length Minimum length of barcode\n") ; printf ("-C max_length Maximum length of barcode\n") ; printf ("Examples:\n") ; printf ("\n") ; printf ("Scan for a Code 39 barcode in a portrait orientation image:\n") ; printf ("\n") ; printf ("bardecode file.tif -t code39 -d 5\n") ; printf ("\n") ; printf ("Scan for a Code 39 barcode in a landscape orientation image:\n") ; printf ("\n") ; printf ("bardecode file.tif -t code39 -d 10\n") ; printf ("\n") ; printf ("Scan for a Code 128 or UPC-A barcode in an image of poor quality:\n") ; printf ("\n") ; printf ("bardecode -n 20 file.tif -t \"code128|upca\"\n") ; printf ("\n") ; printf ("Split a multi-page TIF file into smaller files, each with a barcode on page 1\n") ; printf ("\n") ; printf ("bardecode -t code39 -m -S output%%d.tif multipage.tif\n") ; exit (1) ; } void set_barcode_types(char *arg) { int i ; int s ; s = 0 ; for (i = 0; i <= strlen (arg); i++) { if (!arg[i] || (arg[i] == '|')) { if (! strncasecmp(arg + s, "code39", i - s)) bReadCODE39 = 1 ; else if (! strncasecmp(arg + s, "code128", i - s -1)) bReadCODE128 = 1 ; else if (! strncasecmp(arg + s, "code25", i - s -1)) bReadCODE25 = 1 ; else if (! strncasecmp(arg + s, "ean13", i - s -1)) bReadEAN13 = 1 ; else if (! strncasecmp(arg + s, "ean8", i - s -1)) bReadEAN8 = 1 ; else if (! strncasecmp(arg + s, "upca", i - s -1)) bReadUPCA = 1 ; else if (! strncasecmp(arg + s, "upce", i - s -1)) bReadUPCE = 1 ; else if (! strncasecmp(arg + s, "codabar", i - s -1)) bReadCODABAR = 1 ; else if (! strncasecmp(arg + s, "any", i - s -1)) bReadCODE39 = bReadCODE128 = bReadCODE25 = bReadEAN13 = bReadEAN8 = bReadUPCE = bReadCODABAR = 1 ; s = i + 1; } } }