×
Namespaces

Variants
Actions
Revision as of 07:21, 19 June 2012 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Archived:Example Camera in WidSets

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: 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.

Article Metadata
Article
Created: User:Mirsilla (26 May 2008)
Last edited: hamishwillee (19 Jun 2012)
FNWID.gif

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>
This page was last modified on 19 June 2012, at 07:21.
41 page views in the last 30 days.
×