How to Use Dynamsoft Barcode Reader SDK on Mac with Apple Silicon
Apple announced new Mac models with Apple M1 chip recently. Although Dynamsoft has not released an Apple Silicon version of Dynamsoft Barcode Reader SDK yet, I am curious how will x86_64 barcode SDK perform under Rosetta 2. In this article, I will build a simple command-line barcode reader app on M1-powered MacBook Air, and compare the barcode decoding performance by running the app respectively on Intel-based macOS and M1-based macOS.
Mac Barcode SDK Download
Dynamsoft Barcode Reader v7.6 for macOS
Note: the header file included in the package only supports Objective-C and Swift. To build C/C++ programs, you need to download corresponding header files from Windows and Linux packages.
Barcode Decoding in C/C++
Firstly, you need to include the header file DynamsoftBarcodeReader.h in your *.cpp file:
#include "DynamsoftBarcodeReader.h"
The next step is to initialize a barcode reader object and set a valid license:
CBarcodeReader reader;
reader.InitLicense (license);
To decode barcodes, there are three optional methods:
If you don’t have image codec libraries, you can use either DecodeFile() or DecodeFileInMemory().
Finally, barcode results can be extracted by calling GetAllTextResults():
TextResultArray *paryResult = NULL;reader.GetAllTextResults(&paryResult);for (int index = 0; index < paryResult->resultsCount; index++){}
Intel Chip vs. Apple Silicon
Once coding is done, we can get started to build and test the barcode program on different machines.
Test Image
I used an image containing multiple barcodes.
Intel-based MacBook Air
Build and run the app:
% sysctl -n machdep.cpu.brand_stringIntel(R) Core(TM) i7-5650U CPU @ 2.20GHz% sw_versProductName: Mac OS XProductVersion: 10.15.6BuildVersion: 19G73% g++ -o barcodereader BarcodeReader.cpp -L./ -lDynamsoftBarcodeReader% ./barcodereader test.jpgCPU threads: 4Thread count: 1. Total barcode(s) found: 31. Time cost: 426 msThread count: 2. Total barcode(s) found: 31. Time cost: 505 msThread count: 3. Total barcode(s) found: 31. Time cost: 483 msThread count: 4. Total barcode(s) found: 31. Time cost: 479 msMulti-thread best performance: thread_count = 1, timecost = 426
M1-based MacBook Air
Since Rosetta 2 can translate apps that contain x86_64 instructions to arm64 instructions, I can just copy the program I built on Intel-based MacBook Air to M1-based MacBook Air.
Because Xcode 12.2 and later versions support building universal binaries, I can also link the x86_64 dynamic library and build the program on Mac with Apple M1:
% sysctl -n machdep.cpu.brand_stringApple processor% sw_versProductName: macOSProductVersion: 11.0BuildVersion: 20A2411% g++ -target x86_64-apple-macos10.9 -o barcodereader BarcodeReader.cpp -L./ -lDynamsoftBarcodeReader% ./barcodereader test.jpgCPU threads: 8Thread count: 1. Total barcode(s) found: 31. Time cost: 291 msThread count: 2. Total barcode(s) found: 31. Time cost: 302 msThread count: 3. Total barcode(s) found: 31. Time cost: 300 msThread count: 4. Total barcode(s) found: 31. Time cost: 298 msThread count: 5. Total barcode(s) found: 31. Time cost: 301 msThread count: 6. Total barcode(s) found: 31. Time cost: 297 msThread count: 7. Total barcode(s) found: 31. Time cost: 298 msThread count: 8. Total barcode(s) found: 31. Time cost: 298 msMulti-thread best performance: thread_count = 1, timecost = 291
Note: the build will fail without the argument “-target x86_64-apple-macos10.9”.
We can see the barcode decoding speed on MacBook Air with Apple Silicon is much faster.
Source Code
Originally published at https://www.dynamsoft.com on November 26, 2020.