Back

CameraView

Create a camera preview control using the CreateCameraView function of the app object:

 cam = app.CreateCameraView( width, height, options );

This control allows your program to show a live feed of what is currently being seen by the phone/tablet's camera. If your device has a front facing camera and is at least version 2.3 of Android you can use the "Front" option to show the front camera's preview.

You will need to call the StartPreview function of the CameraView control to start the preview (Note: On some phones it is neccessary to wait at least one second before calling the StartPreview function).

 cam.StartPreview();

The following example simply shows the front camera's preview in the center of the screen.

Example - Show camera preview

function OnStart()
{
  lay = app.CreateLayout( "Linear", "VCenter,FillXY" );

  cam = app.CreateCameraView( 0.8, 0.4 );
  lay.AddChild( cam );
  setTimeout( "cam.StartPreview()", 1000 );

  app.AddLayout( lay );
}

  Copy   Copy All    Run   

Use the TakePicture function to take a picture and save it to a given location.

 cam.TakePicture( filename );

Use the SetPictureSize function to set the dimensions in pixels of the pictures taken (eg. 1024 x 768).

 cam.SetPictureSize( width, height );

The following example shows how to take pictures and save them to the SD card.

Example - Take pictures

function OnStart()
{
  lay = app.CreateLayout( "Linear", "VCenter,FillXY" );

  cam = app.CreateCameraView( 0.8, 0.4 );
  lay.AddChild( cam );
  cam.SetPictureSize( 1024, 768 );
  setTimeout( "cam.StartPreview()", 1000 );

  btn = app.CreateButton( "Snap", 0.3, -1 );
  btn.SetOnTouch( Snap );
  lay.AddChild( btn );

  app.AddLayout( lay );
}

function Snap()
{
  cam.TakePicture( "/sdcard/MyPic.jpg" );
  app.ShowPopup("Picture saved");
}
  Copy   Copy All    Run   

For more sophisticated examples including motion detection and video streaming, check out the 'Camera Stream', 'Camera Snap' and 'Camera Motion' sample programs.

The following methods are avaiable on the CameraView object:

 SetVisibility( visibility )
 GetVisibility()
 SetMargins( left, top, right, bottom )
 SetPosition( left, top, width, height )
 SetSize( width, height )
 GetWidth()
 GetHeight()
 StartPreview()
 StopPreview()
 Focus()
 SetFocusMode( mode )
 SetPictureSize( width, height )
 GetPictureSizes()
 TakePicture( fileName )
 Stream( ip, port, quality, fps, mtu )
 SetFlash( onoff )
 SetSound( onoff )
 GetImageWidth()
 GetImageHeight()
 GetCameraCount()
 SetPreviewImage( imageObj )
 MotionMosaic( xtiles, ytiles, sensitivity, minPeriod, imageObj )
 AutoCapture( folder, fileName, maxCount )
 SetOnReady( callback )
 SetOnMotion( callback )
 SetOnPicture( callback )