使用C ++从Raspberry Pi相机显示Raspberry Pi HDMI

时间:2017-03-31 20:54:57

标签: visual-studio-2015 raspberry-pi visualgdb

问题:如何将视频流从Raspberry Pi相机写入HDMI连接显示器?

答案:见下文

1 个答案:

答案 0 :(得分:1)

我的系统:Raspberry Pi3,Raspbian Jessie发行版 VisualGDB和VisualStudio 2015

花几个小时汇总来自不同帖子的各种测试代码,我发布这个编译的测试代码供社区使用。它的工作效率约为15fps,1200x720p彩色视频输出。

来源: https://raspberrypi.stackexchange.com/questions/7092/display-a-test-pattern-by-writing-the-frame-buffer-and-then-clear-it

https://visualgdb.com/tutorials/raspberry/camera/

注意: VisualGDB在Sysroot同步方面存在一些问题,当我从Visual Studio运行内置操作时,它复制了一堆大小为0的标题。相反,我手动将/opt/vc文件夹直接复制到C:\SysGCC\raspberry\arm-linux-gnueabihf\sysroot < / p>

我还必须将libbcm_host.so(以及链接教程中的libraspicam.so.0.1)从/opt/vc/lib复制到本地build/Debug/src文件夹,

=/opt/vc/include添加到includes(=表示本地sysroom目录) bcm_host到库名。

注意2:Raspbian发行版或编译器不喜欢while(1)循环,所以如果用for(...)替换while(1)循环来绘制没有任何退出条件的框架,你将获得输出黑屏。可能的原因是优化器,良好的做法是避免在没有退出条件的情况下拥有无限循环。

注意3:如果使用HDMI输出来监视分辨率低于1280x720(小于摄像机视频流),可能会出现一些问题,寻找有人编辑较小屏幕的代码。具有复合视频输出的旧版RPi也未经过测试。

谢谢,

#include <stdio.h>
#include <syslog.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include "interface\vmcs_host\vc_dispmanx_types.h"
#include <bcm_host.h>
#include "raspicam.h"
#include <iostream>


typedef struct
{
    DISPMANX_DISPLAY_HANDLE_T   display;
    DISPMANX_MODEINFO_T         info;
    void                        *image;
    DISPMANX_UPDATE_HANDLE_T    update;
    DISPMANX_RESOURCE_HANDLE_T  resource;
    DISPMANX_ELEMENT_HANDLE_T   element;
    uint32_t                    vc_image_ptr;

} RECT_VARS_T;

int main(int argc, char **argv) 
{
    RECT_VARS_T vars;
    VC_RECT_T src_rect;
    VC_RECT_T dst_rect;
    VC_DISPMANX_ALPHA_T alpha = { 
        static_cast<DISPMANX_FLAGS_ALPHA_T>(DISPMANX_FLAGS_ALPHA_FROM_SOURCE | DISPMANX_FLAGS_ALPHA_FIXED_ALL_PIXELS),
        255, /*alpha 0->255*/
        0
    };

    bcm_host_init();

    vars.display = vc_dispmanx_display_open(0);
    vc_dispmanx_display_get_info(vars.display, &vars.info);

    //Grab Camera feed
    raspicam::RaspiCam v_camera; //camera object
    //Open camera 
    std::cout << "Opening Camera..." << std::endl;
    if (!v_camera.open()) 
    {
        std::cerr << "Error opening camera" << std::endl; 
        return -1;
    }
    //may need to wait a while until camera stabilizes

    int cam_width = v_camera.getWidth();
    int cam_height = v_camera.getHeight();

    vars.image = calloc(1, cam_width * cam_height * 3);
    vars.resource = vc_dispmanx_resource_create( VC_IMAGE_RGB888,
        cam_width,
        cam_height,
        &vars.vc_image_ptr);

    vc_dispmanx_rect_set(&dst_rect, 0, 0, cam_width, cam_height);
    vars.update = vc_dispmanx_update_start(10);
    vars.element = vc_dispmanx_element_add( vars.update,
        vars.display,
        2000, // layer
        &dst_rect,
        vars.resource,
        &src_rect, //may not need this
        DISPMANX_PROTECTION_NONE,
        &alpha,
        NULL, // clamp
        static_cast<DISPMANX_TRANSFORM_T>(0));

    //Draw 50 frames to screen
    for (int i = 0; i < 50; i++)
    {    
        vc_dispmanx_resource_write_data( vars.resource,
            VC_IMAGE_RGB888,
            cam_width * 3,
            vars.image,
            &dst_rect);

        unsigned char* fbp = static_cast<unsigned char*>(vars.image);   
        v_camera.grab();
        v_camera.retrieve(fbp, raspicam::RASPICAM_FORMAT_RGB);//get camera image
        vc_dispmanx_update_submit_sync(vars.update); 
    }

    int ret = vc_dispmanx_resource_delete(vars.resource);
    vc_dispmanx_display_close(vars.display);

    return true;
}

如果要执行任何图像处理,只需使用循环内的fbp指针直接写入(例如,不是最有效的方法)

int location_cam = 0;
for (int x = 200; x < 300; x++)
{       
    for (int y = 200; y < 300; y++)
    {
        location_cam = (x) * (3) + (y) * cam_width * 3;
        *(fbp + location_cam) = 255;        //red
        *(fbp + location_cam + 1) = 0;     //green
        *(fbp + location_cam + 2) = 0;    //blue
    }
}