怎么用树莓派4+OLED+USB摄像头搭建条形码扫描设备
小编给大家分享一下怎么用树莓派4+OLED+USB摄像头搭建条形码扫描设备,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
树莓派4相关硬件采购
树莓派4的4GB版本。官方定价65美金,京东300减40到手389。
Micro HDMI转HDMI线。树莓派4更换了电源接口。
HDMI母对母转接头。用于延长HDMI线。
树莓派智能贴身管家。包含可编程风扇,RGB灯和OLED显示模块。
系统安装
官网下载官方系统https://www.raspberrypi.org/downloads/。
用Win32 Disk Imager把镜像写到sdcard里。
卡插入树莓派4,连接电源。注意:电源至少3A输出,不要随便连接USB接口供电。
系统配置
开启I2C, VNC, 和SSH。
要通过Windows远程连接,可以安装tightvncserver和xrdp:
sudo apt updatesudo apt install tightvncserver xrdp
接下来检查下磁盘空间是否足够:
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
如果sdcard存储空间没有被完全利用,可以通过raspi-config来配置:
sudo raspi-config
安装OpenCV
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 buildcd buildcmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DOPENCV_GENERATE_PKGCONFIG=ON ..make -j4sudo make install
C/C++代码
下载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 buildcd buildcmake ..cmake -build ../BarcodeReader
程序开机启动
创建一个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=ApplicationExec=sh /home/pi/autostart.sh
重启系统之后程序就会自动运行。
看完了这篇文章,相信你对"怎么用树莓派4+OLED+USB摄像头搭建条形码扫描设备"有了一定的了解,如果想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!