标签:int Win 彩色 unsigned Halcon char include QImage
说明:通过qt5的摄像头得到QImage图像
h文件
#ifndef WIN_H #define WIN_H #include <QWidget> #include <QDebug> #include "Halcon.h" #include "HalconCpp.h" #include "HDevThread.h" #include <string> #include<opencv2/opencv.hpp> #include <QLabel> #include <QImage> #include <QThread> #include <thread> //C++多线程头文件 #include <QCamera> //导入摄像头类 #include <QCameraInfo> //摄像头信息类 #include <QCameraViewfinder> //取景器类 #include <QCameraImageCapture> //捕获类 #include <QCameraViewfinderSettings> //摄像头设置类 #include <QPushButton> using namespace HalconCpp; class Win : public QWidget { Q_OBJECT public: Win(QWidget *parent = nullptr); ~Win(); QCamera *camera; //摄像头对象 QCameraImageCapture *imageCapture;//捕获对象 QPushButton* buttonCapture; HObject hObject; HTuple hv_WindowHandle,hv_Width, hv_Height; HObject QImageToHObject(QImage image);//Qt彩色转Halcon彩色 private slots: void displayImage(int,QImage); void captureImage(); }; #endif // WIN_H
cpp文件
#include "win.h" Win::Win(QWidget *parent) : QWidget(parent) { this->resize(800,500); buttonCapture=new QPushButton("捕获",this); buttonCapture->move(700,400); camera=new QCamera(this); imageCapture=new QCameraImageCapture(camera);//捕获对象 connect(imageCapture, SIGNAL(imageCaptured(int,QImage)), this, SLOT(displayImage(int,QImage))); connect(buttonCapture, SIGNAL(clicked()), this, SLOT(captureImage())); camera->start(); } Win::~Win() { } void Win::displayImage(int id, QImage image) { qDebug()<<"id="<<id; hObject=QImageToHObject(image); GetImageSize(hObject, &hv_Width, &hv_Height); SetWindowAttr("background_color","black"); OpenWindow(0,0,hv_Width, hv_Height,0,"visible","",&hv_WindowHandle); DispObj(hObject, hv_WindowHandle); } void Win::captureImage() //按钮槽函数 { imageCapture->capture();//捕获图片 } HObject Win::QImageToHObject(QImage image) //QImage彩色转Halcon彩色 { //注意:在使用这个函数之前,最好先判断图像是否三通道 unsigned char *data=image.bits();//获取图像像素字节数据的首地址 int width=image.width();//图像宽 int height=image.height();//图像高 unsigned char* dataRed=(unsigned char*)malloc(width*height);//存储处理后的数据 unsigned char* dataGreen=(unsigned char*)malloc(width*height);//存储处理后的数据 unsigned char* dataBlue=(unsigned char*)malloc(width*height);//存储处理后的数据 for (int i=0;i<height;i++){ for (int j=0;j<width;j++){ dataRed[i*width+j] =*(data+2); dataGreen[i*width+j]=*(data+1); dataBlue[i*width+j]=*data; data+=4; } } HObject Image; GenImage3(&Image, "byte", width, height, (Hlong)(dataRed), (Hlong)(dataGreen), (Hlong)(dataBlue)); return Image; }
标签:int,Win,彩色,unsigned,Halcon,char,include,QImage 来源: https://www.cnblogs.com/liming19680104/p/16071137.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。