Build barcode scanner with raspberry pie 4, OLED, USB camera

Recently, at 618 discounts, I bought Raspberry Pie 4 to use as a computer for my baby.After that, I played it by myself and made a simple scanner.

Raspberry Pie 4 related hardware procurement

  • 4GB version of Raspberry Pie 4.Officially priced at $65, Jingdong 300 is reduced from 40 to 389.
  • Micro HDMI to HDMI cable.Raspberry Pie 4 replaced the power connector.
  • HDMI parent-to-parent adapter.Used to extend HDMI cables.
  • Raspberry pie smart personal housekeeper.Contains programmable fan, RGB lamp and OLED display module.

System Installation

  1. Official Download System https://www.raspberrypi.org/downloads/.
  2. Write the mirror into sdcard using Win32 Disk Imager.
  3. Card into raspberry pie 4, connect to power supply.Note: The power supply must be at least 3A output. Do not connect to the USB interface for power supply at will.

system configuration

Turn on I2C, VNC, and SSH.

To connect remotely through Windows, you can install tightvncserver and xrdp:

sudo apt update
sudo apt install tightvncserver xrdp

Next, check if there is enough disk space:

df -H
Filesystem      Size  Used Avail Use% Mounted on
/dev/root        32G  8.9G   21G  30% /
devtmpfs        1.9G     0  1.9G   0% /dev
tmpfs           2.1G     0  2.1G   0% /dev/shm
tmpfs           2.1G  9.1M  2.1G   1% /run
tmpfs           5.3M  4.1k  5.3M   1% /run/lock
tmpfs           2.1G     0  2.1G   0% /sys/fs/cgroup
/dev/mmcblk0p1  265M   54M  211M  21% /boot
tmpfs           405M  4.1k  405M   1% /run/user/1000

If the sdcard storage space is not fully utilized, it can be configured with raspi-config:

sudo raspi-config

Install OpenCV

OpenCV opens the camera to get the video frame.

Download the latest version of the source code: https://github.com/opencv/opencv/releases

Install all dependent 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

Compile and run (this can take several hours, very slow):

mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DOPENCV_GENERATE_PKGCONFIG=ON ..
make -j4
sudo make install

C/C++ Code

Download the raspberry pie version of the Dynamsoft Barcode Reader SDK: https://www.dynamsoft.com/Downloads/Dynamic-Barcode-Reader-Download.aspx

Establish CMakeLists.txt File.Add libDy to compile linksNamsoft BarcodeReader.so, OpenCV-related libraries, and WiringPi:

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)

Use OpenCV to capture camera video streams:

#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
 
Mat frame;
VideoCapture capture(0);
for (;;)
{
    int key = waitKey(10);
    if ((key &amp; 0xff) == 27/*ESC*/) break;
 
    capture >> frame; // read the next frame from camera
    if (frame.empty())
    {
        cerr << "ERROR: Can't grab camera frame." << endl;
        break;
    }   
    imshow("Dynamsoft Barcode Reader", frame);
     
}

Barcode recognition:

#include "DynamsoftBarcodeReader.h"
#include "BarcodeReaderConfig.h"
 
void textResultCallback(int frameId, TextResultArray *pResults, void * pUser)
{
    char * pszTemp = NULL;
    pszTemp = (char*)malloc(4096);
 
    if (pResults->resultsCount == 0)
    {
        snprintf(pszTemp, 4096, "No barcode found.\r\n\r\n");
        printf(pszTemp);
        free(pszTemp);
        CBarcodeReader::FreeTextResults(&amp;pResults);
        return;
    }
     
    for (int iIndex = 0; iIndex < pResults->resultsCount; iIndex++)
    {
        snprintf(pszTemp, 4096, "Barcode %d:\r\n", iIndex + 1);
        printf(pszTemp);
        snprintf(pszTemp, 4096, "Type: %s, Value: %s\r\n", pResults->results[iIndex]->barcodeFormatString, pResults->results[iIndex]->barcodeText);
        printf(pszTemp);
 
        draw_OLED(pszTemp);
    }
    free(pszTemp);
    CBarcodeReader::FreeTextResults(&amp;pResults);
}
 
 
CBarcodeReader reader;
int iRet = reader.InitLicense("LICENSE-KEY");
reader.SetTextResultCallback(textResultCallback,NULL);
capture >> frame;
int width = capture.get(CAP_PROP_FRAME_WIDTH);
int height = capture.get(CAP_PROP_FRAME_HEIGHT);
 
iRet = reader.StartFrameDecoding(10, 10, width, height, frame.step.p[0], IPF_RGB_888, "");
for (;;)
{
        int key = waitKey(10);
        if ((key &amp; 0xff) == 27/*ESC*/) break;
 
        capture >> frame; // read the next frame from camera
        if (frame.empty())
        {
            cerr << "ERROR: Can't grab camera frame." << endl;
            break;
        }   
        reader.AppendFrame(frame.data);
 
        imshow("Dynamsoft Barcode Reader", frame);
         
}
 
reader.StopFrameDecoding();    

OLED Display Results:

#include <wiringPi.h>
#include <wiringPiI2C.h>
 
#include "ssd1306_i2c.h"
 
void draw_OLED(char* content)
{
    ssd1306_clearDisplay();
    ssd1306_drawString(content);
    ssd1306_display();
}

Finally compile the runner:

mkdir build
cd build
cmake ..
cmake –build .
./BarcodeReader

Video Demo

https://www.bilibili.com/video/BV18z4y197cn/

Program startup

Create a shell script/home/pi/autostart.sh:

#!/bin/sh
/home/pi/raspberry-pi-cpp-barcode/build/BarcodeReader

Modify Execution Permissions:

chmod a+x autostart.sh

Create/home/pi/.config/autostart/autostart.desktop:

[Desktop Entry]
Type=Application
Exec=sh /home/pi/autostart.sh

The program will run automatically when the system is restarted.

Source code

https://github.com/yushulx/raspberry-pi-cpp-barcode

Tags: OpenCV sudo cmake github

Posted on Wed, 10 Jun 2020 20:57:03 -0400 by evdawg80