Using Dynamsoft Barcode Reader C++ API for Mac

Dynamsoft's Barcode Reader SDK provides C++ and C APIs for Mac. You can use the barcode APIs to easily create a barcode reading software for Mac.

In this article, I am going to demonstrate how to use the C++ barcode reading API for Mac to build a Command Line Application for 1D and 2D barcode recognition on macOS.

Start a new file

  • New a Command Line Tool magnify

    New a Command Line Tool

    First, let's open Xcode.

    Choose Command Line Tool as the project template, and under Type select C++ stdc++. Let's name the project BarcodeReaderDemo.

    New a Command Line Tool close
    New a Command Line Tool
  • Add references magnify

    Add references

    In the main.cpp, please include the .H files.

    In our case, if you have Barcode Reader installed under Applications, the path will be "/Applications/Dynamsoft/Barcode Reader 4.1/Include/If_DBRP.h."

    #include <stdio.h>
    #include "/Applications/Dynamsoft/Barcode Reader 4.1/Include/If_DBRP.h"
                    
    Add references close
    Add references
  • Copy the main function magnify

    Copy the main function

    Next, insert the following code to the main function.

    int  main  ( int  argc,   const  char *  argv[ ] )
    {
       //Define variables
       const char * pszImageFile = "<your image file full path>";
       int  iIndex = 0;
       int  iRet = -1;
            
       //Initialize license prior to any decoding
       CBarcodeReader reader;
       reader.InitLicense("<your license key here>");
    
       //Initialize ReaderOptions
       ReaderOptions ro = {0}; 	
       ro.llBarcodeFormat = OneD;      //Expected barcode types to read.
       ro.iMaxBarcodesNumPerPage = 100;      //Expected barcode numbers to read.
       reader.SetReaderOptions ( ro );
    
       //Start decoding
       iRet = reader.DecodeFile( pszImageFile );
        
       //If not DBR_OK
       if ( iRet != DBR_OK )
       {
    	  printf( "Failed to read barcode: %d\r\n%s\r\n",
              iRet,GetErrorString(iRet) );
    	  return iRet;
       }
    
       //If DBR_OK
       pBarcodeResultArray paryResult = NULL;
       reader.GetBarcodes(&paryResult);
       printf("%d total barcodes found. \r\n",paryResult->iBarcodeCount);
       for (iIndex = 0; iIndex < paryResult->iBarcodeCount; iIndex++)
       {
              printf("Result %d\r\n", iIndex + 1);
    	  printf("PageNum: %d\r\n", paryResult->
              ppBarcodes[iIndex]->iPageNum);
    	  printf("BarcodeFormat: %lld\r\n", paryResult->
              ppBarcodes[iIndex]->llFormat);
    	  printf("Text read: %s\r\n", paryResult->
              ppBarcodes[iIndex]->pBarcodeData);
       }
    
       //Finally release BarcodeResultArray
       CBarcodeReader::FreeBarcodeResults(&paryResult);
            
       return 0;
    }
    
                    
    Copy the main function close
    Copy the main function
  • Select linked library magnify

    Select linked library

    Select Targets -> BarcodeReaderDemo, click Info button, in the popped dialog, add libDynamsoftBarcodeReader.dylib dependency.

    Select linked library close
    Update license and source image
  • Update license and source image magnify

    Update license and source image

    Please update the <your image file full path> and <your license key here> with valid values respectively in the code.

    For image path, you can use AllSupportedBarcodeTypes.tif in Images folder.
    For license key, please find it in BarcodeReaderTrialLic.txt.

    const char * pszImageFile = "/Applications/Dynamsoft/Barcode Reader 4.1/
    Images/AllSupportedBarcodeTypes.tif";
                    

    Now you can build the project and have a run.

    Update license and source image close
    Select linked library
  • Barcode result magnify

    Barcode result

    To verify if it is working, please open Terminal.app, and execute the application.

    Barcode result close
    Barcode result