最近618优惠,买了树莓派4,打算给娃当电脑用。到手之后先自己玩了下,做了一个简单的扫码程序。
开启I2C, VNC, 和SSH。
要通过Windows远程连接,可以安装tightvncserver和xrdp:
sudo apt update
sudo apt install tightvncserver xrdp
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
如果sdcard存储空间没有被完全利用,可以通过raspi-config来配置:
sudo raspi-config
OpenCV用来打开摄像头获取视频帧。
下载最新版本源码:https://github.com/opencv/opencv/releases
安装所有依赖的包:
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
编译运行(这里要花上几个小时,很慢):
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DOPENCV_GENERATE_PKGCONFIG=ON ..
make -j4
sudo make install
下载Dynamsoft Barcode Reader SDK的树莓派版本: https://www.dynamsoft.com/Downloads/Dynamic-Barcode-Reader-Download.aspx
创建CMakeLists.txt文件。里面添加编译链接需要的libDynamsoftBarcodeReader.so, OpenCV相关的库,以及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)
使用OpenCV获取摄像头视频流:
#include
#include
#include
#include
#include
Mat frame;
VideoCapture capture(0);
for (;;)
{
int key = waitKey(10);
if ((key & 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);
}
条形码识别:
#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(&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(&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 & 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显示结果:
#include
#include
#include "ssd1306_i2c.h"
void draw_OLED(char* content)
{
ssd1306_clearDisplay();
ssd1306_drawString(content);
ssd1306_display();
}
最后编译运行程序:
mkdir build
cd build
cmake ..
cmake –build .
./BarcodeReader
用树莓派4,OLED,USB摄像头搭建条形码扫描设备
创建一个shell脚本 /home/pi/autostart.sh:
#!/bin/sh
/home/pi/raspberry-pi-cpp-barcode/build/BarcodeReader
修改执行权限:
chmod a+x autostart.sh
创建/home/pi/.config/autostart/autostart.desktop:
[Desktop Entry]
Type=Application
Exec=sh /home/pi/autostart.sh
重启系统之后程序就会自动运行。
https://github.com/yushulx/raspberry-pi-cpp-barcode