Archived:Example Camera in WidSets
Archived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.
The article is believed to be still valid for the original topic scope.
The article is believed to be still valid for the original topic scope.
Article Metadata
This example shows how to use the device camera to take pictures.
camera.he
class
{
const int CMD_BACK = 1;
const int CMD_CAPTURE = 2;
MenuItem BACK = new MenuItem(CMD_BACK, "Back");
MenuItem CAPTURE = new MenuItem(CMD_CAPTURE, "Capture");
List encTypes = new List()
.add("encoding=jpeg&width=320&height=240")
.add("encoding=jpeg");
Flow flow = null;
Camera camera = null;
void startWidget()
{
setMinimizedView(createMinimizedView("viewMini", getStyle("default")));
}
Shell openWidget()
{
camera = new Camera(getStyle("mini"));
if (!camera.isAvailable()) {
setBubble(null, "Camera not available");
return null;
}
flow = new Flow(getStyle("mini"));
camera.setFlags(VISIBLE|LINEFEED);
camera.setPreferredSize(-100,-100);
flow.add(camera);
return new Shell(flow);
}
//gets called when the widget's maximized view is closed
void closeWidget()
{
//remember to close camera if it's still shown
if (camera != null) {
camera.close();
camera = null;
}
}
MenuItem getSoftKey(Shell shell, Component focused, int key)
{
if (key == SOFTKEY_OK && camera != null) {
return CAPTURE;
} else if (key == SOFTKEY_BACK) {
return BACK;
}
return null;
}
void actionPerformed(Shell shell, Component source, int action)
{
if (action == CMD_BACK) {
popShell(shell);
} else if (action == CMD_CAPTURE) {
flow.clear();
//The encoding types are listed in
//encTypes. PNG recommended, as it is mandatory for MIDP
//devices, but most camera phones also support JPEG, which
//packs to a smaller image size.
ByteArray imageData = camera.capture(encTypes);
if (imageData == null) {
setBubble(null, "Image capturing failed");
}
camera.close();
camera = null;
Image image = getImage(imageData);
Picture picture = new Picture(getStyle("default"), image);
picture.setPreferredSize(-100, -100);
flow.add(picture);
shell.updateMenu();
flushScreen(true);
}
}
}
widget.xml
<?xml version="1.0" encoding="utf-8"?>
<widget spec_version="2.0">
<info>
<name>example_camera</name>
<version>1.0</version>
<author>example</author>
<clientversion>1.0</clientversion>
<shortdescription>Camera Example</shortdescription>
<longdescription>Camera Example</longdescription>
<tags>example camera</tags>
</info>
<parameters>
<parameter name="widgetname">Camera Example</parameter>
</parameters>
<resources>
<code src="camera.he"/>
<stylesheet>
mini {
background: solid white;
color: black;
align: vcenter hcenter;
}
</stylesheet>
</resources>
<layout minimizedheight="2em">
<view id="viewMini" class="mini">
<label class="mini">${widgetname}</label>
</view>
</layout>
</widget>


