编辑丨3D视觉开发者社区
✨如果觉得文章内容不错,别忘了三连支持下哦😘~
1.下载并安装OpenNI2
2.在VS2019项目下,打开项目列表中的属性,调整:配置-Debug, 平台-x64,打开VC++目录下的包含目录,添加
C:\Program Files\OpenNI2\Include
在VC++目录下的库目录下添加
C:\Program Files\OpenNI2\Lib
在链接器目录下的输入中的附加依赖项中添加
OpenNI2.lib
将OpenNI2、OpenNI.ini、OpenNI2.dll、SonixCamera.dll复制到项目的bebug文件夹下或者复制到系统的system32下
// ColorPool.h
#pragma once
#include
using namespace openni;
// Callback object that show images
class PrintCallback : public VideoStream::NewFrameListener
{
public:
inline void SetWndHandle(HWND hWnd) { m_hWnd = hWnd; }
static void AnalyzeFrame(HWND hWnd, const VideoFrameRef& frame);
void onNewFrame(VideoStream& stream)
{
stream.readFrame(&m_frame);
AnalyzeFrame(m_hWnd, m_frame);
}
inline HWND GetWndHandle() const { return m_hWnd; }
private:
VideoFrameRef m_frame;
HWND m_hWnd = NULL;
};
class ColorPool
{
public:
BOOL SetWndHandle(HWND hWnd);
BOOL Init();
void Close();
BOOL FrameListener();
private:
void StopFrame() { m_color.stop(); }
void DestroyFrame() { m_color.destroy(); }
void CloseDevice() { m_device.close(); }
void ShutDown() { OpenNI::shutdown(); }
private:
void ShowError(const char* pDescribe, const char* pError);
Status m_retCode;
Device m_device;
VideoStream m_color;
PrintCallback m_colorPrinter;
};
// ColorPool.cpp
#include "pch.h"
#include "ColorPool.h"
void PrintCallback::AnalyzeFrame(HWND hWnd, const VideoFrameRef& frame)
{
RGB888Pixel* pColor;
HDC hdc = GetDC(hWnd);
int h = frame.getHeight();
int w = frame.getWidth();
switch (frame.getVideoMode().getPixelFormat())
{
case PIXEL_FORMAT_RGB888:
pColor = (RGB888Pixel*)frame.getData(); // get array' address of image that in pixels
// traverses the pixels according to the height and width of the image
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
COLORREF& d = (COLORREF&)pColor[i * w + j]; // mandatory reference 3bytes? 4bytes?
SetPixel(hdc, j, i, d & 0x00ffffff); // putout pixel
}
}
ReleaseDC(hWnd, hdc);
break;
default:
::MessageBox(hWnd, L"Unknown format\n", _T("错误提示"), MB_OK);
break;
}
}
/***************************************************/
BOOL ColorPool::SetWndHandle(HWND hWnd)
{
if (hWnd == NULL)
return FALSE;
m_colorPrinter.SetWndHandle(hWnd);
return TRUE;
}
BOOL ColorPool::Init()
{
m_retCode = OpenNI::initialize();
if (m_retCode != STATUS_OK)
{
ShowError("Initialize failed\n%s\n", OpenNI::getExtendedError());
return FALSE;
}
m_retCode = m_device.open(ANY_DEVICE);
if (m_retCode != STATUS_OK)
{
ShowError("Couldn't open device\n%s\n", OpenNI::getExtendedError());
return FALSE;
}
if (NULL != m_device.getSensorInfo(SENSOR_COLOR))
{
m_retCode = m_color.create(m_device, SENSOR_COLOR);
if (m_retCode != STATUS_OK)
{
ShowError("Couldn't create depth stream\n%s\n", OpenNI::getExtendedError());
return FALSE;
}
}
m_retCode = m_color.start();
if (m_retCode != STATUS_OK)
{
ShowError("Couldn't start the depth stream\n", OpenNI::getExtendedError());
return FALSE;
}
return TRUE;
}
void ColorPool::Close()
{
// if status ok, should remove frames from Frame listener
if (m_retCode == STATUS_OK)
{
Sleep(100);
m_color.removeNewFrameListener(&m_colorPrinter);
}
// related operations when closing
StopFrame();
DestroyFrame();
CloseDevice();
ShutDown();
}
BOOL ColorPool::FrameListener()
{
m_retCode = m_color.addNewFrameListener(&m_colorPrinter);
if (m_retCode != STATUS_OK) // if status is not ok then close camera
{
Sleep(100);
m_color.removeNewFrameListener(&m_colorPrinter);
Close();
return FALSE;
}
return TRUE;
}
void ColorPool::ShowError(const char* pDescribe, const char* pError)
{
if (!pDescribe || !pError)
return;
CString str1, str2;
str1 = pDescribe, str2 = pError;
str1 += str2;
::MessageBox(m_colorPrinter.GetWndHandle(), str1, _T("错误提示"), MB_OK);
}
// TestColorPoolDlg.h : header file
//
#pragma once
#include "ColorPool.h"
// CTestColorPoolDlg dialog
class CTestColorPoolDlg : public CDialogEx
{
ColorPool m_colorPool;
BOOL GetImgStreams();
// Construction
public:
CTestColorPoolDlg(CWnd* pParent = nullptr); // standard constructor
// Dialog Data
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_TESTCOLORPOOL_DIALOG };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnDestroy();
};
// TestColorPoolDlg.cpp
BOOL CTestColorPoolDlg::GetImgStreams()
{
// get dialog's handle
HWND hWnd = GetSafeHwnd();
if (!hWnd)
return FALSE;
// pass handle to object of PrintCallback
m_colorPool.SetWndHandle(hWnd);
// initializing
if (!m_colorPool.Init())
return FALSE;
// Listening frames
if (!m_colorPool.FrameListener())
return FALSE;
return TRUE;
}
BOOL CTestColorPoolDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
if (!GetImgStreams()) // start working
return FALSE;
return TRUE; // return TRUE unless you set the focus to a control
}
void CTestColorPoolDlg::OnDestroy()
{
CDialogEx::OnDestroy();
// TODO: Add your message handler code here
m_colorPool.Close(); // release resource and device
}
原文地址:https://developer.orbbec.com.cn/forum_plate_module_details.html?id=1061
版权声明:本文为奥比中光3D视觉开发者社区特约作者授权原创发布,未经授权不得转载,本文仅做学术分享,版权归原作者所有,若涉及侵权内容请联系删文
3D视觉开发者社区是由奥比中光给所有开发者打造的分享与交流平台,旨在将3D视觉技术开放给开发者。平台为开发者提供3D视觉领域免费课程、奥比中光独家资源与专业技术支持。
点击加入3D视觉开发者社区,和开发者们一起讨论分享吧~
也可移步微信关注官方公众号:3D视觉开发者社区 ,获取更多干货知识哦~