techyian / MMALSharp

C# wrapper to Broadcom's MMAL with an API to the Raspberry Pi camera.
MIT License
195 stars 33 forks source link

How to set camera number to capture from #197

Open mkj-stonehill opened 3 years ago

mkj-stonehill commented 3 years ago

I see (in the comments for MMALCameraComponent) that MMALSharp only supports one camera module. I am working on a project for a client that is based on the Pi Compute Module 3+, and they've connected two cameras up.

MMALSharp currently works great capturing images from camera 0. Now I need to be able to capture either from 0 or from 1. I'm at a loss on how to go about this. I see (in the constructor for MMALCameraComponent) where it selects camera 0. How (and where) would I go about selecting camera 1?

Note this is not a stereo thing; I need to capture a single image from either camera 0, or from camera 1 (never both at the same time). Do I need entirely separate image pipelines for each camera? Or can I just select which camera I need, and then start a capture?

Note that currently I can capture from either camera using raspistill (using the -cs argument), so I know the hardware is working. I just need to be able to select the camera in my application using MMALSharp.

techyian commented 3 years ago

Hi, I have very little time to invest in this project at the moment, but I believe you may be able to get around this by doing something like the following (note that the SetParameter method seen in MMALCameraComponent is internal, so this is bypassing that):

MMALCheck(MMALUtil.mmal_port_parameter_set_int32(port.Ptr, (uint)MMALParametersCamera.MMAL_PARAMETER_CAMERA_NUM, 1), "Unable to set int value");

camera.ConfigureCameraSettings();

I'm unsure what variable name you're using to store the MMALCamera instance, so please change that as appropriate. As said previously, I'm unsure whether this will work but that's the only thing I can think of currently which may help in the short term.

mkj-stonehill commented 3 years ago

Thanks for your time, Ian.

I tried this:

    // Select which camera to capture from
    unsafe
    {
        try
        {
            MMALNativeExceptionHelper.MMALCheck( MMALUtil.mmal_port_parameter_set_int32(Cam.Camera.Control.Ptr, MMALParametersCamera.MMAL_PARAMETER_CAMERA_NUM, 1), "Unable to set int value");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Unable to select camera");
            Console.WriteLine(ex.Message);
        }
    }

But I'm getting an invalid argument exception (from MMAL). I had to guess about the port pointer to pass; maybe that's where I went wrong? Seems hard to screw up the parameter ID or the camera number.

mkj-stonehill commented 3 years ago

Looking at the sources for MMAL, I see that it can emit log messages. Where do those appear? What do I have to do to enable them? That should give me some clue as to what MMAL doesn't like about my call...