Class “Camera”
Object > NativeObject > Camera
A Camera
provides access to the device’s camera. The Camera
can be used as a source for a CameraView
to display a live preview feed or to capture a photo.
In order to capture an image or to show a camera preview image the app has to have the 'camera'
permission.
Type: | Camera extends NativeObject |
Constructor: | private |
Singleton: | No |
Namespace: | tabris |
Direct subclasses: | None |
JSX Support: | No |
Examples
JavaScript
import {device} from 'tabris';
const camera = device.cameras[0];
camera.active = true;
camera.captureImage()
.then(({image}) => console.log(`Captured image with size ${image.size}.`));
See also:
TSX Simple example to capture an image
TSX Control Camera
and CameraView
to capture an image
JSX Check and request camera permissions
Methods
captureImage(options?)
Captures an image and returns a result object when the returned promise resolves successfully. The Camera
has to be in an active
state to capture an image. The result object has an image
property of type Blob
which contains the jpg encoded image, as well as a width
and height
property describing the dimensions of the captured image.
Parameter | Type | Description |
---|---|---|
options | { |
A set of capture options to apply when taking a picture. Optional. |
Returns: Promise<{image: Blob, width: number, height: number}>
Properties
active
Setting active
to true activates the camera. If it is currently assigned to a CameraView
, the CameraView
will now show the video feed from the Camera
. It is then possible to capture an image via the captureImage()
method.
Setting active
to false stops the camera and disables any video feed shown on a CameraView
.
It is recommended to stop the Camera
when not in use in order to preserve battery life.
Type: | boolean |
Default: | false |
Settable: | Yes |
Change Event: | activeChanged |
availableCaptureResolutions
An array of resolutions supported by the camera. Each array entry is an object consisting of width
and height
. Eg.: `{width: 4000, height: 3000}
Type: | Array<{width: number, height: number}> |
Settable: | No |
Change Event: | availableCaptureResolutionsChanged |
cameraId
The id of the device camera given by the native platform.
Type: | string |
Settable: | No |
Change Event: | Not supported |
captureResolution
An object determining the pixel dimensions of the captured image. Has to be an object containing width
and height
properties of type number
. The list of natively available resolutions can be obtained from the availableCaptureResolutions
property.
If the given captureResolution
is not in the list of availableCaptureResolutions
, a closely matching resolution larger than the given resolution is used. When no captureResolution
is given (value is null
), the best possible match for the device is chosen automatically. The physical dimensions of the captured image should be checked on the resolved result object of the captureImage()
method.
When setting the captureResolution
on the iOS platform, a small grace period should pass before capturing an image. Otherwise the image might turn out incorrectly exposed.
Type: | {width: number, height: number} |
Settable: | Yes |
Change Event: | captureResolutionChanged |
position
The position of the camera on the device. The external
position is used for devices like usb cameras.
Type: | 'front' |
Settable: | No |
Change Event: | Not supported |
Change Events
activeChanged
Fired when the active property has changed.
EventObject Type: PropertyChangedEvent<Camera, boolean>
Property | Type | Description |
---|---|---|
value | boolean |
The new value of active. |
availableCaptureResolutionsChanged
Fired when the availableCaptureResolutions property has changed.
EventObject Type: PropertyChangedEvent<Camera, Array>
Property | Type | Description |
---|---|---|
value | Array<{width: number, height: number}> |
The new value of availableCaptureResolutions. |
captureResolutionChanged
Fired when the captureResolution property has changed.
EventObject Type: PropertyChangedEvent<Camera, Object>
Property | Type | Description |
---|---|---|
value | {width: number, height: number} |
The new value of captureResolution. |