BlackBerry Java to BlackBerry 10 Cascades Porting Series – Part 6: Camera

Native SDK Development

In BlackBerry OS 7 and 7.1, there were two ways to interact with the camera:

  1. Invoke the camera application and listen for the resulting file
  2. Use the javax.microedition.media API (and RIM extensions) to draw the camera inside your app, and capture images/videos there.

With BlackBerry 10, you can accomplish the first by invoking the camera card, and the second by using the Cascades Camera API.

Invoke the Camera Card

This is similar to invoking the Camera App in Java, but more powerful. It is built using the Invocation Framework and with the Camera card, it will seem as though it’s part of your app. You will also prevent the user from going further in your app until they are done with the camera. Use this if you did so before or just want to get a file handle to a photo or video and don’t want to worry about drawing the UI yourself.

Setup:

	_invokeManager = new InvokeManager(this);
	QObject::connect(_invokeManager,
			SIGNAL(childCardDone(const bb::system::CardDoneMessage&)), this,
			SLOT(onChildCardDone(const bb::system::CardDoneMessage&)));

Invoking:

	InvokeRequest invokeRequest;
	invokeRequest.setTarget("sys.camera.card");
	invokeRequest.setMimeType("image/jpeg");
	invokeRequest.setAction("bb.action.CAPTURE");
	invokeRequest.setData("photo");
	InvokeTargetReply *invokeReply = _invokeManager->invoke(invokeRequest);

Dealing with the result:

void CameraInvoker::onChildCardDone(const bb::system::CardDoneMessage
		&message){
	qDebug() << “File URI:” << message.data() << endl;
}

Cascades Camera API

This is comparable to what was offered in Java, though it has extra capabilities and is not tied into the Media Player classes. You can easily build a camera viewfinder into your application in pure QML/JavaScript, or C++, or a combination of the two. You can control most functions like focus region, file quality, and viewfinder size/position.

This API will draw the viewfinder wherever you put it on your screen. Similar to Java, it is up to you to create any UI interactions (like tapping to capture a picture). Unlike Java, you also need to play the capture sound yourself, which is required to be on sale in BlackBerry World in certain regions like Japan.

#include 
...
soundplayer_play_sound("event_camera_shutter");

To do Barcodes, connect a BarcodeDetector to a Camera control. You can also do your own detector (or something else) by processing the camera preview frames yourself.

C Camera Library

This is the low-level API that the Cascades API is built on top of. It is very powerful and allows for things that are not available in the Cascades API like burst capture, facial detection, and directly modifying the preview stream. This is far more advanced than the Java API and beyond the scope of this article.

It’s worth noting that the Cascades and C Camera APIs are not compatible. If you would like to use the C API in Cascades, you can draw a viewfinder with the C API and the Foreign Window Control.

About Paul Bernhardt

I work for BlackBerry on the Developer relations team helping developers write apps in Cascades. I focus on camera, gaming and Unity mostly. My views are my own. In my free time, I'm a hardcore PC gamer and draw D&D maps for fun!

Join the conversation

Show comments Hide comments
+ -
blog comments powered by Disqus