Back

CreateCameraView

Hello World
Content:
- Methods

CameraViews are used to access the device camera.

cam = app.CreateCameraView( width, height, options ) → app object: CameraView

You can use the “Front” option to show the front camera instead of the default back camera.

The camera needs some time to initialize first.
You can define a callback function via SetOnReady which is called if the camera can be used.
There you have to to call the StartPreview function of the CameraView control to start the preview.

Example - Show Camera Preview

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

    cam = app.CreateCameraView( 0.8, 0.4 );
    cam.SetOnReady( cam_OnReady );
    lay.AddChild( cam );


    app.AddLayout( lay );
}

function cam_OnReady() {
cam.StartPreview();
}
    Copy     Copy All       Run      

The basic functionality of the camera control is taking pictures or recording video.

Example - Take pictures

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

    cam = app.CreateCameraView( 0.8, 0.4 );
    cam.SetOnReady( cam_OnReady );
    lay.AddChild( cam );


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

    app.AddLayout( lay );
}

function cam_OnReady() {
    cam.SetPictureSize( 1024, 768 );
    cam.StartPreview();
}

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

Example - Record Video

function OnStart()
{
    app.SetOrientation("Landscape");

    lay = app.CreateLayout( "Linear", "Horizontal,FillXY,VCenter" );

    cam = app.CreateCameraView( .9, 1 );
    cam.SetOnReady( cam_OnReady );
    lay.AddChild( cam );

    tgl = app.CreateToggle( "Rec", 0.1 );
    tgl.SetOnTouch( Record );
    lay.AddChild( tgl );

    app.AddLayout( lay );

    recFolder = "/sdcard/Videos";
    app.MakeFolder( recFolder );
}

function cam_OnReady()
{
    cam.SetFocusMode( "Video" );
    cam.StartPreview();
}

function Record( start )
{
    if( start )
    {
        cam.Record( recFolder + "/test.mp4" );
        app.ShowPopup("Recording");
    }
    else
    {
        cam.Stop();
        app.ShowPopup("Saved to " + recFolder + "/test.mp4");
    }
}
Copy All       Run      

But it provides advanced image processing functions as well. These are color reporting, motion detection and face recognision.

Example - Motion Detector

var sensitivity = 10; // motion threshold
var minPeriod = 500; // millisecs
var snapFolder = "/sdcard/Snaps";

function OnStart()
{
    app.SetOrientation( "Landscape" );

    lay = app.CreateLayout( "linear", "fillxy,VCenter" );

    layCam = app.CreateLayout( "Frame" );
    lay.AddChild( layCam );

    cam = app.CreateCameraView( 1, 1, "front" );
    cam.SetOnReady( StartDetection );
    layCam.AddChild( cam );

    app.AddLayout( lay );
}

function StartDetection()
{
    var w = cam.GetImageWidth();
    var h = cam.GetImageHeight();

    img = app.CreateImage( null, 1, 1, "Fix", w, h );
    img.SetAlpha( 0.5 );
    layCam.AddChild( img );

    cam.MotionMosaic( 3, 3, sensitivity, minPeriod, img );
    cam.StartPreview();
}
Copy All       Run      

More samples can be found in the samples section of DroidScript.

Methods

The following methods are available on the CameraView object:

FindFaces( maxCount ) → object: { confidence, eyeDistance, midPoint:{ x, y}, pose }
Focus()
GetAbsHeight() → number: integer
GetAbsWidth() → number: integer
GetCameraCount() → number: integer
GetColorEffects() → string: “none”, “mono”, “negative”, “solarize”, “sepia”, “positionterize”, “whiteboard”, “blackboard”, “aqua”, “vage-cold”, “point-blue”, “point-red-yellow”, “emboss”, “sketch”, “neon”
GetHeight( options ) → number
GetImageHeight() → number: fraction (0..1)
GetImageWidth() → number: fraction (0..1)
GetLeft( options ) → number
GetMaxZoom() → number
GetParameters() → string: semicolon “;” separated: “key1=value1;key2=value2;...”
GetParent() → app object
GetPictureSizes() → string: comma “,” separated: “w1 x h1,w2 x h2,...”
GetPixelData( format, left, top, width, height ) → string: base64 encoded
GetPosition( options ) → object: { left, top, right, bottom }
GetTop( options ) → number
GetType() → string: “CameraView”
GetVisibility() → string: “Show” or “Hide” or “Gone”
GetWidth( options ) → number
GetZoom() → number
Gone()
HasFlash() → boolean
Hide()
IsEnabled() → boolean
IsOverlap( obj, depth ) → boolean
IsRecording() → boolean
IsVisible() → boolean
Method( name, types, p1, p2, p3, p4 ) → all types
SetScale( x, y )
Show()
Stop()
boolean
app object
number
object
string
unknown
number: angle in degrees (0..360)
number: factor
number: frames per second
number: fraction (0..1)
number: integer
number: milliseconds
number: maximum transmission unit
number: percent
number: pixel
number: seconds
number: -180..180
number: -100..100
number: 0..100
number: 0..0.99 or 1..256
number: angle in degrees (0..360): 0 or 90 or 180 or 270
string:
  hexadecimal: “#rrggbb”, “#aarrggbb”
  colourName: “red”, “green”, ...
string: comma “,” separated
string: path to file or folder ( “/absolute/...” or “relative/...” )
string: comma “,” separated: “Front”, “UseBitmap”, “NoRotate”, “CIF” or “QVGA” or “SVGA” or “VGA” or “XGA” or “UXGA”
string: “File_n#_motion”
string: “px”
string: “rawbase64” or “pngbase64” or “jpgbase64”
string: “screen”, “px”
string: pipe “|” separated: x1,y1|x2,y2|...”
string: “left-right” or “right-left” or “top-bottom” or “bottom-top” or “bl-tr” or “br-tl” or “tl-br” or “tr-bl”
string: “repeat”
string: “none” or “mono” or “negative” or “solarize” or “sepia” or “positionterize” or “whiteboard” or “blackboard” or “aqua” or “vage-cold” or “point-blue” or “point-red-yellow” or “emboss” or “sketch” or “neon”
string: “Add” or “Multiply” or “clear” or “darken” or “lighten” or “overlay” or “screen” or “xor” or “src_in” or “src_out” or “src_atop” or “src_over” or “dst_in” or “dst_out” or “dst_atop” or “dst_over”
string: “Auto” or Picture or Video or “Macro” or EDOF or “Fixed” or “Infinity”
string: “px” or “sp” or “dip” or “mm” or “pt”
string: “px” or “sp” or “dip” or “dp” or “mm” or “pt”
string: “Show” or “Hide” or “Gone”
string: “Linear.None” or “Quadratic.In/Out” or “Cubic.In/Out” or “Quartic.In/Out” or “Quintic.In/Out” or “Sinusoidal.In/Out” or “Exponential.In/Out” or “Circular.In/Out” or “Elastic.In/Out” or “Back.In/Out” or “Bounce.In/Out”
number
string
object: { x, y, w, w, sw, sh, rot }
app object: Image
list: boolean,char,byte,short,int,long,float,double,String,CharSequence,...
list: [[r1,g1,b1], [r2,g2,b2], ...]
function( data )
function()
function( file )
cam.AdjustColor
Adjust the visual color effect of the control by setting the Hue (by angle in degrees in a color circle), the saturation, brightness and contrast of the control.
cam.AutoCapture
Automatically takes pictures if a motion was detected by cam.MotionMosaic.
The first # in the filename is a placeholder for numbers increasing for every taken picture.
cam.ClearFocus
Removes the focus of the control so that the user no longer has immediate access to it.
cam.FindFaces
Finds faces in the current camera view.
cam.Focus
Set the focus to the control so that the user can interact with it immediately.
cam.GetAbsHeight
Get the absolute height of the control in pixels.
cam.GetAbsWidth
Get the absolute width of the control in pixels.
cam.GetCameraCount
Returns the amount of cameras built in to the device.
cam.GetColorEffects
Returns a list of available color effects for the camera.
cam.GetHeight
Get the height of the control as screen height relative float or in pixels with the px option.
cam.GetImageHeight
Returns the camera capture height in pixels.
cam.GetImageWidth
Returns the camera capture width in pixels.
cam.GetLeft
Get the distance from the control to the left parent border as width relative float or in pixels with the px option.
cam.GetMaxZoom
Returns the hightst possible zoom value.
cam.GetParameters
Get properties, possible values and settings of the camera as “key=value; pairs.
cam.GetParent
Returns the parent control object where the object was added to - commonly a layout.
cam.GetPictureSizes
Returns a list of possible picture dimensions.
cam.GetPixelData
Returns the current camera's raw, png or jpg image datas encoded as base64.
cam.GetPosition
Returns data about the position and size of the control.
If the screen option is given the position on the screen will be returned. Otherwise relative to the parent control.
The px options turns the relative values into pixels.
cam.GetTop
Get the distance from the control to the upper parent border as height relative float or in pixels with the px option.
cam.GetType
Returns the control class name.
cam.GetVisibility
Returns the current visibility state of the control. The Values are:
Show: visible
Hide: invisible but still consuming space
Gone: invisible and not consuming space
cam.GetWidth
Get the width of the control as screen width relative float or in pixels with the px option.
cam.GetZoom
Returns the curent zoom value.
cam.Gone
Hides the control without consuming any more layout space as if it were never there.
cam.HasFlash
Checks if the currently used camera has a camera flash.
cam.Hide
Hide the control but keep the layout space free.
cam.IsEnabled
Returns whether the control is currently useable by the user.
cam.IsOverlap
Returns whether the control overlaps with another by a given distance.
cam.IsRecording
Returns a boolea indicating whether the camera is currently recording a video.
cam.IsVisible
Returns whether the control is currently visible to the user, ignoring overlaying controls.
cam.Method
Allows access to other functions defined on the object in Java via reflection.

Note: This function is a premium feature. Please consider subscribing to Premium to use this feature and support DroidScript in its further development.
cam.MotionMosaic
Defines a raster which is used for motion detection.
Use AutoCapture to take pictures if a motion was detected.
cam.Record
Record a video to a given file. If seconds is specified, the recording will automatically stop after this time. Otherwise call cam.Stop().
cam.ReportColors
Keeps tracks of the average color around a given point.
cam.SetBackAlpha
Set the transparency of the background by an alpha value between 0 (no transparency) and 0.99 (full transparent) or 1 (no transparency) and 256 (full transparent)
cam.SetBackColor
Changes the background color of the control.
cam.SetBackGradient
Define the background color of the control with a gradient. The default gradient direction is from top to bottom, but you can change it from left to right and the reversed versions of course.
cam.SetBackGradientRadial
Define a radial color gradient for the background of control.
cam.SetBackground
Changes the background to an image which can be repeated using the repeat option.
An image which is often used with that option is '/res/drawable/pattern_carbon' - try it out!
cam.SetColorEffect
Applies a color effect to the camera.
cam.SetColorFilter
Adjust the visual color effect with a color and a given BlendMode. More information about BlendMode can be found in the Android Developer page.
cam.SetDuplicateImage
Define two CreateImage controls where the camera view is duplicated to. This method may be used for cardboart AR. Have a look at Daves forum post about that method.
cam.SetEnabled
En/Disable the control physically and visually so that the user can/can not access the control. Events like OnTouch will still be fired.
cam.SetFlash
En- or disables the camera flash if available.
cam.SetFocusMode
Change the cameras focus mode depending on the purpose.
cam.SetMargins
Define a distance to other controls on each side of the control.
cam.SetOnFocus
Define a callback function called when the user focuses the control.
cam.SetOnPicture
Defines a callback function which is called if cam.AutoCapture took a picture. The file parameter contains the path to the image file.
cam.SetOnReady
Defines a callback function which is called when the camera is ready for use.
cam.SetOrientation
Rotates the camera view.
cam.SetPadding
Define distances that elements within the control are to maintain from the control borders.
cam.SetParameter
Change property values of the camera.
See GetProperties to get a full list of available keys.
cam.SetPictureSize
Define a custom picture size in pixels.
cam.SetPosition
Defines the position and size for the control if the parent is an absolute layout.
cam.SetPostRotation
Rotates captured image in any desired angle in degrees.
cam.SetPreviewImage
Defines an image control which will show the captured picture automatically when taken.
cam.SetScale
Scales the control along with its contents by the factors passed to the function.
cam.SetSize
Change the size of the control in either screen relative values or in pixels if the px option was given.
cam.SetSound
En-/Disables the camera sound playing when taking pictures.
cam.SetVideoSize
Define a custom video recording size in pixels.
cam.SetVisibility
Change the visibility of the control to one of the available modes:
Show: visible
Hide: invisible but still consuming space
Gone: invisible and not consuming space
cam.SetZoom
Change the zoom value of the camera.
cam.Show
Set the visibility of the control to “Show”.
cam.StartPreview
Start the camera preview on the display.
cam.Stop
Stop recording a video.
cam.StopPreview
Stop the camera preview on the display.
cam.Stream
Streams the camera view to a local ip.
cam.TakePicture
Takes a picture of the current view and saves it to a file.
cam.Tween
Performs an animation on the control.
The target object is for the position, size and rotation that the control has at the end of the animation.

The type specifies the behavior and the speed of the animation. Separated by a dot, you must also specify whether you want to apply this behavior to the beginning (In), end (Out), or to both (InOut) times of the animation.

With the amount of repeats you can control how many times you want to play the animation.

If you have jojo activated, the animation will alternate between forward and backward playback, so that if the repetition value is odd, the control will be at the start position again at the end of the animation.

Finally the callback function will be called after the animation has finished. Well, it's about time!