Represents the system camera, which supports taking photos and recording videos.
Since: 1.0.4
Required permissions:
Declare ohos.permission.CAMERA and ohos.permission.MICROPHONE in the module.json5 file.
The scope.camera permission is required. In video recording scenarios, the scope.record permission is also required.
Related document: has.createCameraContext
Attributes
| Name | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
| resolution | string | medium | No | Resolution, which cannot be changed dynamically. - low: low. - medium: medium. - high: high. |
| device-position | string | back | No | Orientation of the camera. - back: rear camera. - front: front camera. |
| flash | string | auto | No | Flash. - auto: automatic. - on: enabled. - off: disabled. - torch: always on. |
| bindstop | eventhandle | - | No | Event triggered when the camera terminates abnormally, for example, exiting the background. |
| binderror | eventhandle | - | No | Event triggered when a camera is not allowed to use. |
| bindinitdone | eventhandle | - | No | Event triggered when the camera initialization is complete, with e.detail = {maxZoom}. |
Example:
HXML file:
<view>
<camera
resolution="medium"
flash="{{ enableflash }}"
device-position="{{ position }}"
bindstop="stop"
binderror="error"
bindinitdone="initdone"
/>
<button bind:tap="takePhoto">takePhoto</button>
</view> JavaScript file:
Page({
onReady: function() {
this.ctx = has.createCameraContext();
},
data: {
position: 'front',
enableflash: 'auto'
},
error(e) {
console.error('Camera access denied by the user:', e.detail);
},
stop(e) {
console.info ('Camera terminated abnormally:', e.detail);
},
initdone(e) {
console.info('Camera initialization completed:', e.detail);
},
takePhoto() {
this.ctx.takePhoto({
success: (res) => {
console.info('takePhoto success', res);
},
fail: (err) => {
console.error('takePhoto fail', err);
},
complete: (res) => {
console.info('takePhoto complete', res);
}
});
}
});