当前位置:   article > 正文

【HarmonyOS NEXT】鸿蒙customScan (自定义界面扫码)_systemcapability.multimedia.scan.scanbarcode

systemcapability.multimedia.scan.scanbarcode

起始版本:4.1.0(11)

导入模块

import { customScan } from '@kit.ScanKit';

ViewControl

相机控制参数。

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

起始版本:4.1.0(11)

名称

类型

只读

可选

说明

width

number

XComponent组件的宽,默认使用单位为vp,支持px、lpx和vp。

height

number

XComponent组件的高,默认使用单位为vp,支持px、lpx和vp。

surfaceId

string

XComponent持有surface的ID。

说明

  1. ViewControl的width和height需和XComponent的保持一致,start接口根据设置宽高值会匹配最接近相机分辨率,当未匹配到合适分辨率,接口会返回1000500001内部错误。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: '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. customScan.start(viewControl).then((scanResult: Array<scanBarcode.ScanResult>) => {
  31. hilog.info(0x0001, '[Scan Sample]', `Succeeded in getting ScanResult by promise, scanResult is ${JSON.stringify(scanResult)}`);
  32. }).catch((error: BusinessError) => {
  33. hilog.error(0x0001, '[Scan Sample]',
  34. `Failed to get ScanResult by promise. Code: ${error.code}, message: ${error.message}`);
  35. })
  36. })// 预览流宽、高,默认单位vp,支持px、lpx、vp
  37. .height(this.cameraHeight)
  38. .width(this.cameraWidth)
  39. .position({ x: 0, y: 0 })
  40. }
  41. .alignContent(Alignment.Bottom)
  42. .height('100%')
  43. .width('100%')
  44. .position({ x: 0, y: 0 })
  45. }
  46. }

ScanFrame

相机预览流(YUV)。

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

起始版本:5.0.0(12)

名称

类型

只读

可选

说明

byteBuffer

ArrayBuffer

相机预览流的ArrayBuffer数组。

width

number

相机预览流的宽度,单位:px。

height

number

相机预览流的高度,单位:px。

scanCodeRects

Array<scanBarcode.ScanCodeRect>

相机预览流的码图检测位置信息。

示例:

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

说明

  1. scanCodeRects返回为横向预览流中的码图位置信息,需要将位置转换为纵向坐标系,数组中每一个元素left\top\right\bottom参数转换逻辑,以scanCodeRects第一个元素为例:
    1. // start接口frameCallback回调返回frameResult数据
    2. let rect:scanBarcode.ScanCodeRect = frameResult.scanCodeRects[0];
    3. // 预览流尺寸转换为显示组件Xcomponent尺寸比例
    4. let ratio = this.scanWidth / frameResult.height;
    5. left = (frameResult.height - rect.bottom) * ratio;
    6. top = rect.left * ratio;
    7. right = (frameResult.height - rect.top) * ratio;
    8. bottom = rect.right * ratio;

  2. 对应的二维码区域位置可以使用固定定位position({x: left, y: top}),宽度width: right - left,高度height: bottom - top,画出二维码实际区域范围。

customScan.init

init(options?: scanBarcode.ScanOptions): void

初始化自定义界面扫码。

需要权限:ohos.permission.CAMERA

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

起始版本:4.1.0(11)

参数:

参数名

类型

必填

说明

options

scanBarcode.ScanOptions

自定义界面扫码参数。

错误码:

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

错误码ID

错误信息

401

Parameter error.

1000500001

Internal error.

示例:

  1. import { scanBarcode, scanCore, customScan } from '@kit.ScanKit';
  2. let options: scanBarcode.ScanOptions = {
  3. scanTypes: [scanCore.ScanType.ALL],
  4. enableMultiMode: true,
  5. enableAlbum: true
  6. };
  7. customScan.init(options);

customScan.start

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

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

说明

此接口需要在init接口调用后才能使用。

需要权限:ohos.permission.CAMERA

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

起始版本:4.1.0(11)

参数:

参数名

类型

必填

说明

viewControl

ViewControl

相机控制参数。

返回值:

类型

说明

Promise<Array<scanBarcode.ScanResult>>

Promise对象,返回启动相机流扫码结果对象。

错误码:

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

错误码ID

错误信息

401

Parameter error.

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: '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. customScan.start(viewControl).then((scanResult: Array<scanBarcode.ScanResult>) => {
  31. hilog.info(0x0001, '[Scan Sample]', `Succeeded in getting ScanResult by promise, scanResult is ${JSON.stringify(scanResult)}`);
  32. }).catch((error: BusinessError) => {
  33. hilog.error(0x0001, '[Scan Sample]', `Failed to get ScanResult by promise. Code: ${error.code}, message: ${error.message}`);
  34. });
  35. })// 预览流宽、高,默认单位vp,支持px、lpx、vp
  36. .height(this.cameraHeight)
  37. .width(this.cameraWidth)
  38. .position({ x: 0, y: 0 })
  39. }
  40. .alignContent(Alignment.Bottom)
  41. .height('100%')
  42. .width('100%')
  43. .position({ x: 0, y: 0 })
  44. }
  45. }

customScan.start

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

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

说明

此接口需要在init接口调用后才能使用。

需要权限:ohos.permission.CAMERA

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

起始版本:4.1.0(11)

参数:

参数名

类型

必填

说明

viewControl

ViewControl

相机控制参数。

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错误码

错误码ID

错误信息

401

Parameter error.

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. async (error: BusinessError, result: Array<scanBarcode.ScanResult>) => {
  15. if (error) {
  16. hilog.error(0x0001, '[Scan Sample]', `Failed to get ScanResult by callback. Code: ${error.code}, message: ${error.message}`);
  17. return;
  18. }
  19. hilog.info(0x0001, '[Scan Sample]', `Succeeded in getting ScanResult by callback, result is ${JSON.stringify(result)}`);
  20. }
  21. // 回调获取ScanFrame
  22. private frameCallback: AsyncCallback<customScan.ScanFrame> =
  23. async (error: BusinessError, scanFrame: customScan.ScanFrame) => {
  24. if (error) {
  25. hilog.error(0x0001, '[Scan Sample]', `Failed to get ScanFrame by callback. Code: ${error.code}, message: ${error.message}`);
  26. return;
  27. }
  28. hilog.info(0x0001, '[Scan Sample]', `Succeeded in getting ScanFrame by callback, scanFrame is ${JSON.stringify(scanFrame)}`);
  29. }
  30. build() {
  31. Stack() {
  32. XComponent({
  33. id: 'componentId',
  34. type: '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. customScan.start(viewControl, this.callback, this.frameCallback);
  49. })// 预览流宽、高,默认单位vp,支持px、lpx、vp
  50. .height(this.cameraHeight)
  51. .width(this.cameraWidth)
  52. .position({ x: 0, y: 0 })
  53. }
  54. .alignContent(Alignment.Bottom)
  55. .height('100%')
  56. .width('100%')
  57. .position({ x: 0, y: 0 })
  58. }
  59. }

customScan.getFlashLightStatus

getFlashLightStatus(): boolean

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

说明

本接口必须在启动相机流start接口后使用,相机流初始化、停止和释放阶段使用都会抛出内部错误的异常。

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

起始版本:4.1.0(11)

返回值:

类型

说明

boolean

返回当前相机闪光灯状态。true代表开启,false代表关闭。

错误码:

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

错误码ID

错误信息

1000500001

Internal error.

示例:

  1. import { customScan } from '@kit.ScanKit';
  2. // 根据当前闪光灯状态,选择打开或关闭闪关灯
  3. if (customScan.getFlashLightStatus()) {
  4. customScan.closeFlashLight();
  5. } else {
  6. customScan.openFlashLight();
  7. }

customScan.openFlashLight

openFlashLight(): void

开启相机闪光灯。

说明

本接口必须在启动相机流start接口后使用,相机流初始化、停止和释放阶段使用都会抛出内部错误的异常。

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

起始版本:4.1.0(11)

错误码:

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

错误码ID

错误信息

1000500001

Internal error.

示例:

  1. import { customScan } from '@kit.ScanKit';
  2. // 根据当前闪光灯状态,选择打开或关闭闪关灯
  3. if (customScan.getFlashLightStatus()) {
  4. customScan.closeFlashLight();
  5. } else {
  6. customScan.openFlashLight();
  7. }

customScan.closeFlashLight

closeFlashLight(): void

关闭相机闪光灯。

说明

本接口必须在启动相机流start接口后使用,相机流初始化、停止和释放阶段使用都会抛出内部错误的异常。

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

起始版本:4.1.0(11)

错误码:

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

错误码ID

错误信息

1000500001

Internal error.

示例:

  1. import { customScan } from '@kit.ScanKit';
  2. // 根据当前闪光灯状态,选择打开或关闭闪关灯
  3. if (customScan.getFlashLightStatus()) {
  4. customScan.closeFlashLight();
  5. } else {
  6. customScan.openFlashLight();
  7. }

customScan.setZoom

setZoom(zoomValue: number): void

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

说明

本接口必须在启动相机流start接口后使用。

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

起始版本:5.0.0(12)

参数:

参数名

类型

必填

说明

zoomValue

number

相机变焦比,精度最高为小数点后两位(例如1.45)。

错误码:

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

错误码ID

错误信息

401

Parameter error.

1000500001

Internal error.

示例:

  1. import { customScan } from '@kit.ScanKit';
  2. // 设置变焦比
  3. let zoomValue = 2.0;
  4. customScan.setZoom(zoomValue);

customScan.getZoom

getZoom(): number

获取当前的变焦比。

说明

本接口必须在启动相机流start接口后使用。

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

起始版本:5.0.0(12)

返回值:

类型

说明

number

返回当前的变焦比。

错误码:

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

错误码ID

错误信息

1000500001

Internal error.

示例:

  1. import { hilog } from '@kit.PerformanceAnalysisKit';
  2. import { customScan } from '@kit.ScanKit';
  3. // 获取变焦比
  4. let zoomValue = customScan.getZoom();
  5. hilog.info(0x0001, '[Scan Sample]', `Succeeded in getting zoomValue, zoomValue is ${zoomValue}`);

customScan.setFocusPoint

setFocusPoint(point: scanBarcode.Point): void

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

说明

本接口必须在启动相机流start接口后使用。

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

起始版本:5.0.0(12)

参数:

参数名

类型

必填

说明

point

scanBarcode.Point

焦点。x、y设置范围应在[0,1]之内,超过范围,如果小于0设置0,大于1设置1。

错误码:

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

错误码ID

错误信息

401

Parameter error.

1000500001

Internal error.

示例:

  1. import { customScan } from '@kit.ScanKit';
  2. // 设置对焦点
  3. customScan.setFocusPoint({x:0.5, y:0.5});

customScan.resetFocus

resetFocus(): void

设置连续自动对焦模式。

说明

本接口必须在启动相机流start接口后使用。

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

起始版本:5.0.0(12)

错误码:

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

错误码ID

错误信息

1000500001

Internal error.

示例:

  1. import { customScan } from '@kit.ScanKit';
  2. // 设置连续自动对焦模式
  3. customScan.resetFocus();

customScan.on('lightingFlash')

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

注册闪光灯打开时机回调,当扫码环境昏、亮状态变化时,使用callback异步回调返回打开时机结果。

说明

本接口必须在启动相机流start接口后使用,未启动相机流调用会抛出内部错误的异常。

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

起始版本: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 = (error: BusinessError, bool: boolean) => {
  5. if (error) {
  6. hilog.error(0x0001, '[Scan Sample]',
  7. `Failed to light Flash by callback. Code: ${error.code}, message: ${error.message}`);
  8. return;
  9. }
  10. hilog.info(0x0001, '[Scan Sample]', `Succeeded in lighting Flash by callback, bool is ${bool}`);
  11. }
  12. customScan.on('lightingFlash', callback);

customScan.off('lightingFlash')

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

注销闪光灯打开时机回调,使用callback异步回调返回结果。

说明

本接口必须在启动相机流start接口后使用,未启动相机流调用会抛出内部错误的异常。

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

起始版本:5.0.0(12)

参数:

参数名

类型

必填

说明

type

string

事件回调用类型,固定为'lightingFlash',当扫码环境亮度发生变化建议打开或关闭闪光灯时触发

callback

AsyncCallback<boolean>

回调函数,可选,有就是匹配on('lightingFlash') callback(callback对象不可是匿名函数),不填写callback则取消'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 = (error: BusinessError, bool: boolean) => {
  5. if (error) {
  6. hilog.error(0x0001, '[Scan Sample]',
  7. `Failed to cancel Flash by callback. Code: ${error.code}, message: ${error.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. customScan.off('lightingFlash', callback);

customScan.stop

stop(): Promise<void>

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

说明

本接口必须在启动相机流start接口后使用,未启动相机流调用会抛出内部错误的异常。

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

起始版本: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. customScan.stop().then(() => {
  5. hilog.info(0x0001, '[Scan Sample]', 'Succeeded in stopping scan by promise.');
  6. }).catch((error: BusinessError) => {
  7. hilog.error(0x0001, '[Scan Sample]', `Failed to stop scan by promise. Code: ${error.code}, message: ${error.message}`);
  8. });

customScan.stop

stop(callback: AsyncCallback<void>): void

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

说明

本接口必须在启动相机流start接口后使用,未启动相机流调用会抛出内部错误的异常。

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

起始版本: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. customScan.stop((error: BusinessError) => {
  5. if (error) {
  6. hilog.error(0x0001, '[Scan Sample]', `Failed to stop scan by callback. Code: ${error.code}, message: ${error.message}`);
  7. return;
  8. }
  9. hilog.info(0x0001, '[Scan Sample]', 'Succeeded in stopping scan by callback.');
  10. })

customScan.release

release(): Promise<void>

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

说明

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

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

起始版本: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. customScan.release().then(() => {
  5. hilog.info(0x0001, '[Scan Sample]', 'Succeeded in releasing scan by promise.');
  6. }).catch((error: BusinessError) => {
  7. hilog.error(0x0001, '[Scan Sample]', `Failed to release scan by promise. Code: ${error.code}, message: ${error.message}`);
  8. })

customScan.release

release(callback: AsyncCallback<void>): void

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

说明

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

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

起始版本: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. customScan.release((error: BusinessError) => {
  5. if (error) {
  6. hilog.error(0x0001, '[Scan Sample]', `Failed to release scan by callback. Code: ${error.code}, message: ${error.message}`);
  7. return;
  8. }
  9. hilog.info(0x0001, '[Scan Sample]', 'Succeeded in releasing scan by callback.');
  10. });

内容来源 HarmonyOS NEXT API12 官方文档

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/757325
推荐阅读
相关标签
  

闽ICP备14008679号