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
Gerry_BB
Posts: 38
Registered: ‎12-06-2011
My Carrier: Koodo
Accepted Solution

Camera does not work after screen dims

Hi,

 

I have an app that utilizes the camera in QML. It works fine, but if the screen dims while my app is open the camera no longer works till I restart the app.

 

Here is the entire camera qml page :

import bb.cascades 1.0
import bb.cascades.multimedia 1.0

/**
 * Most of this code was copied from:
 * https://developer.blackberry.com/cascades/documentation/design/camera/taking_a_photo_with_the_front_...
 */

Page {
    property string pictureLocation: ""
    function closeCamera() {
        createReportNavigationPane.cameraClosed(pictureLocation)
        pictureLocation = "";
        saveID.enabled = false;
        sheetCamera.close();
    }
    titleBar: TitleBar {
        acceptAction: ActionItem {
            title: qsTr("Save")
            id: saveID
            enabled: false
            //title: "Save"
            onTriggered: {
                closeCamera();
            }
        }

        // The 'Cancel' action
        dismissAction: ActionItem {
            title: "Cancel"
            onTriggered: {
                pictureLocation = "";
                closeCamera();
            }
        }
    }
    onCreationCompleted: {
        // Check to see if any cameras are currently accessible.
        if (camera.allCamerasAccessible) {
            // Open the rear camera.
            camera.open(CameraUnit.Rear);
            camera.isOpen = true
        }
    }
    Container {
        layout: DockLayout {
        }

        // The Camera object.
        Camera {
            id: camera
            preferredWidth: 768
            preferredHeight: 1280
            objectName: "camera"
            property bool photoBeingTaken
            property bool isOpen: false
            onTouch: {
                if (event.isDown()) {
                    if (photoBeingTaken == false) {

                        // Take a photo, and automatically
                        // save it to the file system.
                        camera.capturePhoto();
                    }
                }
            }

            // The following are examples of slots that can
            // be used with the Camera object to handle
            // the various signals that are emitted.
            onCameraOpened: {
                // Once the camera is open, start the viewfinder.
                camera.startViewfinder();
            }
            onCameraOpenFailed: {
                console.debug("Camera cannot open");
            }
            onViewfinderStarted: {
                console.debug("Viewfinder has been started");
                photoBeingTaken = false;
            }
            onViewfinderStartFailed: {
                console.debug("Viewfinder could not be started");
            }
            onPhotoCaptureFailed: {
                console.debug("Photo could not be taken");
                photoBeingTaken = false;
            }
            onPhotoSaveFailed: {
                console.debug("Photo could not be saved");
                photoBeingTaken = false;
            }
            onPhotoSaved: {
                saveID.enabled = true;
                photoBeingTaken = true;
                console.debug("Photo has been successfully saved");
                photoBeingTaken = false;
                console.log(fileName);
                pictureLocation = fileName;
                var imageName = pictureLocation.substring(pictureLocation.lastIndexOf("/") + 1);
                var imagePath = "/accounts/1000/shared/camera/" + imageName;
                cameraPreview.imageSource = app.getCameraPreview(imageName, imagePath);
            }
        }
        Container {
            layout: StackLayout {
            }
            leftPadding: 10.0
            topPadding: 10.0
            Container {
                preferredHeight: 200
                preferredWidth: 200
                horizontalAlignment: HorizontalAlignment.Left
                Container {
                    verticalAlignment: VerticalAlignment.Center
                    ImageView {
                        id: cameraPreview
                        verticalAlignment: VerticalAlignment.Fill
                        horizontalAlignment: HorizontalAlignment.Fill
                        scalingMethod: ScalingMethod.Fill
                    }
                }
            }
        }
    }
}

 Thanks,

 

Gerry

Please use plain text.
BlackBerry Development Advisor
smcveigh
Posts: 651
Registered: ‎11-29-2011
My Carrier: other

Re: Camera does not work after screen dims

The camera hardware gets powered off when the screen is powered off.

I will ask the Cascades Camera API folks what they recommend doing.. I believe you would just need to restart the viewfinder.  It may not auto-restart for you.

 

Please use plain text.
Developer
slashkyle
Posts: 766
Registered: ‎10-16-2012
My Carrier: Telus

Re: Camera does not work after screen dims

it definately does not auto start :smileywink:... I've encountered this issue as well

Please use plain text.
BlackBerry Development Advisor
smcveigh
Posts: 651
Registered: ‎11-29-2011
My Carrier: other

Re: Camera does not work after screen dims

so, there is a cameraResourceReleased signal which you can listen for which will tell you that the camera has been stopped.

After this happens, you can restart the viewfinder after your app returns to the foreground.

 

We have a corner case that we're still working on which centers around the case where the screen starts to shut-down (causing the camera to stop), but the user wakes it up before navigator tells your app to background.  In this case, you may not receive a foreground event.  You can probably work around that case by starting a timer and checking after a second or so whether the app was removed from the foreground or not.. if it is still in the foreground, then you could try restarting the viewfinder

 

Cheers,

Sean

Please use plain text.