I recently purchased a Raspberry Pi 4 (4GB model) and an I2C OLED display module. In this article, I will share how to implement a C/C++ barcode reader project on Raspberry Pi 4 from scratch.
Raspberry Pi 4 Specifications
- Broadcom BCM2711, Quad core Cortex-A72 (ARM v8) 64-bit SoC @ 1.5GHz
- 2GB, 4GB or 8GB LPDDR4–3200 SDRAM (depending on model)
- 2.4 GHz and 5.0 GHz IEEE 802.11ac wireless, Bluetooth 5.0, BLE
- Gigabit Ethernet
- 2 USB 3.0 ports; 2 USB 2.0 ports.
- 2 × micro-HDMI ports (up to 4kp60 supported)
My Hardware Components
- Raspberry Pi 4 (4GB model)
- Raspberry Pi RGB Cooling HAT with adjustable fan and OLED display
- Micro-HDMI to HDMI cable
- HDMI Female to HDMI Female Coupler Connector (used to extend Micro-HDMI to HDMI cable)
- SanDisk Ultra 32GB MicroSDHC UHS-I Card
- USB Webcam
Raspberry Pi OS Installation and Configuration
Installation
- Download Raspberry Pi OS.
- Write the OS image to an SD card with Win32 Disk Imager.
- Insert the SD card into Raspberry Pi 4 and connect to power via USB-C connector (minimum 3A*).
Configuration
Launch the OS and then enable the I2C, VNC, and SSH interfaces.
If you want to use Windows Remote Desktop Connection, you can install tightvncserver and xrdp:
sudo apt updatesudo apt install tightvncserver xrdp
Check disk space:
df -HFilesystem Size Used Avail Use% Mounted on/dev/root 32G 8.9G 21G 30% /devtmpfs 1.9G 0 1.9G 0% /devtmpfs 2.1G 0 2.1G 0% /dev/shmtmpfs 2.1G 9.1M 2.1G 1% /runtmpfs 5.3M 4.1k 5.3M 1% /run/locktmpfs 2.1G 0 2.1G 0% /sys/fs/cgroup/dev/mmcblk0p1 265M 54M 211M 21% /boottmpfs 405M 4.1k 405M 1% /run/user/1000
If not all of the SD card storage is available, run:
sudo raspi-config
Select Advanced Options:
Then select A1 to expand disk storage. A reboot is required to make it work:
OpenCV Installation
We can use OpenCV to capture webcam frames.
Download the latest OpenCV source code: https://github.com/opencv/opencv/releases
Install the following required packages:
sudo apt install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libjpeg-dev libpng-dev libtiff-dev
Build ( takes more than 1 hour) and install OpenCV:
mkdir buildcd buildcmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DOPENCV_GENERATE_PKGCONFIG=ON ..make -j4sudo make install
C/C++ Barcode Reader Project
Let’s create a CMake project named raspberry-pi-cpp-barcode under /home/pi directory:
cd /home/pimkdir raspberry-pi-cpp-barcode
Download Dynamsoft Raspberry Pi Barcode SDK to get libDynamsoftBarcodeReader.so:
Link the barcode recognition library, WiringPi library and OpenCV libraries in CMakeLists.txt:
link_directories("${PROJECT_SOURCE_DIR}/platforms/linux/")find_package(OpenCV REQUIRED)include_directories("${PROJECT_BINARY_DIR}" "${PROJECT_SOURCE_DIR}/include/")add_executable(BarcodeReader ssd1306_i2c.c BarcodeReader.cxx)target_link_libraries (BarcodeReader "DynamsoftBarcodeReader" ${OpenCV_LIBS} wiringPi)
Here is the OpenCV C/C++ code for grabbing frames from webcam:
To decode barcodes from camera frames, we can use the barcode video APIs which manage a frame queue and do barcode detection on a worker thread:
Once we get barcode decoding results, we can show the text on the OLED display:
Finally, build and run the C/C++ barcode reader program:
mkdir buildcd buildcmake ..cmake –build ../BarcodeReader
How to AutoStart Programs
If you want to start the barcode program on OS startup, you can create a /home/pi/autostart.sh script file:
#!/bin/sh/home/pi/raspberry-pi-cpp-barcode/build/BarcodeReader
Besides, you need to change the file permission to make it executable:
chmod a+x autostart.sh
After that, create a /home/pi/.config/autostart/autostart.desktop file:
[Desktop Entry]Type=ApplicationExec=sh /home/pi/autostart.sh
Now you can reboot the OS to verify whether the barcode program will run automatically:
sudo reboot
Source Code
Originally published at https://www.dynamsoft.com on June 10, 2020.