diff --git a/zh-cn/application-dev/media/camera-performance-improvement.md b/zh-cn/application-dev/media/camera-performance-improvement.md index 2cd4d412cb255e1a4895064cc4014d9ebb120b95..65f21efecf9241c7eb2e07ef3d5b9d39298346ba 100644 --- a/zh-cn/application-dev/media/camera-performance-improvement.md +++ b/zh-cn/application-dev/media/camera-performance-improvement.md @@ -33,7 +33,7 @@ [各类Context的获取方式](../application-models/application-context-stage.md) -```js +```ts import camera from '@ohos.multimedia.camera'; import featureAbility from '@ohos.ability.featureAbility'; @@ -83,7 +83,7 @@ async function preview(context: featureAbility.Context, cameraInfo: camera.Camer ![](figures/quick-thumbnail-sequence-diagram.png) [各类Context的获取方式](../application-models/application-context-stage.md) -```js +```ts import camera from '@ohos.multimedia.camera'; import { BusinessError } from '@ohos.base'; import image from '@ohos.multimedia.image'; @@ -152,7 +152,7 @@ function showOrSavePicture(pixelMap: image.PixelMap): void { - **桌面应用** - ```js + ```ts import camera from '@ohos.multimedia.camera'; import { BusinessError } from '@ohos.base'; import featureAbility from '@ohos.ability.featureAbility'; @@ -174,7 +174,7 @@ function showOrSavePicture(pixelMap: image.PixelMap): void { 具体申请方式及校验方式,请参考[访问控制授权申请指导](../security/accesstoken-guidelines.md)。 - ```js + ```ts import camera from '@ohos.multimedia.camera'; import { BusinessError } from '@ohos.base'; import featureAbility from '@ohos.ability.featureAbility'; diff --git a/zh-cn/application-dev/reference/apis/js-apis-camera.md b/zh-cn/application-dev/reference/apis/js-apis-camera.md index 5dade7559397f5b961c4ac947664ff7e8ad98998..6f9b9d0eeb4db85b386815c852de512a08545026 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-camera.md +++ b/zh-cn/application-dev/reference/apis/js-apis-camera.md @@ -6,13 +6,13 @@ ## 导入模块 -```js +```ts import camera from '@ohos.multimedia.camera'; ``` ## camera.getCameraManager -getCameraManager(context: Context): CameraManager +getCameraManager(context: featureAbility.Context): CameraManager 获取相机管理器实例,同步返回结果。 @@ -41,12 +41,24 @@ getCameraManager(context: Context): CameraManager **示例:** -```js -let cameraManager = camera.getCameraManager(context); +```ts +import featureAbility from '@ohos.ability.featureAbility'; +import { BusinessError } from '@ohos.base'; + +function getCameraManager(context: featureAbility.Context): camera.CameraManager { + let cameraManager: camera.CameraManager; + try { + cameraManager = camera.getCameraManager(context); + } catch (error) { + let err = error as BusinessError; + console.error(`The getCameraManager call failed. error code: ${err.code}`); + } + return cameraManager; +} ``` ## camera.getModeManager -getModeManager(context: Context): ModeManager +getModeManager(context: featureAbility.Context): ModeManager 获取模式化管理器实例,同步返回结果。 @@ -79,8 +91,20 @@ getModeManager(context: Context): ModeManager **示例:** -```js -let modeManager = camera.getModeManager(context); +```ts +import featureAbility from '@ohos.ability.featureAbility'; +import { BusinessError } from '@ohos.base'; + +function getModeManager(context: featureAbility.Context): camera.ModeManager { + let modeManager: camera.ModeManager; + try { + modeManager = camera.getModeManager(context); + } catch (error) { + let err = error as BusinessError; + console.error(`The getModeManager call failed. error code: ${err.code}`); + } + return modeManager; +} ``` ## CameraStatus @@ -241,9 +265,19 @@ getSupportedCameras(): Array\ **示例:** -```js -let cameras = cameraManager.getSupportedCameras(); +```ts +import { BusinessError } from '@ohos.base'; +function getSupportedCameras(cameraManager: camera.CameraManager): Array { + let cameras: Array; + try { + cameras = cameraManager.getSupportedCameras(); + } catch (error) { + let err = error as BusinessError; + console.error(`The getSupportedCameras call failed. error code: ${err.code}`); + } + return cameras; +} ``` ### getSupportedOutputCapability @@ -268,11 +302,11 @@ getSupportedOutputCapability(camera:CameraDevice): CameraOutputCapability **示例:** -```js -let cameras = cameraManager.getSupportedCameras(); -let cameraDevice = cameras[0]; -let cameraOutputCapability = cameraManager.getSupportedOutputCapability(cameraDevice); - +```ts +function getSupportedOutputCapability(cameraDevice: camera.CameraDevice, cameraManager: camera.CameraManager): camera.CameraOutputCapability { + let cameraOutputCapability: camera.CameraOutputCapability = cameraManager.getSupportedOutputCapability(cameraDevice); + return cameraOutputCapability; +} ``` ### isCameraMuted @@ -293,8 +327,11 @@ isCameraMuted(): boolean **示例:** -```js -let ismuted = cameraManager.isCameraMuted(); +```ts +function isCameraMuted(cameraManager: camera.CameraManager): boolean { + let isMuted: boolean = cameraManager.isCameraMuted(); + return isMuted; +} ``` ### isCameraMuteSupported @@ -315,8 +352,11 @@ isCameraMuteSupported(): boolean **示例:** -```js -let ismutesuppotred = cameraManager.isCameraMuteSupported(); +```ts +function isCameraMuteSupported(cameraManager: camera.CameraManager): boolean { + let isMuteSupported: boolean = cameraManager.isCameraMuteSupported(); + return isMuteSupported; +} ``` ### muteCamera @@ -337,9 +377,11 @@ muteCamera(mute: boolean): void **示例:** -```js -let mute = true; -cameraManager.muteCamera(mute); +```ts +function muteCamera(cameraManager: camera.CameraManager): void { + let mute: boolean = true; + cameraManager.muteCamera(mute); +} ``` ### createCameraInput @@ -374,14 +416,19 @@ createCameraInput(camera: CameraDevice): CameraInput **示例:** -```js -let cameraDevice = cameras[0]; -let cameraInput; -try { - cameraInput = cameraManager.createCameraInput(cameraDevice); -} catch (error) { +```ts +import { BusinessError } from '@ohos.base'; + +function createCameraInput(cameraDevice: camera.CameraDevice, cameraManager: camera.CameraManager): camera.CameraInput { + let cameraInput: camera.CameraInput; + try { + cameraInput = cameraManager.createCameraInput(cameraDevice); + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.error(`The createCameraInput call failed. error code: ${err.code}`); + } + return cameraInput; } ``` @@ -418,16 +465,21 @@ createCameraInput(position: CameraPosition, type: CameraType): CameraInput **示例:** -```js -let cameraDevice = cameras[0]; -let position = cameraDevice.cameraPosition; -let type = cameraDevice.cameraType; -let cameraInput; -try { +```ts +import { BusinessError } from '@ohos.base'; + +function createCameraInput(cameraDevice: camera.CameraDevice, cameraManager: camera.CameraManager): camera.CameraInput { + let position: camera.CameraPosition = cameraDevice.cameraPosition; + let type: camera.CameraType = cameraDevice.cameraType; + let cameraInput: camera.CameraInput; + try { cameraInput = cameraManager.createCameraInput(position, type); -} catch (error) { + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.log(`The createCameraInput call failed. error code: ${err.code}`); + } + return cameraInput; } ``` @@ -462,14 +514,20 @@ createPreviewOutput(profile: Profile, surfaceId: string): PreviewOutput **示例:** -```js -let profile = cameraOutputCapability.previewProfiles[0]; -let previewOutput; -try { +```ts +import { BusinessError } from '@ohos.base'; + +function createPreviewOutput(cameraOutputCapability: camera.CameraOutputCapability, cameraManager: camera.CameraManager, surfaceId: string): camera.PreviewOutput { + let profile: camera.Profile = cameraOutputCapability.previewProfiles[0]; + let previewOutput: camera.PreviewOutput; + try { previewOutput = cameraManager.createPreviewOutput(profile, surfaceId); -} catch (error) { + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.log(`The createPreviewOutput call failed. error code: ${err.code}`); + } + return previewOutput; } ``` @@ -504,14 +562,20 @@ createPhotoOutput(profile: Profile, surfaceId: string): PhotoOutput **示例:** -```js -let profile = cameraOutputCapability.photoProfiles[0]; -let photoOutput; -try { +```ts +import { BusinessError } from '@ohos.base'; + +function createPhotoOutput(cameraOutputCapability: camera.CameraOutputCapability, cameraManager: camera.CameraManager, surfaceId: string): camera.PhotoOutput { + let profile: camera.Profile = cameraOutputCapability.photoProfiles[0]; + let photoOutput: camera.PhotoOutput; + try { photoOutput = cameraManager.createPhotoOutput(profile, surfaceId); -} catch (error) { + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.log(`The createPhotoOutput call failed. error code: ${err.code}`); + } + return photoOutput; } ``` @@ -546,14 +610,20 @@ createVideoOutput(profile: VideoProfile, surfaceId: string): VideoOutput **示例:** -```js -let profile = cameraOutputCapability.videoProfiles[0]; -let videoOutput; -try { +```ts +import { BusinessError } from '@ohos.base'; + +function createVideoOutput(cameraOutputCapability: camera.CameraOutputCapability, cameraManager: camera.CameraManager, surfaceId: string): camera.VideoOutput { + let profile: camera.VideoProfile = cameraOutputCapability.videoProfiles[0]; + let videoOutput: camera.VideoOutput; + try { videoOutput = cameraManager.createVideoOutput(profile, surfaceId); -} catch (error) { + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.log(`The createPhotoOutput call failed. error code: ${err.code}`); + } + return videoOutput; } ``` @@ -587,14 +657,19 @@ createMetadataOutput(metadataObjectTypes:Array\): MetadataO **示例:** -```js -let metadataObjectTypes = cameraOutputCapability.supportedMetadataObjectTypes; -let metadataOutput; -try { +```ts +import { BusinessError } from '@ohos.base'; + +function createMetadataOutput(cameraManager: camera.CameraManager, cameraOutputCapability: camera.CameraOutputCapability): void { + let metadataObjectTypes: Array = cameraOutputCapability.supportedMetadataObjectTypes; + let metadataOutput: camera.MetadataOutput; + try { metadataOutput = cameraManager.createMetadataOutput(metadataObjectTypes); -} catch (error) { + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.log(`createMetadataOutput error. error code: ${err.code}`); + } } ``` @@ -622,13 +697,19 @@ createCaptureSession(): CaptureSession **示例:** -```js -let captureSession; -try { +```ts +import { BusinessError } from '@ohos.base'; + +function createCaptureSession(cameraManager: camera.CameraManager): camera.CaptureSession { + let captureSession: camera.CaptureSession; + try { captureSession = cameraManager.createCaptureSession(); -} catch (error) { + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.log(`createCaptureSession error. error code: ${err.code}`); + } + return captureSession; } ``` @@ -649,11 +730,15 @@ on(type: 'cameraStatus', callback: AsyncCallback\): void **示例:** -```js -cameraManager.on('cameraStatus', (err, cameraStatusInfo) => { +```ts +import { BusinessError } from '@ohos.base'; + +function registerCameraStatus(cameraManager: camera.CameraManager): void { + cameraManager.on('cameraStatus', (err: BusinessError, cameraStatusInfo: camera.CameraStatusInfo) => { console.log(`camera : ${cameraStatusInfo.camera.cameraId}`); console.log(`status: ${cameraStatusInfo.status}`); -}) + }); +} ``` ### off('cameraStatus') @@ -673,8 +758,10 @@ off(type: 'cameraStatus', callback?: AsyncCallback\): void **示例:** -```js -cameraManager.off('cameraStatus') +```ts +function unregisterCameraStatus(cameraManager: camera.CameraManager): void { + cameraManager.off('cameraStatus'); +} ``` ### on('cameraMute') @@ -696,10 +783,15 @@ on(type: 'cameraMute', callback: AsyncCallback\): void **示例:** -```js -cameraManager.on('cameraMute', (err, curMuetd) => { - let isMuted = curMuetd; -}) +```ts +import { BusinessError } from '@ohos.base'; + +function registerCameraMute(cameraManager: camera.CameraManager): void { + cameraManager.on('cameraMute', (err: BusinessError, curMuted: boolean) => { + let isMuted: boolean = curMuted; + console.log(`cameraMute status: ${isMuted}`); + }) +} ``` ### off('cameraMute') @@ -721,12 +813,17 @@ off(type: 'cameraMute', callback?: AsyncCallback\): void **示例:** -```js -func(err, curMuetd) { - let isMuted = curMuetd; +```ts +import { BusinessError } from '@ohos.base'; + +function callback(err: BusinessError, curMuetd: boolean): void { + let isMuted: boolean = curMuetd; +} + +function unregisterCameraMute(cameraManager: camera.CameraManager): void { + cameraManager.on('cameraMute', callback); + cameraManager.off('cameraMute', callback); } -cameraManager.on('cameraMute', func) -cameraManager.off('cameraMute', func) ``` ### isPrelaunchSupported @@ -761,11 +858,15 @@ isPrelaunchSupported(camera: CameraDevice): boolean **示例:** -```js -this.cameraManager = camera.getCameraManager(globalThis.abilityContext); -let cameras = this.cameraManager.getSupportedCameras() -if(this.cameraManager.isPrelaunchSupported(cameras[0])) { - this.cameraManager.setPrelaunchConfig({cameraDevice: cameras[0]}); +```ts +import featureAbility from '@ohos.ability.featureAbility'; + +function isPreLaunchSupported(context: featureAbility.Context): boolean { + let cameraManager: camera.CameraManager = camera.getCameraManager(context); + let cameras: Array = cameraManager.getSupportedCameras(); + let isSupported: boolean = cameraManager.isPrelaunchSupported(cameras[0]); + console.log(`PreLaunch supported states: ${isSupported}`); + return isSupported; } ``` @@ -796,15 +897,21 @@ setPrelaunchConfig(prelaunchConfig: PrelaunchConfig): void **示例:** -```js -this.cameraManager = camera.getCameraManager(globalThis.abilityContext); -let cameras = this.cameraManager.getSupportedCameras() -if(this.cameraManager.isPrelaunchSupported(cameras[0])) { +```ts +import featureAbility from '@ohos.ability.featureAbility'; +import { BusinessError } from '@ohos.base'; + +function setPrelaunchConfig(context: featureAbility.Context): void { + let cameraManager: camera.CameraManager = camera.getCameraManager(context); + let cameras: Array = cameraManager.getSupportedCameras(); + if(cameraManager.isPrelaunchSupported(cameras[0])) { try { - this.cameraManager.setPrelaunchConfig({cameraDevice: cameras[0]}); + cameraManager.setPrelaunchConfig({cameraDevice: cameras[0]}); } catch (error) { - console.error(`catch error: Code: ${error.code}, message: ${error.message}`); + let err = error as BusinessError; + console.error(`setPrelaunchConfig error. Code: ${err.code}, message: ${err.message}`); } + } } ``` @@ -820,12 +927,18 @@ prelaunch(): void **示例:** -```js -this.cameraManager = camera.getCameraManager(globalThis.abilityContext); -try { - this.cameraManager.prelaunch(); -} catch (error) { - console.error(`catch error: Code: ${error.code}, message: ${error.message}`); +```ts +import featureAbility from '@ohos.ability.featureAbility'; +import { BusinessError } from '@ohos.base'; + +function preLaunch(context: featureAbility.Context): void { + let cameraManager: camera.CameraManager = camera.getCameraManager(context); + try { + cameraManager.prelaunch(); + } catch (error) { + let err = error as BusinessError; + console.error(`prelaunch error. Code: ${error.code}, message: ${error.message}`); + } } ``` @@ -861,10 +974,10 @@ createDeferredPreviewOutput(profile: Profile): PreviewOutput **示例:** -```js -function getDeferredPreviewOutput(context: Context, previewProfile: camera.Profile): Promise { - const cameraManager = camera.getCameraManager(context); - const output: Promise = cameraManager.createDeferredPreviewOutput(previewProfile); +```ts +function getDeferredPreviewOutput(context: Context, previewProfile: camera.Profile): camera.PreviewOutput { + const cameraManager: camera.CameraManager = camera.getCameraManager(context); + const output: camera.PreviewOutput = cameraManager.createDeferredPreviewOutput(previewProfile); return output; } ``` @@ -896,9 +1009,11 @@ getSupportedModes(device: CameraDevice): Array\ **示例:** -```js -let cameraModes = cameraManager.getSupportedModes(cameraDevices[0]); - +```ts +function getSupportedModes(cameraManager: camera.CameraManager, cameraDevice: camera.CameraDevice): Array { + let cameraModes: Array = cameraManager.getSupportedModes(cameraDevice); + return cameraModes; +} ``` ### getSupportedOutputCapability @@ -926,13 +1041,21 @@ getSupportedOutputCapability(device:CameraDevice, mode: CameraMode): CameraOutpu **示例:** -```js -let cameras = cameraManager.getSupportedCameras(); -let cameraDevice = cameras[0]; -let cameraModes = modeManager.getSupportedModes(cameraDevice); -let mode = cameraModes[0] -let cameraOutputCapability = modeManager.getSupportedOutputCapability(cameraDevice, mode); - +```ts +function getSupportedOutputCapability(cameraManager: camera.CameraManager, modeManager: camera.ModeManager): camera.CameraOutputCapability { + let cameras: Array = cameraManager.getSupportedCameras(); + if (cameras == undefined || cameras.length <= 0) { + return; + } + let cameraDevice: camera.CameraDevice = cameras[0]; + let cameraModes: Array = modeManager.getSupportedModes(cameraDevice); + if (cameraModes === undefined || cameraModes.length <= 0) { + return; + } + let mode: camera.CameraMode = cameraModes[0]; + let cameraOutputCapability: camera.CameraOutputCapability = modeManager.getSupportedOutputCapability(cameraDevice, mode); + return cameraOutputCapability; +} ``` ### createCaptureSession @@ -958,13 +1081,29 @@ createCaptureSession(mode: CameraMode): CaptureSession **示例:** -```js -let cameras = cameraManager.getSupportedCameras(); -let cameraDevice = cameras[0]; -let cameraModes = modeManager.getSupportedModes(cameraDevice); -let mode = cameraModes[0] -let captureSession = modeManager.createCaptureSession(mode); - +```ts +import { BusinessError } from '@ohos.base'; + +function createCaptureSession(cameraManager: camera.CameraManager, modeManager: camera.ModeManager): camera.CaptureSession { + let cameras: Array = cameraManager.getSupportedCameras(); + if (cameras == undefined || cameras.length <= 0) { + return; + } + let cameraDevice: camera.CameraDevice = cameras[0]; + let cameraModes: Array = modeManager.getSupportedModes(cameraDevice); + if (cameraModes === undefined || cameraModes.length <= 0) { + return; + } + let mode: camera.CameraMode = cameraModes[0]; + let captureSession: camera.CaptureSession + try { + captureSession = modeManager.createCaptureSession(mode); + } catch (error) { + let err = error as BusinessError; + console.error(`The createCaptureSession call failed. error code: ${err.code}`); + } + return captureSession; +} ``` ## PrelaunchConfig @@ -1123,14 +1262,18 @@ open\(callback: AsyncCallback\\): void **示例:** -```js -cameraInput.open((err) => { +```ts +import { BusinessError } from '@ohos.base'; + +function openCameraInput(cameraInput: camera.CameraInput): void { + cameraInput.open((err: BusinessError) => { if (err) { - console.error(`Failed to open the camera. ${err.code}`); - return; + console.error(`Failed to open the camera. ${err.code}`); + return; } console.log('Callback returned with camera opened.'); -}) + }); +} ``` ### open @@ -1159,12 +1302,16 @@ open(): Promise\ **示例:** -```js -cameraInput.open().then(() => { +```ts +import { BusinessError } from '@ohos.base'; + +function openCameraInput(cameraInput: camera.CameraInput): void { + cameraInput.open().then(() => { console.log('Promise returned with camera opened.'); -}).catch((err) => { + }).catch((err: BusinessError) => { console.error(`Failed to open the camera. ${err.code}`); -}); + }); +} ``` ### close @@ -1191,14 +1338,18 @@ close\(callback: AsyncCallback\\): void **示例:** -```js -cameraInput.close((err) => { +```ts +import { BusinessError } from '@ohos.base'; + +function closeCameraInput(cameraInput: camera.CameraInput): void { + cameraInput.close((err: BusinessError) => { if (err) { - console.error(`Failed to close the cameras. ${err.code}`); - return; + console.error(`Failed to close the cameras. ${err.code}`); + return; } console.log('Callback returned with camera closed.'); -}) + }); +} ``` ### close @@ -1225,12 +1376,16 @@ close(): Promise\ **示例:** -```js -cameraInput.close().then(() => { +```ts +import { BusinessError } from '@ohos.base'; + +function closeCameraInput(cameraInput: camera.CameraInput): void { + cameraInput.close().then(() => { console.log('Promise returned with camera closed.'); -}).catch((err) => { + }).catch((err: BusinessError) => { console.error(`Failed to close the cameras. ${err.code}`); -}); + }); +} ``` ### on('error') @@ -1251,10 +1406,14 @@ on(type: 'error', camera:CameraDevice, callback: ErrorCallback): void **示例:** -```js -cameraInput.on('error', cameraDevice, (error) => { +```ts +import { BusinessError } from '@ohos.base'; + +function registerCameraInputError(cameraInput: camera.CameraInput, cameraDevice: camera.CameraDevice): void { + cameraInput.on('error', cameraDevice, (error: BusinessError) => { console.log(`Camera input error code: ${error.code}`); -}) + }); +} ``` ### off('error') @@ -1275,8 +1434,11 @@ off(type: 'error', camera:CameraDevice, callback?: ErrorCallback): void **示例:** -```js -cameraInput.off('error', cameraDevice) +```ts + +function unregisterCameraInputError(cameraInput: camera.CameraInput, cameraDevice: camera.CameraDevice): void { + cameraInput.off('error', cameraDevice); +} ``` ## FlashMode @@ -1371,12 +1533,17 @@ beginConfig(): void **示例:** -```js -try { +```ts +import { BusinessError } from '@ohos.base'; + +function beginConfig(captureSession: camera.CaptureSession): void { + try { captureSession.beginConfig(); -} catch (error) { + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.error(`The beginConfig call failed. error code: ${err.code}`); + } } ``` @@ -1405,14 +1572,18 @@ commitConfig(callback: AsyncCallback\): void **示例:** -```js -captureSession.commitConfig((err) => { +```ts +import { BusinessError } from '@ohos.base'; + +function commitConfig(captureSession: camera.CaptureSession): void { + captureSession.commitConfig((err: BusinessError) => { if (err) { - console.log('Failed to commitConfig '+ err.code); - return; + console.error(`The commitConfig call failed. error code: ${err.code}`); + return; } console.log('Callback invoked to indicate the commit config success.'); -}); + }); +} ``` ### commitConfig @@ -1440,13 +1611,17 @@ commitConfig(): Promise\ **示例:** -```js -captureSession.commitConfig().then(() => { +```ts +import { BusinessError } from '@ohos.base'; + +function commitConfig(captureSession: camera.CaptureSession): void { + captureSession.commitConfig().then(() => { console.log('Promise returned to indicate the commit config success.'); -}).catch((err) => { + }).catch((err: BusinessError) => { // 失败返回错误码error.code并处理 - console.log('Failed to commitConfig '+ err.code); -}); + console.error(`The commitConfig call failed. error code: ${err.code}`); + }); +} ``` ### addInput @@ -1480,12 +1655,17 @@ addInput(cameraInput: CameraInput): void **示例:** -```js -try { +```ts +import { BusinessError } from '@ohos.base'; + +function addInput(captureSession: camera.CaptureSession, cameraInput: camera.CameraInput): void { + try { captureSession.addInput(cameraInput); -} catch (error) { + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.log(`The addInput call failed. error code: ${err.code}`); + } } ``` @@ -1520,12 +1700,17 @@ removeInput(cameraInput: CameraInput): void **示例:** -```js -try { +```ts +import { BusinessError } from '@ohos.base'; + +function removeInput(captureSession: camera.CaptureSession, cameraInput: camera.CameraInput): void { + try { captureSession.removeInput(cameraInput); -} catch (error) { + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.log(`The removeInput call failed. error code: ${err.code}`); + } } ``` @@ -1560,12 +1745,17 @@ addOutput(cameraOutput: CameraOutput): void **示例:** -```js -try { +```ts +import { BusinessError } from '@ohos.base'; + +function addOutput(captureSession: camera.CaptureSession, cameraOutput: camera.CameraOutput): void { + try { captureSession.addOutput(cameraOutput); -} catch (error) { + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.log(`The addOutput call failed. error code: ${err.code}`); + } } ``` @@ -1600,12 +1790,17 @@ removeOutput(cameraOutput: CameraOutput): void **示例:** -```js -try { +```ts +import { BusinessError } from '@ohos.base'; + +function removeOutput(captureSession: camera.CaptureSession, previewOutput: camera.PreviewOutput): void { + try { captureSession.removeOutput(previewOutput); -} catch (error) { + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.log(`The removeOutput call failed. error code: ${err.code}`); + } } ``` @@ -1634,14 +1829,18 @@ start\(callback: AsyncCallback\\): void **示例:** -```js -captureSession.start((err) => { +```ts +import { BusinessError } from '@ohos.base'; + +function startCaptureSession(captureSession: camera.CaptureSession): void { + captureSession.start((err: BusinessError) => { if (err) { - console.error(`Failed to start the session ${err.code}`); - return; + console.error(`Failed to start the session ${err.code}`); + return; } console.log('Callback invoked to indicate the session start success.'); -}); + }); +} ``` ### start @@ -1669,12 +1868,16 @@ start\(\): Promise\ **示例:** -```js -captureSession.start().then(() => { +```ts +import { BusinessError } from '@ohos.base'; + +function startCaptureSession(captureSession: camera.CaptureSession): void { + captureSession.start().then(() => { console.log('Promise returned to indicate the session start success.'); -}).catch((err) => { + }).catch((err: BusinessError) => { console.error(`Failed to start the session ${err.code}`); -}); + }); +} ``` ### stop @@ -1701,14 +1904,18 @@ stop\(callback: AsyncCallback\\): void **示例:** -```js -captureSession.stop((err) => { +```ts +import { BusinessError } from '@ohos.base'; + +function stopCaptureSession(captureSession: camera.CaptureSession): void { + captureSession.stop((err: BusinessError) => { if (err) { - console.error(`Failed to stop the session ${err.code}`); - return; + console.error(`Failed to stop the session ${err.code}`); + return; } console.log('Callback invoked to indicate the session stop success.'); -}); + }); +} ``` ### stop @@ -1735,12 +1942,16 @@ stop(): Promise\ **示例:** -```js -captureSession.stop().then(() => { +```ts +import { BusinessError } from '@ohos.base'; + +function stopCaptureSession(captureSession: camera.CaptureSession): void { + captureSession.stop().then(() => { console.log('Promise returned to indicate the session stop success.'); -}).catch((err) => { + }).catch((err: BusinessError) => { console.error(`Failed to stop the session ${err.code}`); -}); + }); +} ``` ### release @@ -1767,14 +1978,18 @@ release\(callback: AsyncCallback\\): void **示例:** -```js -captureSession.release((err) => { +```ts +import { BusinessError } from '@ohos.base'; + +function releaseCaptureSession(captureSession: camera.CaptureSession): void { + captureSession.release((err: BusinessError) => { if (err) { - console.error(`Failed to release the CaptureSession instance ${err.code}`); - return; + console.error(`Failed to release the CaptureSession instance ${err.code}`); + return; } console.log('Callback invoked to indicate that the CaptureSession instance is released successfully.'); -}); + }); +} ``` ### release @@ -1801,12 +2016,16 @@ release(): Promise\ **示例:** -```js -captureSession.release().then(() => { +```ts +import { BusinessError } from '@ohos.base'; + +function releaseCaptureSession(captureSession: camera.CaptureSession): void { + captureSession.release().then(() => { console.log('Promise returned to indicate that the CaptureSession instance is released successfully.'); -}).catch((err) => { + }).catch((err: BusinessError) => { console.error(`Failed to release the CaptureSession instance ${err.code}`); -}); + }); +} ``` ### hasFlash @@ -1833,12 +2052,19 @@ hasFlash(): boolean **示例:** -```js -try { - let status = captureSession.hasFlash(); -} catch (error) { +```ts +import { BusinessError } from '@ohos.base'; + +function hasFlash(captureSession: camera.CaptureSession): boolean { + let status: boolean = false; + try { + status = captureSession.hasFlash(); + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.error(`The hasFlash call failed. error code: ${err.code}`); + } + return status; } ``` @@ -1872,12 +2098,19 @@ isFlashModeSupported(flashMode: FlashMode): boolean **示例:** -```js -try { - let status = captureSession.isFlashModeSupported(camera.FlashMode.FLASH_MODE_AUTO); -} catch (error) { +```ts +import { BusinessError } from '@ohos.base'; + +function isFlashModeSupported(captureSession: camera.CaptureSession): boolean { + let status: boolean = false; + try { + status = captureSession.isFlashModeSupported(camera.FlashMode.FLASH_MODE_AUTO); + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.error(`The isFlashModeSupported call failed. error code: ${err.code}`); + } + return status; } ``` @@ -1916,12 +2149,17 @@ setFlashMode(flashMode: FlashMode): void **示例:** -```js -try { +```ts +import { BusinessError } from '@ohos.base'; + +function setFlashMode(captureSession: camera.CaptureSession): void { + try { captureSession.setFlashMode(camera.FlashMode.FLASH_MODE_AUTO); -} catch (error) { + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.error(`The setFlashMode call failed. error code: ${err.code}`); + } } ``` @@ -1949,12 +2187,19 @@ getFlashMode(): FlashMode **示例:** -```js -try { - let flashMode = captureSession.getFlashMode(); -} catch (error) { +```ts +import { BusinessError } from '@ohos.base'; + +function getFlashMode(captureSession: camera.CaptureSession): camera.FlashMode { + let flashMode: camera.FlashMode; + try { + flashMode = captureSession.getFlashMode(); + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.error(`The getFlashMode call failed.error code: ${err.code}`); + } + return flashMode; } ``` @@ -1988,12 +2233,19 @@ isExposureModeSupported(aeMode: ExposureMode): boolean; **示例:** -```js -try { - let isSupported = captureSession.isExposureModeSupported(camera.ExposureMode.EXPOSURE_MODE_LOCKED); -} catch (error) { +```ts +import { BusinessError } from '@ohos.base'; + +function isExposureModeSupported(captureSession: camera.CaptureSession): boolean { + let isSupported: boolean; + try { + isSupported = captureSession.isExposureModeSupported(camera.ExposureMode.EXPOSURE_MODE_LOCKED); + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.error(`The isExposureModeSupported call failed. error code: ${err.code}`); + } + return isSupported; } ``` @@ -2021,12 +2273,19 @@ getExposureMode(): ExposureMode **示例:** -```js -try { - let exposureMode = captureSession.getExposureMode(); -} catch (error) { +```ts +import { BusinessError } from '@ohos.base'; + +function getExposureMode(captureSession: camera.CaptureSession): camera.ExposureMode { + let exposureMode: camera.ExposureMode; + try { + exposureMode = captureSession.getExposureMode(); + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.error(`The getExposureMode call failed. error code: ${err.code}`); + } + return exposureMode; } ``` @@ -2060,12 +2319,17 @@ setExposureMode(aeMode: ExposureMode): void **示例:** -```js -try { +```ts +import { BusinessError } from '@ohos.base'; + +function setExposureMode(captureSession: camera.CaptureSession): void { + try { captureSession.setExposureMode(camera.ExposureMode.EXPOSURE_MODE_LOCKED); -} catch (error) { + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.error(`The setExposureMode call failed. error code: ${err.code}`); + } } ``` @@ -2093,12 +2357,19 @@ getMeteringPoint(): Point **示例:** -```js -try { - let exposurePoint = captureSession.getMeteringPoint(); -} catch (error) { +```ts +import { BusinessError } from '@ohos.base'; + +function getMeteringPoint(captureSession: camera.CaptureSession): camera.Point { + let exposurePoint: camera.Point; + try { + exposurePoint = captureSession.getMeteringPoint(); + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.error(`The getMeteringPoint call failed. error code: ${err.code}`); + } + return exposurePoint; } ``` @@ -2135,13 +2406,18 @@ setMeteringPoint(point: Point): void **示例:** -```js -const exposurePoint = {x: 1, y: 1}; -try { +```ts +import { BusinessError } from '@ohos.base'; + +function setMeteringPoint(captureSession: camera.CaptureSession): void { + const exposurePoint: camera.Point = {x: 1, y: 1}; + try { captureSession.setMeteringPoint(exposurePoint); -} catch (error) { + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.error(`The setMeteringPoint call failed. error code: ${err.code}`); + } } ``` @@ -2169,12 +2445,19 @@ getExposureBiasRange(): Array\ **示例:** -```js -try { - let biasRangeArray = captureSession.getExposureBiasRange(); -} catch (error) { +```ts +import { BusinessError } from '@ohos.base'; + +function getExposureBiasRange(captureSession: camera.CaptureSession): Array { + let biasRangeArray: Array; + try { + biasRangeArray = captureSession.getExposureBiasRange(); + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.error(`The getExposureBiasRange call failed. error code: ${err.code}`); + } + return biasRangeArray; } ``` @@ -2204,13 +2487,18 @@ setExposureBias(exposureBias: number): void **示例:** -```js -let exposureBias = biasRangeArray[0]; -try { +```ts +import { BusinessError } from '@ohos.base'; + +function setExposureBias(captureSession: camera.CaptureSession, biasRangeArray: Array): void { + let exposureBias = biasRangeArray[0]; + try { captureSession.setExposureBias(exposureBias); -} catch (error) { + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.error(`The setExposureBias call failed. error code: ${err.code}`); + } } ``` @@ -2238,12 +2526,19 @@ getExposureValue(): number **示例:** -```js -try { - let exposureValue = captureSession.getExposureValue(); -} catch (error) { +```ts +import { BusinessError } from '@ohos.base'; + +function getExposureValue(captureSession: camera.CaptureSession): number { + let exposureValue: number; + try { + exposureValue = captureSession.getExposureValue(); + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.error(`The getExposureValue call failed. error code: ${err.code}`); + } + return exposureValue; } ``` @@ -2277,12 +2572,19 @@ isFocusModeSupported(afMode: FocusMode): boolean **示例:** -```js -try { - let status = captureSession.isFocusModeSupported(camera.FocusMode.FOCUS_MODE_AUTO); -} catch (error) { +```ts +import { BusinessError } from '@ohos.base'; + +function isFocusModeSupported(captureSession: camera.CaptureSession): boolean { + let status: boolean = false; + try { + status = captureSession.isFocusModeSupported(camera.FocusMode.FOCUS_MODE_AUTO); + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.error(`The isFocusModeSupported call failed. error code: ${err.code}`); + } + return status; } ``` @@ -2318,12 +2620,17 @@ setFocusMode(afMode: FocusMode): void **示例:** -```js -try { +```ts +import { BusinessError } from '@ohos.base'; + +function setFocusMode(captureSession: camera.CaptureSession): void { + try { captureSession.setFocusMode(camera.FocusMode.FOCUS_MODE_AUTO); -} catch (error) { + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.error(`The setFocusMode call failed. error code: ${err.code}`); + } } ``` @@ -2351,12 +2658,19 @@ getFocusMode(): FocusMode **示例:** -```js -try { - let afMode = captureSession.getFocusMode(); -} catch (error) { +```ts +import { BusinessError } from '@ohos.base'; + +function getFocusMode(captureSession: camera.CaptureSession): camera.FocusMode { + let afMode: camera.FocusMode; + try { + afMode = captureSession.getFocusMode(); + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.error(`The getFocusMode call failed. error code: ${err.code}`); + } + return afMode; } ``` @@ -2393,13 +2707,18 @@ setFocusPoint(point: Point): void **示例:** -```js -const Point1 = {x: 1, y: 1}; -try { +```ts +import { BusinessError } from '@ohos.base'; + +function setFocusPoint(captureSession: camera.CaptureSession): void { + const Point1: camera.Point = {x: 1, y: 1}; + try { captureSession.setFocusPoint(Point1); -} catch (error) { + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.error(`The setFocusPoint call failed. error code: ${err.code}`); + } } ``` @@ -2427,12 +2746,19 @@ getFocusPoint(): Point **示例:** -```js -try { - let point = captureSession.getFocusPoint(); -} catch (error) { +```ts +import { BusinessError } from '@ohos.base'; + +function getFocusPoint(captureSession: camera.CaptureSession): camera.Point { + let point: camera.Point; + try { + point = captureSession.getFocusPoint(); + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.error(`The getFocusPoint call failed. error code: ${err.code}`); + } + return point; } ``` @@ -2460,12 +2786,19 @@ getFocalLength(): number **示例:** -```js -try { - let focalLength = captureSession.getFocalLength(); -} catch (error) { +```ts +import { BusinessError } from '@ohos.base'; + +function getFocalLength(captureSession: camera.CaptureSession): number { + let focalLength: number; + try { + focalLength = captureSession.getFocalLength(); + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.error(`The getFocalLength call failed. error code: ${err.code}`); + } + return focalLength; } ``` @@ -2493,12 +2826,19 @@ getZoomRatioRange(): Array\ **示例:** -```js -try { - let zoomRatioRange = captureSession.getZoomRatioRange(); -} catch (error) { +```ts +import { BusinessError } from '@ohos.base'; + +function getZoomRatioRange(captureSession: camera.CaptureSession): Array { + let zoomRatioRange: Array; + try { + zoomRatioRange = captureSession.getZoomRatioRange(); + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.error(`The getZoomRatioRange call failed. error code: ${err.code}`); + } + return zoomRatioRange; } ``` @@ -2532,13 +2872,21 @@ setZoomRatio(zoomRatio: number): void **示例:** -```js -let zoomRatio = zoomRatioRange[0]; -try { +```ts +import { BusinessError } from '@ohos.base'; + +function setZoomRatio(captureSession: camera.CaptureSession, zoomRatioRange: Array): void { + if (zoomRatioRange === undefined || zoomRatioRange.length <= 0) { + return; + } + let zoomRatio = zoomRatioRange[0]; + try { captureSession.setZoomRatio(zoomRatio); -} catch (error) { + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.error(`The setZoomRatio call failed. error code: ${err.code}`); + } } ``` @@ -2566,12 +2914,19 @@ getZoomRatio(): number **示例:** -```js -try { - let zoomRatio = captureSession.getZoomRatio(); -} catch (error) { +```ts +import { BusinessError } from '@ohos.base'; + +function getZoomRatio(captureSession: camera.CaptureSession): number { + let zoomRatio: number; + try { + zoomRatio = captureSession.getZoomRatio(); + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.error(`The getZoomRatio call failed. error code: ${err.code}`); + } + return zoomRatio; } ``` @@ -2605,12 +2960,19 @@ isVideoStabilizationModeSupported(vsMode: VideoStabilizationMode): boolean **示例:** -```js -try { - let isSupported = captureSession.isVideoStabilizationModeSupported(camera.VideoStabilizationMode.OFF); -} catch (error) { +```ts +import { BusinessError } from '@ohos.base'; + +function isVideoStabilizationModeSupported(captureSession: camera.CaptureSession): boolean { + let isSupported: boolean = false; + try { + isSupported = captureSession.isVideoStabilizationModeSupported(camera.VideoStabilizationMode.OFF); + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.error(`The isVideoStabilizationModeSupported call failed. error code: ${err.code}`); + } + return isSupported; } ``` @@ -2638,12 +3000,19 @@ getActiveVideoStabilizationMode(): VideoStabilizationMode **示例:** -```js -try { - let vsMode = captureSession.getActiveVideoStabilizationMode(); -} catch (error) { +```ts +import { BusinessError } from '@ohos.base'; + +function getActiveVideoStabilizationMode(captureSession: camera.CaptureSession): camera.VideoStabilizationMode { + let vsMode: camera.VideoStabilizationMode; + try { + vsMode = captureSession.getActiveVideoStabilizationMode(); + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.error(`The getActiveVideoStabilizationMode call failed. error code: ${err.code}`); + } + return vsMode; } ``` @@ -2677,12 +3046,17 @@ setVideoStabilizationMode(mode: VideoStabilizationMode): void **示例:** -```js -try { +```ts +import { BusinessError } from '@ohos.base'; + +function setVideoStabilizationMode(captureSession: camera.CaptureSession): void { + try { captureSession.setVideoStabilizationMode(camera.VideoStabilizationMode.OFF); -} catch (error) { + } catch (error) { // 失败返回错误码error.code并处理 - console.log(error.code); + let err = error as BusinessError; + console.error(`The setVideoStabilizationMode call failed. error code: ${err.code}`); + } } ``` ### getSupportedFilters @@ -2711,8 +3085,11 @@ getSupportedFilters(): Array\ **示例:** -```js -let FilterTypes = captureSession.getSupportedFilters(); +```ts +function getSupportedFilters(captureSession: camera.CaptureSession): Array { + let filterTypes: Array = captureSession.getSupportedFilters(); + return filterTypes; +} ``` ### setFilter @@ -2741,10 +3118,12 @@ setFilter(filter: number): void **示例:** -```js -let FilterTypes = captureSession.getSupportedFilters(); -if (!FilterTypes.empty()) { - captureSession.setFilter(FilterTypes[0]); +```ts +function setFilter(captureSession: camera.CaptureSession, filterTypes: Array): void { + if (filterTypes === undefined || filterTypes.length <= 0) { + return; + } + captureSession.setFilter(filterTypes[0]); } ``` @@ -2774,8 +3153,11 @@ if (!FilterTypes.empty()) { **示例:** -```js -let FilterType = captureSession.getFilter(); +```ts +function getFilter(captureSession: camera.CaptureSession): number { + let filterType: number = captureSession.getFilter(); + return filterType; +} ``` ### getSupportedBeautyTypes @@ -2803,8 +3185,11 @@ getSupportedBeautyTypes(): Array<[BeautyType](#beautytype)> **示例:** -```js -let FilterTypes = captureSession.getSupportedBeautyTypes(); +```ts +function getSupportedBeautyTypes(captureSession: camera.CaptureSession): Array { + let beautyTypes: Array = captureSession.getSupportedBeautyTypes(); + return beautyTypes; +} ``` ### getSupportedBeautyRange @@ -2838,10 +3223,14 @@ getSupportedBeautyRange(type: BeautyType): Array **示例:** -```js -let beautyTypes = captureSession.getSupportedBeautyTypes(); -if (!beautyTypes.empty()) { - let nums = captureSession.getSupportedBeautyRange(beautyTypes[0]); +```ts +function getSupportedBeautyRange(captureSession: camera.CaptureSession): Array { + let beautyTypes: Array = captureSession.getSupportedBeautyTypes(); + if (beautyTypes === undefined || beautyTypes.length <= 0) { + return null; + } + let beautyLevels: Array = captureSession.getSupportedBeautyRange(beautyTypes[0]); + return beautyLevels; } ``` @@ -2872,14 +3261,17 @@ setBeauty(type: BeautyType, value: number): void **示例:** -```js -let beautyTypes = captureSession.getSupportedBeautyTypes(); -let beautyLevels; -if (!beautyTypes.empty()) { - beautyLevels = captureSession.getSupportedBeautyRange(beautyTypes[0]); -} -if (!beautyTypes.empty() && beautyLevels.empty()) { - captureSession.setBeauty(beautyTypes[0], beautyLevels[0]); +```ts +function setBeauty(captureSession: camera.CaptureSession): void { + let beautyTypes: Array = captureSession.getSupportedBeautyTypes(); + if (beautyTypes === undefined || beautyTypes.length <= 0) { + return null; + } + let beautyLevels: Array = captureSession.getSupportedBeautyRange(beautyTypes[0]); + if (beautyLevels === undefined || beautyLevels.length <= 0) { + return null; + } + captureSession.setBeauty(beautyTypes[0], beautyLevels[0]); } ``` @@ -2914,16 +3306,20 @@ getBeauty(type: BeautyType): number **示例:** -```js -let BeautyTypes = captureSession.getSupportedBeautyTypes(); -let beautyLevels; -if (!BeautyTypes.empty()) { - beautyLevels = captureSession.getSupportedBeautyRange(BeautyTypes[0]); -} -if (!BeautyTypes.empty() && beautyLevels.empty()) { - captureSession.setBeauty(BeautyTypes[0], beautyLevels[0]); +```ts +function getBeauty(captureSession: camera.CaptureSession): number { + let beautyTypes = captureSession.getSupportedBeautyTypes(); + if (beautyTypes === undefined || beautyTypes.length <= 0) { + return null; + } + let beautyLevels: Array = captureSession.getSupportedBeautyRange(BeautyTypes[0]); + if (beautyLevels === undefined || beautyLevels.length <= 0) { + return null; + } + captureSession.setBeauty(beautyTypes[0], beautyLevels[0]); + let beautyLevel: number = captureSession.getBeauty(beautyTypes[0]); + return beautyLevel; } -let beautyLevel = captureSession.getBeauty(BeautyTypes[0]); ``` ### on('focusStateChange') @@ -2943,10 +3339,14 @@ on(type: 'focusStateChange', callback: AsyncCallback\): void **示例:** -```js -captureSession.on('focusStateChange', (err, focusState) => { - console.log(`Focus state : ${focusState}`); -}) +```ts +import { BusinessError } from '@ohos.base'; + +function registerFocusStateChange(captureSession: camera.CaptureSession): void { + captureSession.on('focusStateChange', (err: BusinessError, focusState: camera.FocusState) => { + console.log(`Focus state: ${focusState}`); + }); +} ``` ### off('focusStateChange') @@ -2966,8 +3366,10 @@ off(type: 'focusStateChange', callback?: AsyncCallback\): void **示例:** -```js -captureSession.off('focusStateChange') +```ts +function unregisterFocusStateChange(captureSession: camera.CaptureSession): void { + captureSession.off('focusStateChange'); +} ``` ### on('error') @@ -2987,10 +3389,14 @@ on(type: 'error', callback: ErrorCallback): void **示例:** -```js -captureSession.on('error', (error) => { +```ts +import { BusinessError } from '@ohos.base'; + +function registerCaptureSessionError(captureSession: camera.CaptureSession): void { + captureSession.on('error', (error: BusinessError) => { console.log(`Capture session error code: ${error.code}`); -}) + }); +} ``` ### off('error') @@ -3010,8 +3416,10 @@ off(type: 'error', callback?: ErrorCallback): void **示例:** -```js -captureSession.off('error') +```ts +function unregisterCaptureSessionError(captureSession: camera.CaptureSession): void { + captureSession.off('error'); +} ``` ## PortraitSession @@ -3043,8 +3451,11 @@ getSupportedPortraitEffects(): Array<[PortraitEffect](#portraiteffect)> **示例:** -```js -let portraitEffect = PortraitSession.getSupportedPortraitEffects(); +```ts +function getSupportedPortraitEffects(portraitSession: camera.PortraitSession): Array { + let portraitEffect: Array = portraitSession.getSupportedPortraitEffects(); + return portraitEffect; +} ``` ### setPortraitEffect @@ -3072,10 +3483,19 @@ setPortraitEffect(effect: PortraitEffect): void **示例:** -```js -let portraitEffects = PortraitSession.getSupportedPortraitEffects(); -if (!portraitEffects.empty()) { - PortraitSession.setPortraitEffect(portraitEffects[0]); +```ts +import { BusinessError } from '@ohos.base'; + +function setPortraitEffect(portraitSession: camera.PortraitSession, portraitEffects: Array): void { + if (portraitEffects === undefined || portraitEffects.length <= 0) { + return; + } + try { + portraitSession.setPortraitEffect(portraitEffects[0]); + } catch (error) { + let err = error as BusinessError; + console.error(`The setPortraitEffect call failed. error code: ${err.code}`); + } } ``` ### getPortraitEffect @@ -3104,10 +3524,10 @@ getPortraitEffect(): PortraitEffect **示例:** -```js -let portraitEffects = PortraitSession.getSupportedPortraitEffects(); -if (!portraitEffects.empty()) { - PortraitSession.setPortraitEffect(portraitEffects[0]); +```ts +function getSupportedPortraitEffects(portraitSession: camera.PortraitSession): Array { + let portraitEffects: Array = portraitSession.getSupportedPortraitEffects(); + return portraitEffects; } ``` @@ -3143,14 +3563,18 @@ start(callback: AsyncCallback\): void **示例:** -```js -previewOutput.start((err) => { +```ts +import { BusinessError } from '@ohos.base'; + +function startPreviewOutput(previewOutput: camera.PreviewOutput): void { + previewOutput.start((err: BusinessError) => { if (err) { - console.error(`Failed to start the previewOutput. ${err.code}`); - return; + console.error(`Failed to start the previewOutput. ${err.code}`); + return; } console.log('Callback returned with previewOutput started.'); -}) + }); +} ``` ### start @@ -3177,12 +3601,16 @@ start(): Promise\ **示例:** -```js -previewOutput.start().then(() => { +```ts +import { BusinessError } from '@ohos.base'; + +function startPreviewOutput(previewOutput: camera.PreviewOutput): void { + previewOutput.start().then(() => { console.log('Promise returned with previewOutput started.'); -}).catch((err) => { + }).catch((err: BusinessError) => { console.log('Failed to previewOutput start '+ err.code); -}); + }); +} ``` ### stop @@ -3201,14 +3629,18 @@ stop(callback: AsyncCallback\): void **示例:** -```js -previewOutput.stop((err) => { +```ts +import { BusinessError } from '@ohos.base'; + +function stopPreviewOutput(previewOutput: camera.PreviewOutput): void { + previewOutput.stop((err: BusinessError) => { if (err) { - console.error(`Failed to stop the previewOutput. ${err.code}`); - return; + console.error(`Failed to stop the previewOutput. ${err.code}`); + return; } console.log('Callback returned with previewOutput stopped.'); -}) + }) +} ``` ### stop @@ -3227,12 +3659,16 @@ stop(): Promise\ **示例:** -```js -previewOutput.stop().then(() => { +```ts +import { BusinessError } from '@ohos.base'; + +function stopPreviewOutput(previewOutput: camera.PreviewOutput): void { + previewOutput.stop().then(() => { console.log('Callback returned with previewOutput stopped.'); -}).catch((err) => { + }).catch((err: BusinessError) => { console.log('Failed to previewOutput stop '+ err.code); -}); + }); +} ``` ### release @@ -3259,14 +3695,18 @@ release(callback: AsyncCallback\): void **示例:** -```js -previewOutput.release((err) => { +```ts +import { BusinessError } from '@ohos.base'; + +function releasePreviewOutput(previewOutput: camera.PreviewOutput): void { + previewOutput.release((err: BusinessError) => { if (err) { - console.error(`Failed to release the PreviewOutput instance ${err.code}`); - return; + console.error(`Failed to release the PreviewOutput instance ${err.code}`); + return; } console.log('Callback invoked to indicate that the PreviewOutput instance is released successfully.'); -}); + }); +} ``` ### release @@ -3293,12 +3733,16 @@ release(): Promise\ **示例:** -```js -previewOutput.release().then(() => { +```ts +import { BusinessError } from '@ohos.base'; + +function releasePreviewOutput(previewOutput: camera.PreviewOutput): void { + previewOutput.release().then(() => { console.log('Promise returned to indicate that the PreviewOutput instance is released successfully.'); -}).catch((err) => { + }).catch((err: BusinessError) => { console.log('Failed to previewOutput release '+ err.code); -}); + }); +} ``` ### on('frameStart') @@ -3318,10 +3762,12 @@ on(type: 'frameStart', callback: AsyncCallback\): void **示例:** -```js -previewOutput.on('frameStart', () => { +```ts +function registerPreviewOutputFrameStart(previewOutput: camera.PreviewOutput): void { + previewOutput.on('frameStart', () => { console.log('Preview frame started'); -}) + }); +} ``` ### off('frameStart') @@ -3341,8 +3787,10 @@ off(type: 'frameStart', callback?: AsyncCallback\): void **示例:** -```js -previewOutput.off('frameStart') +```ts +function unregisterPreviewOutputFrameStart(previewOutput: camera.PreviewOutput): void { + previewOutput.off('frameStart'); +} ``` ### on('frameEnd') @@ -3362,10 +3810,12 @@ on(type: 'frameEnd', callback: AsyncCallback\): void **示例:** -```js -previewOutput.on('frameEnd', () => { +```ts +function registerPreviewOutputFrameEnd(previewOutput: camera.PreviewOutput): void { + previewOutput.on('frameEnd', () => { console.log('Preview frame ended'); -}) + }); +} ``` ### off('frameEnd') @@ -3385,8 +3835,10 @@ off(type: 'frameEnd', callback?: AsyncCallback\): void **示例:** -```js -previewOutput.off('frameEnd') +```ts +function unregisterPreviewOutputFrameEnd(previewOutput: camera.PreviewOutput): void { + previewOutput.off('frameEnd'); +} ``` ### on('error') @@ -3406,10 +3858,14 @@ on(type: 'error', callback: ErrorCallback): void **示例:** -```js -previewOutput.on('error', (previewOutputError) => { +```ts +import { BusinessError } from '@ohos.base'; + +function registerPreviewOutputError(previewOutput: camera.PreviewOutput): void { + previewOutput.on('error', (previewOutputError: BusinessError) => { console.log(`Preview output error code: ${previewOutputError.code}`); -}) + }) +} ``` ### off('error') @@ -3429,8 +3885,10 @@ off(type: 'error', callback?: ErrorCallback): void **示例:** -```js -previewOutput.off('error') +```ts +function unregisterPreviewOutputError(previewOutput: camera.PreviewOutput): void { + previewOutput.off('error'); +} ``` ### addDeferredSurface @@ -3459,17 +3917,19 @@ addDeferredSurface(surfaceId: string): void **示例:** -```js -function async preview(context: Context, cameraInfo: camera.Device, previewProfile: camera.Profile, photoProfile: camera.Profile, surfaceId: string): Promise { +```ts +import featureAbility from '@ohos.ability.featureAbility'; + +async function preview(context: featureAbility.Context, cameraInfo: camera.CameraDevice, previewProfile: camera.Profile, photoProfile: camera.Profile, surfaceId: string): Promise { const cameraManager: camera.CameraManager = camera.getCameraManager(context); - const cameraInput camera.CameraInput = await cameraManager.createCameraInput(cameraInfo) + const cameraInput: camera.CameraInput = cameraManager.createCameraInput(cameraInfo) const previewOutput: camera.PreviewOutput = await cameraManager.createDeferredPreviewOutput(previewProfile); - const photoOutput: camera.PhotoOutput = await cameraManager.createPhotoOutput(photoProfile); + const photoOutput: camera.PhotoOutput = cameraManager.createPhotoOutput(photoProfile); const session: camera.CaptureSession = await this.mCameraManager.createCaptureSession(); - await session.beginConfig(); - await session.addInput(cameraInput); - await session.addOutput(previewOutput); - await session.addOutput(photoOutput); + session.beginConfig(); + session.addInput(cameraInput); + session.addOutput(previewOutput); + session.addOutput(photoOutput); await session.commitConfig(); await session.start(); await previewOutput.addDeferredSurface(surfaceId); @@ -3556,14 +4016,18 @@ capture(callback: AsyncCallback\): void **示例:** -```js -photoOutput.capture((err) => { +```ts +import { BusinessError } from '@ohos.base'; + +function capture(photoOutput: camera.PhotoOutput): void { + photoOutput.capture((err: BusinessError) => { if (err) { - console.error(`Failed to capture the photo ${err.code}`); - return; + console.error(`Failed to capture the photo ${err.code}`); + return; } console.log('Callback invoked to indicate the photo capture request success.'); -}); + }); +} ``` ### capture @@ -3591,12 +4055,16 @@ capture(): Promise\ **示例:** -```js -photoOutput.capture().then(() => { +```ts +import { BusinessError } from '@ohos.base'; + +function capture(photoOutput: camera.PhotoOutput): void { + photoOutput.capture().then(() => { console.log('Promise returned to indicate that photo capture request success.'); -}).catch((err) => { + }).catch((err: BusinessError) => { console.log('Failed to photoOutput capture '+ err.code); -}); + }); +} ``` ### capture @@ -3626,25 +4094,29 @@ capture(setting: PhotoCaptureSetting, callback: AsyncCallback\): void **示例:** -```js -let captureLocation = { - latitude: 0, - longitude: 0, - altitude: 0, -} -let settings = { - quality: camera.QualityLevel.QUALITY_LEVEL_LOW, - rotation: camera.ImageRotation.ROTATION_0, - location: captureLocation, - mirror: false -} -photoOutput.capture(settings, (err) => { +```ts +import { BusinessError } from '@ohos.base'; + +function capture(photoOutput: camera.PhotoOutput): void { + let captureLocation: camera.Location = { + latitude: 0, + longitude: 0, + altitude: 0, + } + let settings: camera.PhotoCaptureSetting = { + quality: camera.QualityLevel.QUALITY_LEVEL_LOW, + rotation: camera.ImageRotation.ROTATION_0, + location: captureLocation, + mirror: false + } + photoOutput.capture(settings, (err: BusinessError) => { if (err) { - console.error(`Failed to capture the photo ${err.code}`); - return; + console.error(`Failed to capture the photo ${err.code}`); + return; } console.log('Callback invoked to indicate the photo capture request success.'); -}); + }); +} ``` ### capture @@ -3679,23 +4151,27 @@ capture(setting?: PhotoCaptureSetting): Promise\ **示例:** -```js -let captureLocation = { - latitude: 0, - longitude: 0, - altitude: 0, -} -let settings = { - quality: camera.QualityLevel.QUALITY_LEVEL_LOW, - rotation: camera.ImageRotation.ROTATION_0, - location: captureLocation, - mirror: false -} -photoOutput.capture(settings).then(() => { +```ts +import { BusinessError } from '@ohos.base'; + +function capture(photoOutput: camera.PhotoOutput): void { + let captureLocation: camera.Location = { + latitude: 0, + longitude: 0, + altitude: 0, + } + let settings: camera.PhotoCaptureSetting = { + quality: camera.QualityLevel.QUALITY_LEVEL_LOW, + rotation: camera.ImageRotation.ROTATION_0, + location: captureLocation, + mirror: false + } + photoOutput.capture(settings).then(() => { console.log('Promise returned to indicate that photo capture request success.'); -}).catch((err) => { + }).catch((err: BusinessError) => { console.log('Failed to photoOutput capture '+ err.code); -}); + }); +} ``` ### isMirrorSupported @@ -3714,8 +4190,11 @@ isMirrorSupported(): boolean **示例:** -```js -let isSupported = photoOutput.isMirrorSupported(); +```ts +function isMirrorSupported(photoOutput: camera.PhotoOutput): boolean { + let isSupported: boolean = photoOutput.isMirrorSupported(); + return isSupported; +} ``` ### release @@ -3742,14 +4221,18 @@ release(callback: AsyncCallback\): void **示例:** -```js -photoOutput.release((err) => { +```ts +import { BusinessError } from '@ohos.base'; + +function releasePhotoOutput(photoOutput: camera.PhotoOutput): void { + photoOutput.release((err: BusinessError) => { if (err) { - console.error(`Failed to release the PreviewOutput instance ${err.code}`); - return; + console.error(`Failed to release the PreviewOutput instance ${err.code}`); + return; } console.log('Callback invoked to indicate that the PreviewOutput instance is released successfully.'); -}); + }); +} ``` ### release @@ -3776,12 +4259,16 @@ release(): Promise\ **示例:** -```js -photoOutput.release().then(() => { +```ts +import { BusinessError } from '@ohos.base'; + +function releasePhotoOutput(photoOutput: camera.PhotoOutput): void { + photoOutput.release().then(() => { console.log('Promise returned to indicate that the PreviewOutput instance is released successfully.'); -}).catch((err) => { + }).catch((err: BusinessError) => { console.log('Failed to photoOutput release '+ err.code); -}); + }); +} ``` ### on('captureStart') @@ -3801,10 +4288,14 @@ on(type: 'captureStart', callback: AsyncCallback\): void **示例:** -```js -photoOutput.on('captureStart', (err, captureId) => { +```ts +import { BusinessError } from '@ohos.base'; + +function registerPhotoOutputCaptureStart(photoOutput: camera.PhotoOutput): void { + photoOutput.on('captureStart', (err: BusinessError, captureId: number) => { console.log(`photo capture stated, captureId : ${captureId}`); -}) + }); +} ``` ### off('captureStart') @@ -3824,8 +4315,10 @@ off(type: 'captureStart', callback?: AsyncCallback\): void **示例:** -```js -photoOutput.off('captureStart') +```ts +function unregisterPhotoOutputCaptureStart(photoOutput: camera.PhotoOutput): void { + photoOutput.off('captureStart'); +} ``` ### on('frameShutter') @@ -3845,11 +4338,15 @@ on(type: 'frameShutter', callback: AsyncCallback\): void **示例:** -```js -photoOutput.on('frameShutter', (err, frameShutterInfo) => { +```ts +import { BusinessError } from '@ohos.base'; + +function registerPhotoOutputFrameShutter(photoOutput: camera.PhotoOutput): void { + photoOutput.on('frameShutter', (err: BusinessError, frameShutterInfo: camera.FrameShutterInfo) => { console.log(`photo capture end, captureId : ${frameShutterInfo.captureId}`); console.log(`Timestamp for frame : ${frameShutterInfo.timestamp}`); -}) + }); +} ``` ### off('frameShutter') @@ -3869,8 +4366,10 @@ off(type: 'frameShutter', callback?: AsyncCallback\): void **示例:** -```js -photoOutput.off('frameShutter') +```ts +function unregisterPhotoOutputFrameShutter(photoOutput: camera.PhotoOutput): void { + photoOutput.off('frameShutter'); +} ``` ### on('captureEnd') @@ -3890,11 +4389,15 @@ on(type: 'captureEnd', callback: AsyncCallback\): void **示例:** -```js -photoOutput.on('captureEnd', (err, captureEndInfo) => { +```ts +import { BusinessError } from '@ohos.base'; + +function registerPhotoOutputCaptureEnd(photoOutput: camera.PhotoOutput): void { + photoOutput.on('captureEnd', (err: BusinessError, captureEndInfo: camera.CaptureEndInfo) => { console.log(`photo capture end, captureId : ${captureEndInfo.captureId}`); console.log(`frameCount : ${captureEndInfo.frameCount}`); -}) + }); +} ``` ### off('captureEnd') @@ -3914,8 +4417,10 @@ off(type: 'captureEnd', callback?: AsyncCallback\): void **示例:** -```js -photoOutput.off('captureEnd') +```ts +function unregisterPhotoOutputCaptureEnd(photoOutput: camera.PhotoOutput): void { + photoOutput.off('captureEnd'); +} ``` ### on('error') @@ -3935,10 +4440,14 @@ on(type: 'error', callback: ErrorCallback): void **示例:** -```js -photoOutput.on('error', (error) => { +```ts +import { BusinessError } from '@ohos.base'; + +function registerPhotoOutputError(photoOutput: camera.PhotoOutput): void { + photoOutput.on('error', (error: BusinessError) => { console.log(`Photo output error code: ${error.code}`); -}) + }); +} ``` ### off('error') @@ -3958,8 +4467,10 @@ off(type: 'error', callback?: ErrorCallback): void **示例:** -```js -photoOutput.off('error') +```ts +function unregisterPhotoOutputError(photoOutput: camera.PhotoOutput): void { + photoOutput.off('error'); +} ``` ### isQuickThumbnailSupported @@ -3990,22 +4501,26 @@ isQuickThumbnailSupported(): boolean **示例:** -```js -this.cameraManager = camera.getCameraManager(globalThis.abilityContext); -let cameras = this.cameraManager.getSupportedCameras() -// 创建CaptureSession实例 -this.captureSession = await this.cameraManager.createCaptureSession() -// 开始配置会话 -await this.captureSession.beginConfig() -// 把CameraInput加入到会话 -this.mCameraInput = await this.cameraManager.createCameraInput(cameras[0]) -await this.cameraInput.open() -await this.captureSession.addInput(this.cameraInput) -// 把PhotoOutPut加入到会话 -this.photoOutPut = await this.cameraManager.createPhotoOutput(photoProfile, surfaceId) -await this.captureSession.addOutput(this.photoOutPut) - -boolean isSupported = this.photoOutPut.isQuickThumbnailSupported() +```ts +import featureAbility from '@ohos.ability.featureAbility'; + +async function isQuickThumbnailSupported(context: featureAbility.Context, photoProfile: camera.Profile, surfaceId: string): Promise { + let cameraManager: camera.CameraManager = camera.getCameraManager(context); + let cameras: Array = cameraManager.getSupportedCameras(); + // 创建CaptureSession实例 + let captureSession: camera.CaptureSession = cameraManager.createCaptureSession(); + // 开始配置会话 + captureSession.beginConfig(); + // 把CameraInput加入到会话 + let cameraInput: camera.CameraInput = cameraManager.createCameraInput(cameras[0]); + await cameraInput.open(); + captureSession.addInput(cameraInput); + // 把photoOutput加入到会话 + let photoOutput: camera.PhotoOutput = cameraManager.createPhotoOutput(photoProfile, surfaceId); + captureSession.addOutput(photoOutput); + let isSupported: boolean = photoOutput.isQuickThumbnailSupported(); + return isSupported; +} ``` ### enableQuickThumbnail @@ -4036,24 +4551,35 @@ enableQuickThumbnail(enabled: boolean): void **示例:** -```js -this.cameraManager = camera.getCameraManager(globalThis.abilityContext); -let cameras = this.cameraManager.getSupportedCameras() -// 创建CaptureSession实例 -this.captureSession = await this.cameraManager.createCaptureSession() -// 开始配置会话 -await this.captureSession.beginConfig() -// 把CameraInput加入到会话 -this.cameraInput = await this.cameraManager.createCameraInput(cameras[0]) -await this.cameraInput.open() -await this.captureSession.addInput(this.cameraInput) -// 把PhotoOutPut加入到会话 -this.photoOutPut = await this.cameraManager.createPhotoOutput(photoProfile, surfaceId) -await this.captureSession.addOutput(this.photoOutPut) -boolean isSupported = this.photoOutPut.isQuickThumbnailSupported() -if (isSupported) { - // 使能快速缩略图 - this.photoOutPut.enableQuickThumbnail(true) +```ts +import featureAbility from '@ohos.ability.featureAbility'; +import { BusinessError } from '@ohos.base'; + +async function enableQuickThumbnail(context: featureAbility.Context, photoProfile: camera.Profile, surfaceId: string): Promise { + let cameraManager: camera.CameraManager = camera.getCameraManager(context); + let cameras: Array = cameraManager.getSupportedCameras(); + // 创建CaptureSession实例 + let captureSession: camera.CaptureSession = cameraManager.createCaptureSession(); + // 开始配置会话 + captureSession.beginConfig(); + // 把CameraInput加入到会话 + let cameraInput: camera.CameraInput = cameraManager.createCameraInput(cameras[0]); + await cameraInput.open(); + captureSession.addInput(cameraInput); + // 把PhotoOutPut加入到会话 + let photoOutput: camera.PhotoOutput = cameraManager.createPhotoOutput(photoProfile, surfaceId); + captureSession.addOutput(photoOutput); + let isSupported: boolean = photoOutput.isQuickThumbnailSupported(); + if (!isSupported) { + console.log('Quick Thumbnail is not supported to be turned on.'); + return; + } + try { + photoOutput.enableQuickThumbnail(true); + } catch (error) { + let err = error as BusinessError; + console.error(`The enableQuickThumbnail call failed. error code: ${err.code}`); + } } ``` @@ -4078,35 +4604,45 @@ on(type: 'quickThumbnail', callback: AsyncCallback\): void **示例:** -```js -import camera from '@ohos.multimedia.camera' - -this.cameraManager = camera.getCameraManager(globalThis.abilityContext); -let cameras = this.cameraManager.getSupportedCameras() -// 创建CaptureSession实例 -this.captureSession = await this.cameraManager.createCaptureSession() -// 开始配置会话 -await this.captureSession.beginConfig() -// 把CameraInput加入到会话 -this.cameraInput = await this.cameraManager.createCameraInput(cameras[0]) -await this.cameraInput.open() -await this.captureSession.addInput(this.cameraInput) -// 把PhotoOutPut加入到会话 -this.photoOutPut = await this.cameraManager.createPhotoOutput(photoProfile, surfaceId) -await this.captureSession.addOutput(this.photoOutPut) -boolean isSupported = this.photoOutPut.isQuickThumbnailSupported() -if (isSupported) { - // 使能快速缩略图 - this.photoOutPut.enableQuickThumbnail(true) -} -this.photoOutPut.on('quickThumbnail', (err, pixelmap) => { - if (err || pixelmap === undefined) { - Logger.error(this.tag, 'photoOutPut on thumbnail failed ') - return +```ts +import featureAbility from '@ohos.ability.featureAbility'; +import { BusinessError } from '@ohos.base'; + +async function registerQuickThumbnail(context: featureAbility.Context, photoProfile: camera.Profile, surfaceId: string): Promise { + let cameraManager: camera.CameraManager = camera.getCameraManager(context); + let cameras: Array = cameraManager.getSupportedCameras(); + // 创建CaptureSession实例 + let captureSession: camera.CaptureSession = cameraManager.createCaptureSession(); + // 开始配置会话 + captureSession.beginConfig(); + // 把CameraInput加入到会话 + let cameraInput: camera.CameraInput = cameraManager.createCameraInput(cameras[0]); + await cameraInput.open(); + captureSession.addInput(cameraInput); + // 把PhotoOutPut加入到会话 + let photoOutput: camera.PhotoOutput = cameraManager.createPhotoOutput(photoProfile, surfaceId); + captureSession.addOutput(photoOutput); + let isSupported: boolean = photoOutput.isQuickThumbnailSupported(); + if (!isSupported) { + console.log('Quick Thumbnail is not supported to be turned on.'); + return; + } + try { + photoOutput.enableQuickThumbnail(true); + } catch (error) { + let err = error as BusinessError; + console.error(`The enableQuickThumbnail call failed. error code: ${err.code}`); + } + + photoOutput.on('quickThumbnail', (err: BusinessError, pixelMap: image.PixelMap) => { + if (err || pixelMap === undefined) { + console.error('photoOutput on thumbnail failed'); + return; } - // 显示或保存pixelmap - this.showOrSavePicture(pixelmap) -}) + // 显示或保存pixelMap + // do something + }) +} ``` ### off('quickThumbnail') @@ -4128,36 +4664,10 @@ off(type: 'quickThumbnail', callback?: AsyncCallback\): void **示例:** -```js -import camera from '@ohos.multimedia.camera' - -this.cameraManager = camera.getCameraManager(globalThis.abilityContext); -let cameras = this.cameraManager.getSupportedCameras() -// 创建CaptureSession实例 -this.captureSession = await this.cameraManager.createCaptureSession() -// 开始配置会话 -await this.captureSession.beginConfig() -// 把CameraInput加入到会话 -this.cameraInput = await this.cameraManager.createCameraInput(cameras[0]) -await this.cameraInput.open() -await this.captureSession.addInput(this.cameraInput) -// 把PhotoOutPut加入到会话 -this.photoOutPut = await this.cameraManager.createPhotoOutput(photoProfile, surfaceId) -await this.captureSession.addOutput(this.photoOutPut) -boolean isSupported = this.photoOutPut.isQuickThumbnailSupported() -if (isSupported) { - // 使能快速缩略图 - this.photoOutPut.enableQuickThumbnail(true) -} -this.photoOutPut.on('quickThumbnail', (err, pixelmap) => { - if (err || pixelmap === undefined) { - Logger.error(this.tag, 'photoOutPut on thumbnail failed ') - return - } - // 显示或保存pixelmap - this.showOrSavePicture(pixelmap) -}) -this.photoOutPut.off('quickThumbnail') +```ts +function unregisterQuickThumbnail(photoOutput: camera.PhotoOutput): void { + this.photoOutput.off('quickThumbnail'); +} ``` ## FrameShutterInfo @@ -4211,14 +4721,18 @@ start(callback: AsyncCallback\): void **示例:** -```js -videoOutput.start((err) => { +```ts +import { BusinessError } from '@ohos.base'; + +function startVideoOutput(videoOutput: camera.VideoOutput): void { + videoOutput.start((err: BusinessError) => { if (err) { - console.error(`Failed to start the video output ${err.code}`); - return; + console.error(`Failed to start the video output ${err.code}`); + return; } console.log('Callback invoked to indicate the video output start success.'); -}); + }); +} ``` ### start @@ -4246,12 +4760,16 @@ start(): Promise\ **示例:** -```js -videoOutput.start().then(() => { +```ts +import { BusinessError } from '@ohos.base'; + +function startVideoOutput(videoOutput: camera.VideoOutput): void { + videoOutput.start().then(() => { console.log('Promise returned to indicate that start method execution success.'); -}).catch((err) => { + }).catch((err: BusinessError) => { console.log('Failed to videoOutput start '+ err.code); -}); + }); +} ``` ### stop @@ -4270,14 +4788,18 @@ stop(callback: AsyncCallback\): void **示例:** -```js -videoOutput.stop((err) => { +```ts +import { BusinessError } from '@ohos.base'; + +function stopVideoOutput(videoOutput: camera.VideoOutput): void { + videoOutput.stop((err: BusinessError) => { if (err) { - console.error(`Failed to stop the video output ${err.code}`); - return; + console.error(`Failed to stop the video output ${err.code}`); + return; } console.log('Callback invoked to indicate the video output stop success.'); -}); + }); +} ``` ### stop @@ -4296,12 +4818,16 @@ stop(): Promise\ **示例:** -```js -videoOutput.stop().then(() => { +```ts +import { BusinessError } from '@ohos.base'; + +function stopVideoOutput(videoOutput: camera.VideoOutput): void { + videoOutput.stop().then(() => { console.log('Promise returned to indicate that stop method execution success.'); -}).catch((err) => { + }).catch((err: BusinessError) => { console.log('Failed to videoOutput stop '+ err.code); -}); + }); +} ``` ### release @@ -4328,14 +4854,18 @@ release(callback: AsyncCallback\): void **示例:** -```js -videoOutput.release((err) => { +```ts +import { BusinessError } from '@ohos.base'; + +function releaseVideoOutput(videoOutput: camera.VideoOutput): void { + videoOutput.release((err: BusinessError) => { if (err) { - console.error(`Failed to release the PreviewOutput instance ${err.code}`); - return; + console.error(`Failed to release the PreviewOutput instance ${err.code}`); + return; } - console.log('Callback invoked to indicate that the PreviewOutput instance is released successfully.'); -}); + console.log('Callback invoked to indicate that the videoOutput instance is released successfully.'); + }); +} ``` ### release @@ -4362,12 +4892,16 @@ release(): Promise\ **示例:** -```js -videoOutput.release().then(() => { - console.log('Promise returned to indicate that the PreviewOutput instance is released successfully.'); -}).catch((err) => { +```ts +import { BusinessError } from '@ohos.base'; + +function releaseVideoOutput(videoOutput: camera.VideoOutput): void { + videoOutput.release().then(() => { + console.log('Promise returned to indicate that the videoOutput instance is released successfully.'); + }).catch((err: BusinessError) => { console.log('Failed to videoOutput release '+ err.code); -}); + }); +} ``` ### on('frameStart') @@ -4387,10 +4921,12 @@ on(type: 'frameStart', callback: AsyncCallback\): void **示例:** -```js -videoOutput.on('frameStart', () => { +```ts +function registerVideoOutputFrameStart(videoOutput: camera.VideoOutput): void { + videoOutput.on('frameStart', () => { console.log('Video frame started'); -}) + }); +} ``` ### off('frameStart') @@ -4410,8 +4946,11 @@ off(type: 'frameStart', callback?: AsyncCallback\): void **示例:** -```js -videoOutput.off('frameStart') +```ts +function unregisterVideoOutputFrameStart(videoOutput: camera.VideoOutput): void { + videoOutput.off('frameStart'); +} + ``` ### on('frameEnd') @@ -4431,10 +4970,12 @@ on(type: 'frameEnd', callback: AsyncCallback\): void **示例:** -```js -videoOutput.on('frameEnd', () => { +```ts +function registerVideoOutputFrameEnd(videoOutput: camera.VideoOutput): void { + videoOutput.on('frameEnd', () => { console.log('Video frame ended'); -}) + }); +} ``` ### off('frameEnd') @@ -4454,8 +4995,10 @@ off(type: 'frameEnd', callback?: AsyncCallback\): void **示例:** -```js -videoOutput.off('frameEnd') +```ts +function unregisterVideoOutputFrameEnd(videoOutput: camera.VideoOutput): void { + videoOutput.off('frameEnd'); +} ``` ### on('error') @@ -4475,10 +5018,14 @@ on(type: 'error', callback: ErrorCallback): void **示例:** -```js -videoOutput.on('error', (error) => { +```ts +import { BusinessError } from '@ohos.base'; + +function registerVideoOutputError(videoOutput: camera.VideoOutput): void { + videoOutput.on('error', (error: BusinessError) => { console.log(`Video output error code: ${error.code}`); -}) + }); +} ``` ### off('error') @@ -4498,8 +5045,10 @@ off(type: 'error', callback?: ErrorCallback): void **示例:** -```js -videoOutput.off('error') +```ts +function unregisterVideoOutputError(videoOutput: camera.VideoOutput): void { + videoOutput.off('error'); +} ``` ## MetadataOutput @@ -4531,14 +5080,18 @@ start(callback: AsyncCallback\): void **示例:** -```js -metadataOutput.start((err) => { +```ts +import { BusinessError } from '@ohos.base'; + +function startMetadataOutput(metadataOutput: camera.MetadataOutput): void { + metadataOutput.start((err: BusinessError) => { if (err) { - console.error(`Failed to start metadataOutput. ${err.code}`); - return; + console.error(`Failed to start metadataOutput. ${err.code}`); + return; } console.log('Callback returned with metadataOutput started.'); -}) + }); +} ``` ### start @@ -4566,12 +5119,16 @@ start(): Promise\ **示例:** -```js -metadataOutput.start().then(() => { +```ts +import { BusinessError } from '@ohos.base'; + +function startMetadataOutput(metadataOutput: camera.MetadataOutput): void { + metadataOutput.start().then(() => { console.log('Callback returned with metadataOutput started.'); -}).catch((err) => { + }).catch((err: BusinessError) => { console.log('Failed to metadataOutput start '+ err.code); -}); + }); +} ``` ### stop @@ -4590,14 +5147,18 @@ stop(callback: AsyncCallback\): void **示例:** -```js -metadataOutput.stop((err) => { +```ts +import { BusinessError } from '@ohos.base'; + +function stopMetadataOutput(metadataOutput: camera.MetadataOutput): void { + metadataOutput.stop((err: BusinessError) => { if (err) { - console.error(`Failed to stop the metadataOutput. ${err.code}`); - return; + console.error(`Failed to stop the metadataOutput. ${err.code}`); + return; } console.log('Callback returned with metadataOutput stopped.'); -}) + }) +} ``` ### stop @@ -4616,12 +5177,16 @@ stop(): Promise\ **示例:** -```js -metadataOutput.stop().then(() => { +```ts +import { BusinessError } from '@ohos.base'; + +function stopMetadataOutput(metadataOutput: camera.MetadataOutput): void { + metadataOutput.stop().then(() => { console.log('Callback returned with metadataOutput stopped.'); -}).catch((err) => { + }).catch((err: BusinessError) => { console.log('Failed to metadataOutput stop '+ err.code); -}); + }); +} ``` ### on('metadataObjectsAvailable') @@ -4641,10 +5206,14 @@ on(type: 'metadataObjectsAvailable', callback: AsyncCallback\ { +```ts +import { BusinessError } from '@ohos.base'; + +function registerMetadataObjectsAvailable(metadataOutput: camera.MetadataOutput): void { + metadataOutput.on('metadataObjectsAvailable', (err: BusinessError, metadataObjectArr: Array) => { console.log(`metadata output metadataObjectsAvailable`); -}) + }); +} ``` ### off('metadataObjectsAvailable') @@ -4664,8 +5233,10 @@ off(type: 'metadataObjectsAvailable', callback?: AsyncCallback\ { +```ts +import { BusinessError } from '@ohos.base'; + +function registerMetadataOutputError(metadataOutput: camera.MetadataOutput): void { + metadataOutput.on('error', (metadataOutputError: BusinessError) => { console.log(`Metadata output error code: ${metadataOutputError.code}`); -}) + }); +} ``` ### off('error') @@ -4708,8 +5283,10 @@ off(type: 'error', callback?: ErrorCallback): void **示例:** -```js -metadataOutput.off('error') +```ts +function unregisterMetadataOutputError(metadataOutput: camera.MetadataOutput): void { + metadataOutput.off('error'); +} ``` ## MetadataObjectType