Qt5--学习笔记-调用笔记本电脑摄像头

工程代码包下载
camera.pro

QT       += core gui widgets multimedia multimediawidgets

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = camera
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
#include 
#include 
#include 


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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


public slots:
    void captureImage();
    void displayImage(int,QImage);
    void saveImage();
private:
    Ui::MainWindow *ui;
    QCamera *camera;
    QCameraViewfinder *viewfinder;
    QCameraImageCapture *imageCapture;
};

#endif // MAINWINDOW_H

main.cpp

#include "mainwindow.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.cpp

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

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    camera=new QCamera(this);
    viewfinder=new QCameraViewfinder(this);
    imageCapture=new QCameraImageCapture(camera);

    ui->ver111->addWidget(viewfinder);
    ui->label->setScaledContents(true);

    camera->setViewfinder(viewfinder);
    camera->start();
    connect(imageCapture, SIGNAL(imageCaptured(int,QImage)), this, SLOT(displayImage(int,QImage)));
    connect(ui->capture, SIGNAL(clicked()), this, SLOT(captureImage()));
    connect(ui->save, SIGNAL(clicked()), this, SLOT(saveImage()));
    connect(ui->quit, SIGNAL(clicked()), qApp, SLOT(quit()));

}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::captureImage()
{
ui->statusBar->showMessage(tr("capturing..."), 1000);
imageCapture->capture();
}

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

ui->statusBar->showMessage(tr("capture OK!"), 5000);
}
void MainWindow::saveImage()
{
QString fileName=QFileDialog::getSaveFileName(this, tr("save file"), QDir::homePath(), tr("jpegfile(*.jpg)"));
if(fileName.isEmpty()) {
ui->statusBar->showMessage(tr("save cancel"), 5000);
return;
}
const QPixmap* pixmap=ui->label->pixmap();
if(pixmap) {
pixmap->save(fileName);
ui->statusBar->showMessage(tr("save OK"), 5000);
}
}

forms


<ui version="4.0">
 <class>MainWindowclass>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0x>
    <y>0y>
    <width>627width>
    <height>405height>
   rect>
  property>
  <property name="windowTitle">
   <string>MainWindowstring>
  property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QWidget" name="verticalLayoutWidget">
    <property name="geometry">
     <rect>
      <x>10x>
      <y>10y>
      <width>391width>
      <height>331height>
     rect>
    property>
    <layout class="QVBoxLayout" name="ver111"/>
   widget>
   <widget class="QWidget" name="verticalLayoutWidget_2">
    <property name="geometry">
     <rect>
      <x>419x>
      <y>10y>
      <width>191width>
      <height>331height>
     rect>
    property>
    <layout class="QVBoxLayout" name="vert2222">
     <item>
      <widget class="QLabel" name="label">
       <property name="text">
        <string>TextLabelstring>
       property>
      widget>
     item>
     <item>
      <widget class="QPushButton" name="capture">
       <property name="text">
        <string>扑捉string>
       property>
      widget>
     item>
     <item>
      <widget class="QPushButton" name="save">
       <property name="text">
        <string>保存string>
       property>
      widget>
     item>
     <item>
      <widget class="QPushButton" name="quit">
       <property name="text">
        <string>退出string>
       property>
      widget>
     item>
    layout>
   widget>
  widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0x>
     <y>0y>
     <width>627width>
     <height>26height>
    rect>
   property>
  widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarAreaenum>
   attribute>
   <attribute name="toolBarBreak">
    <bool>falsebool>
   attribute>
  widget>
  <widget class="QStatusBar" name="statusBar"/>
 widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
ui>

你可能感兴趣的:(Qt)