Having trouble getting the CameraDevice to work

#1

I’m having trouble getting the CameraDevice to work properly. When I try to run my code , it works without any errors , but the viewer component just keeps on having the loading icon. I would assume that the code that I have would access the camera and show in the viewer component what is currently in the camera but it just isn’t working. Maybe I am thinking this should work and it isn’t how it works. Given that there aren’t really any examples out there I have just been going off of what is in the documentation.

Here is the code , it’s nothing complicated :
MainComponent.h

#pragma once
#include <memory>
#include "../JuceLibraryCode/JuceHeader.h"


class camera_listener : public CameraDevice::Listener
{
private:
    int counter = 0;
public:
    camera_listener() : CameraDevice::Listener() {}
    virtual ~camera_listener() {}

    void imageReceived( Image const& image ) override {
    std::cout << "image received for the " << ++counter << "time \n";
}

};


class MainComponent   : public Component
{
public:
    //==============================================================================
    MainComponent();
    ~MainComponent();

    //==============================================================================
    void paint (Graphics&) override;
    void resized() override;

private:
    //==============================================================================
    std::unique_ptr<CameraDevice> camera;
    std::unique_ptr<Component> video_component;

    std::unique_ptr<File> video_file;
    std::unique_ptr<camera_listener> listener;

    TextButton test_button;
    TextButton test_button2;

    void permissions_granted( void );
    void start_recording( void );
    void stop_recording( void );

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
};

and MainComponent.cpp

#include "MainComponent.h"

//==============================================================================
MainComponent::MainComponent()
                : camera( CameraDevice::openDevice(0) )
                , video_component( camera->createViewerComponent() )
                , video_file( new File("~/test.mov") )
                , listener( new camera_listener() )
                , test_button("TEST1","test button")
                , test_button2("TEST2","test button")
{

    setSize (600, 400);
    addAndMakeVisible( video_component.get() );
    addAndMakeVisible( &test_button );
    addAndMakeVisible( &test_button2 );
//    camera->addListener( listener.get() );
    permissions_granted();

    test_button.onClick = [ this ] ( void ) {
//    start_recording();
};
    test_button2.onClick = [ this ] ( void ) {
//        stop_recording();
    };

}
MainComponent::~MainComponent()
{
    // intentionally blank
}

//==============================================================================
void MainComponent::paint (Graphics& g)
{
    g.fillAll (getLookAndFeel().findColour (ResizableWindow::backgroundColourId));
}

void MainComponent::resized()
{
    auto rect = this->getLocalBounds();
    auto top_section = rect.removeFromTop( rect.getHeight() / 2 );
    auto bottom_section = rect.removeFromBottom( rect.getHeight() / 2 );
    video_component->setBounds( top_section );
    test_button.setBounds( bottom_section.removeFromLeft( bottom_section.getWidth() / 2 ) );
    test_button2.setBounds( bottom_section.removeFromRight( bottom_section.getWidth() / 2 ) );
}

void MainComponent::permissions_granted( void )
{
    RuntimePermissions::request( RuntimePermissions::camera , [ this ] ( bool granted ) {
        if( granted ) {
            std::cout << "camera use granted \n";
        }
        else {
            std::cout << "camera use not granted \n";
        }
    });
    RuntimePermissions::request( RuntimePermissions::recordAudio , [ this ] ( bool granted ) {
        if( granted ) {
            std::cout << "audio recording granted \n";
        }
        else {
            std::cout << "audio recording not granted \n";
        }
    });
}

void MainComponent::start_recording( void )
{
    camera->startRecordingToFile( *video_file , 2 );
}

void MainComponent::stop_recording( void ) {
    camera->stopRecording();
}
0 Likes

#2

Or could someone just post CameraDevice code that has worked for them?

0 Likes

#3

Have you looked at the CameraDemo example in examples/GUI/CameraDemo.h?

0 Likes

#4

:man_facepalming: I wish I had someone with me to hit me upside the head right now because that should have been the first thing I did! So yeah , that’ll be the first thing I do tomorrow morning.

0 Likes