MFC下的OpenNI2环境的配置以及ColorPool彩色图像的输出

编辑丨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视觉开发者社区 ,获取更多干货知识哦~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值