树莓派3B--Qt中使用wiringPi来驱动RGB灯、使用opencv打开网络摄像头(代码分享)

效果展示:

代码分享:

 

开始
#include "cameramanage.h"
#include "ui_cameramanage.h"
#include <QPixmap>
#include <QFileDialog>
#include <QPicture>
#include <iostream>
#include <wiringPi.h>

CameraManage::CameraManage(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::CameraManage)
{
    ui->setupUi(this);
    timer = new QTimer(this);
    timer_led = new QTimer(this);
    cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
    cap.set(CV_CAP_PROP_FRAME_HEIGHT, 240);
    cap.set(CV_CAP_PROP_FPS, 30);//帧数
    cap.set(CV_CAP_PROP_BRIGHTNESS, 1);//亮度 1
    cap.set(CV_CAP_PROP_CONTRAST,40);//对比度 40
    cap.set(CV_CAP_PROP_SATURATION, 50);//饱和度 50
    cap.set(CV_CAP_PROP_HUE, 50);//色调 50
    cap.set(CV_CAP_PROP_EXPOSURE, 50);//曝光 50


    /*信号和槽*/
    connect(timer, SIGNAL(timeout()), this, SLOT(readFarme()));  // 时间到,读取当前摄像头信息
    connect(timer_led, SIGNAL(timeout()), this, SLOT(LED_open()));
}

CameraManage::~CameraManage()
{
    delete ui;
    delete timer;
}

QImage  CameraManage::Mat2QImage(Mat& cvImg)
{
    QImage qImg;
    if(cvImg.channels()==3)                             //3 channels color image
    {

        cv::cvtColor(cvImg,cvImg,CV_BGR2RGB);
        qImg =QImage((const unsigned char*)(cvImg.data),
                    cvImg.cols, cvImg.rows,
                    cvImg.cols*cvImg.channels(),
                    QImage::Format_RGB888);
    }
    else if(cvImg.channels()==1)                    //grayscale image
    {
        qImg =QImage((const unsigned char*)(cvImg.data),
                    cvImg.cols,cvImg.rows,
                    cvImg.cols*cvImg.channels(),
                    QImage::Format_Indexed8);
    }
    else
    {
        qImg =QImage((const unsigned char*)(cvImg.data),
                    cvImg.cols,cvImg.rows,
                    cvImg.cols*cvImg.channels(),
                    QImage::Format_RGB888);
    }

    return qImg;
}

QImage CameraManage::ScaleImage2Label(QImage qImage, QLabel* qLabel)
{
    QImage qScaledImage;
    QSize qImageSize = qImage.size();
    QSize qLabelSize = qLabel->size();
    double dWidthRatio = 1.0*qImageSize.width() / qLabelSize.width();
    double dHeightRatio = 1.0*qImageSize.height() / qLabelSize.height();
    if (dWidthRatio>dHeightRatio)
    {
        qScaledImage = qImage.scaledToWidth(qLabelSize.width());
    }
    else
    {
        qScaledImage = qImage.scaledToHeight(qLabelSize.height());
    }
    return qScaledImage;
}

void CameraManage::on_open_clicked()
{    
   // cap.open(-1);
    cap.open( "rtsp://admin:guit123456789@192.168.1.62:554/H.264/ch1/main/av_stream" "width=(int)600, height=(int)600,format=(string)I420, " );

    if (cap.isOpened())
    {
        cout << "cap is opened" << endl;
    }else
    {
        cout << "cap is not opened" << endl;
    }

    timer->start(40);              // 开始计时,超时则发出timeout()信号
}

void CameraManage::readFarme()
{
    cap >> frame;

    image = Mat2QImage(frame);
//    imshow("", frame);
    
    QImage scaleImage = ScaleImage2Label( image, ui->lable );   // 显示到label上
    ui->lable->setPixmap(QPixmap::fromImage(scaleImage));
    ui->lable->setAlignment(Qt::AlignCenter);
    ui->lable->show();
}

void CameraManage::GPIO_init()
{
    wiringPiSetup();
    pinMode(0, OUTPUT);
    timer_led->start(500);
}

void CameraManage::LED_open()
{
    if(led_turn == 0)
    {
        digitalWrite(0, HIGH);
        led_turn++;
    }else{
        digitalWrite(0, LOW);
        led_turn--;
    }

}
截止


完整代码连接完整代码及工程

 

 

 

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读