小编给大家分享一下怎么用树莓派4+OLED+USB摄像头搭建条形码扫描设备,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
树莓派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 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 <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 & 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 <wiringPi.h> #include <wiringPiI2C.h> #include "ssd1306_i2c.h" void draw_OLED(char* content) { ssd1306_clearDisplay(); ssd1306_drawString(content); ssd1306_display(); }
最后编译运行程序:
mkdir build cd build cmake .. 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=Application Exec=sh /home/pi/autostart.sh
重启系统之后程序就会自动运行。
看完了这篇文章,相信你对“怎么用树莓派4+OLED+USB摄像头搭建条形码扫描设备”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。