Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 41
Registered: ‎11-17-2013
My Device: Blackberry Simulator 10.2
My Carrier: Telcel

Switch between Rear and Front camera from C++

Hi, I'm working on a camera app,and I have a problem. which is that I can't switch correctly from rear to front camera and back to rear again.

 

I have already  accessed to the Camera component by the "objectName" property and I can change the resolution of the current camera.

 

Now I have been trying to switch the camera to the Front and back to the Rear like follows, the function "switchCamera" is called when a button is pressed.

 

Camera* camaraQML

ApplicationUI::ApplicationUI(bb::cascades::Application *app) {
//code camaraQML = root->findChild<bb::cascades::multimedia::Camera*>("camera");
//code
} void ApplicationUI::switchCamera() { camaraQML->stopViewfinder(); camaraQML->close(); if (camaraQML->cameraUnit() == CameraUnit::Rear) { camaraQML->open(CameraUnit::Front); } else if (camaraQML->cameraUnit() == CameraUnit::Front) { camaraQML->open(CameraUnit::Rear); } camaraQML->startViewfinder(); }

 

But the result that I'm getting is that randomly I can see the preview image from the Rear or Front cameras, but before that I have to try several times pressing my Switch button

 

I have also tryed the following code with the same results:

 

void ApplicationUI::switchCamera() {
	camaraQML->stopViewfinder();
	if (camaraQML->cameraUnit() == CameraUnit::Rear) {
		camaraQML->close();
		camaraQML->open(CameraUnit::Front);
	} else if (camaraQML->cameraUnit() == CameraUnit::Front) {
		camaraQML->close();
		camaraQML->open(CameraUnit::Rear);
	}
}

 

Almost the same, and with the same result: It doesn't work

 

The Component Definition is at a QML file, and I put some lines inside the "onCameraOpened" slot of my QML Camera object like follows in order to set the initial configurations of the camera

 

Camera { 
objectName: "camera"
id: camaraQML
//...
onCameraOpened: { getSettings(camaraSettings); camaraSettings.cameraMode = CameraMode.Photo; camaraSettings.shootingMode = CameraShootingMode.Normal; camaraSettings.sceneMode = CameraSceneMode.Action; if (camaraQML.cameraUnit == CameraUnit.Rear ) { camaraSettings.flashMode = CameraFlashMode.Off; camaraSettings.focusMode = CameraFocusMode.ContinuousAuto; } applySettings(camaraSettings); camaraQML.startViewfinder(); console.log("<<<<<Viewfinder Iniciado"); }
//...
attachedObjects: [
CameraSettings {
id: camaraSettings

]

//...
}

 

I feel like this last code is causing me throuble but I don't understand why and how to fix it. Hope you guys help me to find the best approach to switch the camera units from C++

 

Thanks in advance!!!

Developer
Posts: 825
Registered: ‎10-16-2012
My Device: Red Z10
My Carrier: Rogers

Re: Switch between Rear and Front camera from C++

[ Edited ]

In order to switch cameras you have to close the one that's currently open.

 

To do this I have a stopCamera function that i call from qml

 

void ApplicationUI::stopCamera()
{
	Camera *camera = bb::cascades::Application::instance()->findChild<Camera*>("camera");
	qDebug() << "Stopping camera";
	camera->stopViewfinder();

	camera->close();
}

 I use a variable to keep track of which camera is opened so that way once i close it i still know which camera was last opened this way I know which camera to switch to.

 

assuming the rear camera was to be opened

void ApplicationUI::startRearPhoto()
{
	Camera *camera = bb::cascades::Application::instance()->findChild<Camera*>("camera");
	qDebug() << "Starting Rear Photo";
	camera->open(CameraUnit::Rear);
	CameraSettings* cameraSettings = new CameraSettings();
	cameraSettings->setCameraMode(CameraMode::Photo);
	cameraSettings->setFocusMode(CameraFocusMode::ContinuousAuto);
	camera->applySettings(cameraSettings);
	camera->startViewfinder();

}
Developer
Posts: 41
Registered: ‎11-17-2013
My Device: Blackberry Simulator 10.2
My Carrier: Telcel

Re: Switch between Rear and Front camera from C++

[ Edited ]

Thanks for your fast answere

 

But I can't do as you say, I mean, I tryed but It seems that the viewfinder doesn't start or maybe the camera is never opened, because I see a black backgrownd but I can't see the image preview.

 

Here Is the definition of My Camera object in QML:

 

Camera {                
                objectName: "camera"
                id: camaraQML

                onTouch: {
                    if (event.isUp()) {               
                        capturePhoto();                       
                    }
                }                
                
                onCameraResourceAvailable: {
                    camaraQML.startViewfinder();
                }                       
}//Camera

 

When the app first starts, in the constructor, I have this code:

 

ApplicationUI::ApplicationUI(bb::cascades::Application *app) :
        QObject(app),
{
//...code
    // Create scene document from main.qml asset
    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
    qml->setContextProperty("_cam", this);

    // Create root object for the UI
    root = qml->createRootObject<AbstractPane>();

    //***Here is my attempt to open the rear camera, wich fails and causes a crash
    openRearCamera();

    // Set created root object as the application scene
    app->setScene(root);
//code...
} 

//And here is the openRearCamera method
void ApplicationUI::openRearCamera() {
    Camera* camera = bb::cascades::Application::instance()->findChild<bb::cascades::multimedia::Camera*>("camera");
    camera->open(CameraUnit::Rear);
    CameraSettings* cameraSettings =  new CameraSettings();
    cameraSettings->setCameraMode(CameraMode::Photo);
    cameraSettings->setFocusMode(CameraFocusMode::ContinuousAuto);
    cameraSettings->setShootingMode(CameraShootingMode::Normal);
    camera->applySettings(cameraSettings);
    camera->startViewfinder();
}

 

As I wrote in the comments, this is causing the app crashes when is launched so I really don't know what's going on, the output only says:

 

Process 35397871 (TestCam) terminated SIGSEGV code=1 fltno=11 ip=7843b09c(/base/usr/lib/libbbcascadesmultimedia.so.1.0.0@_ZN2bb8cascades10multimedia6Camera13applySettingsEPNS1_14CameraSettingsE+0x223) mapaddr=0003b09c. ref=00000014 bdslot=1

 

I don't know what does that mean, It seems to be a problem while applyting settings but I don't know where the problem is.

 

I will appreciate your help or anyone's

 

Thanks again!

Developer
Posts: 825
Registered: ‎10-16-2012
My Device: Red Z10
My Carrier: Rogers

Re: Switch between Rear and Front camera from C++

just a few guesses as to what may be the issue

 

try openRearCamera after setting the scene

app->setScene(root);
openRearCamera();

 

or it may be possible that you have not included everything neccesary to use camerasettings in your .cpp