qt5打开摄像头采集图像并拍照

qt5打开摄像头  主要用了QCamera类  要在pro文件里加入  multimedia multimediawidgets这两个模块

QT += core gui multimedia multimediawidgets

QCamera是摄像头类

QCameraViewfinder 是取景器类,QCamera的拍到的图像就放在这里,QCameraViewfinder继承于QWidget类,使用的时候在ui里加入一个布局控件,然后再代码里添加进入QCameraViewfinder的对象,就ok了。

ui->verticalLayout->addWidget(cameraViewFinder);

 
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
#include 
#include 

#ifdef Q_OS_ANDROID
    #include "opencv2/opencv.hpp"
    using namespace cv;
#endif



namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void cameraImageCaptured(int id, QImage image);

    void on_pushButton_3_clicked();

private:
    Ui::MainWindow *ui;

#ifdef Q_OS_ANDROID
    VideoCapture cap;
    Mat ImageTOmat(QImage image);    //QImage转Mat
    QImage MatTOimage(const Mat &inmat);  //Mat转QImage
#endif

    QCamera *camera;//系统摄像头设备
    QCameraViewfinder *cameraViewFinder;//摄像头取景器部件
    QCameraImageCapture *cameraImageCapture;//截图部件

    int number;



};

#endif // MAINWINDOW_H



#include "mainwindow.h"
#include "ui_mainwindow.h"
#include 

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    number = 0;

//    ui->label->setScaledContents(true);
//    ui->label->setFixedSize(160, 120);

    camera = new QCamera(this);//系统摄像头设备
    cameraViewFinder = new QCameraViewfinder(this);//摄像头取景器部件
    cameraImageCapture = new QCameraImageCapture(camera);//截图部件

    ui->verticalLayout->addWidget(cameraViewFinder);

    connect(cameraImageCapture,SIGNAL(imageCaptured(int,QImage)),this,SLOT(cameraImageCaptured(int,QImage)));

    cameraImageCapture->setCaptureDestination(QCameraImageCapture::CaptureToFile);
    camera->setCaptureMode(QCamera::CaptureStillImage);
    camera->setViewfinder(cameraViewFinder);
}

MainWindow::~MainWindow()
{
    delete ui;
}

//打开
void MainWindow::on_pushButton_clicked()
{
    camera->start();//启动摄像头
//    cap.open(0);
//    if(!cap.isOpened()) {
//        number ++;
//        ui->label->setText(tr("打不开 %1").arg(number));
//        return;
//    }
//    number = 0;
//    Mat img;
//    cap.read(img);
//    QImage ii = QImage((const uchar*)img.data,img.cols,
//                          img.rows,img.step,QImage::Format_RGB888).rgbSwapped();
//    ui->label->setPixmap(QPixmap::fromImage(ii));
}

//关闭
void MainWindow::on_pushButton_2_clicked()
{
    camera->stop();

    //cap.release();
}

//拍照
void MainWindow::on_pushButton_3_clicked()
{
    cameraImageCapture->capture();//捕获图像并保存到文件中   发出信号imageCaptured
}

void MainWindow::cameraImageCaptured(int id, QImage image)
{
    ui->label->setPixmap(QPixmap::fromImage(image));
}



#ifdef Q_OS_ANDROID

//Mat转QImage
QImage MainWindow::MatTOimage(const Mat &inmat)
{
    return QImage();

//    Mat mat = inmat.clone();
//    // 8-bits unsigned, NO. OF CHANNELS=1  8位无符号单通道
//        if(mat.type()==CV_8UC1)
//        {
//            // Set the color table (used to translate colour indexes to qRgb values)
//            //设置颜色表(用于将颜色索引转换为qRgb值)
//            QVector colorTable;
//            for (int i=0; i<256; i++)
//                colorTable.push_back(qRgb(i,i,i));
//            // Copy input Mat 复制输入 Mat
//            const uchar *qImageBuffer = (const uchar*)mat.data;
//            // Create QImage with same dimensions as input Mat
//            //创建与输入Mat相同尺寸的QImage
//            QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8);
//            img.setColorTable(colorTable);
//            return img;
//        }
//        // 8-bits unsigned, NO. OF CHANNELS=3 8位无符号3通道
//        else if(mat.type()==CV_8UC3)
//        {
//            // Copy input Mat  复制输入 Mat
//            const uchar *qImageBuffer = (const uchar*)mat.data;
//            // Create QImage with same dimensions as input Mat
//            //创建与输入Mat相同尺寸的QImage
//            QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
//            return img.rgbSwapped();
//        }
//        else if(mat.type() == CV_8UC4) //8位无符号4通道
//        {
//            // Copy input Mat
//            const uchar *pSrc = (const uchar*)mat.data;
//            // Create QImage with same dimensions as input Mat
//            QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);
//            return image.copy();
//        }
//        else
//        {
//            qDebug() << "ERROR: Mat could not be converted to QImage.";
//            return QImage();
//        }
}
//QImage转Mat
Mat MainWindow::ImageTOmat(QImage image)
{
    Mat mat;
    return mat;

//    Mat mat;
//    //qDebug() << image.format();
//    switch(image.format())
//    {
//        case QImage::Format_ARGB32:
//        case QImage::Format_RGB32:
//        case QImage::Format_ARGB32_Premultiplied:
//            mat = Mat(image.height(), image.width(), CV_8UC4, (void*)image.constBits(), image.bytesPerLine());
//            break;
//        case QImage::Format_RGB888:
//            mat = Mat(image.height(), image.width(), CV_8UC3, (void*)image.constBits(), image.bytesPerLine());
//            cv::cvtColor(mat, mat, COLOR_YUV2RGB_NV12);    //CV_BGR2RGB
//            break;
//        case QImage::Format_Indexed8:
//            mat = Mat(image.height(), image.width(), CV_8UC1, (void*)image.constBits(), image.bytesPerLine());
//            break;
//        default:
//            break;
//    }
//    return mat;
}

#endif



 

 

你可能感兴趣的:(c++,qt)