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
BlackBerry Development Advisor
smcveigh
Posts: 668
Registered: ‎11-29-2011
My Device: developer

Re: set aspect ratio for the Camera custom control

okay, as I mentioned earlier, you have to:

1. open a camera

2. read supported resolutions

3. read current settings

4. modify settings (eg. resolution)

5. write modified settings

6. start the viewfinder

 

and here is the code I just wrote to test this.  I can get a 16:9 viewfinder without any issue.

 

// Default empty project template
#include "Qmlcamera.hpp"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/cascades/DockLayout>
#include <bb/cascades/Container>
#include <bb/cascades/Page>
#include <bb/cascades/multimedia/Camera>
#include <bb/cascades/multimedia/CameraSettings.hpp>
#include <bb/cascades/multimedia/CameraTypes.hpp>

using namespace bb::cascades;
using namespace bb::cascades::multimedia;

#define DELTA(x, y) (x>y?(x-y):(y-x))

// install a custom handler to log to stderr.
static void log_to_stderr(QtMsgType msgType, const char *msg)
{
    (void)msgType;  // go away, warning!
    fprintf(stderr, "%s\n", msg);
}

Qmlcamera::Qmlcamera(bb::cascades::Application *app)
: QObject(app)
{
    qInstallMsgHandler(log_to_stderr);

    mCamera = new Camera();
    mCameraSettings = new CameraSettings();

    Container* container = Container::create()
        .layout(DockLayout::create())
        .add(mCamera);

    QObject::connect(mCamera, SIGNAL(cameraOpened()), this,
                     SLOT(onCameraOpened()));
    app->setScene(Page::create().content(container));

    mCamera->open(CameraUnit::Front);
}


void Qmlcamera::onCameraOpened()
{
    mCamera->getSettings(mCameraSettings);
    QVariantList reslist = mCamera->supportedCaptureResolutions(CameraMode::Photo);
    for (int i=0; i<reslist.count(); i++) {
        QSize res = reslist[i].toSize();
        qDebug() << "supported resolution: " << res.width() << "x" << res.height();
        // check for 16:9 or 9:16 within some margin of error...
        if ((DELTA(res.width() * 9, res.height() * 16) < 5) ||
            (DELTA(res.width() * 16, res.height() * 9) < 5)) {
            qDebug() << "picking resolution: " << res.width() << "x" << res.height();
            mCameraSettings->setCaptureResolution(res);
        }
    }
    mCamera->applySettings(mCameraSettings);
    mCamera->startViewfinder();
}

 

Developer
slashkyle
Posts: 820
Registered: ‎10-16-2012
My Device: Red Z10

Re: set aspect ratio for the Camera custom control

[ Edited ]

the barcode scanner apps camera was written in C++ as well, not in QML which is the language that is displaying the black bars below & to the sides of the camera viewfinder

BlackBerry Development Advisor
smcveigh
Posts: 668
Registered: ‎11-29-2011
My Device: developer

Re: set aspect ratio for the Camera custom control

[ Edited ]

Okay, I just prototyped this solution to the PhotoBomber QML app.

I am not terribly proficient in QML, so I have not yet figured how to convert the QVariantList returned by camera.supportedCaptureResolutions() into a series of (width, height) values I can work with, so I just wrote a C++ stub which is invokable from the QML to do this for me.

 

It looks like this in the header file:

    Q_INVOKABLE
    void selectAspectRatio(bb::cascades::multimedia::Camera *camera, const float aspect);

 And the implementation looks like this:

void PhotoBomberApp::selectAspectRatio(bb::cascades::multimedia::Camera *camera, const float aspect)
{
#define DELTA(x, y) (x>y?(x-y):(y-x))
    CameraSettings camsettings;
    camera->getSettings(&camsettings);
    QVariantList reslist = camera->supportedCaptureResolutions(CameraMode::Photo);
    for (int i=0; i<reslist.count(); i++) {
        QSize res = reslist[i].toSize();
        qDebug() << "supported resolution: " << res.width() << "x" << res.height();
        // check for w:h or h:w within 5px margin of error...
        if ((DELTA(res.width() * aspect, res.height()) < 5) ||
            (DELTA(res.width(), res.height() * aspect) < 5)) {
            qDebug() << "picking resolution: " << res.width() << "x" << res.height();
            camsettings.setCaptureResolution(res);
        }
    }
    camera->applySettings(&camsettings);
}

 

Note that this code will check for "aspect" and "1/aspect" within a margin of error of 5 pixels.

 

 

Now from QML, you can just invoke the function when the camera is opened:

                onCameraOpened: {
                    photoBomber.selectAspectRatio(camera, 16/9);
                    camera.startViewfinder();
                }

 

So for the folks not knowing how to get the aspect ratio set to 16:9 instead of 4:3... use this method (which unfortunately does require a bit of C++... but you probably had a bit anyways) until I can get a pure QML example sorted out.

 

Cheers,

Sean

 

Contributor
nmathew87
Posts: 20
Registered: ‎10-17-2012
My Device: BB10 Dev Alpha

Re: set aspect ratio for the Camera custom control

[ Edited ]

thank you !!! this is excellent