文档管理中心
您当前正在浏览HarmonyOS最新文档,覆盖已发布的所有API版本,可在API参考中筛选您使用的API版本。详细的版本配套关系请参考版本说明
API参考媒体Scan Kit(统一扫码服务)ArkTS APIcustomScan (自定义界面扫码)

customScan (自定义界面扫码)

模块概述

PhoneTabletWearable

customScan模块提供自定义界面扫码能力,允许开发者根据自身需求自定义扫码界面,通过XComponent组件渲染相机预览流,并获取扫码结果。

基础扫码流程

使用customScan模块进行扫码,需要按以下顺序调用API:

  1. 初始化:调用init配置扫码参数。
  2. 启动扫码:调用start启动相机流并开始扫码。
  3. 处理结果:在回调中获取ScanResult并进行业务处理。
  4. 暂停:调用stop暂停相机流。
  5. 释放:调用release释放资源。

流程伪代码如下:

收起
自动换行
深色代码主题
复制
  1. // 1. 初始化扫码参数
  2. let options: scanBarcode.ScanOptions = {
  3. scanTypes: [scanCore.ScanType.ALL],
  4. enableMultiMode: true,
  5. enableAlbum: true
  6. };
  7. customScan.init(options);
  8. // 2. 创建XComponent并获取XComponent的surface的ID,构建ViewControl,启动扫码
  9. let surfaceId: string = xComponentController.getXComponentSurfaceId();
  10. // 相机控制参数,包含width、height、surfaceId三个属性,用于配置自定义界面扫码的相机预览流尺寸和渲染的目标surface
  11. let viewControl: customScan.ViewControl = {
  12. width: 360,
  13. height: 640,
  14. surfaceId: surfaceId
  15. };
  16. customScan.start(viewControl, (err: BusinessError, data: Array<scanBarcode.ScanResult>) => {
  17. // 3. 从data中获取扫码结果,并进行业务处理
  18. // ...
  19. });
  20. // 4. 扫码完成后暂停相机流
  21. await customScan.stop();
  22. // 5. 退出扫码时释放资源
  23. await customScan.release();

生命周期和状态机

customScan模块的扫码全流程包含:初始化(init)、启动相机流扫码(start)、暂停相机流(stop)、释放资源(release),状态图如下:

状态说明

展开
状态 可用API 说明
Initialized start 扫码已初始化,可启动相机流进行扫码。
Running getFlashLightStatus、openFlashLight、closeFlashLight、setZoom、getZoom、setFocusPoint、resetFocus、setAutoZoomEnabled、rescan、on('lightingFlash')、off('lightingFlash')、stop 扫码进行中,可进行闪光灯控制、变焦调节、对焦设置、重新触发扫码、暂停扫码。
Paused off('lightingFlash')、release、start 扫码已暂停,可释放资源或重新启动扫码。

为便于开发者快速上手,建议参考官方提供的示例工程

起始版本: 4.1.0(11)

导入模块

PhoneTabletWearable
收起
自动换行
深色代码主题
复制
  1. import { customScan } from '@kit.ScanKit';

ViewControl

PhoneTabletWearable

相机控制参数,用于配置自定义界面扫码的相机预览流尺寸和渲染的目标surface。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Multimedia.Scan.ScanBarcode

起始版本: 4.1.0(11)

展开
名称 类型 只读 可选 说明
width number XComponent组件的宽度,默认使用单位为vp,支持px、lpx和vp。建议在不同屏幕密度下统一使用vp单位以保证兼容性。
height number XComponent组件的高度,默认使用单位为vp,支持px、lpx和vp。建议在不同屏幕密度下统一使用vp单位以保证兼容性。
surfaceId string XComponent持有surface的ID。 用于指定相机预览流渲染的目标surface。
说明
  1. ViewControl的width和height需和XComponent的保持一致,start接口根据设置宽高值会匹配最接近的相机分辨率,如果宽高比例与相机的分辨率比例相差过大会影响预览流体验。XComponent组件为预览流提供的surface,而XComponent的能力由UI提供,相关介绍可参见XComponent

  2. 当开发设备为折叠屏时,折叠态切换时需自行调整XComponent的宽高,start接口会重新适配相机分辨率比例。

示例:

收起
自动换行
深色代码主题
复制
  1. import { hilog } from '@kit.PerformanceAnalysisKit';
  2. import { BusinessError } from '@kit.BasicServicesKit';
  3. import { scanBarcode, customScan } from '@kit.ScanKit';
  4. @Entry
  5. @Component
  6. struct CustomScanPage {
  7. // 设置预览流高度,默认单位:vp
  8. @State cameraHeight: number = 640;
  9. // 设置预览流宽度,默认单位:vp
  10. @State cameraWidth: number = 360;
  11. private mXComponentController: XComponentController = new XComponentController();
  12. build() {
  13. Stack() {
  14. XComponent({
  15. id: 'componentId',
  16. type: XComponentType.SURFACE,
  17. controller: this.mXComponentController
  18. })
  19. .onLoad(() => {
  20. hilog.info(0x0001, '[Scan Sample]', 'onLoad is called');
  21. // 获取XComponent的surfaceId
  22. let surfaceId: string = this.mXComponentController.getXComponentSurfaceId();
  23. hilog.info(0x0001, 'viewControl', `onLoad surfaceId: ${surfaceId}`);
  24. // 设置ViewControl相应字段
  25. let viewControl: customScan.ViewControl = {
  26. width: this.cameraWidth,
  27. height: this.cameraHeight,
  28. surfaceId: surfaceId
  29. };
  30. try {
  31. customScan.start(viewControl).then((scanResult: Array<scanBarcode.ScanResult>) => {
  32. hilog.info(0x0001, '[Scan Sample]',
  33. `Succeeded in getting ScanResult by promise, scanResult is ${JSON.stringify(scanResult)}`);
  34. }).catch((err: BusinessError) => {
  35. hilog.error(0x0001, '[Scan Sample]',
  36. `Failed to get ScanResult by promise. Code: ${err.code}, message: ${err.message}`);
  37. });
  38. } catch (err) {
  39. hilog.error(0x0001, '[Scan Sample]',
  40. `Failed to start customScan. Code: ${err.code}, message: ${err.message}`);
  41. }
  42. })
  43. .height(this.cameraHeight)
  44. .width(this.cameraWidth)
  45. .position({ x: 0, y: 0 })
  46. }
  47. .alignContent(Alignment.Bottom)
  48. .height('100%')
  49. .width('100%')
  50. .position({ x: 0, y: 0 })
  51. }
  52. }

ScanFrame

PhoneTabletWearable

相机预览流(YUV)。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Multimedia.Scan.ScanBarcode

起始版本: 5.0.0(12)

展开
名称 类型 只读 可选 说明
byteBuffer ArrayBuffer 相机预览流的ArrayBuffer数组,数据格式为YUV(NV21格式,基于4:2:0采样)。
width number 相机预览流的宽度,单位:px。
height number 相机预览流的高度,单位:px。
scanCodeRects Array<scanBarcode.ScanCodeRect>

相机预览流(byteBuffer)中检测到的码图位置信息。

设备行为差异: 该属性在带有Kirin NPU(Neural-network Processing Unit,神经网络处理器)的设备可正常返回,在不带有Kirin NPU的设备上返回undefined。

示例:

收起
自动换行
深色代码主题
复制
  1. import { hilog } from '@kit.PerformanceAnalysisKit';
  2. import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
  3. import { scanBarcode, customScan } from '@kit.ScanKit';
  4. @Entry
  5. @Component
  6. struct CustomScanPage {
  7. // 设置预览流高度,默认单位:vp
  8. @State cameraHeight: number = 640;
  9. // 设置预览流宽度,默认单位:vp
  10. @State cameraWidth: number = 360;
  11. private mXComponentController: XComponentController = new XComponentController();
  12. private callback: AsyncCallback<scanBarcode.ScanResult[]> =
  13. (err: BusinessError, data: scanBarcode.ScanResult[]) => {
  14. if (err) {
  15. hilog.error(0x0001, '[Scan Sample]',
  16. `Failed to get ScanResult by callback. Code: ${err.code}, message: ${err.message}`);
  17. return;
  18. }
  19. hilog.info(0x0001, '[Scan Sample]',
  20. `Succeeded in getting ScanResult by callback, result is ${JSON.stringify(data)}`);
  21. };
  22. // 回调获取ScanFrame
  23. private frameCallback: AsyncCallback<customScan.ScanFrame> =
  24. (err: BusinessError, frameResult: customScan.ScanFrame) => {
  25. if (err) {
  26. hilog.error(0x0001, '[Scan Sample]',
  27. `Failed to get ScanFrame by callback. Code: ${err.code}, message: ${err.message}`);
  28. return;
  29. }
  30. // byteBuffer相机YUV图像数组
  31. hilog.info(0x0001, '[Scan Sample]',
  32. `Succeeded in getting ScanFrame.byteBuffer.byteLength: ${frameResult.byteBuffer.byteLength}`);
  33. hilog.info(0x0001, '[Scan Sample]',
  34. `Succeeded in getting ScanFrame.scanCodeRect: ${JSON.stringify(frameResult.scanCodeRects)}`);
  35. };
  36. build() {
  37. Stack() {
  38. XComponent({
  39. id: 'componentId',
  40. type: XComponentType.SURFACE,
  41. controller: this.mXComponentController
  42. })
  43. .onLoad(() => {
  44. hilog.info(0x0001, '[Scan Sample]', 'Succeeded in loading, onLoad is called');
  45. // 获取XComponent的surfaceId
  46. let surfaceId: string = this.mXComponentController.getXComponentSurfaceId();
  47. hilog.info(0x0001, '[Scan Sample]', `Succeeded in getting surfaceId: ${surfaceId}`);
  48. // 设置ViewControl相应字段
  49. let viewControl: customScan.ViewControl = {
  50. width: this.cameraWidth,
  51. height: this.cameraHeight,
  52. surfaceId: surfaceId
  53. };
  54. try {
  55. customScan.start(viewControl, this.callback, this.frameCallback);
  56. } catch (err) {
  57. hilog.error(0x0001, '[Scan Sample]',
  58. `Failed to start customScan. Code: ${err.code}, message: ${err.message}`);
  59. }
  60. })
  61. .height(this.cameraHeight)
  62. .width(this.cameraWidth)
  63. .position({ x: 0, y: 0 })
  64. }
  65. .alignContent(Alignment.Bottom)
  66. .height('100%')
  67. .width('100%')
  68. .position({ x: 0, y: 0 })
  69. }
  70. }

init

PhoneTabletWearable

init(options?: scanBarcode.ScanOptions): void

初始化自定义界面扫码。

模型约束: 此接口仅可在Stage模型下使用。

需要权限: ohos.permission.CAMERA

系统能力: SystemCapability.Multimedia.Scan.ScanBarcode

设备行为差异: 在API版本6.0.2(22)及之前,该接口在Phone、Tablet中可正常调用。在API版本6.1.0(23)及之后,该接口在Phone、Tablet、带后置相机的Wearable中可正常调用,申请相机权限成功后,在不带后置相机的Wearable中返回错误码1000500001。可以通过getSupportedCameras接口查询是否带后置相机。

起始版本: 4.1.0(11)

参数:

展开
参数名 类型 必填 说明
options scanBarcode.ScanOptions

自定义界面扫码配置参数。

默认值: 参考ScanOptions的默认值。

错误码:

以下错误码的详细介绍请参见通用错误码ArkTS API错误码

从API版本5.0.2(14)开始,customScan模块的init接口新增错误码201。

  • 在API版本5.0.2(14)之前,未申请相机权限时调用customScan模块init接口,返回错误码1000500001。

  • 在API版本5.0.2(14)及之后,未申请相机权限时调用customScan模块init接口,返回错误码201。

展开
错误码ID 错误信息
201

Permission verification failed. The application does not have the permission required to call the API.

适用版本:5.0.2(14)+

401 Parameter error. Possible causes: 1. Incorrect parameter types; 2. Parameter verification failed.
1000500001 Internal error.

示例:

收起
自动换行
深色代码主题
复制
  1. import { scanBarcode, scanCore, customScan } from '@kit.ScanKit';
  2. import { hilog } from '@kit.PerformanceAnalysisKit';
  3. // 定义扫码配置参数
  4. let options: scanBarcode.ScanOptions = {
  5. scanTypes: [scanCore.ScanType.ALL],
  6. enableMultiMode: true,
  7. enableAlbum: true
  8. };
  9. try {
  10. customScan.init(options);
  11. } catch (err) {
  12. hilog.error(0x0001, '[Scan Sample]', `Failed to init customScan. Code: ${err.code}, message: ${err.message}`);
  13. }

start

PhoneTabletWearable

start(viewControl: ViewControl): Promise<Array<scanBarcode.ScanResult>>

启动扫码相机流获取扫码结果。使用Promise异步回调。

说明
  1. 此接口需要在init接口调用后才能使用,否则会抛出错误码1000500001。

  2. 此接口为长期占用相机资源操作,扫码完成后应及时调用stop、release释放资源。

模型约束: 此接口仅可在Stage模型下使用。

需要权限: ohos.permission.CAMERA

系统能力: SystemCapability.Multimedia.Scan.ScanBarcode

设备行为差异: 在API版本6.0.2(22)及之前,该接口在Phone、Tablet中可正常调用。在API版本6.1.0(23)及之后,该接口在Phone、Tablet、带后置相机的Wearable中可正常调用,申请相机权限成功后,在不带后置相机的Wearable中返回错误码1000500001。可以通过getSupportedCameras接口查询是否带后置相机。

起始版本: 4.1.0(11)

参数:

展开
参数名 类型 必填 说明
viewControl ViewControl 相机控制参数,用于配置自定义界面扫码的相机预览流尺寸和渲染的目标surface。

返回值:

展开
类型 说明
Promise<Array<scanBarcode.ScanResult>> Promise对象,返回启动相机流扫码结果对象数组。

错误码:

以下错误码的详细介绍请参见通用错误码ArkTS API错误码

从API版本5.0.2(14)开始,customScan模块的start接口新增错误码201。

  • 在API版本5.0.2(14)之前,未申请相机权限时调用customScan模块start接口,返回错误码1000500001。

  • 在API版本5.0.2(14)及之后,未申请相机权限时调用customScan模块start接口,返回错误码201。

展开
错误码ID 错误信息
201

Permission verification failed. The application does not have the permission required to call the API.

适用版本:5.0.2(14)+

401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
1000500001 Internal error.

示例:

收起
自动换行
深色代码主题
复制
  1. import { hilog } from '@kit.PerformanceAnalysisKit';
  2. import { BusinessError } from '@kit.BasicServicesKit';
  3. import { scanBarcode, customScan } from '@kit.ScanKit';
  4. @Entry
  5. @Component
  6. struct CustomScanPage {
  7. // 设置预览流高度,默认单位:vp
  8. @State cameraHeight: number = 640;
  9. // 设置预览流宽度,默认单位:vp
  10. @State cameraWidth: number = 360;
  11. private mXComponentController: XComponentController = new XComponentController();
  12. build() {
  13. Stack() {
  14. XComponent({
  15. id: 'componentId',
  16. type: XComponentType.SURFACE,
  17. controller: this.mXComponentController
  18. })
  19. .onLoad(() => {
  20. hilog.info(0x0001, '[Scan Sample]', 'Succeeded in loading, onLoad is called');
  21. // 获取XComponent的surfaceId
  22. let surfaceId: string = this.mXComponentController.getXComponentSurfaceId();
  23. hilog.info(0x0001, '[Scan Sample]', `Succeeded in getting surfaceId: ${surfaceId}`);
  24. // 设置ViewControl相应字段
  25. let viewControl: customScan.ViewControl = {
  26. width: this.cameraWidth,
  27. height: this.cameraHeight,
  28. surfaceId: surfaceId
  29. };
  30. try {
  31. customScan.start(viewControl).then((scanResult: Array<scanBarcode.ScanResult>) => {
  32. hilog.info(0x0001, '[Scan Sample]',
  33. `Succeeded in getting ScanResult by promise, scanResult is ${JSON.stringify(scanResult)}`);
  34. }).catch((err: BusinessError) => {
  35. hilog.error(0x0001, '[Scan Sample]',
  36. `Failed to get ScanResult by promise. Code: ${err.code}, message: ${err.message}`);
  37. });
  38. } catch (err) {
  39. hilog.error(0x0001, '[Scan Sample]',
  40. `Failed to start customScan. Code: ${err.code}, message: ${err.message}`);
  41. }
  42. })
  43. .height(this.cameraHeight)
  44. .width(this.cameraWidth)
  45. .position({ x: 0, y: 0 })
  46. }
  47. .alignContent(Alignment.Bottom)
  48. .height('100%')
  49. .width('100%')
  50. .position({ x: 0, y: 0 })
  51. }
  52. }

start

PhoneTabletWearable

start(viewControl: ViewControl, callback: AsyncCallback<Array<scanBarcode.ScanResult>>, frameCallback?: AsyncCallback<ScanFrame>): void

启动扫码相机流获取扫码结果、相机预览流(YUV-图像格式NV21基于4:2:0采样)。使用callback异步回调。

说明
  1. 此接口需要在init接口调用后才能使用,否则会抛出错误码1000500001。

  2. 此接口为长期占用相机资源操作,扫码完成后应及时调用stop、release释放资源。

模型约束: 此接口仅可在Stage模型下使用。

需要权限: ohos.permission.CAMERA

系统能力: SystemCapability.Multimedia.Scan.ScanBarcode

设备行为差异: 在API版本6.0.2(22)及之前,该接口在Phone、Tablet中可正常调用。在API版本6.1.0(23)及之后,该接口在Phone、Tablet、带后置相机的Wearable中可正常调用,申请相机权限成功后,在不带后置相机的Wearable中返回错误码1000500001。可以通过getSupportedCameras接口查询是否带后置相机。

起始版本: 4.1.0(11)

参数:

展开
参数名 类型 必填 说明
viewControl ViewControl 相机控制参数,用于配置自定义界面扫码的相机预览流尺寸和渲染的目标surface。
callback AsyncCallback<Array<scanBarcode.ScanResult>> 回调函数,当启动相机流扫码成功,err为undefined,data为获取到的Array<scanBarcode.ScanResult>;否则为错误对象。
frameCallback AsyncCallback<ScanFrame>

回调函数,当启动相机流成功会持续返回预览流数据,err为undefined,data为获取到的相机预览流(YUV)ScanFrame;否则为错误对象。

起始版本: 5.0.0(12)

错误码:

以下错误码的详细介绍请参见通用错误码ArkTS API错误码

从API版本5.0.2(14)开始,customScan模块的start接口新增错误码201。

  • 在API版本5.0.2(14)之前,未申请相机权限时调用customScan模块start接口,返回错误码1000500001。

  • 在API版本5.0.2(14)及之后,未申请相机权限时调用customScan模块start接口,返回错误码201。

展开
错误码ID 错误信息
201

Permission verification failed. The application does not have the permission required to call the API.

适用版本:5.0.2(14)+

401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
1000500001 Internal error.

示例:

收起
自动换行
深色代码主题
复制
  1. import { hilog } from '@kit.PerformanceAnalysisKit';
  2. import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
  3. import { scanBarcode, customScan } from '@kit.ScanKit';
  4. @Entry
  5. @Component
  6. struct CustomScanPage {
  7. // 设置预览流高度,默认单位:vp
  8. @State cameraHeight: number = 640;
  9. // 设置预览流宽度,默认单位:vp
  10. @State cameraWidth: number = 360;
  11. private mXComponentController: XComponentController = new XComponentController();
  12. // 返回自定义扫描结果的回调
  13. private callback: AsyncCallback<Array<scanBarcode.ScanResult>> =
  14. (err: BusinessError, data: Array<scanBarcode.ScanResult>) => {
  15. if (err) {
  16. hilog.error(0x0001, '[Scan Sample]',
  17. `Failed to get ScanResult by callback. Code: ${err.code}, message: ${err.message}`);
  18. return;
  19. }
  20. hilog.info(0x0001, '[Scan Sample]',
  21. `Succeeded in getting ScanResult by callback, result is ${JSON.stringify(data)}`);
  22. };
  23. // 回调获取ScanFrame
  24. private frameCallback: AsyncCallback<customScan.ScanFrame> =
  25. (err: BusinessError, data: customScan.ScanFrame) => {
  26. if (err) {
  27. hilog.error(0x0001, '[Scan Sample]',
  28. `Failed to get ScanFrame by callback. Code: ${err.code}, message: ${err.message}`);
  29. return;
  30. }
  31. hilog.info(0x0001, '[Scan Sample]',
  32. `Succeeded in getting ScanFrame by callback, scanFrame is ${JSON.stringify(data)}`);
  33. };
  34. build() {
  35. Stack() {
  36. XComponent({
  37. id: 'componentId',
  38. type: XComponentType.SURFACE,
  39. controller: this.mXComponentController
  40. })
  41. .onLoad(() => {
  42. hilog.info(0x0001, '[Scan Sample]', 'Succeeded in loading, onLoad is called');
  43. // 获取XComponent的surfaceId
  44. let surfaceId: string = this.mXComponentController.getXComponentSurfaceId();
  45. hilog.info(0x0001, '[Scan Sample]', `Succeeded in getting surfaceId: ${surfaceId}`);
  46. // 设置ViewControl相应字段
  47. let viewControl: customScan.ViewControl = {
  48. width: this.cameraWidth,
  49. height: this.cameraHeight,
  50. surfaceId: surfaceId
  51. };
  52. try {
  53. customScan.start(viewControl, this.callback, this.frameCallback);
  54. } catch (err) {
  55. hilog.error(0x0001, '[Scan Sample]',
  56. `Failed to start customScan. Code: ${err.code}, message: ${err.message}`);
  57. }
  58. })
  59. .height(this.cameraHeight)
  60. .width(this.cameraWidth)
  61. .position({ x: 0, y: 0 })
  62. }
  63. .alignContent(Alignment.Bottom)
  64. .height('100%')
  65. .width('100%')
  66. .position({ x: 0, y: 0 })
  67. }
  68. }

getFlashLightStatus

PhoneTabletWearable

getFlashLightStatus(): boolean

获取当前相机闪光灯状态。

说明

本接口必须在启动相机流start接口后,stop接口之前使用,未启动相机流调用会抛出错误码1000500001。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Multimedia.Scan.ScanBarcode

设备行为差异: 在API版本6.0.2(22)及之前,该接口在Phone、Tablet中可正常调用。在API版本6.1.0(23)及之后,该接口在Phone、Tablet、带后置相机的Wearable中可正常调用,在不带后置相机的Wearable中返回错误码1000500001。可以通过getSupportedCameras接口查询是否带后置相机。

起始版本: 4.1.0(11)

返回值:

展开
类型 说明
boolean 返回当前相机闪光灯状态。true代表开启,false代表关闭。

错误码:

以下错误码的详细介绍请参见ArkTS API错误码

展开
错误码ID 错误信息
1000500001 Internal error.

示例:

收起
自动换行
深色代码主题
复制
  1. import { customScan } from '@kit.ScanKit';
  2. import { hilog } from '@kit.PerformanceAnalysisKit';
  3. let flashLightStatus: boolean = false;
  4. try {
  5. flashLightStatus = customScan.getFlashLightStatus();
  6. // 根据当前闪光灯状态,选择开启或关闭闪光灯
  7. if (flashLightStatus) {
  8. try {
  9. customScan.closeFlashLight();
  10. } catch (err) {
  11. hilog.error(0x0001, '[Scan Sample]', `Failed to closeFlashLight. Code: ${err.code}, message: ${err.message}`);
  12. }
  13. } else {
  14. try {
  15. customScan.openFlashLight();
  16. } catch (err) {
  17. hilog.error(0x0001, '[Scan Sample]', `Failed to openFlashLight. Code: ${err.code}, message: ${err.message}`);
  18. }
  19. }
  20. } catch (err) {
  21. hilog.error(0x0001, '[Scan Sample]', `Failed to getFlashLightStatus. Code: ${err.code}, message: ${err.message}`);
  22. }

openFlashLight

PhoneTabletWearable

openFlashLight(): void

开启相机闪光灯。

说明

本接口必须在启动相机流start接口后,stop接口之前使用,未启动相机流调用会抛出错误码1000500001。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Multimedia.Scan.ScanBarcode

设备行为差异: 在API版本6.0.2(22)及之前,该接口在Phone、Tablet中可正常调用。在API版本6.1.0(23)及之后,该接口在Phone、Tablet、带后置相机的Wearable中可正常调用,在不带后置相机的Wearable中返回错误码1000500001。可以通过getSupportedCameras接口查询是否带后置相机。

起始版本: 4.1.0(11)

错误码:

以下错误码的详细介绍请参见ArkTS API错误码

展开
错误码ID 错误信息
1000500001 Internal error.

示例:

收起
自动换行
深色代码主题
复制
  1. import { customScan } from '@kit.ScanKit';
  2. import { hilog } from '@kit.PerformanceAnalysisKit';
  3. let flashLightStatus: boolean = false;
  4. try {
  5. flashLightStatus = customScan.getFlashLightStatus();
  6. // 根据当前闪光灯状态,选择开启或关闭闪光灯
  7. if (flashLightStatus) {
  8. try {
  9. customScan.closeFlashLight();
  10. } catch (err) {
  11. hilog.error(0x0001, '[Scan Sample]', `Failed to closeFlashLight. Code: ${err.code}, message: ${err.message}`);
  12. }
  13. } else {
  14. try {
  15. customScan.openFlashLight();
  16. } catch (err) {
  17. hilog.error(0x0001, '[Scan Sample]', `Failed to openFlashLight. Code: ${err.code}, message: ${err.message}`);
  18. }
  19. }
  20. } catch (err) {
  21. hilog.error(0x0001, '[Scan Sample]', `Failed to getFlashLightStatus. Code: ${err.code}, message: ${err.message}`);
  22. }

closeFlashLight

PhoneTabletWearable

closeFlashLight(): void

关闭相机闪光灯。

说明

本接口必须在启动相机流start接口后,stop接口之前使用,未启动相机流调用会抛出错误码1000500001。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Multimedia.Scan.ScanBarcode

设备行为差异: 在API版本6.0.2(22)及之前,该接口在Phone、Tablet中可正常调用。在6.1.0(23)及之后,该接口在Phone、Tablet、带后置相机的Wearable中可正常调用,在不带后置相机的Wearable中返回错误码1000500001。可以通过getSupportedCameras接口查询是否带后置相机。

起始版本: 4.1.0(11)

错误码:

以下错误码的详细介绍请参见ArkTS API错误码

展开
错误码ID 错误信息
1000500001 Internal error.

示例:

收起
自动换行
深色代码主题
复制
  1. import { customScan } from '@kit.ScanKit';
  2. import { hilog } from '@kit.PerformanceAnalysisKit';
  3. let flashLightStatus: boolean = false;
  4. try {
  5. flashLightStatus = customScan.getFlashLightStatus();
  6. } catch (err) {
  7. hilog.error(0x0001, '[Scan Sample]', `Failed to getFlashLightStatus. Code: ${err.code}, message: ${err.message}`);
  8. }
  9. // 根据当前闪光灯状态,选择开启或关闭闪光灯
  10. if (flashLightStatus) {
  11. try {
  12. customScan.closeFlashLight();
  13. } catch (err) {
  14. hilog.error(0x0001, '[Scan Sample]', `Failed to closeFlashLight. Code: ${err.code}, message: ${err.message}`);
  15. }
  16. } else {
  17. try {
  18. customScan.openFlashLight();
  19. } catch (err) {
  20. hilog.error(0x0001, '[Scan Sample]', `Failed to openFlashLight. Code: ${err.code}, message: ${err.message}`);
  21. }
  22. }

setZoom

PhoneTabletWearable

setZoom(zoomValue: number): void

设置变焦比。变焦精度最高为小数点后两位,如果设置超过支持的精度范围,则只保留精度范围内数值。

说明

本接口必须在启动相机流start接口后,stop接口之前使用,未启动相机流调用会抛出错误码1000500001。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Multimedia.Scan.ScanBarcode

设备行为差异: 在API版本6.0.2(22)及之前,该接口在Phone、Tablet中可正常调用。在API版本6.1.0(23)及之后,该接口在Phone、Tablet、带后置相机的Wearable中可正常调用,在不带后置相机的Wearable中返回错误码1000500001。可以通过getSupportedCameras接口查询是否带后置相机。

起始版本: 5.0.0(12)

参数:

展开
参数名 类型 必填 说明
zoomValue number 相机变焦比,精度最高为小数点后两位(例如1.45)。

错误码:

以下错误码的详细介绍请参见通用错误码ArkTS API错误码

展开
错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
1000500001 Internal error.

示例:

收起
自动换行
深色代码主题
复制
  1. import { customScan } from '@kit.ScanKit';
  2. import { hilog } from '@kit.PerformanceAnalysisKit';
  3. // 设置变焦比
  4. let zoomValue = 2.0;
  5. try {
  6. customScan.setZoom(zoomValue);
  7. } catch (err) {
  8. hilog.error(0x0001, '[Scan Sample]', `Failed to setZoom. Code: ${err.code}, message: ${err.message}`);
  9. }

getZoom

PhoneTabletWearable

getZoom(): number

获取当前的变焦比。

说明

本接口必须在启动相机流start接口后,stop接口之前使用,未启动相机流调用会抛出错误码1000500001。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Multimedia.Scan.ScanBarcode

设备行为差异: 在API版本6.0.2(22)及之前,该接口在Phone、Tablet中可正常调用。在API版本6.1.0(23)及之后,该接口在Phone、Tablet、带后置相机的Wearable中可正常调用,在不带后置相机的Wearable中返回错误码1000500001。可以通过getSupportedCameras接口查询是否带后置相机。

起始版本: 5.0.0(12)

返回值:

展开
类型 说明
number 返回当前的变焦比。

错误码:

以下错误码的详细介绍请参见ArkTS API错误码

展开
错误码ID 错误信息
1000500001 Internal error.

示例:

收起
自动换行
深色代码主题
复制
  1. import { hilog } from '@kit.PerformanceAnalysisKit';
  2. import { customScan } from '@kit.ScanKit';
  3. try {
  4. // 获取变焦比
  5. let zoomValue = customScan.getZoom();
  6. hilog.info(0x0001, '[Scan Sample]', `Succeeded in getting zoomValue, zoomValue is ${zoomValue}`);
  7. } catch (err) {
  8. hilog.error(0x0001, '[Scan Sample]', `Failed to get zoomValue. Code: ${err.code}, message: ${err.message}`);
  9. }

setFocusPoint

PhoneTabletWearable

setFocusPoint(point: scanBarcode.Point): void

设置相机焦点,焦点应在0-1坐标系内,该坐标系左上角为{0,0},右下角为{1,1}。此坐标系是以设备充电口在右侧时的横向设备方向为基准的,例如应用的预览界面布局以设备充电口在下侧时的竖向方向为基准,布局宽高为{w,h},且触碰点为{x,y},则转换后的坐标点为{y/h,1-x/w}。

说明

本接口必须在启动相机流start接口后,stop接口之前使用,未启动相机流调用会抛出错误码1000500001。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Multimedia.Scan.ScanBarcode

设备行为差异: 在API版本6.0.2(22)及之前,该接口在Phone、Tablet中可正常调用。在API版本6.1.0(23)及之后,该接口在Phone、Tablet、带后置相机的Wearable中可正常调用,在不带后置相机的Wearable中返回错误码1000500001。可以通过getSupportedCameras接口查询是否带后置相机。

起始版本: 5.0.0(12)

参数:

展开
参数名 类型 必填 说明
point scanBarcode.Point 焦点。x、y设置范围应在[0,1]之内,超过范围,如果小于0设置0,大于1设置1。

错误码:

以下错误码的详细介绍请参见通用错误码ArkTS API错误码

展开
错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
1000500001 Internal error.

示例:

收起
自动换行
深色代码主题
复制
  1. import { customScan } from '@kit.ScanKit';
  2. import { hilog } from '@kit.PerformanceAnalysisKit';
  3. try {
  4. // 设置对焦点
  5. customScan.setFocusPoint({ x: 0.5, y: 0.5 });
  6. } catch (err) {
  7. hilog.error(0x0001, '[Scan Sample]', `Failed to setFocusPoint. Code: ${err.code}, message: ${err.message}`);
  8. }

resetFocus

PhoneTabletWearable

resetFocus(): void

设置连续自动对焦模式。

说明
  1. 本接口必须在启动相机流start接口后,stop接口之前使用,未启动相机流调用会抛出错误码1000500001。

  2. 可与setFocusPoint配合使用:setFocusPoint设置对焦点后,可调用resetFocus恢复连续自动对焦。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Multimedia.Scan.ScanBarcode

设备行为差异: 在API版本6.0.2(22)及之前,该接口在Phone、Tablet中可正常调用。在API版本6.1.0(23)及之后,该接口在Phone、Tablet、带后置相机的Wearable中可正常调用,在不带后置相机的Wearable中返回错误码1000500001。可以通过getSupportedCameras接口查询是否带后置相机。

起始版本: 5.0.0(12)

错误码:

以下错误码的详细介绍请参见ArkTS API错误码

展开
错误码ID 错误信息
1000500001 Internal error.

示例:

收起
自动换行
深色代码主题
复制
  1. import { customScan } from '@kit.ScanKit';
  2. import { hilog } from '@kit.PerformanceAnalysisKit';
  3. try {
  4. // 设置连续自动对焦模式
  5. customScan.resetFocus();
  6. } catch (err) {
  7. hilog.error(0x0001, '[Scan Sample]', `Failed to resetFocus. Code: ${err.code}, message: ${err.message}`);
  8. }

on('lightingFlash')

PhoneTabletWearable

on(type: 'lightingFlash', callback: AsyncCallback<boolean>): void

订阅环境亮度变化事件,当环境暗、亮状态变化时返回闪光灯开启或关闭提示。使用callback异步回调。

说明

本接口必须在启动相机流start接口后,stop接口之前使用,未启动相机流调用会抛出错误码1000500001。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Multimedia.Scan.ScanBarcode

设备行为差异: 在API版本6.0.2(22)及之前,该接口在Phone、Tablet中可正常调用。在API版本6.1.0(23)及之后,该接口在Phone、Tablet、带后置相机的Wearable中可正常调用,在不带后置相机的Wearable中返回错误码1000500001。可以通过getSupportedCameras接口查询是否带后置相机。

起始版本: 5.0.0(12)

参数:

展开
参数名 类型 必填 说明
type string 事件回调类型,固定为'lightingFlash',当环境亮度发生变化时触发。可用于提示用户开启或关闭闪光灯。
callback AsyncCallback<boolean> 回调函数。返回true表示当前环境暗,可以提示用户开启闪光灯,false表示环境亮,可以提示用户关闭闪光灯。

错误码:

以下错误码的详细介绍请参见ArkTS API错误码

展开
错误码ID 错误信息
1000500001 Internal error.

示例:

收起
自动换行
深色代码主题
复制
  1. import { BusinessError } from '@kit.BasicServicesKit';
  2. import { hilog } from '@kit.PerformanceAnalysisKit';
  3. import { customScan } from '@kit.ScanKit';
  4. let callback = (err: BusinessError, bool: boolean) => {
  5. if (err) {
  6. hilog.error(0x0001, '[Scan Sample]',
  7. `Failed to light Flash by callback. Code: ${err.code}, message: ${err.message}`);
  8. return;
  9. }
  10. hilog.info(0x0001, '[Scan Sample]', `Succeeded in lighting Flash by callback, bool is ${bool}`);
  11. };
  12. try {
  13. customScan.on('lightingFlash', callback);
  14. } catch (err) {
  15. hilog.error(0x0001, '[Scan Sample]',
  16. `Failed to listen lightingFlash. Code: ${err.code}, message: ${err.message}`);
  17. }

off('lightingFlash')

PhoneTabletWearable

off(type: 'lightingFlash', callback?: AsyncCallback<boolean>): void

注销环境亮度变化事件。使用callback异步回调。

说明

本接口必须在启动相机流start接口后使用,未启动相机流调用会抛出错误码1000500001。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Multimedia.Scan.ScanBarcode

设备行为差异: 在API版本6.0.2(22)及之前,该接口在Phone、Tablet中可正常调用。在API版本6.1.0(23)及之后,该接口在Phone、Tablet、带后置相机的Wearable中可正常调用,在不带后置相机的Wearable中返回错误码1000500001。可以通过getSupportedCameras接口查询是否带后置相机。

起始版本: 5.0.0(12)

参数:

展开
参数名 类型 必填 说明
type string 事件回调类型,固定为'lightingFlash',当环境亮度发生变化时触发,可用于提示用户开启或关闭闪光灯。
callback AsyncCallback<boolean> 回调函数,如果指定参数则必须和customScan.on中监听的事件保持一致,否则注销所有绑定在'lightingFlash'上的回调函数。

错误码:

以下错误码的详细介绍请参见ArkTS API错误码

展开
错误码ID 错误信息
1000500001 Internal error.

示例:

收起
自动换行
深色代码主题
复制
  1. import { BusinessError } from '@kit.BasicServicesKit';
  2. import { hilog } from '@kit.PerformanceAnalysisKit';
  3. import { customScan } from '@kit.ScanKit';
  4. let callback = (err: BusinessError, bool: boolean) => {
  5. if (err) {
  6. hilog.error(0x0001, '[Scan Sample]',
  7. `Failed to cancel Flash by callback. Code: ${err.code}, message: ${err.message}`);
  8. return;
  9. }
  10. hilog.info(0x0001, '[Scan Sample]', `Succeeded in cancelling Flash by callback, bool is ${bool}`);
  11. };
  12. // 可以不填callback,取消lightingFlash所有监听。填写callback,必须和customScan.on中监听的事件保持一致
  13. try {
  14. customScan.off('lightingFlash', callback);
  15. } catch (err) {
  16. hilog.error(0x0001, '[Scan Sample]',
  17. `Failed to listen lightingFlash. Code: ${err.code}, message: ${err.message}`);
  18. }

rescan

PhoneTabletWearable

rescan(): void

触发一次重新扫码。调用后,会重新检测预览画面中的码图,识别成功后会触发start接口传入的callback回调返回新的扫码结果。适用于扫码结果不是预期结果或连续扫码场景。

说明

本接口必须在启动相机流start接口后,stop接口之前使用,未启动相机流调用会抛出错误码1000500001。

仅对start接口的Callback异步回调有效,Promise异步回调接口无效。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Multimedia.Scan.ScanBarcode

设备行为差异: 在API版本6.0.2(22)及之前,该接口在Phone、Tablet中可正常调用。在API版本6.1.0(23)及之后,该接口在Phone、Tablet、带后置相机的Wearable中可正常调用,在不带后置相机的Wearable中返回错误码1000500001。可以通过getSupportedCameras接口查询是否带后置相机。

起始版本: 5.0.0(12)

错误码:

以下错误码的详细介绍请参见ArkTS API错误码

展开
错误码ID 错误信息
1000500001 Internal error.

示例:

收起
自动换行
深色代码主题
复制
  1. import { hilog } from '@kit.PerformanceAnalysisKit';
  2. import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
  3. import { scanBarcode, customScan } from '@kit.ScanKit';
  4. @Entry
  5. @Component
  6. struct CustomScanPage {
  7. // 设置预览流高度,默认单位:vp
  8. @State cameraHeight: number = 640;
  9. // 设置预览流宽度,默认单位:vp
  10. @State cameraWidth: number = 360;
  11. private mXComponentController: XComponentController = new XComponentController();
  12. // 返回自定义扫描结果的回调
  13. private callback: AsyncCallback<Array<scanBarcode.ScanResult>> =
  14. (err: BusinessError, data: Array<scanBarcode.ScanResult>) => {
  15. if (err) {
  16. hilog.error(0x0001, '[Scan Sample]',
  17. `Failed to get ScanResult by callback. Code: ${err.code}, message: ${err.message}`);
  18. return;
  19. }
  20. hilog.info(0x0001, '[Scan Sample]',
  21. `Succeeded in getting ScanResult by callback, result is ${JSON.stringify(data)}`);
  22. // 重新触发扫码。如需不重启相机并重新触发一次扫码,可以在start接口的Callback异步回调中,调用rescan接口。
  23. try {
  24. customScan.rescan();
  25. } catch (err) {
  26. hilog.error(0x0001, '[Scan Sample]',
  27. `Failed to rescan customScan. Code: ${err.code}, message: ${err.message}`);
  28. }
  29. };
  30. build() {
  31. Stack() {
  32. XComponent({
  33. id: 'componentId',
  34. type: XComponentType.SURFACE,
  35. controller: this.mXComponentController
  36. })
  37. .onLoad(() => {
  38. hilog.info(0x0001, '[Scan Sample]', 'Succeeded in loading, onLoad is called');
  39. // 获取XComponent的surfaceId
  40. let surfaceId: string = this.mXComponentController.getXComponentSurfaceId();
  41. hilog.info(0x0001, '[Scan Sample]', `Succeeded in getting surfaceId: ${surfaceId}`);
  42. // 设置ViewControl相应字段
  43. let viewControl: customScan.ViewControl = {
  44. width: this.cameraWidth,
  45. height: this.cameraHeight,
  46. surfaceId: surfaceId
  47. };
  48. try {
  49. customScan.start(viewControl, this.callback);
  50. } catch (err) {
  51. hilog.error(0x0001, '[Scan Sample]',
  52. `Failed to start customScan. Code: ${err.code}, message: ${err.message}`);
  53. }
  54. })
  55. .height(this.cameraHeight)
  56. .width(this.cameraWidth)
  57. .position({ x: 0, y: 0 })
  58. }
  59. .alignContent(Alignment.Bottom)
  60. .height('100%')
  61. .width('100%')
  62. .position({ x: 0, y: 0 })
  63. }
  64. }

setAutoZoomEnabled

PhoneTabletWearable

setAutoZoomEnabled(enabled: boolean): void

设置自动变焦能力的开启和关闭。未调用时默认开启自动变焦。

说明

本接口必须在启动相机流start接口后,stop接口之前使用,未启动相机流调用会抛出错误码1000500001。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Multimedia.Scan.ScanBarcode

设备行为差异: 在API版本6.0.2(22)及之前,该接口在Phone、Tablet中可正常调用。在API版本6.1.0(23)及之后,该接口在Phone、Tablet、带后置相机的Wearable中可正常调用,在不带后置相机的Wearable中返回错误码1000500001。可以通过getSupportedCameras接口查询是否带后置相机。

起始版本: 5.1.0(18)

参数:

展开
参数名 类型 必填 说明
enabled boolean 是否开启自动变焦能力。true代表开启,false代表关闭。

错误码:

以下错误码的详细介绍请参见ArkTS API错误码

展开
错误码ID 错误信息
1000500001 Internal error.

示例:

收起
自动换行
深色代码主题
复制
  1. import { customScan } from '@kit.ScanKit';
  2. import { hilog } from '@kit.PerformanceAnalysisKit';
  3. try {
  4. // 开启或关闭自动变焦能力,true为开启,false为关闭
  5. customScan.setAutoZoomEnabled(false);
  6. } catch (err) {
  7. hilog.error(0x0001, '[Scan Sample]', `Failed to setAutoZoomEnabled. Code: ${err.code}, message: ${err.message}`);
  8. }

stop

PhoneTabletWearable

stop(): Promise<void>

暂停扫码相机流。使用Promise异步回调。

说明

本接口必须在启动相机流start接口后使用,未启动相机流调用会抛出错误码1000500001。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Multimedia.Scan.ScanBarcode

设备行为差异: 在API版本6.0.2(22)及之前,该接口在Phone、Tablet中可正常调用。在API版本6.1.0(23)及之后,该接口在Phone、Tablet、带后置相机的Wearable中可正常调用,在不带后置相机的Wearable中返回错误码1000500001。可以通过getSupportedCameras接口查询是否带后置相机。

起始版本: 4.1.0(11)

返回值:

展开
类型 说明
Promise<void> Promise对象。无返回结果的Promise对象。

错误码:

以下错误码的详细介绍请参见ArkTS API错误码

展开
错误码ID 错误信息
1000500001 Internal error.

示例:

收起
自动换行
深色代码主题
复制
  1. import { hilog } from '@kit.PerformanceAnalysisKit';
  2. import { customScan } from '@kit.ScanKit';
  3. import { BusinessError } from '@kit.BasicServicesKit';
  4. try {
  5. customScan.stop().then(() => {
  6. hilog.info(0x0001, '[Scan Sample]', 'Succeeded in stopping scan by promise');
  7. }).catch((err: BusinessError) => {
  8. hilog.error(0x0001, '[Scan Sample]',
  9. `Failed to stop scan by promise. Code: ${err.code}, message: ${err.message}`);
  10. });
  11. } catch (err) {
  12. hilog.error(0x0001, '[Scan Sample]',
  13. `Failed to stop customScan. Code: ${err.code}, message: ${err.message}`);
  14. }

stop

PhoneTabletWearable

stop(callback: AsyncCallback<void>): void

暂停扫码相机流。使用callback异步回调。

说明

本接口必须在启动相机流start接口后使用,未启动相机流调用会抛出错误码1000500001。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Multimedia.Scan.ScanBarcode

设备行为差异: 在API版本6.0.2(22)及之前,该接口在Phone、Tablet中可正常调用。在API版本6.1.0(23)及之后,该接口在Phone、Tablet、带后置相机的Wearable中可正常调用,在不带后置相机的Wearable中返回错误码1000500001。可以通过getSupportedCameras接口查询是否带后置相机。

起始版本: 4.1.0(11)

参数:

展开
参数名 类型 必填 说明
callback AsyncCallback<void> 回调函数。当暂停相机流成功,err为undefined,否则为错误对象。

错误码:

以下错误码的详细介绍请参见ArkTS API错误码

展开
错误码ID 错误信息
1000500001 Internal error.

示例:

收起
自动换行
深色代码主题
复制
  1. import { hilog } from '@kit.PerformanceAnalysisKit';
  2. import { customScan } from '@kit.ScanKit';
  3. import { BusinessError } from '@kit.BasicServicesKit';
  4. try {
  5. customScan.stop((err: BusinessError) => {
  6. if (err) {
  7. hilog.error(0x0001, '[Scan Sample]',
  8. `Failed to stop scan by callback. Code: ${err.code}, message: ${err.message}`);
  9. return;
  10. }
  11. hilog.info(0x0001, '[Scan Sample]', 'Succeeded in stopping scan by callback');
  12. });
  13. } catch (err) {
  14. hilog.error(0x0001, '[Scan Sample]',
  15. `Failed to stop customScan. Code: ${err.code}, message: ${err.message}`);
  16. }

release

PhoneTabletWearable

release(): Promise<void>

释放扫码相机流。使用Promise异步回调。

说明

本接口建议在启动相机流start接口且暂停相机流stop接口后使用,未启动相机流调用会抛出错误码1000500001。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Multimedia.Scan.ScanBarcode

设备行为差异: 在API版本6.0.2(22)及之前,该接口在Phone、Tablet中可正常调用。在API版本6.1.0(23)及之后,该接口在Phone、Tablet、带后置相机的Wearable中可正常调用,在不带后置相机的Wearable中返回错误码1000500001。可以通过getSupportedCameras接口查询是否带后置相机。

起始版本: 4.1.0(11)

返回值:

展开
类型 说明
Promise<void> Promise对象。无返回结果的Promise对象。

错误码:

以下错误码的详细介绍请参见ArkTS API错误码

展开
错误码ID 错误信息
1000500001 Internal error.

示例:

收起
自动换行
深色代码主题
复制
  1. import { hilog } from '@kit.PerformanceAnalysisKit';
  2. import { customScan } from '@kit.ScanKit';
  3. import { BusinessError } from '@kit.BasicServicesKit';
  4. try {
  5. customScan.release().then(() => {
  6. hilog.info(0x0001, '[Scan Sample]', 'Succeeded in releasing scan by promise');
  7. }).catch((err: BusinessError) => {
  8. hilog.error(0x0001, '[Scan Sample]',
  9. `Failed to release scan by promise. Code: ${err.code}, message: ${err.message}`);
  10. });
  11. } catch (err) {
  12. hilog.error(0x0001, '[Scan Sample]',
  13. `Failed to release customScan. Code: ${err.code}, message: ${err.message}`);
  14. }

release

PhoneTabletWearable

release(callback: AsyncCallback<void>): void

释放扫码相机流。使用callback异步回调。

说明

本接口建议在启动相机流start接口且暂停相机流stop接口后使用,未启动相机流调用会抛出错误码1000500001。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Multimedia.Scan.ScanBarcode

设备行为差异: 在API版本6.0.2(22)及之前,该接口在Phone、Tablet中可正常调用。在API版本6.1.0(23)及之后,该接口在Phone、Tablet、带后置相机的Wearable中可正常调用,在不带后置相机的Wearable中返回错误码1000500001。可以通过getSupportedCameras接口查询是否带后置相机。

起始版本: 4.1.0(11)

参数:

展开
参数名 类型 必填 说明
callback AsyncCallback<void> 回调函数。当释放相机流成功,err为undefined,否则为错误对象。

错误码:

以下错误码的详细介绍请参见ArkTS API错误码

展开
错误码ID 错误信息
1000500001 Internal error.

示例:

收起
自动换行
深色代码主题
复制
  1. import { hilog } from '@kit.PerformanceAnalysisKit';
  2. import { customScan } from '@kit.ScanKit';
  3. import { BusinessError } from '@kit.BasicServicesKit';
  4. try {
  5. customScan.release((err: BusinessError) => {
  6. if (err) {
  7. hilog.error(0x0001, '[Scan Sample]',
  8. `Failed to release scan by callback. Code: ${err.code}, message: ${err.message}`);
  9. return;
  10. }
  11. hilog.info(0x0001, '[Scan Sample]', 'Succeeded in releasing scan by callback');
  12. });
  13. } catch (err) {
  14. hilog.error(0x0001, '[Scan Sample]',
  15. `Failed to release customScan. Code: ${err.code}, message: ${err.message}`);
  16. }
在 API参考 中进行搜索
请输入您想要搜索的关键词