How to Decode QR Code 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 QR Code 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.
What You Should Know
QR Code 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:
- DecodeFile – Decode barcodes from a specified image file.
- DecodeFileInMemory – Decode barcodes from an image file in memory.
- DecodeBuffer – Decode barcodes from raw buffer.
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 QR Code program on different machines.
Test Image
I used an image containing multiple QR codes.
Intel-based MacBook Air
Build and run the app:
% sysctl -n machdep.cpu.brand_string
Intel(R) Core(TM) i7-5650U CPU @ 2.20GHz
% sw_vers
ProductName: Mac OS X
ProductVersion: 10.15.6
BuildVersion: 19G73
% g++ -o barcodereader BarcodeReader.cpp -L./ -lDynamsoftBarcodeReader
% ./barcodereader test.jpg
CPU threads: 4
Thread count: 1. Total barcode(s) found: 49. Time cost: 426 ms
Thread count: 2. Total barcode(s) found: 49. Time cost: 505 ms
Thread count: 3. Total barcode(s) found: 49. Time cost: 483 ms
Thread count: 4. Total barcode(s) found: 49. Time cost: 479 ms
Multi-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_string
Apple processor
% sw_vers
ProductName: macOS
ProductVersion: 11.0
BuildVersion: 20A2411
% g++ -target x86_64-apple-macos10.9 -o barcodereader BarcodeReader.cpp -L./ -lDynamsoftBarcodeReader
% ./barcodereader test.jpg
CPU threads: 8
Thread count: 1. Total barcode(s) found: 49. Time cost: 291 ms
Thread count: 2. Total barcode(s) found: 49. Time cost: 302 ms
Thread count: 3. Total barcode(s) found: 49. Time cost: 300 ms
Thread count: 4. Total barcode(s) found: 49. Time cost: 298 ms
Thread count: 5. Total barcode(s) found: 49. Time cost: 301 ms
Thread count: 6. Total barcode(s) found: 49. Time cost: 297 ms
Thread count: 7. Total barcode(s) found: 49. Time cost: 298 ms
Thread count: 8. Total barcode(s) found: 49. Time cost: 298 ms
Multi-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 QR Code decoding speed on MacBook Air with Apple Silicon is much faster.