Use the Camera object to capture video from the client system or device camera. 


A Camera instance captures video in landscape aspect ratio. On devices that can change the screen orientation, such as mobile phones, the camera will only show upright video in a landscape-aspect orientation. Thus, mobile apps should use a landscape orientation when displaying video. Each video frame rendered to BitmapData object. You can applay transformation to this object.


On iOS, the video from the front camera is mirrored. On Android, it is not.



To get istance of Camera object use static method:

Lib.Media.Camera.getCamera(name)

Returns a reference to a Camera object for capturing video. Thus, if your code contains code like firstCam = Lib.Media.Camera.getCamera(nil) and secondCam = Lib.Media.Camera.getCamera(nil), both firstCam and secondCam reference the same camera, which is the user's default camera. On mobile devices with a both a front- and a rear-facing camera, you can only capture video from one camera at a time. In general, you can pass a nil value for the name parameter; simply use getCamera(nil) to return a reference to the default camera. 

Scanning the hardware for cameras takes time. When the runtime finds at least one camera, the hardware is not scanned again for the lifetime of the player instance. However, if the runtime doesn't find any cameras, it will scan each time getCamera is called. This is helpful if the camera is present but is disabled; if your code provides a Try Again button that calls getCamera, app can find the camera without the user having to restart the code.

name (default = nil) - string, specifies which camera to get, as determined from the table returned by the names property. For most applications, get the default camera by passing nil parameter. To specify a value for this parameter, use the name from index position within the Camera.names table.


Camera has static properties:

Lib.Media.Camera.names

An table of strings containing the names of all available cameras.

[read-only]


Created Camera object has properties:

bitmapData

Each video frame rendered to BitmapData object.

BitmapData object

name

The name of the current camera, as returned by the camera hardware.

[read-only], string

position

Specifies the side of a device on which the camera is located.

[read-only], string from CameraPosition object

width

The current capture width, in pixels.

[read-only]

height

The current capture height, in pixels.

[read-only]


Created Camera object has methods:

activate()

Activate camera, starts processing video frames


destroy()

Destroy camera, stops processing video frames and destroy camera object. If you want use the same camera initiate getCamera() method again.



Events:

Lib.Media.Events.Event.VIDEO_FRAME

Dispatched after a new camera frame is processed by the runtime and is available to be copied.



Examples:


-- Main.lua


Event = Lib.Media.Events.Event

Display = Lib.Media.Display

BitmapData = Display.BitmapData

Bitmap = Display.Bitmap

Geom = Lib.Media.Geom

stage = Display.stage

Camera = Lib.Media.Camera


bitmap = nil


function setBmpSize()

  if bitmap ~= nil then

    local sw = stage.stageWidth

    local sh = stage.stageHeight

    local w = bitmap.bitmapData.width

    local h = bitmap.bitmapData.height

    if w*sh > h*sw then

      bitmap.width = sw

      sh = h*sw/w

      bitmap.height = sh

      bitmap.x = 0

      bitmap.y = (stage.stageHeight-sh)*0.5

    else

      bitmap.height = sh

      sw = w*sh/h

      bitmap.width = sw

      bitmap.y = 0

      bitmap.x = (stage.stageWidth-sw)*0.5

    end

  end

end


function onFrame(e)

  print("onFrame! "..camera.width.."x"..camera.height)

  if camera ~= nil then

    camera.removeEventListener(Event.VIDEO_FRAME,onFrame,false)

    bitmap = Bitmap.new( camera.bitmapData, nil, true )

    stage.addChild(bitmap)

    setBmpSize()

  end

end


print("Camera devices:")

names = Camera.names

if names ~= nil then

  for i = 1, #names do

    camera = Camera.getCamera(names[i]) 

    print("camera: "..camera.name.." ["..camera.width.."x"..camera.height.."] position: "..camera.position)

    camera.destroy()--not all devicess can open multiple cameras, destroy previously created before you can get next camera info

  end


  camera = Camera.getCamera(nil) --get default camera 

  if camera ~= nil then

    print("Found default camera: "..camera.name.." ["..camera.width.."x"..camera.height.."] position: "..camera.position)

    camera.addEventListener(Event.VIDEO_FRAME,onFrame,false,0,false)

    stage.addEventListener(Event.RESIZE, function(e) setBmpSize() end, false, 0, false)

    camera.activate()

  end

else

  print("not found")

end


Created with the Personal Edition of HelpNDoc: Full-featured Kindle eBooks generator