整合功能到主界面,但是出现playerwidget如果作为子widget图像会变成绿色的底色
parent
0e3f6f3b97
commit
67836fb226
|
@ -203,7 +203,7 @@ void CPlayWidget::initializeGL()
|
|||
if(this->mType == TYPE_RGB32){
|
||||
initShaderRgb();
|
||||
}
|
||||
glClearColor(0.3,0.3,0.3,0.0);//设置背景色
|
||||
glClearColor(0.0,0.0,0.0,0.0);//设置背景色
|
||||
}
|
||||
void CPlayWidget::resizeGL(int w, int h)
|
||||
{
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include "cplaywidget.h"
|
||||
#include <QTimer>
|
||||
#include "CameraCapture.h"
|
||||
#include "mainwindow.h"
|
||||
|
||||
void CameraDataCallback(double ts, BYTE *dat, LONG size){
|
||||
qDebug()<<"reieve cnt"<<size;
|
||||
|
@ -11,22 +12,26 @@ void CameraDataCallback(double ts, BYTE *dat, LONG size){
|
|||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
|
||||
MainWindow main;
|
||||
main.show();
|
||||
/*
|
||||
CPlayWidget gPlayer(nullptr);
|
||||
gPlayer.SetDataType(CPlayWidget::IMG_TYPE::TYPE_RGB32);
|
||||
gPlayer.SetImgSize(640,480);
|
||||
gPlayer.show();
|
||||
|
||||
Camera *gCam = Camera::GetInstance();
|
||||
std::vector<std::wstring> names = gCam->EnumAllCamera();
|
||||
Camera *gCam;
|
||||
std::vector<std::wstring> names = Camera::EnumAllCamera();
|
||||
for(auto itr = names.begin();itr != names.end();itr++){
|
||||
qDebug()<<QString::fromStdWString(*itr);
|
||||
}
|
||||
gCam->SetObserver(&gPlayer);
|
||||
if(gCam->Open(*names.begin()) ){
|
||||
Camera *gCam = new Camera(*names.begin());
|
||||
gCam->SetObserver(&gPlayer);
|
||||
qDebug("open success");
|
||||
}else{
|
||||
qDebug("open fail");
|
||||
|
||||
}
|
||||
qDebug()<<QString::fromStdWString(*itr);
|
||||
}
|
||||
*/
|
||||
return a.exec();
|
||||
}
|
||||
|
|
|
@ -3,12 +3,44 @@
|
|||
|
||||
MainWindow::MainWindow(QWidget *parent) :
|
||||
QMainWindow(parent),
|
||||
ui(new Ui::MainWindow)
|
||||
ui(new Ui::MainWindow),
|
||||
m_bCameraOpen(false),
|
||||
mCamera(nullptr)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
std::vector<std::wstring> cameras = Camera::EnumAllCamera();
|
||||
for(std::wstring x : cameras){
|
||||
ui->comboBox->addItem(QString::fromWCharArray(x.c_str(),x.size()),
|
||||
QString::fromWCharArray(x.c_str(),x.size()));
|
||||
}
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
{
|
||||
MainWindow::~MainWindow(){
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void MainWindow::on_pushButton_clicked(){
|
||||
if(nullptr == mPlayerWidget){
|
||||
mPlayerWidget = new CPlayWidget(nullptr);
|
||||
}
|
||||
if(!m_bCameraOpen){
|
||||
ui->widget->SetDataType(CPlayWidget::IMG_TYPE::TYPE_RGB32);
|
||||
ui->widget->SetImgSize(640,480);
|
||||
|
||||
qDebug()<<ui->comboBox->currentText().size()<<ui->comboBox->currentText();
|
||||
wchar_t *opencamera = new wchar_t[ui->comboBox->currentText().size()];
|
||||
ui->comboBox->currentText().toWCharArray(opencamera);
|
||||
wstring ss = wstring(opencamera,ui->comboBox->currentText().size());
|
||||
if(nullptr == mCamera){
|
||||
this->mCamera = new Camera(ss);
|
||||
}
|
||||
this->mCamera->SetObserver(ui->widget);
|
||||
qDebug()<<ui->comboBox->currentText();
|
||||
ui->pushButton->setText("关闭摄像头");
|
||||
m_bCameraOpen = true;
|
||||
ui->widget->show();
|
||||
}else{
|
||||
m_bCameraOpen = false;
|
||||
ui->pushButton->setText("打开摄像头");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
#define MAINWINDOW_H
|
||||
|
||||
#include <QMainWindow>
|
||||
#include "media/CameraCapture.h"
|
||||
#include "cplaywidget.h"
|
||||
|
||||
namespace Ui {
|
||||
class MainWindow;
|
||||
|
@ -15,8 +17,15 @@ public:
|
|||
explicit MainWindow(QWidget *parent = nullptr);
|
||||
~MainWindow();
|
||||
|
||||
private slots:
|
||||
void on_pushButton_clicked();
|
||||
|
||||
private:
|
||||
Ui::MainWindow *ui;
|
||||
Camera *mCamera;
|
||||
QStringList mCameraList;
|
||||
bool m_bCameraOpen;
|
||||
CPlayWidget *mPlayerWidget;
|
||||
};
|
||||
|
||||
#endif // MAINWINDOW_H
|
||||
|
|
|
@ -6,31 +6,106 @@
|
|||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>1208</width>
|
||||
<height>943</height>
|
||||
<width>1383</width>
|
||||
<height>1116</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>MainWindow</string>
|
||||
</property>
|
||||
<widget class="QWidget" name="centralWidget">
|
||||
<widget class="CPlayWidget" name="widget" native="true">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>19</x>
|
||||
<y>20</y>
|
||||
<width>1171</width>
|
||||
<height>851</height>
|
||||
</rect>
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout_2">
|
||||
<item>
|
||||
<layout class="QVBoxLayout" name="verticalLayout" stretch="1,10">
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout" stretch="1,2,13">
|
||||
<property name="leftMargin">
|
||||
<number>2</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>2</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>2</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>1</number>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QPushButton" name="pushButton">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>0</width>
|
||||
<height>50</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="sizeIncrement">
|
||||
<size>
|
||||
<width>0</width>
|
||||
<height>6</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="baseSize">
|
||||
<size>
|
||||
<width>0</width>
|
||||
<height>50</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>打开摄像头</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QComboBox" name="comboBox">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>0</width>
|
||||
<height>35</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="baseSize">
|
||||
<size>
|
||||
<width>0</width>
|
||||
<height>50</height>
|
||||
</size>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="horizontalSpacer_2">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>40</width>
|
||||
<height>20</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="CPlayWidget" name="widget" native="true"/>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QMenuBar" name="menuBar">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>1208</width>
|
||||
<width>1383</width>
|
||||
<height>26</height>
|
||||
</rect>
|
||||
</property>
|
||||
|
|
|
@ -4,10 +4,10 @@
|
|||
#pragma comment(lib, "strmiids")
|
||||
//define release maco
|
||||
#define ReleaseInterface(x) \
|
||||
if ( NULL != x ) \
|
||||
if ( nullptr != x ) \
|
||||
{ \
|
||||
x->Release( ); \
|
||||
x = NULL; \
|
||||
x = nullptr; \
|
||||
}
|
||||
// Application-defined message to notify app of filter graph events
|
||||
#define WM_GRAPHNOTIFY WM_APP+100
|
||||
|
@ -16,19 +16,32 @@ Camera::Camera():
|
|||
mInitOK(false),
|
||||
mVideoHeight(0),
|
||||
mVideoWidth(0),
|
||||
mDevFilter(NULL),
|
||||
mCaptureGB(NULL),
|
||||
mGraphBuilder(NULL),
|
||||
mMediaControl(NULL),
|
||||
mMediaEvent(NULL),
|
||||
mSampGrabber(NULL),
|
||||
mDevFilter(nullptr),
|
||||
mCaptureGB(nullptr),
|
||||
mGraphBuilder(nullptr),
|
||||
mMediaControl(nullptr),
|
||||
mMediaEvent(nullptr),
|
||||
mSampGrabber(nullptr),
|
||||
mIsVideoOpened(false),
|
||||
mDebug(false)
|
||||
{
|
||||
//HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
|
||||
//if (SUCCEEDED(hr))
|
||||
|
||||
}
|
||||
|
||||
Camera::Camera(wstring camera)
|
||||
{
|
||||
mInitOK = true;
|
||||
mInitOK = false;
|
||||
mVideoHeight = 0;
|
||||
mVideoWidth = 0;
|
||||
mDevFilter = nullptr;
|
||||
mCaptureGB = nullptr;
|
||||
mGraphBuilder = nullptr;
|
||||
mMediaControl = nullptr;
|
||||
mMediaEvent = nullptr;
|
||||
mSampGrabber = nullptr;
|
||||
mIsVideoOpened = false;
|
||||
if(!this->Open(camera)){
|
||||
mStatus = FAIL;
|
||||
}
|
||||
mStatus = STOP;
|
||||
}
|
||||
|
@ -43,13 +56,13 @@ HRESULT Camera::InitializeEnv() {
|
|||
HRESULT hr;
|
||||
|
||||
//Create the filter graph
|
||||
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
|
||||
hr = CoCreateInstance(CLSID_FilterGraph, nullptr, CLSCTX_INPROC_SERVER,
|
||||
IID_IGraphBuilder, (LPVOID*)&mGraphBuilder);
|
||||
if (FAILED(hr))
|
||||
return hr;
|
||||
|
||||
//Create the capture graph builder
|
||||
hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC_SERVER,
|
||||
hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, nullptr, CLSCTX_INPROC_SERVER,
|
||||
IID_ICaptureGraphBuilder2, (LPVOID*)&mCaptureGB);
|
||||
if (FAILED(hr))
|
||||
return hr;
|
||||
|
@ -71,14 +84,12 @@ HRESULT Camera::InitializeEnv() {
|
|||
}
|
||||
|
||||
std::vector<std::wstring> Camera::EnumAllCamera(void) {
|
||||
if (mInitOK == false)
|
||||
return std::vector<std::wstring>();
|
||||
|
||||
std::vector<std::wstring> names;
|
||||
IEnumMoniker *pEnum = nullptr;
|
||||
// Create the System Device Enumerator.
|
||||
ICreateDevEnum *pDevEnum;
|
||||
HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL,
|
||||
HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, nullptr,
|
||||
CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDevEnum));
|
||||
|
||||
if (SUCCEEDED(hr))
|
||||
|
@ -95,12 +106,12 @@ std::vector<std::wstring> Camera::EnumAllCamera(void) {
|
|||
if (!SUCCEEDED(hr))
|
||||
return std::vector<std::wstring>();
|
||||
|
||||
IMoniker *pMoniker = NULL;
|
||||
while (pEnum->Next(1, &pMoniker, NULL) == S_OK)
|
||||
IMoniker *pMoniker = nullptr;
|
||||
while (pEnum->Next(1, &pMoniker, nullptr) == S_OK)
|
||||
{
|
||||
IPropertyBag *pPropBag;
|
||||
IBindCtx* bindCtx = NULL;
|
||||
LPOLESTR str = NULL;
|
||||
IBindCtx* bindCtx = nullptr;
|
||||
LPOLESTR str = nullptr;
|
||||
VARIANT var;
|
||||
VariantInit(&var);
|
||||
|
||||
|
@ -137,7 +148,7 @@ HRESULT Camera::BindFilter(int deviceID, IBaseFilter **pBaseFilter) {
|
|||
ICreateDevEnum *pDevEnum;
|
||||
IEnumMoniker *pEnumMon;
|
||||
IMoniker *pMoniker;
|
||||
HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
|
||||
HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, nullptr, CLSCTX_INPROC_SERVER,
|
||||
IID_ICreateDevEnum, (LPVOID*)&pDevEnum);
|
||||
if (SUCCEEDED(hr))
|
||||
{
|
||||
|
@ -185,12 +196,14 @@ int Camera::SampleGrabberCallback::SetObserver(CameraObserver *p) {
|
|||
return -1;
|
||||
mMux.lock();
|
||||
for (auto itr = this->mObserver.begin(); itr != mObserver.end(); itr++) {
|
||||
if (p == *itr) return 0;
|
||||
if (p == *itr) {
|
||||
mMux.unlock();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
this->mObserver.push_back(p);
|
||||
mMux.unlock();
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Camera::SampleGrabberCallback::RemoveObserver(CameraObserver * p)
|
||||
|
@ -213,14 +226,9 @@ int Camera::SampleGrabberCallback::RemoveObserver(CameraObserver * p)
|
|||
|
||||
bool Camera::Open(std::wstring &camera_name)
|
||||
{
|
||||
if (mInitOK == false)
|
||||
return false;
|
||||
|
||||
if (mIsVideoOpened)
|
||||
return true;
|
||||
|
||||
HRESULT hr;
|
||||
|
||||
#define CHECK_HR(x) do{ hr = (x); if (FAILED(hr)){ Close(); return false;}}while(0)
|
||||
|
||||
CHECK_HR(InitializeEnv());
|
||||
|
@ -246,7 +254,7 @@ bool Camera::Open(std::wstring &camera_name)
|
|||
return false;
|
||||
}
|
||||
// create grabber filter instance
|
||||
CHECK_HR(CoCreateInstance(CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER,
|
||||
CHECK_HR(CoCreateInstance(CLSID_SampleGrabber, nullptr, CLSCTX_INPROC_SERVER,
|
||||
IID_IBaseFilter, (LPVOID*)&pSampleGrabberFilter));
|
||||
|
||||
// bind source device
|
||||
|
@ -262,9 +270,9 @@ bool Camera::Open(std::wstring &camera_name)
|
|||
CHECK_HR(pSampleGrabberFilter->QueryInterface(IID_ISampleGrabber, (LPVOID*)&mSampGrabber));
|
||||
|
||||
// find the current bit depth
|
||||
HDC hdc = GetDC(NULL);
|
||||
HDC hdc = GetDC(nullptr);
|
||||
mBitDepth = GetDeviceCaps(hdc, BITSPIXEL);
|
||||
ReleaseDC(NULL, hdc);
|
||||
ReleaseDC(nullptr, hdc);
|
||||
|
||||
// set the media type for grabber filter
|
||||
AM_MEDIA_TYPE mediaType;
|
||||
|
@ -291,8 +299,8 @@ bool Camera::Open(std::wstring &camera_name)
|
|||
mediaType.formattype = FORMAT_VideoInfo;
|
||||
hr = mSampGrabber->SetMediaType(&mediaType);
|
||||
// 意味着最后的数据是丢掉的
|
||||
CHECK_HR(CoCreateInstance(CLSID_NullRenderer, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void**)(&dest_filter)));
|
||||
mGraphBuilder->AddFilter(dest_filter, L"NullRenderer");
|
||||
CHECK_HR(CoCreateInstance(CLSID_NullRenderer, nullptr, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void**)(&dest_filter)));
|
||||
mGraphBuilder->AddFilter(dest_filter, L"nullptrRenderer");
|
||||
|
||||
// connect source filter to grabber filter
|
||||
CHECK_HR(mCaptureGB->RenderStream(&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video,
|
||||
|
@ -329,12 +337,12 @@ bool Camera::Open(std::wstring &camera_name)
|
|||
{
|
||||
CoTaskMemFree((PVOID)mediaType.pbFormat);
|
||||
mediaType.cbFormat = 0;
|
||||
mediaType.pbFormat = NULL;
|
||||
mediaType.pbFormat = nullptr;
|
||||
}
|
||||
if (mediaType.pUnk != NULL)
|
||||
if (mediaType.pUnk != nullptr)
|
||||
{
|
||||
mediaType.pUnk->Release();
|
||||
mediaType.pUnk = NULL;
|
||||
mediaType.pUnk = nullptr;
|
||||
}
|
||||
mIsVideoOpened = TRUE;
|
||||
mStatus = RUNNING;
|
||||
|
@ -375,7 +383,7 @@ ULONG STDMETHODCALLTYPE Camera::SampleGrabberCallback::Release() {
|
|||
}
|
||||
|
||||
HRESULT STDMETHODCALLTYPE Camera::SampleGrabberCallback::QueryInterface(REFIID riid, void** ppvObject) {
|
||||
if (NULL == ppvObject) return E_POINTER;
|
||||
if (nullptr == ppvObject) return E_POINTER;
|
||||
if (riid == __uuidof(IUnknown))
|
||||
{
|
||||
*ppvObject = static_cast<IUnknown*>(this);
|
||||
|
@ -395,12 +403,13 @@ HRESULT STDMETHODCALLTYPE Camera::SampleGrabberCallback::SampleCB(double Time, I
|
|||
|
||||
HRESULT STDMETHODCALLTYPE Camera::SampleGrabberCallback::BufferCB(double Time, BYTE * pBuffer, long BufferLen)
|
||||
{
|
||||
//Debuger::Debug(L"receieve %d \r\n", BufferLen);
|
||||
#define DEBUG_CAMERA
|
||||
#ifdef DEBUG_CAMERA
|
||||
static FILE *p = fopen("camera_test.yuv","wb+");
|
||||
fwrite(pBuffer,BufferLen,1,p);
|
||||
fflush(p);
|
||||
#endif
|
||||
|
||||
if (mNewDataCallBack)
|
||||
{
|
||||
mNewDataCallBack(Time, pBuffer, BufferLen);
|
||||
|
|
|
@ -22,12 +22,14 @@ public:
|
|||
public:
|
||||
virtual int OnCameraData(uint8_t *dat, uint32_t size) { return 0; };
|
||||
};
|
||||
private:
|
||||
Camera();
|
||||
Camera(wstring camera);
|
||||
Camera(const Camera &) = delete;
|
||||
Camera& operator =(const Camera&) = delete;
|
||||
~Camera();
|
||||
|
||||
private:
|
||||
Camera();
|
||||
|
||||
bool mInitOK;
|
||||
bool mIsVideoOpened;
|
||||
|
||||
|
@ -66,15 +68,9 @@ public:
|
|||
int SetObserver(CameraObserver *);
|
||||
int RemoveObserver(CameraObserver *p);
|
||||
CAP_STATUS mStatus;
|
||||
static Camera *GetInstance(void)
|
||||
{
|
||||
static Camera *instance = nullptr;
|
||||
if (nullptr == instance)
|
||||
instance = new Camera();
|
||||
return instance;
|
||||
}
|
||||
|
||||
void SetDebug(bool);
|
||||
std::vector<std::wstring> EnumAllCamera(void);
|
||||
static std::vector<std::wstring> EnumAllCamera(void);
|
||||
GUID mPixFmt;
|
||||
bool Open(std::wstring &camera_name);
|
||||
bool Close(void);
|
||||
|
|
Loading…
Reference in New Issue