Adds a camera view into your app to take pictures and record videos.
The capture method returns the image data. To save the image in DroidScript, you can use the WriteFile method like this:
img = cam.capture("jpeg", "base64")
filePath = "image.jpeg"
app.WriteFile(filePath, img, "base64")
Likewise, the record method requires a callback to be called when the video data is ready. To save videos in DroidScript, you can use the WriteFile method like this:
cam.record("mp4", "base64", onRecord)
onRecord( data ) {
filePath = "video.mp4"
app.WriteFile(filePath, data, "base64")
}
Note: Don't forget to add permission to "Camera" and/or "Microphone" when you are taking videos.
These are the setter and getter properties for CameraView component.
Example - Camera app in DroidScript
cfg.Portrait
_AddPermissions("Camera")
class Main extends App
{
onStart()
{
this.main = ui.addLayout("main", "Frame", "FillXY")
this.cam = ui.addCameraView(this.main, "Fill", 1, 1)
this.layBtm = ui.addLayout(this.main, "Linear", "Horizontal,Vcenter", 1, 0.1)
this.layBtm.childSpacing = "Between"
this.layBtm.setPosition(0, 0.9)
this.layBtm.padding = [1, null, 1, null, "rem"]
this.toggleCam = ui.addButton(this.layBtm, "flip_camera_android", "icon")
this.toggleCam.textColor = "#fff"
this.toggleCam.setOnTouch( this.onSwitchCam )
this.snapBtn = ui.addButton(this.layBtm, "camera", "icon")
this.snapBtn.textColor = "#fff"
this.snapBtn.iconSize = "2.5rem"
this.snapBtn.setOnTouch( this.captureImg )
this.prevImg = ui.addImage(this.layBtm, "", "avatar,initial", null)
this.cam.previewImage = this.prevImg
this.prevImg.setSize(2.5, 2.5, "rem")
this.prevImg.cornerRadius = "50%"
this.prevImg.backColor = "black"
this.cam.startPreview()
}
onSwitchCam() {
let src = this.cam.source == "front" ? "back" : "front"
this.cam.source = src
this.cam.updatePreview()
}
captureImg() {
const img = this.cam.capture("jpg", "base64")
const name = "img-" + new Date().getTime() + ".jpg"
const path = "/Internal/DCIM/" + name
app.WriteFile(path, img, "base64")
ui.showPopup( "Image has been saved!" )
}
}
cfg.Portrait
_AddPermissions("Camera")
class Main extends App
onStart()
this.main = ui.addLayout("main", "Frame", "FillXY")
this.cam = ui.addCameraView(this.main, "Fill", 1, 1)
this.layBtm = ui.addLayout(this.main, "Linear", "Horizontal,Vcenter", 1, 0.1)
this.layBtm.childSpacing = "Between"
this.layBtm.setPosition(0, 0.9)
this.layBtm.padding = [1, null, 1, null, "rem"]
this.toggleCam = ui.addButton(this.layBtm, "flip_camera_android", "icon")
this.toggleCam.textColor = "#fff"
this.toggleCam.setOnTouch( this.onSwitchCam )
this.snapBtn = ui.addButton(this.layBtm, "camera", "icon")
this.snapBtn.textColor = "#fff"
this.snapBtn.iconSize = "2.5rem"
this.snapBtn.setOnTouch( this.captureImg )
this.prevImg = ui.addImage(this.layBtm, "", "avatar,initial", null)
this.cam.previewImage = this.prevImg
this.prevImg.setSize(2.5, 2.5, "rem")
this.prevImg.cornerRadius = "50%"
this.prevImg.backColor = "black"
this.cam.startPreview()
onSwitchCam()
src = this.cam.source == "front" ? "back" : "front"
this.cam.source = src
this.cam.updatePreview()
captureImg()
img = this.cam.capture("jpg", "base64")
name = "img-" + new Date().getTime() + ".jpg"
path = "/Internal/DCIM/" + name
app.WriteFile(path, img, "base64")
ui.showPopup( "Image has been saved!" )
Example - Video recorder in DroidScript
cfg.Portrait
_AddPermissions("Camera")
class Main extends App
{
onStart()
{
this.time = 0
this.main = ui.addLayout("main", "Absolute", "FillXY")
this.cam = ui.addCameraView(this.main, "Fill,Video", 1, 1)
this.timer = ui.addText(this.main, "", "Center,Caption", 1)
this.timer.textColor = "#fff"
this.timer.setPosition(0, 0.025)
this.layBtm = ui.addLayout(this.main, "Linear", "Horizontal", 1, 0.1)
this.layBtm.setPosition(0, 0.9)
this.vidBtn = ui.addButton(this.layBtm, "videocam", "icon")
this.vidBtn.textColor = "#fff"
this.vidBtn.iconSize = "2.5rem"
this.vidBtn.setOnTouch( this.handleRecord )
this.cam.startPreview()
}
handleRecord() {
if(this.vidBtn.icon == "videocam") {
this.cam.record("mp4", "base64", this.onRecord)
this.interval = setInterval(() => {
this.timer.text = (++ this.time) + " sec"
}, 1000)
}
else this.cam.stop()
this.vidBtn.icon = this.cam.recording ? "stop" : "videocam"
}
onRecord( data ) {
clearInterval( this.interval )
this.interval = null
this.timer.text = ""
this.time = 0
const name = "vid-" + new Date().getTime() + ".mp4"
const path = "/Internal/DCIM/" + name
app.WriteFile(path, data, "base64")
ui.showPopup( "Video has been saved!" )
}
}
cfg.Portrait
_AddPermissions("Camera")
class Main extends App
onStart()
this.time = 0
this.main = ui.addLayout("main", "Absolute", "FillXY")
this.cam = ui.addCameraView(this.main, "Fill,Video", 1, 1)
this.timer = ui.addText(this.main, "", "Center,Caption", 1)
this.timer.textColor = "#fff"
this.timer.setPosition(0, 0.025)
this.layBtm = ui.addLayout(this.main, "Linear", "Horizontal", 1, 0.1)
this.layBtm.setPosition(0, 0.9)
this.vidBtn = ui.addButton(this.layBtm, "videocam", "icon")
this.vidBtn.textColor = "#fff"
this.vidBtn.iconSize = "2.5rem"
this.vidBtn.setOnTouch( this.handleRecord )
this.cam.startPreview()
handleRecord()
if this.vidBtn.icon == "videocam":
this.cam.record("mp4", "base64", this.onRecord)
this.interval = setInterval(() =>
this.timer.text = (++ this.time) + " sec", 1000)
else: this.cam.stop()
this.vidBtn.icon = this.cam.recording ? "stop" : "videocam"
onRecord( data )
clearInterval( this.interval )
this.interval = null
this.timer.text = ""
this.time = 0
name = "vid-" + new Date().getTime() + ".mp4"
path = "/Internal/DCIM/" + name
app.WriteFile(path, data, "base64")
ui.showPopup( "Video has been saved!" )
Properties
The following properties are available on the CameraView object:
Methods
The following methods are available on the CameraView object:
Number: Fraction of the parent width [0-1].
Number: Fraction of the parent height [0-1].
Number: Starting pixel from the left.
Number: Starting pixel from the top.
Number: The width of the image portion in pixels.
Number: The height of the image portion in pixels.
Number: The image width in pixels. See `capabilities` for minimum and maximum values.
Number: The image height in pixels. See `capabilities` for minimum and maximum values.
String: “Comma separated options for the camera view. Available values are "Front"”, “ "Back"”, “ "Video"”, “ "Fill" and "Stretch". "Video" option includes audio.”
String: “The image type. Values are "jpg" or "jpeg""”, “ "png"”, “ "webp"”, “ "gif". Default is "jpg".”
String: “The returned data format. Can be "base64"”, “ "bytes"(regular array)”, “ "uint8array"(typed array) or "arraybuffer". Default is "base64"”
String: “Values are "rgba" array”, “ or "pngbase64" or "jpgbase64".”
String: “Video output encoding format. Values can be "mp4"”, “ "ogg" and "webm".Default is "mp4"”
String: “Return video data format. Values can be "blob"”, “ "base64"”, “ "bytes" (regular array)”, “ "objecturl"”, “ "arraybuffer"”, “ "uint8array" (typed array). Default is "base64"”
Object: The parent layout where to add the camera view.
function()
rvw.capabilities
Returns the capabilities of the camera. You can inspect this object to set camera constraints.
rvw.capture
Capture an image. This will return an image data which you can save or manipulate.
rvw.focusDistance
Sets or returns the focus distance. See capabilities for min and max values.
rvw.focusMode
Sets or returns the camera focus mode. Available values are "manual", "single-shot", "continuous"
rvw.frameRate
Sets or returns the video frame rate. See capabilites for min and max values.
rvw.getPixelData
Returns the pixel data of the image preview or a given portion of it.
rvw.imageHeight
Sets or returns the image height in pixels.
rvw.img
Sets the preview image taken on capture. Must be a UI image component.
rvw.iso
Sets or returns the camera iso. See capabilities for max and min values.
rvw.record
Record a video.
rvw.setOnError
Add a callback handler when an error occured.
rvw.setOnReady
Add a callback handler when the camera is ready.
rvw.setPictureSize
Sets the size of the image.
rvw.sound
Sets or returns a boolean value whether the sound is enabled or disabled
rvw.source
The camera source to be use. Values can be "front" or "back".
rvw.startPreview
Start streaming the camera.
rvw.stop
Stop recording a video.
rvw.updatePreview
Update the camera preview. Call it when you have updated the constraints of the camera such as setting the source from back to front, or setting zoom level etc.
rvw.width
Sets or returns the image width in pixels.