1# @ohos.multimedia.camera (相机管理) 2 3> **说明:** 4> 5> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 6 7## 导入模块 8 9```ts 10import camera from '@ohos.multimedia.camera'; 11``` 12 13## camera.getCameraManager 14 15getCameraManager(context: BaseContext): CameraManager 16 17获取相机管理器实例,同步返回结果。 18 19**系统能力:** SystemCapability.Multimedia.Camera.Core 20 21**参数:** 22 23| 参数名 | 类型 | 必填 | 说明 | 24| -------- | ----------------------------------------------- | ---- | ---------------------------- | 25| context | [BaseContext](js-apis-inner-application-baseContext.md) | 是 | 应用上下文。 | 26 27**返回值:** 28 29| 类型 | 说明 | 30| ----------------------------------------------- | ---------------------------- | 31| [CameraManager](#cameramanager) | 相机管理器。 | 32 33**错误码:** 34 35以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 36 37| 错误码ID | 错误信息 | 38| --------------- | --------------- | 39| 7400101 | Parameter missing or parameter type incorrect | 40| 7400201 | Camera service fatal error. | 41 42**示例:** 43 44```ts 45import common from '@ohos.app.ability.common'; 46import { BusinessError } from '@ohos.base'; 47 48function getCameraManager(context: common.BaseContext): camera.CameraManager | undefined { 49 let cameraManager: camera.CameraManager | undefined = undefined; 50 try { 51 cameraManager = camera.getCameraManager(context); 52 } catch (error) { 53 let err = error as BusinessError; 54 console.error(`The getCameraManager call failed. error code: ${err.code}`); 55 } 56 return cameraManager; 57} 58``` 59## camera.getModeManager 60 61getModeManager(context: BaseContext): ModeManager 62 63获取模式化管理器实例,同步返回结果。 64 65模式化管理是对于cameraManager功能的增强与扩充,主要用于一些高级功能的管理(如人像模式)。 66 67**系统接口:** 此接口为系统接口。 68 69**系统能力:** SystemCapability.Multimedia.Camera.Core 70 71**参数:** 72 73| 参数名 | 类型 | 必填 | 说明 | 74| -------- | ----------------------------------------------- | ---- | ---------------------------- | 75| context | [BaseContext](js-apis-inner-application-baseContext.md) | 是 | 应用上下文。 | 76 77**返回值:** 78 79| 类型 | 说明 | 80| --------------------------------------| -----------| 81| [ModeManager](#modemanager) | 模式化管理器。| 82 83**错误码:** 84 85以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md) 86 87| 错误码ID | 错误信息 | 88| --------------- | --------------- | 89| 7400101 | Parameter missing or parameter type incorrect | 90| 7400201 | Camera service fatal error. | 91 92**示例:** 93 94```ts 95import common from '@ohos.app.ability.common'; 96import { BusinessError } from '@ohos.base'; 97 98function getModeManager(context: common.BaseContext): camera.ModeManager | undefined { 99 let modeManager: camera.ModeManager | undefined = undefined; 100 try { 101 modeManager = camera.getModeManager(context); 102 } catch (error) { 103 let err = error as BusinessError; 104 console.error(`The getModeManager call failed. error code: ${err.code}`); 105 } 106 return modeManager; 107} 108``` 109 110## CameraStatus 111 112枚举,相机状态。 113 114**系统能力:** SystemCapability.Multimedia.Camera.Core 115 116| 名称 | 值 | 说明 | 117| ------------------------- | ---- | ------------ | 118| CAMERA_STATUS_APPEAR | 0 | 新的相机出现。 | 119| CAMERA_STATUS_DISAPPEAR | 1 | 相机被移除。 | 120| CAMERA_STATUS_AVAILABLE | 2 | 相机可用。 | 121| CAMERA_STATUS_UNAVAILABLE | 3 | 相机不可用。 | 122 123## Profile 124 125相机配置信息项。 126 127**系统能力:** SystemCapability.Multimedia.Camera.Core 128 129| 名称 | 类型 | 必填 | 说明 | 130| -------- | ----------------------------- |---- | ------------- | 131| format | [CameraFormat](#cameraformat) | 是 | 输出格式。 | 132| size | [Size](#size) | 是 | 分辨率。 | 133 134## FrameRateRange 135 136帧率范围。 137 138**系统能力:** SystemCapability.Multimedia.Camera.Core 139 140| 名称 | 类型 | 必填 | 说明 | 141| -------- | ----------------------------- |---- | ------------- | 142| min | number | 是 | 最小帧率。 | 143| max | number | 是 | 最大帧率。 | 144 145## VideoProfile 146 147视频配置信息项。 148 149**系统能力:** SystemCapability.Multimedia.Camera.Core 150 151| 名称 | 类型 | 必填 | 说明 | 152| ------------------------- | ----------------------------------------- | --- |----------- | 153| frameRateRange | [FrameRateRange](#frameraterange) | 是 | 帧率范围,fps(frames per second)。 | 154 155## CameraOutputCapability 156 157相机输出能力项。 158 159**系统能力:** SystemCapability.Multimedia.Camera.Core 160 161| 名称 | 类型 | 必填 | 说明 | 162| ----------------------------- | -------------------------------------------------- | --- |------------------- | 163| previewProfiles | Array\<[Profile](#profile)\> | 是 | 支持的预览配置信息。 | 164| photoProfiles | Array\<[Profile](#profile)\> | 是 | 支持的拍照配置信息。 | 165| videoProfiles | Array\<[VideoProfile](#videoprofile)\> | 是 | 支持的录像配置信息。 | 166| supportedMetadataObjectTypes | Array\<[MetadataObjectType](#metadataobjecttype)\> | 是 | 支持的metadata流类型信息。| 167 168## CameraErrorCode 169 170相机错误码。接口使用不正确以及on接口监听error状态返回。 171 172**系统能力:** SystemCapability.Multimedia.Camera.Core 173 174| 名称 | 值 | 说明 | 175| ------------------------- | ---- | ------------ | 176| INVALID_ARGUMENT | 7400101 | 参数缺失或者参数类型不对。 | 177| OPERATION_NOT_ALLOWED | 7400102 | 操作流程不对,不允许。 | 178| SESSION_NOT_CONFIG | 7400103 | session 未配置返回。 | 179| SESSION_NOT_RUNNING | 7400104 | session 未运行返回。 | 180| SESSION_CONFIG_LOCKED | 7400105 | session 配置已锁定返回。 | 181| DEVICE_SETTING_LOCKED | 7400106 | 设备设置已锁定返回。 | 182| CONFLICT_CAMERA | 7400107 | 设备重复打开返回。 | 183| DEVICE_DISABLED | 7400108 | 安全原因摄像头被禁用。 | 184| DEVICE_PREEMPTED | 7400109 | 相机被抢占导致无法使用 | 185| SERVICE_FATAL_ERROR | 7400201 | 相机服务错误返回。 | 186 187## CameraMode 188 189相机模式。 190 191**系统接口:** 此接口为系统接口。 192 193**系统能力:** SystemCapability.Multimedia.Camera.Core 194 195| 名称 | 值 | 说明 | 196| ----------------| ---- | ---------| 197| PORTRAIT | 1 | 人像模式 | 198 199## FilterType 200 201滤镜类型。 202 203**系统接口:** 此接口为系统接口。 204 205**系统能力:** SystemCapability.Multimedia.Camera.Core 206 207| 名称 | 值 | 说明 | 208| ----------------| ---- | ---------| 209| NONE | 0 | 原图。 | 210| CLASSIC | 1 | 经典。 | 211| DAWN | 2 | 晨光。 | 212| PURE | 3 | 清纯。 | 213| GREY | 4 | 灰调。 | 214| NATURAL | 5 | 自然。 | 215| MORI | 6 | 森系。 | 216| FAIR | 7 | 白皙。 | 217| PINK | 8 | 粉调。 | 218 219 220## PortraitEffect 221 222人像效果类型。 223 224**系统接口:** 此接口为系统接口。 225 226**系统能力:** SystemCapability.Multimedia.Camera.Core 227 228| 名称 | 值 | 说明 | 229| ----------------| ---- | ---------| 230| OFF | 0 | 关闭。 | 231| CIRCLES | 1 | 圆形。 | 232 233## BeautyType 234 235美颜类型。 236 237**系统接口:** 此接口为系统接口。 238 239**系统能力:** SystemCapability.Multimedia.Camera.Core 240 241| 名称 | 值 | 说明 | 242| ----------------| ---- | ---------| 243| AUTO | 0 | 自动。 | 244| SKIN_SMOOTH | 1 | 光滑。 | 245| FACE_SLENDER | 2 | 瘦脸。 | 246| SKIN_TONE | 3 | 肤色。 | 247 248## CameraManager 249 250相机管理器类,使用前需要通过getCameraManager获取相机管理实例。 251 252### getSupportedCameras 253 254getSupportedCameras(): Array\<CameraDevice\> 255 256获取支持指定的相机设备对象,同步返回结果。 257 258**系统能力:** SystemCapability.Multimedia.Camera.Core 259 260**返回值:** 261 262| 类型 | 说明 | 263| ----------------------------------------------- | ---------------------------- | 264| Array\<[CameraDevice](#cameradevice)> | 相机设备列表。 | 265 266**示例:** 267 268```ts 269import { BusinessError } from '@ohos.base'; 270 271function getSupportedCameras(cameraManager: camera.CameraManager): Array<camera.CameraDevice> { 272 let cameras: Array<camera.CameraDevice> = []; 273 try { 274 cameras = cameraManager.getSupportedCameras(); 275 } catch (error) { 276 let err = error as BusinessError; 277 console.error(`The getSupportedCameras call failed. error code: ${err.code}`); 278 } 279 return cameras; 280} 281``` 282 283### getSupportedOutputCapability 284 285getSupportedOutputCapability(camera:CameraDevice): CameraOutputCapability 286 287查询相机设备在模式下支持的输出能力,同步返回结果。 288 289**系统能力:** SystemCapability.Multimedia.Camera.Core 290 291**参数:** 292 293| 参数名 | 类型 | 必填 | 说明 | 294| ------------ |--------------------------------------------------------------- | -- | -------------------------- | 295| cameraDevice | [CameraDevice](#cameradevice) | 是 | 相机设备,通过 getSupportedCameras 接口获取 | 296 297**返回值:** 298 299| 类型 | 说明 | 300| ----------------------------------------------- | ---------------------------- | 301| [CameraOutputCapability](#cameraoutputcapability) | 相机输出能力。 | 302 303**示例:** 304 305```ts 306function getSupportedOutputCapability(cameraDevice: camera.CameraDevice, cameraManager: camera.CameraManager): camera.CameraOutputCapability { 307 let cameraOutputCapability: camera.CameraOutputCapability = cameraManager.getSupportedOutputCapability(cameraDevice); 308 return cameraOutputCapability; 309} 310``` 311 312### isCameraMuted 313 314isCameraMuted(): boolean 315 316查询相机当前的禁用状态(禁用/未禁用)。 317 318在此之前,需要通过[isCameraMuteSupported](#iscameramutesupported)确认当前设备支持禁用相机。 319 320**系统能力:** SystemCapability.Multimedia.Camera.Core 321 322**返回值:** 323 324| 类型 | 说明 | 325| ---------- | -------------------------------------------- | 326| boolean | 返回true表示相机被禁用,返回false表示相机未被禁用。 | 327 328**示例:** 329 330```ts 331function isCameraMuted(cameraManager: camera.CameraManager): boolean { 332 let isMuted: boolean = cameraManager.isCameraMuted(); 333 return isMuted; 334} 335``` 336 337### isCameraMuteSupported 338 339isCameraMuteSupported(): boolean 340 341查询当前设备是否支持禁用相机,通过返回值返回结果。 342 343**系统接口:** 此接口为系统接口。 344 345**系统能力:** SystemCapability.Multimedia.Camera.Core 346 347**返回值:** 348 349| 类型 | 说明 | 350| ---------- | ----------------------------- | 351| boolean | 返回true表示相机可以被禁用,返回false表示相机不能被禁用。 | 352 353**示例:** 354 355```ts 356function isCameraMuteSupported(cameraManager: camera.CameraManager): boolean { 357 let isMuteSupported: boolean = cameraManager.isCameraMuteSupported(); 358 return isMuteSupported; 359} 360``` 361 362### muteCamera 363 364muteCamera(mute: boolean): void 365 366禁用相机。 367 368**系统接口:** 此接口为系统接口。 369 370**系统能力:** SystemCapability.Multimedia.Camera.Core 371 372**参数:** 373 374| 参数名 | 类型 | 必填 | 说明 | 375| -------- | --------------------------------- | ---- | ---------- | 376| mute | boolean | 是 | 禁用相机。 | 377 378**示例:** 379 380```ts 381function muteCamera(cameraManager: camera.CameraManager): void { 382 let mute: boolean = true; 383 cameraManager.muteCamera(mute); 384} 385``` 386 387### createCameraInput 388 389createCameraInput(camera: CameraDevice): CameraInput 390 391使用CameraDevice对象创建CameraInput实例,同步返回结果。 392 393**需要权限:** ohos.permission.CAMERA 394 395**系统能力:** SystemCapability.Multimedia.Camera.Core 396 397**参数:** 398 399| 参数名 | 类型 | 必填 | 说明 | 400| -------- | ------------------------------------------- | ---- | --------------------------------- | 401| cameraDevice | [CameraDevice](#cameradevice) | 是 | CameraDevice对象,通过 getSupportedCameras 接口获取 | 402 403**返回值:** 404 405| 类型 | 说明 | 406| ---------- | ----------------------------- | 407| [CameraInput](#camerainput) | CameraInput实例。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 408 409**错误码:** 410 411以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 412 413| 错误码ID | 错误信息 | 414| --------------- | --------------- | 415| 7400101 | Parameter missing or parameter type incorrect | 416 417**示例:** 418 419```ts 420import { BusinessError } from '@ohos.base'; 421 422function createCameraInput(cameraDevice: camera.CameraDevice, cameraManager: camera.CameraManager): camera.CameraInput { 423 let cameraInput: camera.CameraInput; 424 try { 425 cameraInput = cameraManager.createCameraInput(cameraDevice); 426 } catch (error) { 427 // 失败返回错误码error.code并处理 428 let err = error as BusinessError; 429 console.error(`The createCameraInput call failed. error code: ${err.code}`); 430 } 431 return cameraInput; 432} 433``` 434 435### createCameraInput 436 437createCameraInput(position: CameraPosition, type: CameraType): CameraInput 438 439根据相机位置和类型创建CameraInput实例,同步返回结果。 440 441**需要权限:** ohos.permission.CAMERA 442 443**系统能力:** SystemCapability.Multimedia.Camera.Core 444 445**参数:** 446 447| 参数名 | 类型 | 必填 | 说明 | 448| -------- | ------------------------------------------- | ---- | --------------------------------- | 449| position | [CameraPosition](#cameraposition) | 是 | 相机位置,通过 getSupportedCameras 接口获取设备,然后获取设备位置信息 | 450| type | [CameraType](#cameratype) | 是 | 相机类型,通过 getSupportedCameras 接口获取设备,然后获取设备类型信息 | 451 452**返回值:** 453 454| 类型 | 说明 | 455| ---------- | ----------------------------- | 456| [CameraInput](#camerainput) | CameraInput实例。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 457 458**错误码:** 459 460以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 461 462| 错误码ID | 错误信息 | 463| --------------- | --------------- | 464| 7400101 | Parameter missing or parameter type incorrect | 465 466**示例:** 467 468```ts 469import { BusinessError } from '@ohos.base'; 470 471function createCameraInput(cameraDevice: camera.CameraDevice, cameraManager: camera.CameraManager): camera.CameraInput | undefined { 472 let position: camera.CameraPosition = cameraDevice.cameraPosition; 473 let type: camera.CameraType = cameraDevice.cameraType; 474 let cameraInput: camera.CameraInput | undefined = undefined; 475 try { 476 cameraInput = cameraManager.createCameraInput(position, type); 477 } catch (error) { 478 // 失败返回错误码error.code并处理 479 let err = error as BusinessError; 480 console.log(`The createCameraInput call failed. error code: ${err.code}`); 481 } 482 return cameraInput; 483} 484``` 485 486### createPreviewOutput 487 488createPreviewOutput(profile: Profile, surfaceId: string): PreviewOutput 489 490创建预览输出对象,同步返回结果。 491 492**系统能力:** SystemCapability.Multimedia.Camera.Core 493 494**参数:** 495 496| 参数名 | 类型 | 必填 | 说明 | 497| -------- | ----------------------------------------------- | ---- | ------------------------------- | 498| profile | [Profile](#profile) | 是 | 支持的预览配置信息,通过getSupportedOutputCapability接口获取。| 499| surfaceId| string | 是 | 从[XComponent](../arkui-ts/ts-basic-components-xcomponent.md)或者[ImageReceiver](js-apis-image.md#imagereceiver9)组件获取的surfaceId。| 500 501**返回值:** 502 503| 类型 | 说明 | 504| ---------- | ----------------------------- | 505| [PreviewOutput](#previewoutput) | PreviewOutput实例。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 506 507**错误码:** 508 509以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 510 511| 错误码ID | 错误信息 | 512| --------------- | --------------- | 513| 7400101 | Parameter missing or parameter type incorrect | 514 515**示例:** 516 517```ts 518import { BusinessError } from '@ohos.base'; 519 520function createPreviewOutput(cameraOutputCapability: camera.CameraOutputCapability, cameraManager: camera.CameraManager, surfaceId: string): camera.PreviewOutput | undefined { 521 let profile: camera.Profile = cameraOutputCapability.previewProfiles[0]; 522 let previewOutput: camera.PreviewOutput | undefined = undefined; 523 try { 524 previewOutput = cameraManager.createPreviewOutput(profile, surfaceId); 525 } catch (error) { 526 // 失败返回错误码error.code并处理 527 let err = error as BusinessError; 528 console.log(`The createPreviewOutput call failed. error code: ${err.code}`); 529 } 530 return previewOutput; 531} 532``` 533 534### createPhotoOutput 535 536createPhotoOutput(profile: Profile, surfaceId: string): PhotoOutput 537 538创建拍照输出对象,同步返回结果。 539 540**系统能力:** SystemCapability.Multimedia.Camera.Core 541 542**参数:** 543 544| 参数名 | 类型 | 必填 | 说明 | 545| -------- | ------------------------------------------- | ---- | ----------------------------------- | 546| profile | [Profile](#profile) | 是 | 支持的拍照配置信息,通过getSupportedOutputCapability接口获取。| 547| surfaceId| string | 是 | 从[ImageReceiver](js-apis-image.md#imagereceiver9)获取的surfaceId。| 548 549**返回值:** 550 551| 类型 | 说明 | 552| ---------- | ----------------------------- | 553| [PhotoOutput](#photooutput) | PhotoOutput实例。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 554 555**错误码:** 556 557以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 558 559| 错误码ID | 错误信息 | 560| --------------- | --------------- | 561| 7400101 | Parameter missing or parameter type incorrect | 562 563**示例:** 564 565```ts 566import { BusinessError } from '@ohos.base'; 567 568function createPhotoOutput(cameraOutputCapability: camera.CameraOutputCapability, cameraManager: camera.CameraManager, surfaceId: string): camera.PhotoOutput | undefined { 569 let profile: camera.Profile = cameraOutputCapability.photoProfiles[0]; 570 let photoOutput: camera.PhotoOutput | undefined = undefined; 571 try { 572 photoOutput = cameraManager.createPhotoOutput(profile, surfaceId); 573 } catch (error) { 574 // 失败返回错误码error.code并处理 575 let err = error as BusinessError; 576 console.log(`The createPhotoOutput call failed. error code: ${err.code}`); 577 } 578 return photoOutput; 579} 580``` 581 582### createVideoOutput 583 584createVideoOutput(profile: VideoProfile, surfaceId: string): VideoOutput 585 586创建录像输出对象,同步返回结果。 587 588**系统能力:** SystemCapability.Multimedia.Camera.Core 589 590**参数:** 591 592| 参数名 | 类型 | 必填 | 说明 | 593| -------- | ------------------------------------------- | ---- | ------------------------------ | 594| profile | [VideoProfile](#videoprofile) | 是 | 支持的录像配置信息,通过getSupportedOutputCapability接口获取。 | 595| surfaceId| string | 是 | 从[VideoRecorder](js-apis-media.md#videorecorder9)获取的surfaceId。| 596 597**返回值:** 598 599| 类型 | 说明 | 600| ---------- | ----------------------------- | 601| [VideoOutput](#videooutput) | VideoOutput实例。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 602 603**错误码:** 604 605以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 606 607| 错误码ID | 错误信息 | 608| --------------- | --------------- | 609| 7400101 | Parameter missing or parameter type incorrect | 610 611**示例:** 612 613```ts 614import { BusinessError } from '@ohos.base'; 615 616function createVideoOutput(cameraOutputCapability: camera.CameraOutputCapability, cameraManager: camera.CameraManager, surfaceId: string): camera.VideoOutput | undefined { 617 let profile: camera.VideoProfile = cameraOutputCapability.videoProfiles[0]; 618 let videoOutput: camera.VideoOutput | undefined = undefined; 619 try { 620 videoOutput = cameraManager.createVideoOutput(profile, surfaceId); 621 } catch (error) { 622 // 失败返回错误码error.code并处理 623 let err = error as BusinessError; 624 console.log(`The createPhotoOutput call failed. error code: ${err.code}`); 625 } 626 return videoOutput; 627} 628``` 629 630### createMetadataOutput 631 632createMetadataOutput(metadataObjectTypes:Array\<MetadataObjectType\>): MetadataOutput 633 634创建metadata流输出对象,同步返回结果。 635 636**系统能力:** SystemCapability.Multimedia.Camera.Core 637 638**参数:** 639 640| 参数名 | 类型 | 必填 | 说明 | 641| -------------------- | -------------------------------------------------- | --- | ---------------------------- | 642| metadataObjectTypes | Array\<[MetadataObjectType](#metadataobjecttype)\> | 是 | metadata流类型信息,通过getSupportedOutputCapability接口获取。 | 643 644**返回值:** 645 646| 类型 | 说明 | 647| ---------- | ----------------------------- | 648| [MetadataOutput](#metadataoutput) | MetadataOutput实例。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 649 650**错误码:** 651 652以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 653 654| 错误码ID | 错误信息 | 655| --------------- | --------------- | 656| 7400101 | Parameter missing or parameter type incorrect | 657 658**示例:** 659 660```ts 661import { BusinessError } from '@ohos.base'; 662 663function createMetadataOutput(cameraManager: camera.CameraManager, cameraOutputCapability: camera.CameraOutputCapability): void { 664 let metadataObjectTypes: Array<camera.MetadataObjectType> = cameraOutputCapability.supportedMetadataObjectTypes; 665 let metadataOutput: camera.MetadataOutput; 666 try { 667 metadataOutput = cameraManager.createMetadataOutput(metadataObjectTypes); 668 } catch (error) { 669 // 失败返回错误码error.code并处理 670 let err = error as BusinessError; 671 console.log(`createMetadataOutput error. error code: ${err.code}`); 672 } 673} 674``` 675 676### createCaptureSession 677 678createCaptureSession(): CaptureSession 679 680创建CaptureSession实例,通过注册回调函数获取结果。 681 682**系统能力:** SystemCapability.Multimedia.Camera.Core 683 684**返回值:** 685 686| 类型 | 说明 | 687| ---------- | ----------------------------- | 688| [CaptureSession](#capturesession) | CaptureSession实例。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 689 690**错误码:** 691 692以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 693 694| 错误码ID | 错误信息 | 695| --------------- | --------------- | 696| 7400201 | Camera service fatal error. | 697 698**示例:** 699 700```ts 701import { BusinessError } from '@ohos.base'; 702 703function createCaptureSession(cameraManager: camera.CameraManager): camera.CaptureSession | undefined { 704 let captureSession: camera.CaptureSession | undefined = undefined; 705 try { 706 captureSession = cameraManager.createCaptureSession(); 707 } catch (error) { 708 // 失败返回错误码error.code并处理 709 let err = error as BusinessError; 710 console.log(`createCaptureSession error. error code: ${err.code}`); 711 } 712 return captureSession; 713} 714``` 715 716### on('cameraStatus') 717 718on(type: 'cameraStatus', callback: AsyncCallback\<CameraStatusInfo\>): void 719 720相机设备状态回调,通过注册回调函数获取相机的状态变化。 721 722**系统能力:** SystemCapability.Multimedia.Camera.Core 723 724**参数:** 725 726| 参数名 | 类型 | 必填 | 说明 | 727| -------- | -----------------| ---- | --------- | 728| type | string | 是 | 监听事件,固定为'cameraStatus'。cameraManager对象获取成功后可监听。目前只支持对设备打开或者关闭会触发该事件并返回对应信息 | 729| callback | AsyncCallback\<[CameraStatusInfo](#camerastatusinfo)\> | 是 | 回调函数,用于获取镜头状态变化信息。 | | 730 731**示例:** 732 733```ts 734import { BusinessError } from '@ohos.base'; 735 736function registerCameraStatus(cameraManager: camera.CameraManager): void { 737 cameraManager.on('cameraStatus', (err: BusinessError, cameraStatusInfo: camera.CameraStatusInfo) => { 738 console.log(`camera : ${cameraStatusInfo.camera.cameraId}`); 739 console.log(`status: ${cameraStatusInfo.status}`); 740 }); 741} 742``` 743 744### off('cameraStatus') 745 746off(type: 'cameraStatus', callback?: AsyncCallback\<CameraStatusInfo\>): void 747 748相机设备状态注销回调,通过注销回调函数取消获取相机的状态变化。 749 750**系统能力:** SystemCapability.Multimedia.Camera.Core 751 752**参数:** 753 754| 参数名 | 类型 | 必填 | 说明 | 755| -------- | -----------------| ---- | --------- | 756| type | string | 是 | 监听事件,固定为'cameraStatus'。cameraManager对象获取成功后可监听。 | 757| callback | AsyncCallback\<[CameraStatusInfo](#camerastatusinfo)\> | 否 | 可选参数,有表明匹配on接口callback。 | | 758 759**示例:** 760 761```ts 762function unregisterCameraStatus(cameraManager: camera.CameraManager): void { 763 cameraManager.off('cameraStatus'); 764} 765``` 766 767### on('cameraMute') 768 769on(type: 'cameraMute', callback: AsyncCallback\<boolean\>): void 770 771禁用回调,通过注册回调函数获取相机禁用状态变化。 772 773**系统接口:** 此接口为系统接口。 774 775**系统能力:** SystemCapability.Multimedia.Camera.Core 776 777**参数:** 778 779| 参数名 | 类型 | 必填 | 说明 | 780| -------- | --------------- | ---- | --------- | 781| type | string | 是 | 监听事件,固定为'cameraMute',系统相机摄像头开关,cameraManager对象获取成功后可监听。系统设置打开或禁用相机会触发该事件并返回状态 | 782| callback | AsyncCallback\<boolean> | 是 | 回调函数,用于获取禁用状态变化信息,返回true是开启状态,返回false是禁用状态。 | 783 784**示例:** 785 786```ts 787import { BusinessError } from '@ohos.base'; 788 789function registerCameraMute(cameraManager: camera.CameraManager): void { 790 cameraManager.on('cameraMute', (err: BusinessError, curMuted: boolean) => { 791 let isMuted: boolean = curMuted; 792 console.log(`cameraMute status: ${isMuted}`); 793 }) 794} 795``` 796 797### off('cameraMute') 798 799off(type: 'cameraMute', callback?: AsyncCallback\<boolean\>): void 800 801禁用回调,通过注销注册回调函数获取相机禁用状态变化。 802 803**系统接口:** 此接口为系统接口。 804 805**系统能力:** SystemCapability.Multimedia.Camera.Core 806 807**参数:** 808 809| 参数名 | 类型 | 必填 | 说明 | 810| -------- | --------------- | ---- | --------- | 811| type | string | 是 | 监听事件,固定为'cameraMute',系统相机摄像头开关,cameraManager对象获取成功后可监听。 | 812| callback | AsyncCallback\<boolean> | 否 | 回调函数,可选参数,有表明匹配on接口callback。 | 813 814**示例:** 815 816```ts 817import { BusinessError } from '@ohos.base'; 818 819function callback(err: BusinessError, curMuetd: boolean): void { 820 let isMuted: boolean = curMuetd; 821} 822 823function unregisterCameraMute(cameraManager: camera.CameraManager): void { 824 cameraManager.on('cameraMute', callback); 825 cameraManager.off('cameraMute', callback); 826} 827``` 828 829### isPrelaunchSupported 830 831isPrelaunchSupported(camera: CameraDevice): boolean 832 833**系统接口:** 此接口为系统接口。 834 835**系统能力:** SystemCapability.Multimedia.Camera.Core 836 837**参数:** 838 839| 参数名 | 类型 | 必填 | 说明 | 840| -------- | --------------- | ---- | --------- | 841| camera | [CameraDevice](#cameradevice) | 是 | 相机信息。| 842 843**返回值:** 844 845| 类型 | 说明 | 846| -------- | --------------- | 847| boolean | 返回指定cameraDevice是否支持预热启动。| 848 849**错误码:** 850 851以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。。 852 853| 错误码ID | 错误信息 | 854| --------------- | --------------- | 855| 7400101 | Parameter missing or parameter type incorrect. | 856 857**示例:** 858 859```ts 860import common from '@ohos.app.ability.common'; 861 862function isPreLaunchSupported(context: common.BaseContext): boolean { 863 let cameraManager: camera.CameraManager = camera.getCameraManager(context); 864 let cameras: Array<camera.CameraDevice> = cameraManager.getSupportedCameras(); 865 let isSupported: boolean = false; 866 if (cameras && cameras.length >= 1) { 867 isSupported = cameraManager.isPrelaunchSupported(cameras[0]); 868 console.log(`PreLaunch supported states: ${isSupported}`); 869 return isSupported; 870 } 871 return isSupported; 872} 873``` 874 875### setPrelaunchConfig 876 877setPrelaunchConfig(prelaunchConfig: PrelaunchConfig): void 878 879在调用setPrelaunchConfig接口前,需要先检查设备是否支持预热启动,使用[isPrelaunchSupported](#isprelaunchsupported)判断指定cameraDevice是否支持预热启动。 880 881**系统接口:** 此接口为系统接口。 882 883**需要权限:** ohos.permission.CAMERA 884 885**系统能力:** SystemCapability.Multimedia.Camera.Core 886 887**参数:** 888 889| 参数名 | 类型 | 必填 | 说明 | 890| -------- | --------------- | ---- | --------- | 891| prelaunchConfig | [PrelaunchConfig](#prelaunchconfig) | 是 | 预启动配置参数。| 892 893**错误码:** 894 895以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。。 896 897| 错误码ID | 错误信息 | 898| --------------- | --------------- | 899| 7400101 | Parameter missing or parameter type incorrect. | 900| 7400102 | Operation not allow. | 901 902**示例:** 903 904```ts 905import common from '@ohos.app.ability.common'; 906import { BusinessError } from '@ohos.base'; 907 908function setPrelaunchConfig(context: common.BaseContext): void { 909 let cameraManager: camera.CameraManager = camera.getCameraManager(context); 910 let cameras: Array<camera.CameraDevice> = cameraManager.getSupportedCameras(); 911 if (cameras && cameras.length >= 1) { 912 let cameraDevice: camera.CameraDevice = cameras[0]; 913 if(cameraManager.isPrelaunchSupported(cameraDevice)) { 914 try { 915 cameraManager.setPrelaunchConfig({cameraDevice: cameraDevice}); 916 } catch (error) { 917 let err = error as BusinessError; 918 console.error(`setPrelaunchConfig error. Code: ${err.code}, message: ${err.message}`); 919 } 920 } 921 } 922} 923``` 924 925### prelaunch 926 927prelaunch(): void 928 929用户点击系统相机图标,拉起相机应用同时调用,下发预热请求,使能相机预热启动。 930 931**系统接口:** 此接口为系统接口。 932 933**系统能力:** SystemCapability.Multimedia.Camera.Core 934 935**示例:** 936 937```ts 938import common from '@ohos.app.ability.common'; 939import { BusinessError } from '@ohos.base'; 940 941function preLaunch(context: common.BaseContext): void { 942 let cameraManager: camera.CameraManager = camera.getCameraManager(context); 943 try { 944 cameraManager.prelaunch(); 945 } catch (error) { 946 let err = error as BusinessError; 947 console.error(`prelaunch error. Code: ${err.code}, message: ${err.message}`); 948 } 949} 950``` 951 952### createDeferredPreviewOutput 953 954createDeferredPreviewOutput(profile: Profile): PreviewOutput 955 956创建延迟预览输出对象,在配流时替代普通的预览输出对象加入数据流。 957 958**系统接口:** 此接口为系统接口。 959 960**系统能力:** SystemCapability.Multimedia.Camera.Core 961 962**参数:** 963 964| 参数名 | 类型 | 必填 | 说明 | 965| -------- | --------------- | ---- | --------- | 966| profile | [Profile](#profile) | 是 | 相机预览流的配置文件。 | 967 968**返回值:** 969 970| 类型 | 说明 | 971| -------- | --------------- | 972| [PreviewOutput](#previewoutput) | 返回预览输出对象。 | 973 974**错误码:** 975 976以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。。 977 978| 错误码ID | 错误信息 | 979| --------------- | --------------- | 980| 7400101 | Parameter missing or parameter type incorrect. | 981 982**示例:** 983 984```ts 985import common from '@ohos.app.ability.common'; 986 987function getDeferredPreviewOutput(context: common.BaseContext, previewProfile: camera.Profile): camera.PreviewOutput { 988 const cameraManager: camera.CameraManager = camera.getCameraManager(context); 989 const output: camera.PreviewOutput = cameraManager.createDeferredPreviewOutput(previewProfile); 990 return output; 991} 992``` 993## ModeManager 994 995相机模式化管理器类,使用前需要通过[getModeManager](#cameragetmodemanager)获取相机模式化管理实例。 996 997### getSupportedModes 998 999getSupportedModes(device: CameraDevice): Array\<CameraMode\> 1000 1001获取指定相机设备支持的模式列表,同步返回结果。 1002 1003**系统接口:** 此接口为系统接口。 1004 1005**系统能力:** SystemCapability.Multimedia.Camera.Core 1006 1007**参数:** 1008 1009| 参数名 | 类型 | 必填 | 说明 | 1010| -------- | -------------------------- | ---- | ------------------- | 1011| device | [CameraDevice](#cameradevice) | 是 | 相机设备实例,通过[getSupportedCameras](#getsupportedcameras)接口获取。 | 1012 1013**返回值:** 1014 1015| 类型 | 说明 | 1016| ----------------------------------------------- | ---------------------------- | 1017| Array\<[CameraMode](#cameramode)> | 支持的模式列表。 | 1018 1019**示例:** 1020 1021```ts 1022function getSupportedModes(modeManager: camera.ModeManager, cameraDevice: camera.CameraDevice): Array<camera.CameraMode> { 1023 let cameraModes: Array<camera.CameraMode> = modeManager.getSupportedModes(cameraDevice); 1024 return cameraModes; 1025} 1026``` 1027 1028### getSupportedOutputCapability 1029 1030getSupportedOutputCapability(device:CameraDevice, mode: CameraMode): CameraOutputCapability 1031 1032获取指定模式下相机设备支持的输出能力,同步返回结果。 1033 1034**系统接口:** 此接口为系统接口。 1035 1036**系统能力:** SystemCapability.Multimedia.Camera.Core 1037 1038**参数:** 1039 1040| 参数名 | 类型 | 必填 | 说明 | 1041| ------------ |--------------------------------------------------------------- | -- | -------------------------- | 1042| device | [CameraDevice](#cameradevice) | 是 | 相机设备,通过[getSupportedCameras](#getsupportedcameras)接口获取。 | 1043| mode | [CameraMode](#cameramode) | 是 | 指定模式,通过[getSupportedModes](#getsupportedmodes)接口获取。 | 1044 1045**返回值:** 1046 1047| 类型 | 说明 | 1048| ----------------------------------------------- | ---------------------------- | 1049| [CameraOutputCapability](#cameraoutputcapability) | 相机输出能力。 | 1050 1051**示例:** 1052 1053```ts 1054function getSupportedOutputCapability(cameraManager: camera.CameraManager, modeManager: camera.ModeManager): camera.CameraOutputCapability | undefined { 1055 let cameras: Array<camera.CameraDevice> = cameraManager.getSupportedCameras(); 1056 if (cameras == undefined || cameras.length <= 0) { 1057 return undefined; 1058 } 1059 let cameraDevice: camera.CameraDevice = cameras[0]; 1060 let cameraModes: Array<camera.CameraMode> = modeManager.getSupportedModes(cameraDevice); 1061 if (cameraModes === undefined || cameraModes.length <= 0) { 1062 return undefined; 1063 } 1064 let mode: camera.CameraMode = cameraModes[0]; 1065 let cameraOutputCapability: camera.CameraOutputCapability = modeManager.getSupportedOutputCapability(cameraDevice, mode); 1066 return cameraOutputCapability; 1067} 1068``` 1069### createCaptureSession 1070 1071createCaptureSession(mode: CameraMode): CaptureSession 1072 1073根据当前的模式名,创建指定模式的会话。 1074 1075**系统接口:** 此接口为系统接口。 1076 1077**系统能力:** SystemCapability.Multimedia.Camera.Core 1078 1079**参数:** 1080 1081| 参数名 | 类型 | 必填 | 说明 | 1082| ------------ |--------------------------------------------------------------- | -- | -------------------------- | 1083| mode | [CameraMode](#cameramode) | 是 | 指定模式,通过[getSupportedModes](#getsupportedmodes)获取。 | 1084 1085**返回值:** 1086 1087| 类型 | 说明 | 1088| ----------------------------------------------- | ---------------------------- | 1089| [CaptureSession](#capturesession) | 指定模式的会话实例。 | 1090 1091**示例:** 1092 1093```ts 1094import { BusinessError } from '@ohos.base'; 1095 1096function createCaptureSession(cameraManager: camera.CameraManager, modeManager: camera.ModeManager): camera.CaptureSession { 1097 let cameras: Array<camera.CameraDevice> = cameraManager.getSupportedCameras(); 1098 if (cameras == undefined || cameras.length <= 0) { 1099 return; 1100 } 1101 let cameraDevice: camera.CameraDevice = cameras[0]; 1102 let cameraModes: Array<camera.CameraMode> = modeManager.getSupportedModes(cameraDevice); 1103 if (cameraModes === undefined || cameraModes.length <= 0) { 1104 return; 1105 } 1106 let mode: camera.CameraMode = cameraModes[0]; 1107 let captureSession: camera.CaptureSession 1108 try { 1109 captureSession = modeManager.createCaptureSession(mode); 1110 } catch (error) { 1111 let err = error as BusinessError; 1112 console.error(`The createCaptureSession call failed. error code: ${err.code}`); 1113 } 1114 return captureSession; 1115} 1116``` 1117 1118## PrelaunchConfig 1119 1120相机预启动配置参数。 1121 1122当前sensor级别预热,待扩展流预热参数。 1123 1124**系统接口:** 此接口为系统接口。 1125 1126**系统能力:** SystemCapability.Multimedia.Camera.Core 1127 1128| 名称 | 类型 | 必填 | 说明 | 1129| ------ | ----------------------------- | -------------- | ---------- | 1130| cameraDevice | [CameraDevice](#cameradevice) | 是 | 相机信息。 | 1131 1132## CameraStatusInfo 1133 1134相机管理器回调返回的接口实例,表示相机状态信息。 1135 1136**系统能力:** SystemCapability.Multimedia.Camera.Core。 1137 1138| 名称 | 类型 | 必填 | 说明 | 1139| ------ | ----------------------------- | -------------- | ---------- | 1140| camera | [CameraDevice](#cameradevice) | 是 | 相机信息。 | 1141| status | [CameraStatus](#camerastatus) | 是 | 相机状态。 | 1142 1143## CameraPosition 1144 1145枚举,相机位置。 1146 1147**系统能力:** SystemCapability.Multimedia.Camera.Core 1148 1149| 名称 | 值 | 说明 | 1150| --------------------------- | ---- | -------------- | 1151| CAMERA_POSITION_UNSPECIFIED | 0 | 相机位置未指定。 | 1152| CAMERA_POSITION_BACK | 1 | 后置相机。 | 1153| CAMERA_POSITION_FRONT | 2 | 前置相机。 | 1154 1155## CameraType 1156 1157枚举,相机类型。 1158 1159**系统能力:** SystemCapability.Multimedia.Camera.Core 1160 1161| 名称 | 值 | 说明 | 1162| ----------------------- | ---- | -------------- | 1163| CAMERA_TYPE_DEFAULT | 0 | 相机类型未指定。 | 1164| CAMERA_TYPE_WIDE_ANGLE | 1 | 广角相机。 | 1165| CAMERA_TYPE_ULTRA_WIDE | 2 | 超广角相机。 | 1166| CAMERA_TYPE_TELEPHOTO | 3 | 长焦相机。 | 1167| CAMERA_TYPE_TRUE_DEPTH | 4 | 带景深信息的相机。 | 1168 1169## ConnectionType 1170 1171枚举,相机连接类型。 1172 1173**系统能力:** SystemCapability.Multimedia.Camera.Core 1174 1175| 名称 | 值 | 说明 | 1176| ---------------------------- | ---- | ------------- | 1177| CAMERA_CONNECTION_BUILT_IN | 0 | 内置相机。 | 1178| CAMERA_CONNECTION_USB_PLUGIN | 1 | USB连接的相机。 | 1179| CAMERA_CONNECTION_REMOTE | 2 | 远程连接的相机。 | 1180 1181## HostDeviceType 1182 1183枚举,远端相机设备类型。 1184 1185**系统接口:** 此接口为系统接口。 1186 1187**系统能力:** SystemCapability.Multimedia.Camera.Core 1188 1189| 名称 | 值 | 说明 | 1190| ---------------------------- | ---- | ------------- | 1191| UNKNOWN_TYPE | 0 | 未知设备类型。 | 1192| PHONE | 0x0E | 智能手机相机设备类型。 | 1193| TABLET | 0x11 | 平板电脑相机设备类型。 | 1194 1195## CameraDevice 1196 1197相机设备信息。 1198 1199**系统能力:** SystemCapability.Multimedia.Camera.Core。 1200 1201| 名称 | 类型 | 必填 | 说明 | 1202| -------------- | --------------------------------- | ---- | ---------- | 1203| cameraId | string | 是 | CameraDevice对象| 1204| cameraPosition | [CameraPosition](#cameraposition) | 是 | 相机位置。 | 1205| cameraType | [CameraType](#cameratype) | 是 | 相机类型。 | 1206| connectionType | [ConnectionType](#connectiontype) | 是 | 相机连接类型。 | 1207| hostDeviceName | string | 是 | 远端设备名称,**系统接口:** 此接口为系统接口。 | 1208| hostDeviceType | [hostDeviceType](#hostdevicetype) | 是 | 远端相机设备类型,**系统接口:** 此接口为系统接口。 | 1209 1210## Size 1211 1212枚举,输出能力查询。 1213 1214**系统能力:** SystemCapability.Multimedia.Camera.Core 1215 1216| 名称 | 类型 | 可读 | 可写 | 说明 | 1217| ------ | ------ | ---- | ---- | ------------ | 1218| height | number | 是 | 是 | 图像尺寸高(像素)。 | 1219| width | number | 是 | 是 | 图像尺寸宽(像素)。 | 1220 1221## Point 1222 1223枚举,点坐标用于对焦、曝光配置。 1224 1225**系统能力:** SystemCapability.Multimedia.Camera.Core 1226 1227| 名称 | 类型 | 必填 | 说明 | 1228| ------ | ------ | ---- | ------------ | 1229| x | number | 是 | 点的x坐标。 | 1230| y | number | 是 | 点的y坐标。 | 1231 1232## CameraFormat 1233 1234枚举,输出格式。 1235 1236**系统能力:** SystemCapability.Multimedia.Camera.Core 1237 1238| 名称 | 值 | 说明 | 1239| ----------------------- | --------- | ------------ | 1240| CAMERA_FORMAT_RGBA_8888 | 3 | RGB格式的图片。 | 1241| CAMERA_FORMAT_YUV_420_SP| 1003 | YUV 420 SP格式的图片。 | 1242| CAMERA_FORMAT_JPEG | 2000 | JPEG格式的图片。 | 1243 1244## CameraInput 1245 1246会话中[CaptureSession](#capturesession)使用的相机信息。 1247 1248### open 1249 1250open\(callback: AsyncCallback\<void\>\): void 1251 1252打开相机,通过注册回调函数获取状态。 1253 1254**系统能力:** SystemCapability.Multimedia.Camera.Core 1255 1256**参数:** 1257 1258| 参数名 | 类型 | 必填 | 说明 | 1259| -------- | -------------------- | ---- | ------------------- | 1260| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1261 1262**错误码:** 1263 1264以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1265 1266| 错误码ID | 错误信息 | 1267| --------------- | --------------- | 1268| 7400107 | Can not use camera cause of conflict. | 1269| 7400108 | Camera disabled cause of security reason. | 1270| 7400201 | Camera service fatal error. | 1271 1272**示例:** 1273 1274```ts 1275import { BusinessError } from '@ohos.base'; 1276 1277function openCameraInput(cameraInput: camera.CameraInput): void { 1278 cameraInput.open((err: BusinessError) => { 1279 if (err) { 1280 console.error(`Failed to open the camera. ${err.code}`); 1281 return; 1282 } 1283 console.log('Callback returned with camera opened.'); 1284 }); 1285} 1286``` 1287 1288### open 1289 1290open(): Promise\<void\> 1291 1292打开相机,通过Promise获取相机的状态。 1293 1294**系统能力:** SystemCapability.Multimedia.Camera.Core 1295 1296**返回值:** 1297 1298| 类型 | 说明 | 1299| -------------- | ----------------------- | 1300| Promise\<void\> | 使用Promise的方式获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1301 1302**错误码:** 1303 1304以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1305 1306| 错误码ID | 错误信息 | 1307| --------------- | --------------- | 1308| 7400107 | Can not use camera cause of conflict. | 1309| 7400108 | Camera disabled cause of security reason. | 1310| 7400201 | Camera service fatal error. | 1311 1312**示例:** 1313 1314```ts 1315import { BusinessError } from '@ohos.base'; 1316 1317function openCameraInput(cameraInput: camera.CameraInput): void { 1318 cameraInput.open().then(() => { 1319 console.log('Promise returned with camera opened.'); 1320 }).catch((err: BusinessError) => { 1321 console.error(`Failed to open the camera. ${err.code}`); 1322 }); 1323} 1324``` 1325 1326### close 1327 1328close\(callback: AsyncCallback\<void\>\): void 1329 1330关闭相机,通过注册回调函数获取状态。 1331 1332**系统能力:** SystemCapability.Multimedia.Camera.Core 1333 1334**参数:** 1335 1336| 参数名 | 类型 | 必填 | 说明 | 1337| -------- | -------------------- | ---- | -------------------- | 1338| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1339 1340**错误码:** 1341 1342以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1343 1344| 错误码ID | 错误信息 | 1345| --------------- | --------------- | 1346| 7400201 | Camera service fatal error. | 1347 1348**示例:** 1349 1350```ts 1351import { BusinessError } from '@ohos.base'; 1352 1353function closeCameraInput(cameraInput: camera.CameraInput): void { 1354 cameraInput.close((err: BusinessError) => { 1355 if (err) { 1356 console.error(`Failed to close the cameras. ${err.code}`); 1357 return; 1358 } 1359 console.log('Callback returned with camera closed.'); 1360 }); 1361} 1362``` 1363 1364### close 1365 1366close(): Promise\<void\> 1367 1368关闭相机,通过Promise获取状态。 1369 1370**系统能力:** SystemCapability.Multimedia.Camera.Core 1371 1372**返回值:** 1373 1374| 类型 | 说明 | 1375| -------------- | ----------------------- | 1376| Promise\<void\> | 使用Promise的方式获取结果。 | 1377 1378**错误码:** 1379 1380以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1381 1382| 错误码ID | 错误信息 | 1383| --------------- | --------------- | 1384| 7400201 | Camera service fatal error. | 1385 1386**示例:** 1387 1388```ts 1389import { BusinessError } from '@ohos.base'; 1390 1391function closeCameraInput(cameraInput: camera.CameraInput): void { 1392 cameraInput.close().then(() => { 1393 console.log('Promise returned with camera closed.'); 1394 }).catch((err: BusinessError) => { 1395 console.error(`Failed to close the cameras. ${err.code}`); 1396 }); 1397} 1398``` 1399 1400### on('error') 1401 1402on(type: 'error', camera:CameraDevice, callback: ErrorCallback): void 1403 1404监听CameraInput的错误事件,通过注册回调函数获取结果。 1405 1406**系统能力:** SystemCapability.Multimedia.Camera.Core 1407 1408**参数:** 1409 1410| 参数名 | 类型 | 必填 | 说明 | 1411| -------- | -------------------------------- | --- | ------------------------------------------- | 1412| type | string | 是 | 监听事件,固定为'error',CameraInput对象创建成功可监听。相机设备出错情况下可触发该事件并返回结果,比如(设备不可用或者冲突等返回对应错误信息) | 1413| cameraDevice | [CameraDevice](#cameradevice) | 是 | CameraDevice对象。 | 1414| callback | ErrorCallback | 是 | 回调函数,用于获取结果。返回错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1415 1416**示例:** 1417 1418```ts 1419import { BusinessError } from '@ohos.base'; 1420 1421function registerCameraInputError(cameraInput: camera.CameraInput, cameraDevice: camera.CameraDevice): void { 1422 cameraInput.on('error', cameraDevice, (error: BusinessError) => { 1423 console.log(`Camera input error code: ${error.code}`); 1424 }); 1425} 1426``` 1427 1428### off('error') 1429 1430off(type: 'error', camera:CameraDevice, callback?: ErrorCallback): void 1431 1432注销监听CameraInput的错误事件。 1433 1434**系统能力:** SystemCapability.Multimedia.Camera.Core 1435 1436**参数:** 1437 1438| 参数名 | 类型 | 必填 | 说明 | 1439| -------- | -------------------------------- | --- | ------------------------------------------- | 1440| type | string | 是 | 监听事件,固定为'error',CameraInput对象创建成功可监听。相机设备出错情况下可触发该事件并返回结果,比如(设备不可用或者冲突等返回对应错误信息) | 1441| cameraDevice | [CameraDevice](#cameradevice) | 是 | CameraDevice对象。 | 1442| callback | ErrorCallback | 否 | 回调函数,可选,有就是匹配on('error') callback | 1443 1444**示例:** 1445 1446```ts 1447 1448function unregisterCameraInputError(cameraInput: camera.CameraInput, cameraDevice: camera.CameraDevice): void { 1449 cameraInput.off('error', cameraDevice); 1450} 1451``` 1452 1453## FlashMode 1454 1455枚举,闪光灯模式。 1456 1457**系统能力:** SystemCapability.Multimedia.Camera.Core。 1458 1459| 名称 | 值 | 说明 | 1460| ---------------------- | ---- | ---------- | 1461| FLASH_MODE_CLOSE | 0 | 闪光灯关闭。 | 1462| FLASH_MODE_OPEN | 1 | 闪光灯打开。 | 1463| FLASH_MODE_AUTO | 2 | 自动闪光灯。 | 1464| FLASH_MODE_ALWAYS_OPEN | 3 | 闪光灯常亮。 | 1465 1466## ExposureMode 1467 1468枚举,曝光模式。 1469 1470**系统能力:** SystemCapability.Multimedia.Camera.Core。 1471 1472| 名称 | 值 | 说明 | 1473| ----------------------------- | ---- | ----------- | 1474| EXPOSURE_MODE_LOCKED | 0 | 锁定曝光模式。不支持曝光区域中心点设置。 | 1475| EXPOSURE_MODE_AUTO | 1 | 自动曝光模式。支持曝光区域中心点设置,可以使用[setMeteringPoint](#setmeteringpoint)设置曝光区域中心点。 | 1476| EXPOSURE_MODE_CONTINUOUS_AUTO | 2 | 连续自动曝光。不支持曝光区域中心点设置。 | 1477 1478## FocusMode 1479 1480枚举,焦距模式。 1481 1482**系统能力:** SystemCapability.Multimedia.Camera.Core。 1483 1484| 名称 | 值 | 说明 | 1485| -------------------------- | ---- | ------------ | 1486| FOCUS_MODE_MANUAL | 0 | 手动对焦。通过手动修改相机焦距来改变对焦位置,不支持对焦点设置。 | 1487| FOCUS_MODE_CONTINUOUS_AUTO | 1 | 连续自动对焦。不支持对焦点设置。 | 1488| FOCUS_MODE_AUTO | 2 | 自动对焦。支持对焦点设置,可以使用[setFocusPoint](#setfocuspoint)设置对焦点,根据对焦点执行一次自动对焦。对焦动作完成后(无论对焦成功或是对焦失败),都进入对焦锁定。应用层需要再次调用CONTINUOUS_AUTO后才能再次进入连续自动对焦。 | 1489| FOCUS_MODE_LOCKED | 3 | 对焦锁定。不支持对焦点设置。 | 1490 1491## FocusState 1492 1493枚举,焦距状态。 1494 1495**系统能力:** SystemCapability.Multimedia.Camera.Core。 1496 1497| 名称 | 值 | 说明 | 1498| --------------------- | ---- | --------- | 1499| FOCUS_STATE_SCAN | 0 | 触发对焦。 | 1500| FOCUS_STATE_FOCUSED | 1 | 对焦成功。 | 1501| FOCUS_STATE_UNFOCUSED | 2 | 未完成对焦。 | 1502 1503## VideoStabilizationMode 1504 1505枚举,视频防抖模式。 1506 1507**系统能力:** SystemCapability.Multimedia.Camera.Core。 1508 1509| 名称 | 值 | 说明 | 1510| --------- | ---- | ------------ | 1511| OFF | 0 | 关闭视频防抖功能。 | 1512| LOW | 1 | 使用基础防抖算法。 | 1513| MIDDLE | 2 | 使用防抖效果一般的防抖算法,防抖效果优于LOW类型。 | 1514| HIGH | 3 | 使用防抖效果最好的防抖算法,防抖效果优于MIDDLE类型。 | 1515| AUTO | 4 | 自动进行选择。 | 1516 1517## CaptureSession 1518 1519拍照会话类,保存一次相机运行所需要的所有资源[CameraInput](#camerainput)、[CameraOutput](#cameraoutput),并向相机设备申请完成相机功能(录像,拍照)。 1520 1521### beginConfig 1522 1523beginConfig(): void 1524 1525开始配置会话。 1526 1527**系统能力:** SystemCapability.Multimedia.Camera.Core 1528 1529**返回值:** 1530 1531| 类型 | 说明 | 1532| ---------- | ----------------------------- | 1533| [CameraErrorCode](#cameraerrorcode) | 接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1534 1535**错误码:** 1536 1537以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1538 1539| 错误码ID | 错误信息 | 1540| --------------- | --------------- | 1541| 7400105 | Session config locked. | 1542 1543**示例:** 1544 1545```ts 1546import { BusinessError } from '@ohos.base'; 1547 1548function beginConfig(captureSession: camera.CaptureSession): void { 1549 try { 1550 captureSession.beginConfig(); 1551 } catch (error) { 1552 // 失败返回错误码error.code并处理 1553 let err = error as BusinessError; 1554 console.error(`The beginConfig call failed. error code: ${err.code}`); 1555 } 1556} 1557``` 1558 1559### commitConfig 1560 1561commitConfig(callback: AsyncCallback\<void\>): void 1562 1563提交配置信息,通过注册回调函数获取结果。 1564 1565**系统能力:** SystemCapability.Multimedia.Camera.Core 1566 1567**参数:** 1568 1569| 参数名 | 类型 | 必填 | 说明 | 1570| -------- | -------------------- | ---- | -------------------- | 1571| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1572 1573**错误码:** 1574 1575以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1576 1577| 错误码ID | 错误信息 | 1578| --------------- | --------------- | 1579| 7400102 | Operation not allow. | 1580| 7400201 | Camera service fatal error. | 1581 1582**示例:** 1583 1584```ts 1585import { BusinessError } from '@ohos.base'; 1586 1587function commitConfig(captureSession: camera.CaptureSession): void { 1588 captureSession.commitConfig((err: BusinessError) => { 1589 if (err) { 1590 console.error(`The commitConfig call failed. error code: ${err.code}`); 1591 return; 1592 } 1593 console.log('Callback invoked to indicate the commit config success.'); 1594 }); 1595} 1596``` 1597 1598### commitConfig 1599 1600commitConfig(): Promise\<void\> 1601 1602提交配置信息,通过Promise获取结果。 1603 1604**系统能力:** SystemCapability.Multimedia.Camera.Core 1605 1606**返回值:** 1607 1608| 类型 | 说明 | 1609| -------------- | ------------------------ | 1610| Promise\<void\> | 使用Promise的方式获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1611 1612**错误码:** 1613 1614以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1615 1616| 错误码ID | 错误信息 | 1617| --------------- | --------------- | 1618| 7400102 | Operation not allow. | 1619| 7400201 | Camera service fatal error. | 1620 1621**示例:** 1622 1623```ts 1624import { BusinessError } from '@ohos.base'; 1625 1626function commitConfig(captureSession: camera.CaptureSession): void { 1627 captureSession.commitConfig().then(() => { 1628 console.log('Promise returned to indicate the commit config success.'); 1629 }).catch((err: BusinessError) => { 1630 // 失败返回错误码error.code并处理 1631 console.error(`The commitConfig call failed. error code: ${err.code}`); 1632 }); 1633} 1634``` 1635 1636### addInput 1637 1638addInput(cameraInput: CameraInput): void 1639 1640把[CameraInput](#camerainput)加入到会话。 1641 1642**系统能力:** SystemCapability.Multimedia.Camera.Core 1643 1644**参数:** 1645 1646| 参数名 | 类型 | 必填 | 说明 | 1647| ----------- | --------------------------- | ---- | ------------------------ | 1648| cameraInput | [CameraInput](#camerainput) | 是 | 需要添加的CameraInput实例。 | 1649 1650**返回值:** 1651 1652| 类型 | 说明 | 1653| ---------- | ----------------------------- | 1654| [CameraErrorCode](#cameraerrorcode) | 接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1655 1656**错误码:** 1657 1658以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1659 1660| 错误码ID | 错误信息 | 1661| --------------- | --------------- | 1662| 7400101 | Parameter missing or parameter type incorrect | 1663| 7400102 | Operation not allow. | 1664 1665**示例:** 1666 1667```ts 1668import { BusinessError } from '@ohos.base'; 1669 1670function addInput(captureSession: camera.CaptureSession, cameraInput: camera.CameraInput): void { 1671 try { 1672 captureSession.addInput(cameraInput); 1673 } catch (error) { 1674 // 失败返回错误码error.code并处理 1675 let err = error as BusinessError; 1676 console.log(`The addInput call failed. error code: ${err.code}`); 1677 } 1678} 1679``` 1680 1681### removeInput 1682 1683removeInput(cameraInput: CameraInput): void 1684 1685移除[CameraInput](#camerainput)。 1686 1687**系统能力:** SystemCapability.Multimedia.Camera.Core 1688 1689**参数:** 1690 1691| 参数名 | 类型 | 必填 | 说明 | 1692| ----------- | --------------------------- | ---- | ------------------------ | 1693| cameraInput | [CameraInput](#camerainput) | 是 | 需要移除的CameraInput实例。 | 1694 1695**返回值:** 1696 1697| 类型 | 说明 | 1698| ---------- | ----------------------------- | 1699| [CameraErrorCode](#cameraerrorcode) | 接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1700 1701**错误码:** 1702 1703以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1704 1705| 错误码ID | 错误信息 | 1706| --------------- | --------------- | 1707| 7400101 | Parameter missing or parameter type incorrect | 1708| 7400102 | Operation not allow. | 1709 1710**示例:** 1711 1712```ts 1713import { BusinessError } from '@ohos.base'; 1714 1715function removeInput(captureSession: camera.CaptureSession, cameraInput: camera.CameraInput): void { 1716 try { 1717 captureSession.removeInput(cameraInput); 1718 } catch (error) { 1719 // 失败返回错误码error.code并处理 1720 let err = error as BusinessError; 1721 console.log(`The removeInput call failed. error code: ${err.code}`); 1722 } 1723} 1724``` 1725 1726### addOutput 1727 1728addOutput(cameraOutput: CameraOutput): void 1729 1730把[CameraOutput](#cameraoutput)加入到会话。 1731 1732**系统能力:** SystemCapability.Multimedia.Camera.Core 1733 1734**参数:** 1735 1736| 参数名 | 类型 | 必填 | 说明 | 1737| ------------- | ------------------------------- | ---- | ------------------------ | 1738| cameraOutput | [CameraOutput](#cameraoutput) | 是 | 需要添加的CameraOutput实例。 | 1739 1740**返回值:** 1741 1742| 类型 | 说明 | 1743| ---------- | ----------------------------- | 1744| [CameraErrorCode](#cameraerrorcode) | 接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1745 1746**错误码:** 1747 1748以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1749 1750| 错误码ID | 错误信息 | 1751| --------------- | --------------- | 1752| 7400101 | Parameter missing or parameter type incorrect | 1753| 7400102 | Operation not allow. | 1754 1755**示例:** 1756 1757```ts 1758import { BusinessError } from '@ohos.base'; 1759 1760function addOutput(captureSession: camera.CaptureSession, cameraOutput: camera.CameraOutput): void { 1761 try { 1762 captureSession.addOutput(cameraOutput); 1763 } catch (error) { 1764 // 失败返回错误码error.code并处理 1765 let err = error as BusinessError; 1766 console.log(`The addOutput call failed. error code: ${err.code}`); 1767 } 1768} 1769``` 1770 1771### removeOutput 1772 1773removeOutput(cameraOutput: CameraOutput): void 1774 1775从会话中移除[CameraOutput](#cameraoutput)。 1776 1777**系统能力:** SystemCapability.Multimedia.Camera.Core 1778 1779**参数:** 1780 1781| 参数名 | 类型 | 必填 | 说明 | 1782| ------------- | ------------------------------- | ---- | ------------------------ | 1783| cameraOutput | [CameraOutput](#cameraoutput) | 是 | 需要移除的CameraOutput实例。 | 1784 1785**返回值:** 1786 1787| 类型 | 说明 | 1788| ---------- | ----------------------------- | 1789| [CameraErrorCode](#cameraerrorcode) | 接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1790 1791**错误码:** 1792 1793以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1794 1795| 错误码ID | 错误信息 | 1796| --------------- | --------------- | 1797| 7400101 | Parameter missing or parameter type incorrect | 1798| 7400102 | Operation not allow. | 1799 1800**示例:** 1801 1802```ts 1803import { BusinessError } from '@ohos.base'; 1804 1805function removeOutput(captureSession: camera.CaptureSession, previewOutput: camera.PreviewOutput): void { 1806 try { 1807 captureSession.removeOutput(previewOutput); 1808 } catch (error) { 1809 // 失败返回错误码error.code并处理 1810 let err = error as BusinessError; 1811 console.log(`The removeOutput call failed. error code: ${err.code}`); 1812 } 1813} 1814``` 1815 1816### start 1817 1818start\(callback: AsyncCallback\<void\>\): void 1819 1820开始会话工作,通过注册回调函数获取结果。 1821 1822**系统能力:** SystemCapability.Multimedia.Camera.Core 1823 1824**参数:** 1825 1826| 参数名 | 类型 | 必填 | 说明 | 1827| -------- | -------------------- | ---- | -------------------- | 1828| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1829 1830**错误码:** 1831 1832以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1833 1834| 错误码ID | 错误信息 | 1835| --------------- | --------------- | 1836| 7400103 | Session not config. | 1837| 7400201 | Camera service fatal error. | 1838 1839**示例:** 1840 1841```ts 1842import { BusinessError } from '@ohos.base'; 1843 1844function startCaptureSession(captureSession: camera.CaptureSession): void { 1845 captureSession.start((err: BusinessError) => { 1846 if (err) { 1847 console.error(`Failed to start the session ${err.code}`); 1848 return; 1849 } 1850 console.log('Callback invoked to indicate the session start success.'); 1851 }); 1852} 1853``` 1854 1855### start 1856 1857start\(\): Promise\<void\> 1858 1859开始会话工作,通过Promise获取结果。 1860 1861**系统能力:** SystemCapability.Multimedia.Camera.Core 1862 1863**返回值:** 1864 1865| 类型 | 说明 | 1866| -------------- | ------------------------ | 1867| Promise\<void\> | 使用Promise的方式获取结果。 | 1868 1869**错误码:** 1870 1871以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1872 1873| 错误码ID | 错误信息 | 1874| --------------- | --------------- | 1875| 7400103 | Session not config. | 1876| 7400201 | Camera service fatal error. | 1877 1878**示例:** 1879 1880```ts 1881import { BusinessError } from '@ohos.base'; 1882 1883function startCaptureSession(captureSession: camera.CaptureSession): void { 1884 captureSession.start().then(() => { 1885 console.log('Promise returned to indicate the session start success.'); 1886 }).catch((err: BusinessError) => { 1887 console.error(`Failed to start the session ${err.code}`); 1888 }); 1889} 1890``` 1891 1892### stop 1893 1894stop\(callback: AsyncCallback\<void\>\): void 1895 1896停止会话工作,通过注册回调函数获取结果。 1897 1898**系统能力:** SystemCapability.Multimedia.Camera.Core 1899 1900**参数:** 1901 1902| 参数名 | 类型 | 必填 | 说明 | 1903| -------- | -------------------- | ---- | ------------------- | 1904| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1905 1906**错误码:** 1907 1908以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1909 1910| 错误码ID | 错误信息 | 1911| --------------- | --------------- | 1912| 7400201 | Camera service fatal error. | 1913 1914**示例:** 1915 1916```ts 1917import { BusinessError } from '@ohos.base'; 1918 1919function stopCaptureSession(captureSession: camera.CaptureSession): void { 1920 captureSession.stop((err: BusinessError) => { 1921 if (err) { 1922 console.error(`Failed to stop the session ${err.code}`); 1923 return; 1924 } 1925 console.log('Callback invoked to indicate the session stop success.'); 1926 }); 1927} 1928``` 1929 1930### stop 1931 1932stop(): Promise\<void\> 1933 1934停止会话工作,通过Promise获取结果。 1935 1936**系统能力:** SystemCapability.Multimedia.Camera.Core 1937 1938**返回值:** 1939 1940| 类型 | 说明 | 1941| -------------- | ----------------------- | 1942| Promise\<void\> | 使用Promise的方式获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1943 1944**错误码:** 1945 1946以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1947 1948| 错误码ID | 错误信息 | 1949| --------------- | --------------- | 1950| 7400201 | Camera service fatal error. | 1951 1952**示例:** 1953 1954```ts 1955import { BusinessError } from '@ohos.base'; 1956 1957function stopCaptureSession(captureSession: camera.CaptureSession): void { 1958 captureSession.stop().then(() => { 1959 console.log('Promise returned to indicate the session stop success.'); 1960 }).catch((err: BusinessError) => { 1961 console.error(`Failed to stop the session ${err.code}`); 1962 }); 1963} 1964``` 1965 1966### release 1967 1968release\(callback: AsyncCallback\<void\>\): void 1969 1970释放会话资源,通过注册回调函数获取结果。 1971 1972**系统能力:** SystemCapability.Multimedia.Camera.Core 1973 1974**参数:** 1975 1976| 参数名 | 类型 | 必填 | 说明 | 1977| -------- | -------------------- | ---- | -------------------- | 1978| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 1979 1980**错误码:** 1981 1982以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 1983 1984| 错误码ID | 错误信息 | 1985| --------------- | --------------- | 1986| 7400201 | Camera service fatal error. | 1987 1988**示例:** 1989 1990```ts 1991import { BusinessError } from '@ohos.base'; 1992 1993function releaseCaptureSession(captureSession: camera.CaptureSession): void { 1994 captureSession.release((err: BusinessError) => { 1995 if (err) { 1996 console.error(`Failed to release the CaptureSession instance ${err.code}`); 1997 return; 1998 } 1999 console.log('Callback invoked to indicate that the CaptureSession instance is released successfully.'); 2000 }); 2001} 2002``` 2003 2004### release 2005 2006release(): Promise\<void\> 2007 2008释放会话资源,通过Promise获取结果。 2009 2010**系统能力:** SystemCapability.Multimedia.Camera.Core 2011 2012**返回值:** 2013 2014| 类型 | 说明 | 2015| -------------- | ------------------------ | 2016| Promise\<void\> | 使用Promise的方式获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2017 2018**错误码:** 2019 2020以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2021 2022| 错误码ID | 错误信息 | 2023| --------------- | --------------- | 2024| 7400201 | Camera service fatal error. | 2025 2026**示例:** 2027 2028```ts 2029import { BusinessError } from '@ohos.base'; 2030 2031function releaseCaptureSession(captureSession: camera.CaptureSession): void { 2032 captureSession.release().then(() => { 2033 console.log('Promise returned to indicate that the CaptureSession instance is released successfully.'); 2034 }).catch((err: BusinessError) => { 2035 console.error(`Failed to release the CaptureSession instance ${err.code}`); 2036 }); 2037} 2038``` 2039 2040### hasFlash 2041 2042hasFlash(): boolean 2043 2044检测是否有闪光灯,通过注册回调函数获取结果。 2045 2046**系统能力:** SystemCapability.Multimedia.Camera.Core 2047 2048**返回值:** 2049 2050| 类型 | 说明 | 2051| ---------- | ----------------------------- | 2052| boolean | 返回true表示设备支持闪光灯。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2053 2054**错误码:** 2055 2056以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2057 2058| 错误码ID | 错误信息 | 2059| --------------- | --------------- | 2060| 7400103 | Session not config. | 2061 2062**示例:** 2063 2064```ts 2065import { BusinessError } from '@ohos.base'; 2066 2067function hasFlash(captureSession: camera.CaptureSession): boolean { 2068 let status: boolean = false; 2069 try { 2070 status = captureSession.hasFlash(); 2071 } catch (error) { 2072 // 失败返回错误码error.code并处理 2073 let err = error as BusinessError; 2074 console.error(`The hasFlash call failed. error code: ${err.code}`); 2075 } 2076 return status; 2077} 2078``` 2079 2080### isFlashModeSupported 2081 2082isFlashModeSupported(flashMode: FlashMode): boolean 2083 2084检测闪光灯模式是否支持。 2085 2086**系统能力:** SystemCapability.Multimedia.Camera.Core 2087 2088**参数:** 2089 2090| 参数名 | 类型 | 必填 | 说明 | 2091| --------- | ----------------------- | ---- | --------------------------------- | 2092| flashMode | [FlashMode](#flashmode) | 是 | 指定闪光灯模式。 | 2093 2094**返回值:** 2095 2096| 类型 | 说明 | 2097| ---------- | ----------------------------- | 2098| boolean | 返回true表示支持该闪光灯模式。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2099 2100**错误码:** 2101 2102以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2103 2104| 错误码ID | 错误信息 | 2105| --------------- | --------------- | 2106| 7400103 | Session not config. | 2107 2108**示例:** 2109 2110```ts 2111import { BusinessError } from '@ohos.base'; 2112 2113function isFlashModeSupported(captureSession: camera.CaptureSession): boolean { 2114 let status: boolean = false; 2115 try { 2116 status = captureSession.isFlashModeSupported(camera.FlashMode.FLASH_MODE_AUTO); 2117 } catch (error) { 2118 // 失败返回错误码error.code并处理 2119 let err = error as BusinessError; 2120 console.error(`The isFlashModeSupported call failed. error code: ${err.code}`); 2121 } 2122 return status; 2123} 2124``` 2125 2126### setFlashMode 2127 2128setFlashMode(flashMode: FlashMode): void 2129 2130设置闪光灯模式。 2131 2132进行设置之前,需要先检查: 2133 21341. 设备是否支持闪光灯,可使用方法[hasFlash](#hasflash)。 21352. 设备是否支持指定的闪光灯模式,可使用方法[isFlashModeSupported](#isflashmodesupported)。 2136 2137**系统能力:** SystemCapability.Multimedia.Camera.Core 2138 2139**参数:** 2140 2141| 参数名 | 类型 | 必填 | 说明 | 2142| --------- | ----------------------- | ---- | --------------------- | 2143| flashMode | [FlashMode](#flashmode) | 是 | 指定闪光灯模式。 | 2144 2145**返回值:** 2146 2147| 类型 | 说明 | 2148| ---------- | ----------------------------- | 2149| [CameraErrorCode](#cameraerrorcode) | 接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2150 2151**错误码:** 2152 2153以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2154 2155| 错误码ID | 错误信息 | 2156| --------------- | --------------- | 2157| 7400103 | Session not config. | 2158 2159**示例:** 2160 2161```ts 2162import { BusinessError } from '@ohos.base'; 2163 2164function setFlashMode(captureSession: camera.CaptureSession): void { 2165 try { 2166 captureSession.setFlashMode(camera.FlashMode.FLASH_MODE_AUTO); 2167 } catch (error) { 2168 // 失败返回错误码error.code并处理 2169 let err = error as BusinessError; 2170 console.error(`The setFlashMode call failed. error code: ${err.code}`); 2171 } 2172} 2173``` 2174 2175### getFlashMode 2176 2177getFlashMode(): FlashMode 2178 2179获取当前设备的闪光灯模式。 2180 2181**系统能力:** SystemCapability.Multimedia.Camera.Core 2182 2183**返回值:** 2184 2185| 类型 | 说明 | 2186| ---------- | ----------------------------- | 2187| [FlashMode](#flashmode) | 获取当前设备的闪光灯模式。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2188 2189**错误码:** 2190 2191以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2192 2193| 错误码ID | 错误信息 | 2194| --------------- | --------------- | 2195| 7400103 | Session not config. | 2196 2197**示例:** 2198 2199```ts 2200import { BusinessError } from '@ohos.base'; 2201 2202function getFlashMode(captureSession: camera.CaptureSession): camera.FlashMode { 2203 let flashMode: camera.FlashMode; 2204 try { 2205 flashMode = captureSession.getFlashMode(); 2206 } catch (error) { 2207 // 失败返回错误码error.code并处理 2208 let err = error as BusinessError; 2209 console.error(`The getFlashMode call failed.error code: ${err.code}`); 2210 } 2211 return flashMode; 2212} 2213``` 2214 2215### isExposureModeSupported 2216 2217isExposureModeSupported(aeMode: ExposureMode): boolean; 2218 2219检测曝光模式是否支持。 2220 2221**系统能力:** SystemCapability.Multimedia.Camera.Core 2222 2223**参数:** 2224 2225| 参数名 | 类型 | 必填 | 说明 | 2226| -------- | -------------------------------| ---- | ----------------------------- | 2227| aeMode | [ExposureMode](#exposuremode) | 是 | 曝光模式。 | 2228 2229**返回值:** 2230 2231| 类型 | 说明 | 2232| ---------- | ----------------------------- | 2233| boolean | 获取是否支持曝光模式。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2234 2235**错误码:** 2236 2237以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2238 2239| 错误码ID | 错误信息 | 2240| --------------- | --------------- | 2241| 7400103 | Session not config. | 2242 2243**示例:** 2244 2245```ts 2246import { BusinessError } from '@ohos.base'; 2247 2248function isExposureModeSupported(captureSession: camera.CaptureSession): boolean { 2249 let isSupported: boolean = false; 2250 try { 2251 isSupported = captureSession.isExposureModeSupported(camera.ExposureMode.EXPOSURE_MODE_LOCKED); 2252 } catch (error) { 2253 // 失败返回错误码error.code并处理 2254 let err = error as BusinessError; 2255 console.error(`The isExposureModeSupported call failed. error code: ${err.code}`); 2256 } 2257 return isSupported; 2258} 2259``` 2260 2261### getExposureMode 2262 2263getExposureMode(): ExposureMode 2264 2265获取当前曝光模式。 2266 2267**系统能力:** SystemCapability.Multimedia.Camera.Core 2268 2269**返回值:** 2270 2271| 类型 | 说明 | 2272| ---------- | ----------------------------- | 2273| [ExposureMode](#exposuremode) | 获取当前曝光模式。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2274 2275**错误码:** 2276 2277以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2278 2279| 错误码ID | 错误信息 | 2280| --------------- | --------------- | 2281| 7400103 | Session not config. | 2282 2283**示例:** 2284 2285```ts 2286import { BusinessError } from '@ohos.base'; 2287 2288function getExposureMode(captureSession: camera.CaptureSession): camera.ExposureMode { 2289 let exposureMode: camera.ExposureMode; 2290 try { 2291 exposureMode = captureSession.getExposureMode(); 2292 } catch (error) { 2293 // 失败返回错误码error.code并处理 2294 let err = error as BusinessError; 2295 console.error(`The getExposureMode call failed. error code: ${err.code}`); 2296 } 2297 return exposureMode; 2298} 2299``` 2300 2301### setExposureMode 2302 2303setExposureMode(aeMode: ExposureMode): void 2304 2305设置曝光模式。进行设置之前,需要先检查设备是否支持指定的曝光模式,可使用方法[isExposureModeSupported](#isexposuremodesupported)。 2306 2307**系统能力:** SystemCapability.Multimedia.Camera.Core 2308 2309**参数:** 2310 2311| 参数名 | 类型 | 必填 | 说明 | 2312| -------- | -------------------------------| ---- | ----------------------- | 2313| aeMode | [ExposureMode](#exposuremode) | 是 | 曝光模式。 | 2314 2315**返回值:** 2316 2317| 类型 | 说明 | 2318| ---------- | ----------------------------- | 2319| [CameraErrorCode](#cameraerrorcode) | 接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2320 2321**错误码:** 2322 2323以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2324 2325| 错误码ID | 错误信息 | 2326| --------------- | --------------- | 2327| 7400103 | Session not config. | 2328 2329**示例:** 2330 2331```ts 2332import { BusinessError } from '@ohos.base'; 2333 2334function setExposureMode(captureSession: camera.CaptureSession): void { 2335 try { 2336 captureSession.setExposureMode(camera.ExposureMode.EXPOSURE_MODE_LOCKED); 2337 } catch (error) { 2338 // 失败返回错误码error.code并处理 2339 let err = error as BusinessError; 2340 console.error(`The setExposureMode call failed. error code: ${err.code}`); 2341 } 2342} 2343``` 2344 2345### getMeteringPoint 2346 2347getMeteringPoint(): Point 2348 2349查询曝光区域中心点。(该接口目前为预留,将在3.2版本开放) 2350 2351**系统能力:** SystemCapability.Multimedia.Camera.Core 2352 2353**返回值:** 2354 2355| 类型 | 说明 | 2356| ---------- | ----------------------------- | 2357| [Point](#point) | 获取当前曝光点。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2358 2359**错误码:** 2360 2361以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2362 2363| 错误码ID | 错误信息 | 2364| --------------- | --------------- | 2365| 7400103 | Session not config. | 2366 2367**示例:** 2368 2369```ts 2370import { BusinessError } from '@ohos.base'; 2371 2372function getMeteringPoint(captureSession: camera.CaptureSession): camera.Point { 2373 let exposurePoint: camera.Point; 2374 try { 2375 exposurePoint = captureSession.getMeteringPoint(); 2376 } catch (error) { 2377 // 失败返回错误码error.code并处理 2378 let err = error as BusinessError; 2379 console.error(`The getMeteringPoint call failed. error code: ${err.code}`); 2380 } 2381 return exposurePoint; 2382} 2383``` 2384 2385### setMeteringPoint 2386 2387setMeteringPoint(point: Point): void 2388 2389设置曝光区域中心点,曝光点应在0-1坐标系内,该坐标系左上角为{0,0},右下角为{1,1}。 2390此坐标系是以设备充电口在右侧时的横向设备方向为基准的,例如应用的预览界面布局以 2391设备充电口在下侧时的竖向方向为基准,布局宽高为{w,h},且触碰点为{x,y}, 2392则转换后的坐标点为{y/h,1-x/w}。 2393 2394**系统能力:** SystemCapability.Multimedia.Camera.Core 2395 2396**参数:** 2397 2398| 参数名 | 类型 | 必填 | 说明 | 2399| ------------- | -------------------------------| ---- | ------------------- | 2400| exposurePoint | [Point](#point) | 是 | 曝光点,x,y设置范围应在[0,1]之内,超过范围,如果小于0设置0,大于1设置1。 | 2401 2402**返回值:** 2403 2404| 类型 | 说明 | 2405| ---------- | ----------------------------- | 2406| [CameraErrorCode](#cameraerrorcode) | 接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2407 2408**错误码:** 2409 2410以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2411 2412| 错误码ID | 错误信息 | 2413| --------------- | --------------- | 2414| 7400103 | Session not config. | 2415 2416**示例:** 2417 2418```ts 2419import { BusinessError } from '@ohos.base'; 2420 2421function setMeteringPoint(captureSession: camera.CaptureSession): void { 2422 const exposurePoint: camera.Point = {x: 1, y: 1}; 2423 try { 2424 captureSession.setMeteringPoint(exposurePoint); 2425 } catch (error) { 2426 // 失败返回错误码error.code并处理 2427 let err = error as BusinessError; 2428 console.error(`The setMeteringPoint call failed. error code: ${err.code}`); 2429 } 2430} 2431``` 2432 2433### getExposureBiasRange 2434 2435getExposureBiasRange(): Array\<number\> 2436 2437查询曝光补偿范围。 2438 2439**系统能力:** SystemCapability.Multimedia.Camera.Core 2440 2441**返回值:** 2442 2443| 类型 | 说明 | 2444| ---------- | ----------------------------- | 2445| Array\<number\> | 获取补偿范围的数组。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2446 2447**错误码:** 2448 2449以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2450 2451| 错误码ID | 错误信息 | 2452| --------------- | --------------- | 2453| 7400103 | Session not config. | 2454 2455**示例:** 2456 2457```ts 2458import { BusinessError } from '@ohos.base'; 2459 2460function getExposureBiasRange(captureSession: camera.CaptureSession): Array<number> { 2461 let biasRangeArray: Array<number> = []; 2462 try { 2463 biasRangeArray = captureSession.getExposureBiasRange(); 2464 } catch (error) { 2465 // 失败返回错误码error.code并处理 2466 let err = error as BusinessError; 2467 console.error(`The getExposureBiasRange call failed. error code: ${err.code}`); 2468 } 2469 return biasRangeArray; 2470} 2471``` 2472 2473### setExposureBias 2474 2475setExposureBias(exposureBias: number): void 2476 2477设置曝光补偿,曝光补偿值(EV)。 2478 2479进行设置之前,建议先通过方法[getExposureBiasRange](#getexposurebiasrange)查询支持的范围。 2480 2481**系统能力:** SystemCapability.Multimedia.Camera.Core 2482 2483**参数:** 2484 2485| 参数名 | 类型 | 必填 | 说明 | 2486| -------- | -------------------------------| ---- | ------------------- | 2487| exposureBias | number | 是 | 曝光补偿,getExposureBiasRange查询支持的范围,如果设置超过支持范围的值,自动匹配到就近临界点。曝光补偿存在步长,如步长为0.5。则设置1.2时,获取到实际生效曝光补偿为1.0。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode), | 2488 2489**错误码:** 2490 2491以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2492 2493| 错误码ID | 错误信息 | 2494| --------------- | --------------- | 2495| 7400103 | Session not config. | 2496 2497**示例:** 2498 2499```ts 2500import { BusinessError } from '@ohos.base'; 2501 2502function setExposureBias(captureSession: camera.CaptureSession, biasRangeArray: Array<number>): void { 2503 let exposureBias = biasRangeArray[0]; 2504 try { 2505 captureSession.setExposureBias(exposureBias); 2506 } catch (error) { 2507 // 失败返回错误码error.code并处理 2508 let err = error as BusinessError; 2509 console.error(`The setExposureBias call failed. error code: ${err.code}`); 2510 } 2511} 2512``` 2513 2514### getExposureValue 2515 2516getExposureValue(): number 2517 2518查询当前曝光值。 2519 2520**系统能力:** SystemCapability.Multimedia.Camera.Core 2521 2522**返回值:** 2523 2524| 类型 | 说明 | 2525| ---------- | ----------------------------- | 2526| number | 获取曝光值。曝光补偿存在步长,如步长为0.5。则设置1.2时,获取到实际生效曝光补偿为1.0。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2527 2528**错误码:** 2529 2530以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2531 2532| 错误码ID | 错误信息 | 2533| --------------- | --------------- | 2534| 7400103 | Session not config. | 2535 2536**示例:** 2537 2538```ts 2539import { BusinessError } from '@ohos.base'; 2540 2541function getExposureValue(captureSession: camera.CaptureSession): number { 2542 const invalid_value: number = -1; 2543 let exposureValue: number = invalid_value; 2544 try { 2545 exposureValue = captureSession.getExposureValue(); 2546 } catch (error) { 2547 // 失败返回错误码error.code并处理 2548 let err = error as BusinessError; 2549 console.error(`The getExposureValue call failed. error code: ${err.code}`); 2550 } 2551 return exposureValue; 2552} 2553``` 2554 2555### isFocusModeSupported 2556 2557isFocusModeSupported(afMode: FocusMode): boolean 2558 2559检测对焦模式是否支持。 2560 2561**系统能力:** SystemCapability.Multimedia.Camera.Core 2562 2563**参数:** 2564 2565| 参数名 | 类型 | 必填 | 说明 | 2566| -------- | ----------------------- | ---- | -------------------------------- | 2567| afMode | [FocusMode](#focusmode) | 是 | 指定的焦距模式。 | 2568 2569**返回值:** 2570 2571| 类型 | 说明 | 2572| ---------- | ----------------------------- | 2573| boolean | 返回true表示支持该焦距模式。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2574 2575**错误码:** 2576 2577以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2578 2579| 错误码ID | 错误信息 | 2580| --------------- | --------------- | 2581| 7400103 | Session not config. | 2582 2583**示例:** 2584 2585```ts 2586import { BusinessError } from '@ohos.base'; 2587 2588function isFocusModeSupported(captureSession: camera.CaptureSession): boolean { 2589 let status: boolean = false; 2590 try { 2591 status = captureSession.isFocusModeSupported(camera.FocusMode.FOCUS_MODE_AUTO); 2592 } catch (error) { 2593 // 失败返回错误码error.code并处理 2594 let err = error as BusinessError; 2595 console.error(`The isFocusModeSupported call failed. error code: ${err.code}`); 2596 } 2597 return status; 2598} 2599``` 2600 2601### setFocusMode 2602 2603setFocusMode(afMode: FocusMode): void 2604 2605设置对焦模式。 2606 2607进行设置之前,需要先检查设备是否支持指定的焦距模式,可使用方法[isFocusModeSupported](#isfocusmodesupported)。 2608 2609**系统能力:** SystemCapability.Multimedia.Camera.Core 2610 2611**参数:** 2612 2613| 参数名 | 类型 | 必填 | 说明 | 2614| -------- | ----------------------- | ---- | ------------------- | 2615| afMode | [FocusMode](#focusmode) | 是 | 指定的焦距模式。 | 2616 2617**返回值:** 2618 2619| 类型 | 说明 | 2620| ---------- | ----------------------------- | 2621| [CameraErrorCode](#cameraerrorcode) | 接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2622 2623**错误码:** 2624 2625以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2626 2627| 错误码ID | 错误信息 | 2628| --------------- | --------------- | 2629| 7400103 | Session not config. | 2630 2631**示例:** 2632 2633```ts 2634import { BusinessError } from '@ohos.base'; 2635 2636function setFocusMode(captureSession: camera.CaptureSession): void { 2637 try { 2638 captureSession.setFocusMode(camera.FocusMode.FOCUS_MODE_AUTO); 2639 } catch (error) { 2640 // 失败返回错误码error.code并处理 2641 let err = error as BusinessError; 2642 console.error(`The setFocusMode call failed. error code: ${err.code}`); 2643 } 2644} 2645``` 2646 2647### getFocusMode 2648 2649getFocusMode(): FocusMode 2650 2651获取当前的对焦模式。 2652 2653**系统能力:** SystemCapability.Multimedia.Camera.Core 2654 2655**返回值:** 2656 2657| 类型 | 说明 | 2658| ---------- | ----------------------------- | 2659| [FocusMode](#focusmode) | 获取当前设备的焦距模式。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2660 2661**错误码:** 2662 2663以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2664 2665| 错误码ID | 错误信息 | 2666| --------------- | --------------- | 2667| 7400103 | Session not config. | 2668 2669**示例:** 2670 2671```ts 2672import { BusinessError } from '@ohos.base'; 2673 2674function getFocusMode(captureSession: camera.CaptureSession): camera.FocusMode { 2675 let afMode: camera.FocusMode; 2676 try { 2677 afMode = captureSession.getFocusMode(); 2678 } catch (error) { 2679 // 失败返回错误码error.code并处理 2680 let err = error as BusinessError; 2681 console.error(`The getFocusMode call failed. error code: ${err.code}`); 2682 } 2683 return afMode; 2684} 2685``` 2686 2687### setFocusPoint 2688 2689setFocusPoint(point: Point): void 2690 2691设置焦点,焦点应在0-1坐标系内,该坐标系左上角为{0,0},右下角为{1,1}。 2692此坐标系是以设备充电口在右侧时的横向设备方向为基准的,例如应用的预览界面布局以 2693设备充电口在下侧时的竖向方向为基准,布局宽高为{w,h},且触碰点为{x,y}, 2694则转换后的坐标点为{y/h,1-x/w}。 2695 2696**系统能力:** SystemCapability.Multimedia.Camera.Core 2697 2698**参数:** 2699 2700| 参数名 | 类型 | 必填 | 说明 | 2701| -------- | ----------------------- | ---- | ------------------- | 2702| Point1 | [Point](#point) | 是 | 焦点。x,y设置范围应在[0,1]之内,超过范围,如果小于0设置0,大于1设置1。 | 2703 2704**返回值:** 2705 2706| 类型 | 说明 | 2707| ---------- | ----------------------------- | 2708| [CameraErrorCode](#cameraerrorcode) | 接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2709 2710**错误码:** 2711 2712以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2713 2714| 错误码ID | 错误信息 | 2715| --------------- | --------------- | 2716| 7400103 | Session not config. | 2717 2718**示例:** 2719 2720```ts 2721import { BusinessError } from '@ohos.base'; 2722 2723function setFocusPoint(captureSession: camera.CaptureSession): void { 2724 const Point1: camera.Point = {x: 1, y: 1}; 2725 try { 2726 captureSession.setFocusPoint(Point1); 2727 } catch (error) { 2728 // 失败返回错误码error.code并处理 2729 let err = error as BusinessError; 2730 console.error(`The setFocusPoint call failed. error code: ${err.code}`); 2731 } 2732} 2733``` 2734 2735### getFocusPoint 2736 2737getFocusPoint(): Point 2738 2739查询焦点。 2740 2741**系统能力:** SystemCapability.Multimedia.Camera.Core 2742 2743**返回值:** 2744 2745| 类型 | 说明 | 2746| ---------- | ----------------------------- | 2747| [Point](#point) | 用于获取当前焦点。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2748 2749**错误码:** 2750 2751以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2752 2753| 错误码ID | 错误信息 | 2754| --------------- | --------------- | 2755| 7400103 | Session not config. | 2756 2757**示例:** 2758 2759```ts 2760import { BusinessError } from '@ohos.base'; 2761 2762function getFocusPoint(captureSession: camera.CaptureSession): camera.Point { 2763 let point: camera.Point; 2764 try { 2765 point = captureSession.getFocusPoint(); 2766 } catch (error) { 2767 // 失败返回错误码error.code并处理 2768 let err = error as BusinessError; 2769 console.error(`The getFocusPoint call failed. error code: ${err.code}`); 2770 } 2771 return point; 2772} 2773``` 2774 2775### getFocalLength 2776 2777getFocalLength(): number 2778 2779查询焦距值。 2780 2781**系统能力:** SystemCapability.Multimedia.Camera.Core 2782 2783**返回值:** 2784 2785| 类型 | 说明 | 2786| ---------- | ----------------------------- | 2787| number | 用于获取当前焦距。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2788 2789**错误码:** 2790 2791以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2792 2793| 错误码ID | 错误信息 | 2794| --------------- | --------------- | 2795| 7400103 | Session not config. | 2796 2797**示例:** 2798 2799```ts 2800import { BusinessError } from '@ohos.base'; 2801 2802function getFocalLength(captureSession: camera.CaptureSession): number { 2803 const invalid_value: number = -1; 2804 let focalLength: number = invalid_value; 2805 try { 2806 focalLength = captureSession.getFocalLength(); 2807 } catch (error) { 2808 // 失败返回错误码error.code并处理 2809 let err = error as BusinessError; 2810 console.error(`The getFocalLength call failed. error code: ${err.code}`); 2811 } 2812 return focalLength; 2813} 2814``` 2815 2816### getZoomRatioRange 2817 2818getZoomRatioRange(): Array\<number\> 2819 2820获取支持的变焦范围。 2821 2822**系统能力:** SystemCapability.Multimedia.Camera.Core 2823 2824**返回值:** 2825 2826| 类型 | 说明 | 2827| ---------- | ----------------------------- | 2828| Array\<number\> | 用于获取可变焦距比范围,返回的数组包括其最小值和最大值。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2829 2830**错误码:** 2831 2832以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2833 2834| 错误码ID | 错误信息 | 2835| --------------- | --------------- | 2836| 7400103 | Session not config. | 2837 2838**示例:** 2839 2840```ts 2841import { BusinessError } from '@ohos.base'; 2842 2843function getZoomRatioRange(captureSession: camera.CaptureSession): Array<number> { 2844 let zoomRatioRange: Array<number> = []; 2845 try { 2846 zoomRatioRange = captureSession.getZoomRatioRange(); 2847 } catch (error) { 2848 // 失败返回错误码error.code并处理 2849 let err = error as BusinessError; 2850 console.error(`The getZoomRatioRange call failed. error code: ${err.code}`); 2851 } 2852 return zoomRatioRange; 2853} 2854``` 2855 2856### setZoomRatio 2857 2858setZoomRatio(zoomRatio: number): void 2859 2860设置变焦比,变焦精度最高为小数点后两位,超过变焦精度的变焦值系统无法响应。 2861 2862**系统能力:** SystemCapability.Multimedia.Camera.Core 2863 2864**参数:** 2865 2866| 参数名 | 类型 | 必填 | 说明 | 2867| --------- | -------------------- | ---- | ------------------- | 2868| zoomRatio | number | 是 | 可变焦距比,通过getZoomRatioRange获取支持的变焦范围,如果设置超过支持范围的值,自动匹配到就近临界点。 | 2869 2870**返回值:** 2871 2872| 类型 | 说明 | 2873| ---------- | ----------------------------- | 2874| [CameraErrorCode](#cameraerrorcode) | 接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2875 2876**错误码:** 2877 2878以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2879 2880| 错误码ID | 错误信息 | 2881| --------------- | --------------- | 2882| 7400103 | Session not config. | 2883 2884**示例:** 2885 2886```ts 2887import { BusinessError } from '@ohos.base'; 2888 2889function setZoomRatio(captureSession: camera.CaptureSession, zoomRatioRange: Array<number>): void { 2890 if (zoomRatioRange === undefined || zoomRatioRange.length <= 0) { 2891 return; 2892 } 2893 let zoomRatio = zoomRatioRange[0]; 2894 try { 2895 captureSession.setZoomRatio(zoomRatio); 2896 } catch (error) { 2897 // 失败返回错误码error.code并处理 2898 let err = error as BusinessError; 2899 console.error(`The setZoomRatio call failed. error code: ${err.code}`); 2900 } 2901} 2902``` 2903 2904### getZoomRatio 2905 2906getZoomRatio(): number 2907 2908获取当前的变焦比。 2909 2910**系统能力:** SystemCapability.Multimedia.Camera.Core 2911 2912**返回值:** 2913 2914| 类型 | 说明 | 2915| ---------- | ----------------------------- | 2916| number | 获取当前的变焦比结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2917 2918**错误码:** 2919 2920以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2921 2922| 错误码ID | 错误信息 | 2923| --------------- | --------------- | 2924| 7400103 | Session not config. | 2925 2926**示例:** 2927 2928```ts 2929import { BusinessError } from '@ohos.base'; 2930 2931function getZoomRatio(captureSession: camera.CaptureSession): number { 2932 const invalid_value: number = -1; 2933 let zoomRatio: number = invalid_value; 2934 try { 2935 zoomRatio = captureSession.getZoomRatio(); 2936 } catch (error) { 2937 // 失败返回错误码error.code并处理 2938 let err = error as BusinessError; 2939 console.error(`The getZoomRatio call failed. error code: ${err.code}`); 2940 } 2941 return zoomRatio; 2942} 2943``` 2944 2945### isVideoStabilizationModeSupported 2946 2947isVideoStabilizationModeSupported(vsMode: VideoStabilizationMode): boolean 2948 2949查询是否支持指定的视频防抖模式。 2950 2951**系统能力:** SystemCapability.Multimedia.Camera.Core 2952 2953**参数:** 2954 2955| 参数名 | 类型 | 必填 | 说明 | 2956| -------- | ------------------------------------------------- | ---- | ------------------------------ | 2957| vsMode | [VideoStabilizationMode](#videostabilizationmode) | 是 | 视频防抖模式。 | 2958 2959**返回值:** 2960 2961| 类型 | 说明 | 2962| ---------- | ----------------------------- | 2963| boolean | 返回视频防抖模式是否支持。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 2964 2965**错误码:** 2966 2967以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 2968 2969| 错误码ID | 错误信息 | 2970| --------------- | --------------- | 2971| 7400103 | Session not config. | 2972 2973**示例:** 2974 2975```ts 2976import { BusinessError } from '@ohos.base'; 2977 2978function isVideoStabilizationModeSupported(captureSession: camera.CaptureSession): boolean { 2979 let isSupported: boolean = false; 2980 try { 2981 isSupported = captureSession.isVideoStabilizationModeSupported(camera.VideoStabilizationMode.OFF); 2982 } catch (error) { 2983 // 失败返回错误码error.code并处理 2984 let err = error as BusinessError; 2985 console.error(`The isVideoStabilizationModeSupported call failed. error code: ${err.code}`); 2986 } 2987 return isSupported; 2988} 2989``` 2990 2991### getActiveVideoStabilizationMode 2992 2993getActiveVideoStabilizationMode(): VideoStabilizationMode 2994 2995查询当前正在使用的视频防抖模式。 2996 2997**系统能力:** SystemCapability.Multimedia.Camera.Core 2998 2999**返回值:** 3000 3001| 类型 | 说明 | 3002| ---------- | ----------------------------- | 3003| VideoStabilizationMode | 视频防抖是否正在使用。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 3004 3005**错误码:** 3006 3007以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3008 3009| 错误码ID | 错误信息 | 3010| --------------- | --------------- | 3011| 7400103 | Session not config. | 3012 3013**示例:** 3014 3015```ts 3016import { BusinessError } from '@ohos.base'; 3017 3018function getActiveVideoStabilizationMode(captureSession: camera.CaptureSession): camera.VideoStabilizationMode { 3019 let vsMode: camera.VideoStabilizationMode; 3020 try { 3021 vsMode = captureSession.getActiveVideoStabilizationMode(); 3022 } catch (error) { 3023 // 失败返回错误码error.code并处理 3024 let err = error as BusinessError; 3025 console.error(`The getActiveVideoStabilizationMode call failed. error code: ${err.code}`); 3026 } 3027 return vsMode; 3028} 3029``` 3030 3031### setVideoStabilizationMode 3032 3033setVideoStabilizationMode(mode: VideoStabilizationMode): void 3034 3035设置视频防抖模式。需要先检查设备是否支持对应的防抖模式,可以通过[isVideoStabilizationModeSupported](#isvideostabilizationmodesupported)方法判断所设置的模式是否支持。 3036 3037**系统能力:** SystemCapability.Multimedia.Camera.Core 3038 3039**参数:** 3040 3041| 参数名 | 类型 | 必填 | 说明 | 3042| -------- | ------------------------------------------------- | ---- | --------------------- | 3043| mode | [VideoStabilizationMode](#videostabilizationmode) | 是 | 需要设置的视频防抖模式。 | 3044 3045**返回值:** 3046 3047| 类型 | 说明 | 3048| ---------- | ----------------------------- | 3049| [CameraErrorCode](#cameraerrorcode) | 接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 3050 3051**错误码:** 3052 3053以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3054 3055| 错误码ID | 错误信息 | 3056| --------------- | --------------- | 3057| 7400103 | Session not config. | 3058 3059**示例:** 3060 3061```ts 3062import { BusinessError } from '@ohos.base'; 3063 3064function setVideoStabilizationMode(captureSession: camera.CaptureSession): void { 3065 try { 3066 captureSession.setVideoStabilizationMode(camera.VideoStabilizationMode.OFF); 3067 } catch (error) { 3068 // 失败返回错误码error.code并处理 3069 let err = error as BusinessError; 3070 console.error(`The setVideoStabilizationMode call failed. error code: ${err.code}`); 3071 } 3072} 3073``` 3074### getSupportedFilters 3075 3076getSupportedFilters(): Array\<number> 3077 3078获取当前支持的滤镜效果列表。 3079 3080**系统接口:** 此接口为系统接口。 3081 3082**系统能力:** SystemCapability.Multimedia.Camera.Core 3083 3084**返回值:** 3085 3086| 类型 | 说明 | 3087| ---------- | ----------------------------- | 3088| Array\<FilterType\> | 返回支持的滤镜效果列表。 | 3089 3090**错误码:** 3091 3092以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3093 3094| 错误码ID | 错误信息 | 3095| --------------- | --------------- | 3096| 7400103 | Session not config. | 3097 3098**示例:** 3099 3100```ts 3101function getSupportedFilters(portraitSession: camera.PortraitSession): Array<camera.FilterType> { 3102 let filterTypes: Array<camera.FilterType> = portraitSession.getSupportedFilters(); 3103 return filterTypes; 3104} 3105``` 3106### setFilter 3107 3108setFilter(filter: number): void 3109 3110设置滤镜效果。需要先检查设备是否支持对应的滤镜效果,可以通过[getSupportedFilters](#getsupportedfilters)方法获取所支持的类型。 3111 3112**系统接口:** 此接口为系统接口。 3113 3114**系统能力:** SystemCapability.Multimedia.Camera.Core 3115 3116**参数:** 3117 3118| 参数名 | 类型 | 必填 | 说明 | 3119| -------- | ----------------------------| ---- | ---------------------| 3120| filter | [number](#filtertype) | 是 | 当前用户设置的滤镜类型。 | 3121 3122**错误码:** 3123 3124以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3125 3126| 错误码ID | 错误信息 | 3127| --------------- | --------------- | 3128| 7400101 | Parameter missing or parameter type incorrect. | 3129| 7400103 | Session not config. | 3130 3131**示例:** 3132 3133```ts 3134function setFilter(portraitSession: camera.PortraitSession, filterTypes: Array<camera.FilterType>): void { 3135 if (filterTypes === undefined || filterTypes.length <= 0) { 3136 return; 3137 } 3138 portraitSession.setFilter(filterTypes[0]); 3139} 3140``` 3141 3142### getFilter 3143 3144 getFilter(): number 3145 3146获取当前已设置的滤镜效果。 3147 3148**系统接口:** 此接口为系统接口。 3149 3150**系统能力:** SystemCapability.Multimedia.Camera.Core 3151 3152**返回值:** 3153 3154| 类型 | 说明 | 3155| ---------- | ----------------------------| 3156| [number](#filtertype)| 已设置的滤镜效果。可查阅[FilterType](#filtertype)。| 3157 3158**错误码:** 3159 3160以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3161 3162| 错误码ID | 错误信息 | 3163| --------------- | --------------- | 3164| 7400103 | Session not config. | 3165 3166**示例:** 3167 3168```ts 3169function getFilter(portraitSession: camera.PortraitSession): number { 3170 let filterType: number = portraitSession.getFilter(); 3171 return filterType; 3172} 3173``` 3174### getSupportedBeautyTypes 3175 3176getSupportedBeautyTypes(): Array<[BeautyType](#beautytype)> 3177 3178获取当前支持的美颜效果列表。 3179 3180**系统接口:** 此接口为系统接口。 3181 3182**系统能力:** SystemCapability.Multimedia.Camera.Core 3183 3184**返回值:** 3185 3186| 类型 | 说明 | 3187| ---------- | ----------------------------- | 3188| Array\<[BeautyType](#beautytype)\>| 返回当前支持的美颜效果列表。 | 3189 3190**错误码:** 3191 3192以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3193 3194| 错误码ID | 错误信息 | 3195| --------------- | --------------- | 3196| 7400103 | Session not config. | 3197 3198**示例:** 3199 3200```ts 3201function getSupportedBeautyTypes(portraitSession: camera.PortraitSession): Array<camera.BeautyType> { 3202 let beautyTypes: Array<camera.BeautyType> = portraitSession.getSupportedBeautyTypes(); 3203 return beautyTypes; 3204} 3205``` 3206### getSupportedBeautyRange 3207 3208getSupportedBeautyRange(type: BeautyType): Array\<number\> 3209 3210获取指定美颜效果的范围值。在不同设备返回的美颜强度有所不同,下表仅做示例。 3211 3212| 传入参数 | 示例返回值 | 返回值说明 | 3213| ----------------| ---- | ---------| 3214| AUTO | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |美颜类型为自动时支持的美颜强度,0表明关闭美颜,其余正值表明自动的美颜强度。 | 3215| SKIN_SMOOTH | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | 美颜类型为光滑时支持的美颜强度,0表明关闭光滑,其余正值表明光滑的美颜强度。 | 3216| FACE_SLENDER | [0, 1, 2, 3, 4, 5] | 美颜类型为瘦脸时支持的美颜强度,0表明关闭瘦脸,其余正值表明瘦脸的美颜强度。 | 3217| SKIN_TONE | [-1, 16242611] | 美颜类型为美肤时支持的美颜强度,-1表明关闭美肤,其余非负值为使用RGB表示的美肤美颜强度,<br> 16242611转化为16进制为0xF7D7B3,F7为R通道值,D7为G通道值,B3位B通道值。 | 3218 3219**系统接口:** 此接口为系统接口。 3220 3221**系统能力:** SystemCapability.Multimedia.Camera.Core 3222 3223**参数:** 3224 3225| 参数名 | 类型 | 必填 | 说明 | 3226| -------- | --------------------------| ---- | ----------| 3227| type | [BeautyType](#beautytype) | 是 | 美颜类型。 | 3228 3229**返回值:** 3230 3231| 类型 | 说明 | 3232| ---------- | ----------------------------- | 3233| Array\<number\> | 当前美颜类型所支持的美颜强度。 | 3234 3235**错误码:** 3236 3237以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3238 3239| 错误码ID | 错误信息 | 3240| --------------- | --------------- | 3241| 7400103 | Session not config. | 3242 3243**示例:** 3244 3245```ts 3246function getSupportedBeautyRange(portraitSession: camera.PortraitSession): Array<number> { 3247 let beautyTypes: Array<camera.BeautyType> = portraitSession.getSupportedBeautyTypes(); 3248 if (beautyTypes === undefined || beautyTypes.length <= 0) { 3249 return []; 3250 } 3251 let beautyLevels: Array<number> = portraitSession.getSupportedBeautyRange(beautyTypes[0]); 3252 return beautyLevels; 3253} 3254``` 3255 3256### setBeauty 3257 3258setBeauty(type: BeautyType, value: number): void 3259 3260设置美颜类型以及对应的美颜强度。将通过[getSupportedBeautyTypes](#getsupportedbeautytypes)获取得到的[BeautyType](#beautytype)都关闭,表明当前美颜关闭;若有一种美颜类型未关闭,表明当前美颜打开。 3261 3262**系统接口:** 此接口为系统接口。 3263 3264**系统能力:** SystemCapability.Multimedia.Camera.Core 3265 3266**参数:** 3267 3268| 参数名 | 类型 | 必填 | 说明 | 3269| -------- | --------------------------| ---- | --------------------- | 3270| type | [BeautyType](#beautytype) | 是 | 美颜类型 | 3271| value | number | 是 | 美颜强度,通过[getSupportedBeautyRange](#getsupportedbeautyrange)接口获取。| 3272 3273**错误码:** 3274 3275以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3276 3277| 错误码ID | 错误信息 | 3278| --------------- | --------------- | 3279| 7400103 | Session not config. | 3280 3281**示例:** 3282 3283```ts 3284function setBeauty(portraitSession: camera.PortraitSession): void { 3285 let beautyTypes: Array<camera.BeautyType> = portraitSession.getSupportedBeautyTypes(); 3286 if (beautyTypes === undefined || beautyTypes.length <= 0) { 3287 return; 3288 } 3289 let beautyLevels: Array<number> = portraitSession.getSupportedBeautyRange(beautyTypes[0]); 3290 if (beautyLevels === undefined || beautyLevels.length <= 0) { 3291 return; 3292 } 3293 portraitSession.setBeauty(beautyTypes[0], beautyLevels[0]); 3294} 3295``` 3296 3297### getBeauty 3298 3299getBeauty(type: BeautyType): number 3300 3301查询当前已设置的美颜效果对应的美颜强度。 3302 3303**系统接口:** 此接口为系统接口。 3304 3305**系统能力:** SystemCapability.Multimedia.Camera.Core 3306 3307**参数:** 3308 3309| 参数名 | 类型 | 必填 | 说明 | 3310| -------- | ------------------------------------------------- | ---- | --------------------- | 3311| type | [BeautyType](#beautytype) | 是 | 美颜类型 | 3312 3313**返回值:** 3314| 参数名 | 类型 | 必填 | 说明 | 3315| -------- | ------------------------------------------------- | ---- | --------------------- | 3316| value | number | 是 | 美颜强度 | 3317 3318**错误码:** 3319 3320以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3321 3322| 错误码ID | 错误信息 | 3323| --------------- | --------------- | 3324| 7400103 | Session not config. | 3325 3326**示例:** 3327 3328```ts 3329function getBeauty(portraitSession: camera.PortraitSession): number { 3330 const invalid_value: number = -1; 3331 let beautyTypes = portraitSession.getSupportedBeautyTypes(); 3332 if (beautyTypes === undefined || beautyTypes.length <= 0) { 3333 return invalid_value; 3334 } 3335 let beautyLevels: Array<number> = portraitSession.getSupportedBeautyRange(beautyTypes[0]); 3336 if (beautyLevels === undefined || beautyLevels.length <= 0) { 3337 return invalid_value; 3338 } 3339 portraitSession.setBeauty(beautyTypes[0], beautyLevels[0]); 3340 let beautyLevel: number = portraitSession.getBeauty(beautyTypes[0]); 3341 return beautyLevel; 3342} 3343``` 3344 3345### on('focusStateChange') 3346 3347on(type: 'focusStateChange', callback: AsyncCallback\<FocusState\>): void 3348 3349监听相机聚焦的状态变化,通过注册回调函数获取结果。 3350 3351**系统能力:** SystemCapability.Multimedia.Camera.Core 3352 3353**参数:** 3354 3355| 参数名 | 类型 | 必填 | 说明 | 3356| -------- | ----------------------------------------- | ---- | ------------------------ | 3357| type | string | 是 | 监听事件,固定为'focusStateChange',session 创建成功可监听。仅当自动对焦模式时,且相机对焦状态发生改变时可触发该事件 | 3358| callback | AsyncCallback\<[FocusState](#focusstate)\> | 是 | 回调函数,用于获取当前对焦状态。 | 3359 3360**示例:** 3361 3362```ts 3363import { BusinessError } from '@ohos.base'; 3364 3365function registerFocusStateChange(captureSession: camera.CaptureSession): void { 3366 captureSession.on('focusStateChange', (err: BusinessError, focusState: camera.FocusState) => { 3367 console.log(`Focus state: ${focusState}`); 3368 }); 3369} 3370``` 3371 3372### off('focusStateChange') 3373 3374off(type: 'focusStateChange', callback?: AsyncCallback\<FocusState\>): void 3375 3376注销监听相机聚焦的状态变化。 3377 3378**系统能力:** SystemCapability.Multimedia.Camera.Core 3379 3380**参数:** 3381 3382| 参数名 | 类型 | 必填 | 说明 | 3383| -------- | ----------------------------------------- | ---- | ------------------------ | 3384| type | string | 是 | 监听事件,固定为'focusStateChange',session 创建成功可监听。| 3385| callback | AsyncCallback\<[FocusState](#focusstate)\> | 否 | 回调函数,可选 | 3386 3387**示例:** 3388 3389```ts 3390function unregisterFocusStateChange(captureSession: camera.CaptureSession): void { 3391 captureSession.off('focusStateChange'); 3392} 3393``` 3394 3395### on('error') 3396 3397on(type: 'error', callback: ErrorCallback): void 3398 3399监听拍照会话的错误事件,通过注册回调函数获取结果。 3400 3401**系统能力:** SystemCapability.Multimedia.Camera.Core 3402 3403**参数:** 3404 3405| 参数名 | 类型 | 必填 | 说明 | 3406| -------- | ----------------------------------------------------------- | ---- | ------------------------------ | 3407| type | string | 是 | 监听事件,固定为'error',session创建成功之后可监听该接口。session调用相关接口出现错误时会触发该事件,比如调用(beginConfig(),commitConfig(),addInput)等接口发生错误时返回错误信息。 | 3408| callback | ErrorCallback| 是 | 回调函数,用于获取错误信息。返回错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 3409 3410**示例:** 3411 3412```ts 3413import { BusinessError } from '@ohos.base'; 3414 3415function registerCaptureSessionError(captureSession: camera.CaptureSession): void { 3416 captureSession.on('error', (error: BusinessError) => { 3417 console.log(`Capture session error code: ${error.code}`); 3418 }); 3419} 3420``` 3421 3422### off('error') 3423 3424off(type: 'error', callback?: ErrorCallback): void 3425 3426监听拍照会话的错误事件,通过注册回调函数获取结果。 3427 3428**系统能力:** SystemCapability.Multimedia.Camera.Core 3429 3430**参数:** 3431 3432| 参数名 | 类型 | 必填 | 说明 | 3433| -------- | ----------------------------------------------------------- | ---- | ------------------------------ | 3434| type | string | 是 | 监听事件,固定为'error',session创建成功之后可监听该接口。 | 3435| callback | ErrorCallback| 否 | 回调函数,可选 | 3436 3437**示例:** 3438 3439```ts 3440function unregisterCaptureSessionError(captureSession: camera.CaptureSession): void { 3441 captureSession.off('error'); 3442} 3443``` 3444## PortraitSession 3445 3446继承自[CaptureSession](#capturesession),用于设置人像模式的参数。 3447 3448### getSupportedPortraitEffects 3449 3450getSupportedPortraitEffects(): Array<[PortraitEffect](#portraiteffect)> 3451 3452获取支持的人像虚化效果列表。 3453 3454**系统接口:** 此接口为系统接口。 3455 3456**系统能力:** SystemCapability.Multimedia.Camera.Core 3457 3458**返回值:** 3459 3460| 类型 | 说明 | 3461| ----------------------------------------------- | ---------------------------- | 3462| Array<[PortraitEffect](#portraiteffect) > | 支持的人像虚化效果列表。 | 3463 3464**错误码:** 3465 3466以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3467 3468| 错误码ID | 错误信息 | 3469| --------------- | --------------- | 3470| 7400103 | Session not config. | 3471 3472**示例:** 3473 3474```ts 3475function getSupportedPortraitEffects(portraitSession: camera.PortraitSession): Array<camera.PortraitEffect> { 3476 let portraitEffects: Array<camera.PortraitEffect> = portraitSession.getSupportedPortraitEffects(); 3477 return portraitEffects; 3478} 3479``` 3480### setPortraitEffect 3481 3482setPortraitEffect(effect: PortraitEffect): void 3483 3484设置人像虚化效果。需要先检查设备是否支持人像虚化模式,可以通过[getSupportedPortraitEffects](#getsupportedportraiteffects)获取当前设备所支持的PortraitEffect。 3485 3486**系统接口:** 此接口为系统接口。 3487 3488**系统能力:** SystemCapability.Multimedia.Camera.Core 3489 3490**参数:** 3491 3492| 参数名 | 类型 | 必填 | 说明 | 3493| ------------ |--------------------------------------------------------------- | -- | -------------------------- | 3494| effect | [PortraitEffect](#portraiteffect) | 是 | 人像虚化效果,通过[getSupportedPortraitEffects](#getsupportedportraiteffects)接口获取。 | 3495 3496**错误码:** 3497 3498以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3499 3500| 错误码ID | 错误信息 | 3501| --------------- | --------------- | 3502| 7400103 | Session not config. | 3503 3504**示例:** 3505 3506```ts 3507import { BusinessError } from '@ohos.base'; 3508 3509function setPortraitEffect(portraitSession: camera.PortraitSession, portraitEffects: Array<camera.PortraitEffect>): void { 3510 if (portraitEffects === undefined || portraitEffects.length <= 0) { 3511 return; 3512 } 3513 try { 3514 portraitSession.setPortraitEffect(portraitEffects[0]); 3515 } catch (error) { 3516 let err = error as BusinessError; 3517 console.error(`The setPortraitEffect call failed. error code: ${err.code}`); 3518 } 3519} 3520``` 3521### getPortraitEffect 3522 3523getPortraitEffect(): PortraitEffect 3524 3525获取当前设置的人像虚化效果。 3526 3527**系统接口:** 此接口为系统接口。 3528 3529**系统能力:** SystemCapability.Multimedia.Camera.Core 3530 3531**返回值:** 3532 3533| 类型 | 说明 | 3534| ----------------------------------------------- | ---------------------------- | 3535| [PortraitEffect](#portraiteffect) | 当前设置的人像虚化效果。 | 3536 3537**错误码:** 3538 3539以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3540 3541| 错误码ID | 错误信息 | 3542| --------------- | --------------- | 3543| 7400103 | Session not config. | 3544 3545**示例:** 3546 3547```ts 3548function getSupportedPortraitEffects(portraitSession: camera.PortraitSession): Array<camera.PortraitEffect> { 3549 let portraitEffects: Array<camera.PortraitEffect> = portraitSession.getSupportedPortraitEffects(); 3550 return portraitEffects; 3551} 3552``` 3553 3554## CameraOutput 3555 3556会话中[CaptureSession](#capturesession)使用的输出信息,output的基类。 3557 3558## PreviewOutput 3559 3560预览输出类。继承[CameraOutput](#cameraoutput) 3561 3562### start 3563 3564start(callback: AsyncCallback\<void\>): void 3565 3566开始输出预览流,通过注册回调函数获取结果。 3567 3568**系统能力:** SystemCapability.Multimedia.Camera.Core 3569 3570**参数:** 3571 3572| 参数名 | 类型 | 必填 | 说明 | 3573| -------- | -------------------- | ---- | -------------------- | 3574| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 3575 3576**错误码:** 3577 3578以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3579 3580| 错误码ID | 错误信息 | 3581| --------------- | --------------- | 3582| 7400103 | Session not config. | 3583 3584**示例:** 3585 3586```ts 3587import { BusinessError } from '@ohos.base'; 3588 3589function startPreviewOutput(previewOutput: camera.PreviewOutput): void { 3590 previewOutput.start((err: BusinessError) => { 3591 if (err) { 3592 console.error(`Failed to start the previewOutput. ${err.code}`); 3593 return; 3594 } 3595 console.log('Callback returned with previewOutput started.'); 3596 }); 3597} 3598``` 3599 3600### start 3601 3602start(): Promise\<void\> 3603 3604开始输出预览流,通过Promise获取结果。 3605 3606**系统能力:** SystemCapability.Multimedia.Camera.Core 3607 3608**返回值:** 3609 3610| 类型 | 说明 | 3611| -------------- | ----------------------- | 3612| Promise\<void\> | 使用Promise的方式获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode)| 3613 3614**错误码:** 3615 3616以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3617 3618| 错误码ID | 错误信息 | 3619| --------------- | --------------- | 3620| 7400103 | Session not config. | 3621 3622**示例:** 3623 3624```ts 3625import { BusinessError } from '@ohos.base'; 3626 3627function startPreviewOutput(previewOutput: camera.PreviewOutput): void { 3628 previewOutput.start().then(() => { 3629 console.log('Promise returned with previewOutput started.'); 3630 }).catch((err: BusinessError) => { 3631 console.log('Failed to previewOutput start '+ err.code); 3632 }); 3633} 3634``` 3635 3636### stop 3637 3638stop(callback: AsyncCallback\<void\>): void 3639 3640停止输出预览流,通过注册回调函数获取结果。 3641 3642**系统能力:** SystemCapability.Multimedia.Camera.Core 3643 3644**参数:** 3645 3646| 参数名 | 类型 | 必填 | 说明 | 3647| -------- | -------------------- | ---- | -------------------- | 3648| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。 | 3649 3650**示例:** 3651 3652```ts 3653import { BusinessError } from '@ohos.base'; 3654 3655function stopPreviewOutput(previewOutput: camera.PreviewOutput): void { 3656 previewOutput.stop((err: BusinessError) => { 3657 if (err) { 3658 console.error(`Failed to stop the previewOutput. ${err.code}`); 3659 return; 3660 } 3661 console.log('Callback returned with previewOutput stopped.'); 3662 }) 3663} 3664``` 3665 3666### stop 3667 3668stop(): Promise\<void\> 3669 3670停止输出预览流,通过Promise获取结果。 3671 3672**系统能力:** SystemCapability.Multimedia.Camera.Core 3673 3674**返回值:** 3675 3676| 类型 | 说明 | 3677| -------------- | ------------------------ | 3678| Promise\<void\> | 使用Promise的方式获取结果。 | 3679 3680**示例:** 3681 3682```ts 3683import { BusinessError } from '@ohos.base'; 3684 3685function stopPreviewOutput(previewOutput: camera.PreviewOutput): void { 3686 previewOutput.stop().then(() => { 3687 console.log('Callback returned with previewOutput stopped.'); 3688 }).catch((err: BusinessError) => { 3689 console.log('Failed to previewOutput stop '+ err.code); 3690 }); 3691} 3692``` 3693 3694### release 3695 3696release(callback: AsyncCallback\<void\>): void 3697 3698释放输出资源,通过注册回调函数获取结果。 3699 3700**系统能力:** SystemCapability.Multimedia.Camera.Core 3701 3702**参数:** 3703 3704| 参数名 | 类型 | 必填 | 说明 | 3705| -------- | -------------------- | ---- | ------------------- | 3706| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 3707 3708**错误码:** 3709 3710以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3711 3712| 错误码ID | 错误信息 | 3713| --------------- | --------------- | 3714| 7400201 | Camera service fatal error. | 3715 3716**示例:** 3717 3718```ts 3719import { BusinessError } from '@ohos.base'; 3720 3721function releasePreviewOutput(previewOutput: camera.PreviewOutput): void { 3722 previewOutput.release((err: BusinessError) => { 3723 if (err) { 3724 console.error(`Failed to release the PreviewOutput instance ${err.code}`); 3725 return; 3726 } 3727 console.log('Callback invoked to indicate that the PreviewOutput instance is released successfully.'); 3728 }); 3729} 3730``` 3731 3732### release 3733 3734release(): Promise\<void\> 3735 3736释放输出资源,通过Promise获取结果。 3737 3738**系统能力:** SystemCapability.Multimedia.Camera.Core 3739 3740**返回值:** 3741 3742| 类型 | 说明 | 3743| -------------- | ----------------------- | 3744| Promise\<void\> | 使用Promise的方式获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 3745 3746**错误码:** 3747 3748以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3749 3750| 错误码ID | 错误信息 | 3751| --------------- | --------------- | 3752| 7400201 | Camera service fatal error. | 3753 3754**示例:** 3755 3756```ts 3757import { BusinessError } from '@ohos.base'; 3758 3759function releasePreviewOutput(previewOutput: camera.PreviewOutput): void { 3760 previewOutput.release().then(() => { 3761 console.log('Promise returned to indicate that the PreviewOutput instance is released successfully.'); 3762 }).catch((err: BusinessError) => { 3763 console.log('Failed to previewOutput release '+ err.code); 3764 }); 3765} 3766``` 3767 3768### on('frameStart') 3769 3770on(type: 'frameStart', callback: AsyncCallback\<void\>): void 3771 3772监听预览帧启动,通过注册回调函数获取结果。 3773 3774**系统能力:** SystemCapability.Multimedia.Camera.Core 3775 3776**参数:** 3777 3778| 参数名 | 类型 | 必填 | 说明 | 3779| -------- | -------------------- | ---- | --------------------------------------- | 3780| type | string | 是 | 监听事件,固定为'frameStart',previewOutput创建成功可监听。底层第一次开始曝光时触发该事件并返回 | 3781| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。只要有该事件返回就证明预览开始 | 3782 3783**示例:** 3784 3785```ts 3786function registerPreviewOutputFrameStart(previewOutput: camera.PreviewOutput): void { 3787 previewOutput.on('frameStart', () => { 3788 console.log('Preview frame started'); 3789 }); 3790} 3791``` 3792 3793### off('frameStart') 3794 3795off(type: 'frameStart', callback?: AsyncCallback\<void\>): void 3796 3797注销监听预览帧启动。 3798 3799**系统能力:** SystemCapability.Multimedia.Camera.Core 3800 3801**参数:** 3802 3803| 参数名 | 类型 | 必填 | 说明 | 3804| -------- | -------------------- | ---- | --------------------------------------- | 3805| type | string | 是 | 监听事件,固定为'frameStart',previewOutput创建成功可监听。 | 3806| callback | AsyncCallback\<void\> | 否 | 回调函数,可选 | 3807 3808**示例:** 3809 3810```ts 3811function unregisterPreviewOutputFrameStart(previewOutput: camera.PreviewOutput): void { 3812 previewOutput.off('frameStart'); 3813} 3814``` 3815 3816### on('frameEnd') 3817 3818on(type: 'frameEnd', callback: AsyncCallback\<void\>): void 3819 3820监听预览帧结束,通过注册回调函数获取结果。 3821 3822**系统能力:** SystemCapability.Multimedia.Camera.Core 3823 3824**参数:** 3825 3826| 参数名 | 类型 | 必填 | 说明 | 3827| -------- | -------------------- | ---- | ------------------------------------- | 3828| type | string | 是 | 监听事件,固定为'frameEnd',previewOutput创建成功可监听。预览完全结束最后一帧时触发该事件并返回, | 3829| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。只要有该事件返回就证明预览结束 | 3830 3831**示例:** 3832 3833```ts 3834function registerPreviewOutputFrameEnd(previewOutput: camera.PreviewOutput): void { 3835 previewOutput.on('frameEnd', () => { 3836 console.log('Preview frame ended'); 3837 }); 3838} 3839``` 3840 3841### off('frameEnd') 3842 3843off(type: 'frameEnd', callback?: AsyncCallback\<void\>): void 3844 3845注销监听预览帧结束。 3846 3847**系统能力:** SystemCapability.Multimedia.Camera.Core 3848 3849**参数:** 3850 3851| 参数名 | 类型 | 必填 | 说明 | 3852| -------- | -------------------- | ---- | ------------------------------------- | 3853| type | string | 是 | 监听事件,固定为'frameEnd',previewOutput创建成功可监听。 | 3854| callback | AsyncCallback\<void\> | 否 | 回调函数,可选 | 3855 3856**示例:** 3857 3858```ts 3859function unregisterPreviewOutputFrameEnd(previewOutput: camera.PreviewOutput): void { 3860 previewOutput.off('frameEnd'); 3861} 3862``` 3863 3864### on('error') 3865 3866on(type: 'error', callback: ErrorCallback): void 3867 3868监听预览输出的错误事件,通过注册回调函数获取结果。 3869 3870**系统能力:** SystemCapability.Multimedia.Camera.Core 3871 3872**参数:** 3873 3874| 参数名 | 类型 | 必填 | 说明 | 3875| -------- | --------------| ---- | ------------------------ | 3876| type | string | 是 | 监听事件,固定为'error',previewOutput创建成功可监听。预览接口使用错误时触发该事件,比如调用(start(),release())等接口发生错误时返回对应错误信息。| 3877| callback | ErrorCallback | 是 | 回调函数,用于获取错误信息。返回错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 3878 3879**示例:** 3880 3881```ts 3882import { BusinessError } from '@ohos.base'; 3883 3884function registerPreviewOutputError(previewOutput: camera.PreviewOutput): void { 3885 previewOutput.on('error', (previewOutputError: BusinessError) => { 3886 console.log(`Preview output error code: ${previewOutputError.code}`); 3887 }) 3888} 3889``` 3890 3891### off('error') 3892 3893off(type: 'error', callback?: ErrorCallback): void 3894 3895注销监听预览输出的错误事件。 3896 3897**系统能力:** SystemCapability.Multimedia.Camera.Core 3898 3899**参数:** 3900 3901| 参数名 | 类型 | 必填 | 说明 | 3902| -------- | --------------| ---- | ------------------------ | 3903| type | string | 是 | 监听事件,固定为'error',previewOutput创建成功可监听。| 3904| callback | ErrorCallback | 否 | 回调函数,可选 | 3905 3906**示例:** 3907 3908```ts 3909function unregisterPreviewOutputError(previewOutput: camera.PreviewOutput): void { 3910 previewOutput.off('error'); 3911} 3912``` 3913 3914### addDeferredSurface 3915 3916addDeferredSurface(surfaceId: string): void 3917 3918配置延迟预览的Surface,可以在session.commitConfig()配流和session.start()启流之后运行。 3919 3920**系统接口:** 此接口为系统接口。 3921 3922**系统能力:** SystemCapability.Multimedia.Camera.Core 3923 3924**参数:** 3925 3926| 参数名 | 类型 | 必填 | 说明 | 3927| -------- | --------------| ---- | ------------------------ | 3928| surfaceId | string | 是 | 从[XComponent](../arkui-ts/ts-basic-components-xcomponent.md)组件获取的surfaceId。| 3929 3930**错误码:** 3931 3932以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 3933 3934| 错误码ID | 错误信息 | 3935| --------------- | --------------- | 3936| 7400101 | Parameter missing or parameter type incorrect | 3937 3938**示例:** 3939 3940```ts 3941import common from '@ohos.app.ability.common'; 3942 3943async function preview(context: common.BaseContext, cameraInfo: camera.CameraDevice, previewProfile: camera.Profile, photoProfile: camera.Profile, photoSurfaceId: string, previewSurfaceId: string): Promise<void> { 3944 const cameraManager: camera.CameraManager = camera.getCameraManager(context); 3945 const cameraInput: camera.CameraInput = cameraManager.createCameraInput(cameraInfo) 3946 const previewOutput: camera.PreviewOutput = cameraManager.createDeferredPreviewOutput(previewProfile); 3947 const photoOutput: camera.PhotoOutput = cameraManager.createPhotoOutput(photoProfile, photoSurfaceId); 3948 const session: camera.CaptureSession = cameraManager.createCaptureSession(); 3949 session.beginConfig(); 3950 session.addInput(cameraInput); 3951 session.addOutput(previewOutput); 3952 session.addOutput(photoOutput); 3953 await session.commitConfig(); 3954 await session.start(); 3955 previewOutput.addDeferredSurface(previewSurfaceId); 3956} 3957``` 3958 3959## ImageRotation 3960 3961枚举,图片旋转角度。 3962 3963**系统能力:** SystemCapability.Multimedia.Camera.Core 3964 3965| 名称 | 值 | 说明 | 3966| ------------ | ---- | ------------- | 3967| ROTATION_0 | 0 | 图片旋转0度。 | 3968| ROTATION_90 | 90 | 图片旋转90度。 | 3969| ROTATION_180 | 180 | 图片旋转180度。 | 3970| ROTATION_270 | 270 | 图片旋转270度。 | 3971 3972## Location 3973 3974图片地理位置信息。 3975 3976**系统能力:** SystemCapability.Multimedia.Camera.Core 3977 3978| 名称 | 类型 | 必填 |说明 | 3979| ------------ | ------ | --- |------------ | 3980| latitude | number | 是 |纬度(度)。 | 3981| longitude | number | 是 |经度(度)。 | 3982| altitude | number | 是 |海拔(米)。 | 3983 3984## QualityLevel 3985 3986枚举,图片质量。 3987 3988**系统能力:** SystemCapability.Multimedia.Camera.Core 3989 3990| 名称 | 值 | 说明 | 3991| -------------------- | ---- | ------------ | 3992| QUALITY_LEVEL_HIGH | 0 | 图片质量高。 | 3993| QUALITY_LEVEL_MEDIUM | 1 | 图片质量中等。 | 3994| QUALITY_LEVEL_LOW | 2 | 图片质量差。 | 3995 3996 3997## PhotoCaptureSetting 3998 3999拍摄照片的设置。 4000 4001**系统能力:** SystemCapability.Multimedia.Camera.Core 4002 4003| 名称 | 类型 | 必填 | 说明 | 4004| -------- | ------------------------------- | ---- | -----------------| 4005| quality | [QualityLevel](#qualitylevel) | 否 | 图片质量。 | 4006| rotation | [ImageRotation](#imagerotation) | 否 | 图片旋转角度。 | 4007| location | [Location](#location) | 否 | 图片地理位置信息。 | 4008| mirror | boolean | 否 |镜像使能开关(默认关)。 | 4009 4010## PhotoOutput 4011 4012拍照会话中使用的输出信息,继承[CameraOutput](#cameraoutput) 4013 4014### capture 4015 4016capture(callback: AsyncCallback\<void\>): void 4017 4018以默认设置触发一次拍照,通过注册回调函数获取结果。 4019 4020**系统能力:** SystemCapability.Multimedia.Camera.Core 4021 4022**参数:** 4023 4024| 参数名 | 类型 | 必填 | 说明 | 4025| -------- | -------------------- | ---- | ------------------- | 4026| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 4027 4028**错误码:** 4029 4030以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 4031 4032| 错误码ID | 错误信息 | 4033| --------------- | --------------- | 4034| 7400104 | Session not running. | 4035| 7400201 | Camera service fatal error. | 4036 4037**示例:** 4038 4039```ts 4040import { BusinessError } from '@ohos.base'; 4041 4042function capture(photoOutput: camera.PhotoOutput): void { 4043 photoOutput.capture((err: BusinessError) => { 4044 if (err) { 4045 console.error(`Failed to capture the photo ${err.code}`); 4046 return; 4047 } 4048 console.log('Callback invoked to indicate the photo capture request success.'); 4049 }); 4050} 4051``` 4052 4053### capture 4054 4055capture(): Promise\<void\> 4056 4057以默认设置触发一次拍照,通过Promise获取结果。 4058 4059**系统能力:** SystemCapability.Multimedia.Camera.Core 4060 4061**返回值:** 4062 4063| 类型 | 说明 | 4064| -------------- | ------------------------ | 4065| Promise\<void\> | 使用Promise的方式获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 4066 4067**错误码:** 4068 4069以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 4070 4071| 错误码ID | 错误信息 | 4072| --------------- | --------------- | 4073| 7400104 | Session not running. | 4074| 7400201 | Camera service fatal error. | 4075 4076**示例:** 4077 4078```ts 4079import { BusinessError } from '@ohos.base'; 4080 4081function capture(photoOutput: camera.PhotoOutput): void { 4082 photoOutput.capture().then(() => { 4083 console.log('Promise returned to indicate that photo capture request success.'); 4084 }).catch((err: BusinessError) => { 4085 console.log('Failed to photoOutput capture '+ err.code); 4086 }); 4087} 4088``` 4089 4090### capture 4091 4092capture(setting: PhotoCaptureSetting, callback: AsyncCallback\<void\>): void 4093 4094以指定参数触发一次拍照,通过注册回调函数获取结果。 4095 4096**系统能力:** SystemCapability.Multimedia.Camera.Core 4097 4098**参数:** 4099 4100| 参数名 | 类型 | 必填 | 说明 | 4101| -------- | ------------------------------------------- | ---- | -------------------- | 4102| setting | [PhotoCaptureSetting](#photocapturesetting) | 是 | 拍照设置。 | 4103| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 4104 4105**错误码:** 4106 4107以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 4108 4109| 错误码ID | 错误信息 | 4110| --------------- | --------------- | 4111| 7400101 | Parameter missing or parameter type incorrect | 4112| 7400104 | Session not running. | 4113| 7400201 | Camera service fatal error. | 4114 4115**示例:** 4116 4117```ts 4118import { BusinessError } from '@ohos.base'; 4119 4120function capture(photoOutput: camera.PhotoOutput): void { 4121 let captureLocation: camera.Location = { 4122 latitude: 0, 4123 longitude: 0, 4124 altitude: 0, 4125 } 4126 let settings: camera.PhotoCaptureSetting = { 4127 quality: camera.QualityLevel.QUALITY_LEVEL_LOW, 4128 rotation: camera.ImageRotation.ROTATION_0, 4129 location: captureLocation, 4130 mirror: false 4131 } 4132 photoOutput.capture(settings, (err: BusinessError) => { 4133 if (err) { 4134 console.error(`Failed to capture the photo ${err.code}`); 4135 return; 4136 } 4137 console.log('Callback invoked to indicate the photo capture request success.'); 4138 }); 4139} 4140``` 4141 4142### capture 4143 4144capture(setting?: PhotoCaptureSetting): Promise\<void\> 4145 4146以指定参数触发一次拍照,通过Promise获取结果。 4147 4148**系统能力:** SystemCapability.Multimedia.Camera.Core 4149 4150**参数:** 4151 4152| 参数名 | 类型 | 必填 | 说明 | 4153| ------- | ------------------------------------------- | ---- | -------- | 4154| setting | [PhotoCaptureSetting](#photocapturesetting) | 否 | 拍照设置,传入undefined类型数据按默认无参处理。 | 4155 4156**返回值:** 4157 4158| 类型 | 说明 | 4159| -------------- | ------------------------ | 4160| Promise\<void\> | 使用Promise的方式获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 4161 4162**错误码:** 4163 4164以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 4165 4166| 错误码ID | 错误信息 | 4167| --------------- | --------------- | 4168| 7400101 | Parameter missing or parameter type incorrect | 4169| 7400104 | Session not running. | 4170| 7400201 | Camera service fatal error. | 4171 4172**示例:** 4173 4174```ts 4175import { BusinessError } from '@ohos.base'; 4176 4177function capture(photoOutput: camera.PhotoOutput): void { 4178 let captureLocation: camera.Location = { 4179 latitude: 0, 4180 longitude: 0, 4181 altitude: 0, 4182 } 4183 let settings: camera.PhotoCaptureSetting = { 4184 quality: camera.QualityLevel.QUALITY_LEVEL_LOW, 4185 rotation: camera.ImageRotation.ROTATION_0, 4186 location: captureLocation, 4187 mirror: false 4188 } 4189 photoOutput.capture(settings).then(() => { 4190 console.log('Promise returned to indicate that photo capture request success.'); 4191 }).catch((err: BusinessError) => { 4192 console.log('Failed to photoOutput capture '+ err.code); 4193 }); 4194} 4195``` 4196 4197### isMirrorSupported 4198 4199isMirrorSupported(): boolean 4200 4201查询是否支持镜像拍照。 4202 4203**系统能力:** SystemCapability.Multimedia.Camera.Core 4204 4205**返回值:** 4206 4207| 类型 | 说明 | 4208| -------------- | ----------------------- | 4209| boolean | 返回是否支持镜像拍照。 | 4210 4211**示例:** 4212 4213```ts 4214function isMirrorSupported(photoOutput: camera.PhotoOutput): boolean { 4215 let isSupported: boolean = photoOutput.isMirrorSupported(); 4216 return isSupported; 4217} 4218``` 4219 4220### release 4221 4222release(callback: AsyncCallback\<void\>): void 4223 4224释放输出资源,通过注册回调函数获取结果。 4225 4226**系统能力:** SystemCapability.Multimedia.Camera.Core 4227 4228**参数:** 4229 4230| 参数名 | 类型 | 必填 | 说明 | 4231| -------- | -------------------- | ---- | ------------------- | 4232| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 4233 4234**错误码:** 4235 4236以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 4237 4238| 错误码ID | 错误信息 | 4239| --------------- | --------------- | 4240| 7400201 | Camera service fatal error. | 4241 4242**示例:** 4243 4244```ts 4245import { BusinessError } from '@ohos.base'; 4246 4247function releasePhotoOutput(photoOutput: camera.PhotoOutput): void { 4248 photoOutput.release((err: BusinessError) => { 4249 if (err) { 4250 console.error(`Failed to release the PreviewOutput instance ${err.code}`); 4251 return; 4252 } 4253 console.log('Callback invoked to indicate that the PreviewOutput instance is released successfully.'); 4254 }); 4255} 4256``` 4257 4258### release 4259 4260release(): Promise\<void\> 4261 4262释放输出资源,通过Promise获取结果。 4263 4264**系统能力:** SystemCapability.Multimedia.Camera.Core 4265 4266**返回值:** 4267 4268| 类型 | 说明 | 4269| -------------- | ----------------------- | 4270| Promise\<void\> | 使用Promise的方式获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 4271 4272**错误码:** 4273 4274以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 4275 4276| 错误码ID | 错误信息 | 4277| --------------- | --------------- | 4278| 7400201 | Camera service fatal error. | 4279 4280**示例:** 4281 4282```ts 4283import { BusinessError } from '@ohos.base'; 4284 4285function releasePhotoOutput(photoOutput: camera.PhotoOutput): void { 4286 photoOutput.release().then(() => { 4287 console.log('Promise returned to indicate that the PreviewOutput instance is released successfully.'); 4288 }).catch((err: BusinessError) => { 4289 console.log('Failed to photoOutput release '+ err.code); 4290 }); 4291} 4292``` 4293 4294### on('captureStart') 4295 4296on(type: 'captureStart', callback: AsyncCallback\<number\>): void 4297 4298监听拍照开始,通过注册回调函数获取Capture ID。 4299 4300**系统能力:** SystemCapability.Multimedia.Camera.Core 4301 4302**参数:** 4303 4304| 参数名 | 类型 | 必填 | 说明 | 4305| -------- | ---------------------- | ---- | ------------------------------------------ | 4306| type | string | 是 | 监听事件,固定为'captureStart',photoOutput创建成功后可监听。每次拍照,底层开始曝光时触发该事件并返回。 | 4307| callback | AsyncCallback\<number\> | 是 | 使用callback的方式获取Capture ID。 | 4308 4309**示例:** 4310 4311```ts 4312import { BusinessError } from '@ohos.base'; 4313 4314function registerPhotoOutputCaptureStart(photoOutput: camera.PhotoOutput): void { 4315 photoOutput.on('captureStart', (err: BusinessError, captureId: number) => { 4316 console.log(`photo capture stated, captureId : ${captureId}`); 4317 }); 4318} 4319``` 4320 4321### off('captureStart') 4322 4323off(type: 'captureStart', callback?: AsyncCallback\<number\>): void 4324 4325注销监听拍照开始。 4326 4327**系统能力:** SystemCapability.Multimedia.Camera.Core 4328 4329**参数:** 4330 4331| 参数名 | 类型 | 必填 | 说明 | 4332| -------- | ---------------------- | ---- | ------------------------------------------ | 4333| type | string | 是 | 监听事件,固定为'captureStart',photoOutput创建成功后可监听 | 4334| callback | AsyncCallback\<number\> | 否 | 可选。 | 4335 4336**示例:** 4337 4338```ts 4339function unregisterPhotoOutputCaptureStart(photoOutput: camera.PhotoOutput): void { 4340 photoOutput.off('captureStart'); 4341} 4342``` 4343 4344### on('frameShutter') 4345 4346on(type: 'frameShutter', callback: AsyncCallback\<FrameShutterInfo\>): void 4347 4348监听拍照帧输出捕获,通过注册回调函数获取结果。 4349 4350**系统能力:** SystemCapability.Multimedia.Camera.Core 4351 4352**参数:** 4353 4354| 参数名 | 类型 | 必填 | 说明 | 4355| -------- | ---------- | --- | ------------------------------------ | 4356| type | string | 是 | 监听事件,固定为'frameShutter',photoOutput创建成功后可监听。 | 4357| callback | AsyncCallback\<[FrameShutterInfo](#frameshutterinfo)\> | 是 | 回调函数,用于获取相关信息。该回调返回意味着可以再次下发拍照请求。 | 4358 4359**示例:** 4360 4361```ts 4362import { BusinessError } from '@ohos.base'; 4363 4364function registerPhotoOutputFrameShutter(photoOutput: camera.PhotoOutput): void { 4365 photoOutput.on('frameShutter', (err: BusinessError, frameShutterInfo: camera.FrameShutterInfo) => { 4366 console.log(`photo capture end, captureId : ${frameShutterInfo.captureId}`); 4367 console.log(`Timestamp for frame : ${frameShutterInfo.timestamp}`); 4368 }); 4369} 4370``` 4371 4372### off('frameShutter') 4373 4374off(type: 'frameShutter', callback?: AsyncCallback\<FrameShutterInfo\>): void 4375 4376注销监听拍照帧输出捕获。 4377 4378**系统能力:** SystemCapability.Multimedia.Camera.Core 4379 4380**参数:** 4381 4382| 参数名 | 类型 | 必填 | 说明 | 4383| -------- | ---------- | --- | ------------------------------------ | 4384| type | string | 是 | 监听事件,固定为'frameShutter',photoOutput创建成功后可监听。 | 4385| callback | AsyncCallback\<[FrameShutterInfo](#frameshutterinfo)\> | 否 | 回调函数,可选。 | 4386 4387**示例:** 4388 4389```ts 4390function unregisterPhotoOutputFrameShutter(photoOutput: camera.PhotoOutput): void { 4391 photoOutput.off('frameShutter'); 4392} 4393``` 4394 4395### on('captureEnd') 4396 4397on(type: 'captureEnd', callback: AsyncCallback\<CaptureEndInfo\>): void 4398 4399监听拍照结束,通过注册回调函数获取结果。 4400 4401**系统能力:** SystemCapability.Multimedia.Camera.Core 4402 4403**参数:** 4404 4405| 参数名 | 类型 | 必填 | 说明 | 4406| -------- | --------------- | ---- | ---------------------------------------- | 4407| type | string | 是 | 监听事件,固定为'captureEnd',photoOutput创建成功后可监听。拍照完全结束可触发该事件发生并返回相应信息。 | 4408| callback | AsyncCallback\<[CaptureEndInfo](#captureendinfo)\> | 是 | 回调函数,用于获取相关信息。 | 4409 4410**示例:** 4411 4412```ts 4413import { BusinessError } from '@ohos.base'; 4414 4415function registerPhotoOutputCaptureEnd(photoOutput: camera.PhotoOutput): void { 4416 photoOutput.on('captureEnd', (err: BusinessError, captureEndInfo: camera.CaptureEndInfo) => { 4417 console.log(`photo capture end, captureId : ${captureEndInfo.captureId}`); 4418 console.log(`frameCount : ${captureEndInfo.frameCount}`); 4419 }); 4420} 4421``` 4422 4423### off('captureEnd') 4424 4425off(type: 'captureEnd', callback?: AsyncCallback\<CaptureEndInfo\>): void 4426 4427注销监听拍照结束。 4428 4429**系统能力:** SystemCapability.Multimedia.Camera.Core 4430 4431**参数:** 4432 4433| 参数名 | 类型 | 必填 | 说明 | 4434| -------- | --------------- | ---- | ---------------------------------------- | 4435| type | string | 是 | 监听事件,固定为'captureEnd',photoOutput创建成功后可监听。 | 4436| callback | AsyncCallback\<[CaptureEndInfo](#captureendinfo)\> | 否 | 回调函数,可选。 | 4437 4438**示例:** 4439 4440```ts 4441function unregisterPhotoOutputCaptureEnd(photoOutput: camera.PhotoOutput): void { 4442 photoOutput.off('captureEnd'); 4443} 4444``` 4445 4446### on('error') 4447 4448on(type: 'error', callback: ErrorCallback): void 4449 4450监听拍照输出发生错误,通过注册回调函数获取结果。 4451 4452**系统能力:** SystemCapability.Multimedia.Camera.Core 4453 4454**参数:** 4455 4456| 参数名 | 类型 | 必填 | 说明 | 4457| -------- | ------------- | ---- | ----------------------------------- | 4458| type | string | 是 | 监听事件,固定为'error',photoOutput创建成功后可监听。拍照接口调用时出现错误触发该事件并返回错误信息。 | 4459| callback | ErrorCallback | 是 | 回调函数,用于获取错误信息。返回错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 4460 4461**示例:** 4462 4463```ts 4464import { BusinessError } from '@ohos.base'; 4465 4466function registerPhotoOutputError(photoOutput: camera.PhotoOutput): void { 4467 photoOutput.on('error', (error: BusinessError) => { 4468 console.log(`Photo output error code: ${error.code}`); 4469 }); 4470} 4471``` 4472 4473### off('error') 4474 4475off(type: 'error', callback?: ErrorCallback): void 4476 4477注销监听拍照输出发生错误。 4478 4479**系统能力:** SystemCapability.Multimedia.Camera.Core 4480 4481**参数:** 4482 4483| 参数名 | 类型 | 必填 | 说明 | 4484| -------- | ------------- | ---- | ----------------------------------- | 4485| type | string | 是 | 监听事件,固定为'error',photoOutput创建成功后可监听。 | 4486| callback | ErrorCallback | 否 | 回调函数,可选 | 4487 4488**示例:** 4489 4490```ts 4491function unregisterPhotoOutputError(photoOutput: camera.PhotoOutput): void { 4492 photoOutput.off('error'); 4493} 4494``` 4495 4496### isQuickThumbnailSupported 4497 4498isQuickThumbnailSupported(): boolean 4499 4500是否支持输出快速缩略图。 4501 4502在CaptureSession.addOutput、CaptureSession.addInput之后,CaptureSession.commitConfig之前生效。 4503 4504**系统接口:** 此接口为系统接口。 4505 4506**系统能力:** SystemCapability.Multimedia.Camera.Core 4507 4508**返回值:** 4509 4510| 类型 | 说明 | 4511| --------- | ------ | 4512| boolean | 返回支持情况,如果返回true表示支持,否则不支持。 | 4513 4514**错误码:** 4515 4516以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 4517 4518| 错误码ID | 错误信息 | 4519| --------------- | --------------- | 4520| 7400104 | session is not running | 4521 4522**示例:** 4523 4524```ts 4525import common from '@ohos.app.ability.common'; 4526 4527async function isQuickThumbnailSupported(context: common.BaseContext, photoProfile: camera.Profile, surfaceId: string): Promise<boolean> { 4528 let cameraManager: camera.CameraManager = camera.getCameraManager(context); 4529 let cameras: Array<camera.CameraDevice> = cameraManager.getSupportedCameras(); 4530 // 创建CaptureSession实例 4531 let captureSession: camera.CaptureSession = cameraManager.createCaptureSession(); 4532 // 开始配置会话 4533 captureSession.beginConfig(); 4534 // 把CameraInput加入到会话 4535 let cameraInput: camera.CameraInput = cameraManager.createCameraInput(cameras[0]); 4536 await cameraInput.open(); 4537 captureSession.addInput(cameraInput); 4538 // 把photoOutput加入到会话 4539 let photoOutput: camera.PhotoOutput = cameraManager.createPhotoOutput(photoProfile, surfaceId); 4540 captureSession.addOutput(photoOutput); 4541 let isSupported: boolean = photoOutput.isQuickThumbnailSupported(); 4542 return isSupported; 4543} 4544``` 4545 4546### enableQuickThumbnail 4547 4548enableQuickThumbnail(enabled: boolean): void 4549 4550使能/去使能快速缩略图。 4551 4552在CaptureSession.addOutput、CaptureSession.addInput之后,CaptureSession.commitConfig之前生效。 4553 4554**系统接口:** 此接口为系统接口。 4555 4556**系统能力:** SystemCapability.Multimedia.Camera.Core 4557 4558**参数:** 4559 4560| 参数名 | 类型 | 必填 | 说明 | 4561| -------- | ------------- | ---- | ----------------------------------- | 4562| enabled | boolean | 是 | true:使能快速缩略图;false:去使能快速缩略图。 | 4563 4564**错误码:** 4565 4566以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 4567 4568| 错误码ID | 错误信息 | 4569| --------------- | --------------- | 4570| 7400104 | session is not running | 4571 4572**示例:** 4573 4574```ts 4575import common from '@ohos.app.ability.common'; 4576import { BusinessError } from '@ohos.base'; 4577 4578async function enableQuickThumbnail(context: common.BaseContext, photoProfile: camera.Profile, surfaceId: string): Promise<void> { 4579 let cameraManager: camera.CameraManager = camera.getCameraManager(context); 4580 let cameras: Array<camera.CameraDevice> = cameraManager.getSupportedCameras(); 4581 // 创建CaptureSession实例 4582 let captureSession: camera.CaptureSession = cameraManager.createCaptureSession(); 4583 // 开始配置会话 4584 captureSession.beginConfig(); 4585 // 把CameraInput加入到会话 4586 let cameraInput: camera.CameraInput = cameraManager.createCameraInput(cameras[0]); 4587 await cameraInput.open(); 4588 captureSession.addInput(cameraInput); 4589 // 把PhotoOutPut加入到会话 4590 let photoOutput: camera.PhotoOutput = cameraManager.createPhotoOutput(photoProfile, surfaceId); 4591 captureSession.addOutput(photoOutput); 4592 let isSupported: boolean = photoOutput.isQuickThumbnailSupported(); 4593 if (!isSupported) { 4594 console.log('Quick Thumbnail is not supported to be turned on.'); 4595 return; 4596 } 4597 try { 4598 photoOutput.enableQuickThumbnail(true); 4599 } catch (error) { 4600 let err = error as BusinessError; 4601 console.error(`The enableQuickThumbnail call failed. error code: ${err.code}`); 4602 } 4603} 4604``` 4605 4606### on('quickThumbnail') 4607 4608on(type: 'quickThumbnail', callback: AsyncCallback\<image.PixelMap>): void 4609 4610监听快速缩略图输出事件。 4611 4612在enableQuickThumbnail(true)使能快速缩略图之后监听生效。 4613 4614**系统接口:** 此接口为系统接口。 4615 4616**系统能力:** SystemCapability.Multimedia.Camera.Core 4617 4618**参数:** 4619 4620| 参数名 | 类型 | 必填 | 说明 | 4621| -------- | ------------- | ---- | ----------------------------------- | 4622| type | string | 是 | 监听事件,固定为'quickThumbnail'。 | 4623| callback | AsyncCallback\<[image.PixelMap](js-apis-image.md#pixelmap7)> | 是 | 回调返回PixelMap。 | 4624 4625**示例:** 4626 4627```ts 4628import common from '@ohos.app.ability.common'; 4629import { BusinessError } from '@ohos.base'; 4630 4631async function registerQuickThumbnail(context: common.BaseContext, photoProfile: camera.Profile, surfaceId: string): Promise<void> { 4632 let cameraManager: camera.CameraManager = camera.getCameraManager(context); 4633 let cameras: Array<camera.CameraDevice> = cameraManager.getSupportedCameras(); 4634 // 创建CaptureSession实例 4635 let captureSession: camera.CaptureSession = cameraManager.createCaptureSession(); 4636 // 开始配置会话 4637 captureSession.beginConfig(); 4638 // 把CameraInput加入到会话 4639 let cameraInput: camera.CameraInput = cameraManager.createCameraInput(cameras[0]); 4640 await cameraInput.open(); 4641 captureSession.addInput(cameraInput); 4642 // 把PhotoOutPut加入到会话 4643 let photoOutput: camera.PhotoOutput = cameraManager.createPhotoOutput(photoProfile, surfaceId); 4644 captureSession.addOutput(photoOutput); 4645 let isSupported: boolean = photoOutput.isQuickThumbnailSupported(); 4646 if (!isSupported) { 4647 console.log('Quick Thumbnail is not supported to be turned on.'); 4648 return; 4649 } 4650 try { 4651 photoOutput.enableQuickThumbnail(true); 4652 } catch (error) { 4653 let err = error as BusinessError; 4654 console.error(`The enableQuickThumbnail call failed. error code: ${err.code}`); 4655 } 4656 4657 photoOutput.on('quickThumbnail', (err: BusinessError, pixelMap: image.PixelMap) => { 4658 if (err || pixelMap === undefined) { 4659 console.error('photoOutput on thumbnail failed'); 4660 return; 4661 } 4662 // 显示或保存pixelMap 4663 // do something 4664 }) 4665} 4666``` 4667 4668### off('quickThumbnail') 4669 4670off(type: 'quickThumbnail', callback?: AsyncCallback\<image.PixelMap>): void 4671 4672注销监听快速缩略图输出事件。 4673 4674**系统接口:** 此接口为系统接口。 4675 4676**系统能力:** SystemCapability.Multimedia.Camera.Core 4677 4678**参数:** 4679 4680| 参数名 | 类型 | 必填 | 说明 | 4681| -------- | ------------- | ---- | ----------------------------------- | 4682| type | string | 是 | 监听事件,固定为'quickThumbnail'。 | 4683| callback | AsyncCallback\<[image.PixelMap](js-apis-image.md#pixelmap7)> | 否 | 可选。 | 4684 4685**示例:** 4686 4687```ts 4688function unregisterQuickThumbnail(photoOutput: camera.PhotoOutput): void { 4689 photoOutput.off('quickThumbnail'); 4690} 4691``` 4692 4693## FrameShutterInfo 4694 4695拍照帧输出信息。 4696 4697**系统能力:** SystemCapability.Multimedia.Camera.Core 4698 4699| 名称 | 类型 | 必填 | 说明 | 4700| --------- | ------ | ---- | ---------- | 4701| captureId | number | 是 | 拍照的ID。 | 4702| timestamp | number | 是 | 快门时间戳。 | 4703 4704## CaptureEndInfo 4705 4706拍照停止信息。 4707 4708**系统能力:** SystemCapability.Multimedia.Camera.Core 4709 4710| 名称 | 类型 | 必填 | 说明 | 4711| ---------- | ------ | ---- | ---------| 4712| captureId | number | 是 | 拍照的ID。 | 4713| frameCount | number | 是 | 帧数。 | 4714 4715## VideoOutput 4716 4717录像会话中使用的输出信息,继承[CameraOutput](#cameraoutput) 4718 4719### start 4720 4721start(callback: AsyncCallback\<void\>): void 4722 4723启动录制,通过注册回调函数获取结果。 4724 4725**系统能力:** SystemCapability.Multimedia.Camera.Core 4726 4727**参数:** 4728 4729| 参数名 | 类型 | 必填 | 说明 | 4730| -------- | -------------------- | ---- | -------------------- | 4731| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 4732 4733**错误码:** 4734 4735以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 4736 4737| 错误码ID | 错误信息 | 4738| --------------- | --------------- | 4739| 7400103 | Session not config. | 4740| 7400201 | Camera service fatal error. | 4741 4742**示例:** 4743 4744```ts 4745import { BusinessError } from '@ohos.base'; 4746 4747function startVideoOutput(videoOutput: camera.VideoOutput): void { 4748 videoOutput.start((err: BusinessError) => { 4749 if (err) { 4750 console.error(`Failed to start the video output ${err.code}`); 4751 return; 4752 } 4753 console.log('Callback invoked to indicate the video output start success.'); 4754 }); 4755} 4756``` 4757 4758### start 4759 4760start(): Promise\<void\> 4761 4762启动录制,通过Promise获取结果。 4763 4764**系统能力:** SystemCapability.Multimedia.Camera.Core 4765 4766**返回值:** 4767 4768| 类型 | 说明 | 4769| -------------- | ----------------------- | 4770| Promise\<void\> | 使用Promise的方式获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 4771 4772**错误码:** 4773 4774以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 4775 4776| 错误码ID | 错误信息 | 4777| --------------- | --------------- | 4778| 7400103 | Session not config. | 4779| 7400201 | Camera service fatal error. | 4780 4781**示例:** 4782 4783```ts 4784import { BusinessError } from '@ohos.base'; 4785 4786function startVideoOutput(videoOutput: camera.VideoOutput): void { 4787 videoOutput.start().then(() => { 4788 console.log('Promise returned to indicate that start method execution success.'); 4789 }).catch((err: BusinessError) => { 4790 console.log('Failed to videoOutput start '+ err.code); 4791 }); 4792} 4793``` 4794 4795### stop 4796 4797stop(callback: AsyncCallback\<void\>): void 4798 4799结束录制,通过注册回调函数获取结果。 4800 4801**系统能力:** SystemCapability.Multimedia.Camera.Core 4802 4803**参数:** 4804 4805| 参数名 | 类型 | 必填 | 说明 | 4806| -------- | -------------------- | ---- | ------------------------ | 4807| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。 | 4808 4809**示例:** 4810 4811```ts 4812import { BusinessError } from '@ohos.base'; 4813 4814function stopVideoOutput(videoOutput: camera.VideoOutput): void { 4815 videoOutput.stop((err: BusinessError) => { 4816 if (err) { 4817 console.error(`Failed to stop the video output ${err.code}`); 4818 return; 4819 } 4820 console.log('Callback invoked to indicate the video output stop success.'); 4821 }); 4822} 4823``` 4824 4825### stop 4826 4827stop(): Promise\<void\> 4828 4829结束录制,通过Promise获取结果。 4830 4831**系统能力:** SystemCapability.Multimedia.Camera.Core 4832 4833**返回值:** 4834 4835| 类型 | 说明 | 4836| -------------- | ----------------------- | 4837| Promise\<void\> | 使用Promise的方式获取结果。 | 4838 4839**示例:** 4840 4841```ts 4842import { BusinessError } from '@ohos.base'; 4843 4844function stopVideoOutput(videoOutput: camera.VideoOutput): void { 4845 videoOutput.stop().then(() => { 4846 console.log('Promise returned to indicate that stop method execution success.'); 4847 }).catch((err: BusinessError) => { 4848 console.log('Failed to videoOutput stop '+ err.code); 4849 }); 4850} 4851``` 4852 4853### release 4854 4855release(callback: AsyncCallback\<void\>): void 4856 4857释放输出资源,通过注册回调函数获取结果。 4858 4859**系统能力:** SystemCapability.Multimedia.Camera.Core 4860 4861**参数:** 4862 4863| 参数名 | 类型 | 必填 | 说明 | 4864| -------- | -------------------- | ---- | ------------------- | 4865| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 4866 4867**错误码:** 4868 4869以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 4870 4871| 错误码ID | 错误信息 | 4872| --------------- | --------------- | 4873| 7400201 | Camera service fatal error. | 4874 4875**示例:** 4876 4877```ts 4878import { BusinessError } from '@ohos.base'; 4879 4880function releaseVideoOutput(videoOutput: camera.VideoOutput): void { 4881 videoOutput.release((err: BusinessError) => { 4882 if (err) { 4883 console.error(`Failed to release the PreviewOutput instance ${err.code}`); 4884 return; 4885 } 4886 console.log('Callback invoked to indicate that the videoOutput instance is released successfully.'); 4887 }); 4888} 4889``` 4890 4891### release 4892 4893release(): Promise\<void\> 4894 4895释放输出资源,通过Promise获取结果。 4896 4897**系统能力:** SystemCapability.Multimedia.Camera.Core 4898 4899**返回值:** 4900 4901| 类型 | 说明 | 4902| -------------- | ----------------------- | 4903| Promise\<void\> | 使用Promise的方式获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 4904 4905**错误码:** 4906 4907以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 4908 4909| 错误码ID | 错误信息 | 4910| --------------- | --------------- | 4911| 7400201 | Camera service fatal error. | 4912 4913**示例:** 4914 4915```ts 4916import { BusinessError } from '@ohos.base'; 4917 4918function releaseVideoOutput(videoOutput: camera.VideoOutput): void { 4919 videoOutput.release().then(() => { 4920 console.log('Promise returned to indicate that the videoOutput instance is released successfully.'); 4921 }).catch((err: BusinessError) => { 4922 console.log('Failed to videoOutput release '+ err.code); 4923 }); 4924} 4925``` 4926 4927### on('frameStart') 4928 4929on(type: 'frameStart', callback: AsyncCallback\<void\>): void 4930 4931监听录像开始,通过注册回调函数获取结果。 4932 4933**系统能力:** SystemCapability.Multimedia.Camera.Core 4934 4935**参数:** 4936 4937| 参数名 | 类型 | 必填 | 说明 | 4938| -------- | -------------------- | ---- | ----------------------------------------- | 4939| type | string | 是 | 监听事件,固定为'frameStart',videoOutput创建成功后可监听。底层第一次曝光时触发该事件并返回。 | 4940| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。 只要有该事件返回就证明录像开始 | 4941 4942**示例:** 4943 4944```ts 4945function registerVideoOutputFrameStart(videoOutput: camera.VideoOutput): void { 4946 videoOutput.on('frameStart', () => { 4947 console.log('Video frame started'); 4948 }); 4949} 4950``` 4951 4952### off('frameStart') 4953 4954off(type: 'frameStart', callback?: AsyncCallback\<void\>): void 4955 4956注销监听录像开始。 4957 4958**系统能力:** SystemCapability.Multimedia.Camera.Core 4959 4960**参数:** 4961 4962| 参数名 | 类型 | 必填 | 说明 | 4963| -------- | -------------------- | ---- | ----------------------------------------- | 4964| type | string | 是 | 监听事件,固定为'frameStart',videoOutput创建成功后可监听。 | 4965| callback | AsyncCallback\<void\> | 否 | 回调函数,可选 | 4966 4967**示例:** 4968 4969```ts 4970function unregisterVideoOutputFrameStart(videoOutput: camera.VideoOutput): void { 4971 videoOutput.off('frameStart'); 4972} 4973 4974``` 4975 4976### on('frameEnd') 4977 4978on(type: 'frameEnd', callback: AsyncCallback\<void\>): void 4979 4980监听录像结束,通过注册回调函数获取结果。 4981 4982**系统能力:** SystemCapability.Multimedia.Camera.Core 4983 4984**参数:** 4985 4986| 参数名 | 类型 | 必填 | 说明 | 4987| -------- | -------------------- | ---- | ------------------------------------------ | 4988| type | string | 是 | 监听事件,固定为'frameEnd',videoOutput创建成功后可监听。录像完全结束最后一帧时触发该事件并返回 。 | 4989| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。 只要有该事件返回就证明录像结束 | 4990 4991**示例:** 4992 4993```ts 4994function registerVideoOutputFrameEnd(videoOutput: camera.VideoOutput): void { 4995 videoOutput.on('frameEnd', () => { 4996 console.log('Video frame ended'); 4997 }); 4998} 4999``` 5000 5001### off('frameEnd') 5002 5003off(type: 'frameEnd', callback?: AsyncCallback\<void\>): void 5004 5005注销监听录像结束。 5006 5007**系统能力:** SystemCapability.Multimedia.Camera.Core 5008 5009**参数:** 5010 5011| 参数名 | 类型 | 必填 | 说明 | 5012| -------- | -------------------- | ---- | ------------------------------------------ | 5013| type | string | 是 | 监听事件,固定为'frameEnd',videoOutput创建成功后可监听 。 | 5014| callback | AsyncCallback\<void\> | 否 | 回调函数,可选 | 5015 5016**示例:** 5017 5018```ts 5019function unregisterVideoOutputFrameEnd(videoOutput: camera.VideoOutput): void { 5020 videoOutput.off('frameEnd'); 5021} 5022``` 5023 5024### on('error') 5025 5026on(type: 'error', callback: ErrorCallback): void 5027 5028监听录像输出发生错误,通过注册回调函数获取结果。 5029 5030**系统能力:** SystemCapability.Multimedia.Camera.Core 5031 5032**参数:** 5033 5034| 参数名 | 类型 | 必填 | 说明 | 5035| -------- | ----------- | ---- | -------------------------------------- | 5036| type | string | 是 | 监听事件,固定为'error',videoOutput创建成功后可监听。录像接口调用出现错误时触发该事件并返回对应错误码,比如调用(start(),release())接口时出现错误返回对应错误信息。 | 5037| callback | ErrorCallback | 是 | 回调函数,用于获取错误信息。返回错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 5038 5039**示例:** 5040 5041```ts 5042import { BusinessError } from '@ohos.base'; 5043 5044function registerVideoOutputError(videoOutput: camera.VideoOutput): void { 5045 videoOutput.on('error', (error: BusinessError) => { 5046 console.log(`Video output error code: ${error.code}`); 5047 }); 5048} 5049``` 5050 5051### off('error') 5052 5053off(type: 'error', callback?: ErrorCallback): void 5054 5055注销监听录像输出发生错误。 5056 5057**系统能力:** SystemCapability.Multimedia.Camera.Core 5058 5059**参数:** 5060 5061| 参数名 | 类型 | 必填 | 说明 | 5062| -------- | ----------- | ---- | -------------------------------------- | 5063| type | string | 是 | 监听事件,固定为'error',videoOutput创建成功后可监听。 | 5064| callback | ErrorCallback | 否 | 回调函数,可选 | 5065 5066**示例:** 5067 5068```ts 5069function unregisterVideoOutputError(videoOutput: camera.VideoOutput): void { 5070 videoOutput.off('error'); 5071} 5072``` 5073 5074## MetadataOutput 5075 5076metadata流。继承[CameraOutput](#cameraoutput) 5077 5078### start 5079 5080start(callback: AsyncCallback\<void\>): void 5081 5082开始输出metadata,通过注册回调函数获取结果。 5083 5084**系统能力:** SystemCapability.Multimedia.Camera.Core 5085 5086**参数:** 5087 5088| 参数名 | 类型 | 必填 | 说明 | 5089| -------- | -------------------------- | ---- | ------------------- | 5090| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 5091 5092**错误码:** 5093 5094以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 5095 5096| 错误码ID | 错误信息 | 5097| --------------- | --------------- | 5098| 7400103 | Session not config. | 5099| 7400201 | Camera service fatal error. | 5100 5101**示例:** 5102 5103```ts 5104import { BusinessError } from '@ohos.base'; 5105 5106function startMetadataOutput(metadataOutput: camera.MetadataOutput): void { 5107 metadataOutput.start((err: BusinessError) => { 5108 if (err) { 5109 console.error(`Failed to start metadataOutput. ${err.code}`); 5110 return; 5111 } 5112 console.log('Callback returned with metadataOutput started.'); 5113 }); 5114} 5115``` 5116 5117### start 5118 5119start(): Promise\<void\> 5120 5121开始输出metadata,通过Promise获取结果。 5122 5123**系统能力:** SystemCapability.Multimedia.Camera.Core 5124 5125**返回值:** 5126 5127| 类型 | 说明 | 5128| ---------------------- | ------------------------ | 5129| Promise\<void\> | 使用Promise的方式获取结果。接口调用失败会返回相应错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 5130 5131**错误码:** 5132 5133以下错误码的详细介绍请参见[Camera错误码](../errorcodes/errorcode-camera.md)。 5134 5135| 错误码ID | 错误信息 | 5136| --------------- | --------------- | 5137| 7400103 | Session not config. | 5138| 7400201 | Camera service fatal error. | 5139 5140**示例:** 5141 5142```ts 5143import { BusinessError } from '@ohos.base'; 5144 5145function startMetadataOutput(metadataOutput: camera.MetadataOutput): void { 5146 metadataOutput.start().then(() => { 5147 console.log('Callback returned with metadataOutput started.'); 5148 }).catch((err: BusinessError) => { 5149 console.log('Failed to metadataOutput start '+ err.code); 5150 }); 5151} 5152``` 5153 5154### stop 5155 5156stop(callback: AsyncCallback\<void\>): void 5157 5158停止输出metadata,通过注册回调函数获取结果。 5159 5160**系统能力:** SystemCapability.Multimedia.Camera.Core 5161 5162**参数:** 5163 5164| 参数名 | 类型 | 必填 | 说明 | 5165| -------- | -------------------------- | ---- | ------------------- | 5166| callback | AsyncCallback\<void\> | 是 | 回调函数,用于获取结果。 | 5167 5168**示例:** 5169 5170```ts 5171import { BusinessError } from '@ohos.base'; 5172 5173function stopMetadataOutput(metadataOutput: camera.MetadataOutput): void { 5174 metadataOutput.stop((err: BusinessError) => { 5175 if (err) { 5176 console.error(`Failed to stop the metadataOutput. ${err.code}`); 5177 return; 5178 } 5179 console.log('Callback returned with metadataOutput stopped.'); 5180 }) 5181} 5182``` 5183 5184### stop 5185 5186stop(): Promise\<void\> 5187 5188停止输出metadata,通过Promise获取结果。 5189 5190**系统能力:** SystemCapability.Multimedia.Camera.Core 5191 5192**返回值:** 5193 5194| 类型 | 说明 | 5195| ---------------------- | --------------------------- | 5196| Promise\<void\> | 使用Promise的方式获取结果。 | 5197 5198**示例:** 5199 5200```ts 5201import { BusinessError } from '@ohos.base'; 5202 5203function stopMetadataOutput(metadataOutput: camera.MetadataOutput): void { 5204 metadataOutput.stop().then(() => { 5205 console.log('Callback returned with metadataOutput stopped.'); 5206 }).catch((err: BusinessError) => { 5207 console.log('Failed to metadataOutput stop '+ err.code); 5208 }); 5209} 5210``` 5211 5212### on('metadataObjectsAvailable') 5213 5214on(type: 'metadataObjectsAvailable', callback: AsyncCallback\<Array\<MetadataObject\>\>): void 5215 5216监听检测到的metadata对象,通过注册回调函数获取结果。 5217 5218**系统能力:** SystemCapability.Multimedia.Camera.Core 5219 5220**参数:** 5221 5222| 参数名 | 类型 | 必填 | 说明 | 5223| -------- | -------------- | ---- | ------------------------------------ | 5224| type | string | 是 | 监听事件,固定为'metadataObjectsAvailable',metadataOutput创建成功后可监听。检测到有效的metadata数据时触发该事件发生并返回相应的metadata数据 | 5225| callback | Callback\<Array\<[MetadataObject](#metadataobject)\>\> | 是 | 回调函数,用于获取metadata数据。 | 5226 5227**示例:** 5228 5229```ts 5230import { BusinessError } from '@ohos.base'; 5231 5232function registerMetadataObjectsAvailable(metadataOutput: camera.MetadataOutput): void { 5233 metadataOutput.on('metadataObjectsAvailable', (err: BusinessError, metadataObjectArr: Array<camera.MetadataObject>) => { 5234 console.log(`metadata output metadataObjectsAvailable`); 5235 }); 5236} 5237``` 5238 5239### off('metadataObjectsAvailable') 5240 5241off(type: 'metadataObjectsAvailable', callback?: AsyncCallback\<Array\<MetadataObject\>\>): void 5242 5243注销监听检测到的metadata对象。 5244 5245**系统能力:** SystemCapability.Multimedia.Camera.Core 5246 5247**参数:** 5248 5249| 参数名 | 类型 | 必填 | 说明 | 5250| -------- | -------------- | ---- | ------------------------------------ | 5251| type | string | 是 | 监听事件,固定为'metadataObjectsAvailable',metadataOutput创建成功后可监听。 | 5252| callback | Callback\<Array\<[MetadataObject](#metadataobject)\>\> | 否 | 回调函数,可选。 | 5253 5254**示例:** 5255 5256```ts 5257function unregisterMetadataObjectsAvailable(metadataOutput: camera.MetadataOutput): void { 5258 metadataOutput.off('metadataObjectsAvailable'); 5259} 5260``` 5261 5262### on('error') 5263 5264on(type: 'error', callback: ErrorCallback): void 5265 5266监听metadata流的错误,通过注册回调函数获取结果。 5267 5268**系统能力:** SystemCapability.Multimedia.Camera.Core 5269 5270**参数:** 5271 5272| 参数名 | 类型 | 必填 | 说明 | 5273| -------- | ------------- | ---- | --------------------------------------- | 5274| type | string | 是 | 监听事件,固定为'error',metadataOutput创建成功后可监听。metadata接口使用错误时触发该事件并返回对应错误码,比如调用(start(),release())接口时发生错误返回对应错误信息。 | 5275| callback | ErrorCallback | 是 | 回调函数,用于获取错误信息。返回错误码,错误码类型[CameraErrorCode](#cameraerrorcode) | 5276 5277**示例:** 5278 5279```ts 5280import { BusinessError } from '@ohos.base'; 5281 5282function registerMetadataOutputError(metadataOutput: camera.MetadataOutput): void { 5283 metadataOutput.on('error', (metadataOutputError: BusinessError) => { 5284 console.log(`Metadata output error code: ${metadataOutputError.code}`); 5285 }); 5286} 5287``` 5288 5289### off('error') 5290 5291off(type: 'error', callback?: ErrorCallback): void 5292 5293注销监听metadata流的错误。 5294 5295**系统能力:** SystemCapability.Multimedia.Camera.Core 5296 5297**参数:** 5298 5299| 参数名 | 类型 | 必填 | 说明 | 5300| -------- | ------------- | ---- | --------------------------------------- | 5301| type | string | 是 | 监听事件,固定为'error',metadataOutput创建成功后可监听。 | 5302| callback | ErrorCallback | 否 | 回调函数,可选 | 5303 5304**示例:** 5305 5306```ts 5307function unregisterMetadataOutputError(metadataOutput: camera.MetadataOutput): void { 5308 metadataOutput.off('error'); 5309} 5310``` 5311 5312## MetadataObjectType 5313 5314枚举,metadata流。 5315 5316**系统能力:** SystemCapability.Multimedia.Camera.Core 5317 5318| 名称 | 值 | 说明 | 5319| ------------------------- | ---- | ----------------- | 5320| FACE_DETECTION | 0 | metadata对象类型,人脸检测。检测点应在0-1坐标系内,该坐标系左上角为{0,0},右下角为{1,1}。<br> 此坐标系是以设备充电口在右侧时的横向设备方向为基准的,<br> 例如应用的预览界面布局以设备充电口在下侧时的竖向方向为基准,<br> 布局宽高为{w,h}, 返回点为{x,y},则转换后的坐标点为{1-y,x}。 | 5321 5322## Rect 5323 5324矩形定义。 5325 5326**系统能力:** SystemCapability.Multimedia.Camera.Core 5327 5328| 名称 | 类型 | 必填 | 说明 | 5329| -------- | ------ | --------------- | -------------------- | 5330| topLeftX | number | 是 | 矩形区域左上角x坐标。 | 5331| topLeftY | number | 是 | 矩形区域左上角y坐标。 | 5332| width | number | 是 | 矩形宽。 | 5333| height | number | 是 |矩形高。 | 5334 5335## MetadataObject 5336 5337相机元能力信息,[CameraInput](#camerainput)相机信息中的数据来源,通过metadataOutput.on('metadataObjectsAvailable')接口获取 5338 5339**系统能力:** SystemCapability.Multimedia.Camera.Core 5340 5341| 名称 | 类型 | 必填 | 说明 | 5342| -------- | ------------------------------- | ---- | -----------------| 5343| type | [MetadataObjectType](#metadataobjecttype) | 否 | metadata 类型,目前只有人脸识别。 | 5344| timestamp | number | 否 | 当前时间戳(毫秒)。 | 5345| boundingBox | [Rect](#rect) | 否 | metadata 区域框 |