本地摄像头的采集可以有多种方式,一般本地摄像头会通过USB的方式连接,在嵌入式上可能大部分是CMOS之类的软带的接口,这些都统称本地摄像头,和网络摄像头最大区别就是一个是通过网络来通信,一个是直接本地通信。本地摄像机的采集可以用qcamera来做,但是qcamera类在很多平台没有实现,比如嵌入式linux系统上几乎是没有用的,所以更加推荐用兼容性跨平台最好的ffmpeg来做,在linux上当然也可以通过v4l2来实现,这个其实是整个linux系统中通用的USB摄像头采集的框架,无论是ffmpeg还是qcamera,在linux系统上底层其他都是用v4l2来实现的,所以如果在嵌入式板子受限于内存或者存储空间大小,用不了ffmpeg一堆库,这个时候就可以用最底层的v4l2来采集。
很多人的应用场景要求不止采集1路本地摄像头,可能有多路都需要同时采集,其实这个和程序没有太大关系,能够采集一路肯定就能多路,毕竟就是封装的一个类,直接new出来就行,要能采集多路最大的性能瓶颈在USB带宽,数据带宽,还有就是USB口子的供电足不足,供电不足,也只能采集1-2路,比如有些人用的是USB集线器,上面一排USB口子,由于供电不足,就算你插4个USB摄像头,也只能最多采集2路,带宽又限制了分辨率和帧率,比如一个普通PC机器可以同时采集4路640x480分辨率,但是由于带宽不足,无法同时采集4路1080P,尽管4个摄像头都支持1080p分辨率,所以这是个综合因素的叠加影响,遇到问题慢慢查。
这个摄像头综合应用,陆陆续续完善了很多年,最开始是v4l2版本,只能用在嵌入式linux上,而后从Qt5开始集成了qcamera类,所以有用qcamera来做了一个版本,现在Qt6在多媒体框架有了巨大的性能提升,又用Qt6的qcamera做了个版本,为什么不通用了?因为Qt6的多媒体框架做了巨大更新调整,完全不兼容之前的类。在做视频组件的实现用ffmpeg采集本地摄像头也实现了,而且做的比较完善,所以后面单独提炼出来做了ffmpeg的版本,最后测试下来发现ffmpeg是最通用的,在哪里都能正常采集。
做二维码采集这个功能,用的是zxing类,最初的做法是将采集的图片发给zxing解析,实际过程发现大图片解析很慢,比如1080P图片大概1s只能解析3张,这个速度看起来就慢了点,如果是2K的分辨率呢。后面经过一些真实场景应用,包括菜鸟驿站里面带的扫描识别,发现都有个放大区域,用户可以设置一个关心的矩形区域,要放在这个矩形区域内,能够最大最快的识别,这是个不错的策略,在ffmpeg中通过设置crop裁剪区域就可以直接将对应内容放大显示,然后采集的实时画面可以是原视频,发送给zxing解析的就是裁剪后的画面,这样速度大大提升。之前大图就算二维码在里面,由于在整个图片中的占比区域太小,也可能解析失败,自动调整后裁剪区域,性能大幅度提升,完美解决。
#include "frmzxing.h"
#include "frmmain.h"
#include "ui_frmzxing.h"
#include "qthelper.h"
#include "camerahelper.h"
#include "widgethelper.h"
#include "zxingthread.h"
frmZxing::frmZxing(QWidget *parent) : QWidget(parent), ui(new Ui::frmZxing)
{
ui->setupUi(this);
this->initForm();
this->initConfig();
}
frmZxing::~frmZxing()
{
delete ui;
}
void frmZxing::initForm()
{
ui->frame->setFixedWidth(AppData::RightWidth);
//关联信号槽
connect(ui->cameraWidget, SIGNAL(sig_receivePlayStart(int)), this, SLOT(receivePlayStart(int)));
connect(ui->cameraWidget, SIGNAL(sig_receivePoint(int, QPoint)), this, SLOT(receivePoint(int, QPoint)));
//实例化解析类并启动线程
zxing = new ZXingThread(this);
connect(zxing, SIGNAL(receiveResult(QString, QString, int)), this, SLOT(receiveResult(QString, QString, int)));
zxing->start();
//启动定时器截图
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(on_btnSnap_clicked()));
if (AppConfig::Zxing_Interval > 0) {
timer->start(AppConfig::Zxing_Interval);
}
}
void frmZxing::initConfig()
{
CameraHelper::loadCameraCore(ui->cboxCameraCore, AppConfig::Zxing_CameraCore);
connect(ui->cboxCameraCore, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
this->cameraDeviceChanged();
connect(ui->cboxCameraName->lineEdit(), SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
CameraHelper::loadVideoSize(ui->cboxVideoSize);
ui->cboxVideoSize->lineEdit()->setText(AppConfig::Zxing_VideoSize);
connect(ui->cboxVideoSize->lineEdit(), SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
CameraHelper::loadFrameRate(ui->cboxFrameRate);
ui->cboxFrameRate->lineEdit()->setText(QString::number(AppConfig::Zxing_FrameRate));
connect(ui->cboxFrameRate->lineEdit(), SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
ui->cboxVideoMode->setCurrentIndex(AppConfig::Zxing_VideoMode);
connect(ui->cboxVideoMode, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
zxing->setDecoder(AppConfig::Zxing_DecoderFormat);
ui->cboxDecoderFormat->addItem("仅二维码");
ui->cboxDecoderFormat->addItem("一维二维");
ui->cboxDecoderFormat->addItem("所有格式");
ui->cboxDecoderFormat->setCurrentIndex(AppConfig::Zxing_DecoderFormat);
connect(ui->cboxDecoderFormat, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
ui->cboxInterval->addItem("暂停");
ui->cboxInterval->addItem("300");
ui->cboxInterval->addItem("500");
ui->cboxInterval->addItem("800");
ui->cboxInterval->addItem("1000");
int index = ui->cboxInterval->findText(QString::number(AppConfig::Zxing_Interval));
ui->cboxInterval->setCurrentIndex(index < 0 ? 0 : index);
connect(ui->cboxInterval, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
ui->txtTopLeft->setText(AppConfig::Zxing_TopLeft);
connect(ui->txtTopLeft, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
ui->txtBottomRight->setText(AppConfig::Zxing_BottomRight);
connect(ui->txtBottomRight, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
}
void frmZxing::saveConfig()
{
//内核变了需要重新搜索设备
int cameraCore = ui->cboxCameraCore->itemData(ui->cboxCameraCore->currentIndex()).toInt();
if (AppConfig::Zxing_CameraCore != cameraCore) {
AppConfig::Zxing_CameraCore = cameraCore;
this->cameraDeviceChanged();
}
//不为空才需要改变
QString cameraName = ui->cboxCameraName->currentText();
if (!cameraName.isEmpty()) {
AppConfig::Zxing_CameraName = cameraName;
}
AppConfig::Zxing_VideoSize = ui->cboxVideoSize->currentText();
AppConfig::Zxing_FrameRate = ui->cboxFrameRate->currentText().toInt();
AppConfig::Zxing_VideoMode = ui->cboxVideoMode->currentIndex();
//变了立即重新设置
int decoderFormat = ui->cboxDecoderFormat->currentIndex();
if (AppConfig::Zxing_DecoderFormat != decoderFormat) {
AppConfig::Zxing_DecoderFormat = decoderFormat;
zxing->setDecoder(AppConfig::Zxing_DecoderFormat);
}
int interval = ui->cboxInterval->currentText().toInt();
if (AppConfig::Zxing_Interval != interval) {
AppConfig::Zxing_Interval = interval;
if (interval > 0) {
timer->start(interval);
} else {
timer->stop();
}
}
AppConfig::Zxing_TopLeft = ui->txtTopLeft->text().trimmed();
AppConfig::Zxing_BottomRight = ui->txtBottomRight->text().trimmed();
AppConfig::writeConfig();
}
void frmZxing::initPara()
{
//设置显示窗体参数
WidgetPara widgetPara = ui->cameraWidget->getWidgetPara();
widgetPara.borderWidth = 1;
widgetPara.videoMode = (VideoMode)AppConfig::Zxing_VideoMode;
ui->cameraWidget->setWidgetPara(widgetPara);
//设置采集线程参数
CameraPara cameraPara = ui->cameraWidget->getCameraPara();
cameraPara.cameraCore = (CameraCore)AppConfig::Zxing_CameraCore;
cameraPara.cameraName = AppConfig::Zxing_CameraName;
cameraPara.videoSize = AppConfig::Zxing_VideoSize;
cameraPara.frameRate = AppConfig::Zxing_FrameRate;
ui->cameraWidget->setCameraPara(cameraPara);
}
void frmZxing::append(int type, const QString &data, bool clear)
{
static int maxCount = 50;
static int currentCount = 0;
QtHelper::appendMsg(ui->txtMain, type, data, maxCount, currentCount, clear);
ui->txtMain->moveCursor(QTextCursor::End);
}
void frmZxing::createImage(const QString &text)
{
if (!text.isEmpty()) {
QImage image = zxing->encodeData(text, QSize(250, 250));
ui->labResult->setImage(image, true);
}
}
void frmZxing::cameraDeviceChanged()
{
frmMain::getCameraInfo((CameraCore)AppConfig::Zxing_CameraCore);
CameraHelper::loadCameraName(ui->cboxCameraName, AppConfig::Zxing_CameraName);
}
void frmZxing::snapImage(const QImage &image, const QString &snapName)
{
QImage img = image;
if (snapName != "file") {
//有左上右下坐标说明需要裁减
int w = image.width();
int h = image.height();
if (!AppConfig::Zxing_TopLeft.isEmpty() && !AppConfig::Zxing_BottomRight.isEmpty()) {
QRect rect = WidgetHelper::getRect(AppConfig::Zxing_TopLeft, AppConfig::Zxing_BottomRight);
img = img.copy(rect);
QString msg = QString("执行裁剪(原分辨率: %1 x %2 新分辨率: %3 x %4)").arg(w).arg(h).arg(img.width()).arg(img.height());
append(4, msg);
img.save("f:/1.jpg", "jpg");
}
//防止图片过大导致解析很慢
static int maxWidth = 1280;
static int maxHeight = 720;
w = img.width();
h = img.height();
if (w > maxWidth || h > maxHeight) {
img = img.scaled(QSize(maxWidth, maxHeight), Qt::KeepAspectRatio);
QString msg = QString("执行缩放(原分辨率: %1 x %2 新分辨率: %3 x %4)").arg(w).arg(h).arg(maxWidth).arg(maxHeight);
append(4, msg);
}
}
//添加到线程中解析
zxing->append("", img);
//显示图片
ui->labImage->setImage(img, true);
}
void frmZxing::receiveResult(const QString &flag, const QString &text, int time)
{
append(2, QString("(用时: %1 毫秒) 结果: %2").arg(time).arg(text));
ui->txtResult->setText(text);
//重新生成新的二维码图片
createImage(text);
}
void frmZxing::receivePlayStart(int time)
{
//如果存在裁剪区域则设置图形
if (!AppConfig::Zxing_TopLeft.isEmpty() && !AppConfig::Zxing_BottomRight.isEmpty()) {
GraphInfo graph;
graph.rect = WidgetHelper::getRect(AppConfig::Zxing_TopLeft, AppConfig::Zxing_BottomRight);
graph.borderWidth = WidgetHelper::getBorderWidth(ui->cameraWidget);
ui->cameraWidget->setGraph(graph);
}
}
void frmZxing::receivePoint(int type, const QPoint &point)
{
QString text = QString("%1, %2").arg(point.x()).arg(point.y());
if (ui->rbtnTopLeft->isChecked()) {
ui->txtTopLeft->setText(text);
} else {
ui->txtBottomRight->setText(text);
}
}
void frmZxing::on_btnPlay_clicked()
{
this->initPara();
if (!ui->cameraWidget->init()) {
return;
}
//关联采集线程信号槽
CameraThread *cameraThread = ui->cameraWidget->getCameraThread();
connect(cameraThread, SIGNAL(cameraDeviceChanged()), this, SLOT(cameraDeviceChanged()));
connect(cameraThread, SIGNAL(snapImage(QImage, QString)), this, SLOT(snapImage(QImage, QString)));
ui->cameraWidget->play();
ui->widget->setEnabled(false);
}
void frmZxing::on_btnStop_clicked()
{
ui->cameraWidget->stop();
ui->widget->setEnabled(true);
}
void frmZxing::on_btnPause_clicked()
{
ui->cameraWidget->pause();
}
void frmZxing::on_btnNext_clicked()
{
ui->cameraWidget->next();
}
void frmZxing::on_btnSnap_clicked()
{
CameraThread *thread = ui->cameraWidget->getCameraThread();
if (thread && thread->getIsOk()) {
append(4, "执行截图并解析二维码");
ui->cameraWidget->snap();
}
}
void frmZxing::on_btnFile_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this, "打开", "", "*.jpg *.png");
if (!fileName.isEmpty()) {
QImage image(fileName);
snapImage(image, "file");
}
}
void frmZxing::on_btnCreate_clicked()
{
createImage(ui->txtResult->text());
}