【HarmonyOS NEXT】鸿蒙customScan (自定义界面扫码)

**起始版本:**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接口会重新适配相机分辨率比例。

示例:

javascript 复制代码
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { scanBarcode, customScan } from '@kit.ScanKit';

@Entry
@Component
struct customScanPage {
  // 设置预览流高度,默认单位:vp
  @State cameraHeight: number = 640;
  // 设置预览流宽度,默认单位:vp
  @State cameraWidth: number = 360;
  private mXComponentController: XComponentController = new XComponentController();

  build() {
    Stack() {
      XComponent({
        id: 'componentId',
        type: 'surface',
        controller: this.mXComponentController
      })
        .onLoad(() => {
          hilog.info(0x0001, '[Scan Sample]', 'onLoad is called')
          // 获取XComponent的surfaceId
          let surfaceId: string = this.mXComponentController.getXComponentSurfaceId();
          hilog.info(0x0001, 'viewControl', `onLoad surfaceId: ${surfaceId}`);
          // 设置ViewControl相应字段
          let viewControl: customScan.ViewControl = {
            width: this.cameraWidth,
            height: this.cameraHeight,
            surfaceId: surfaceId
          };
          customScan.start(viewControl).then((scanResult: Array<scanBarcode.ScanResult>) => {
            hilog.info(0x0001, '[Scan Sample]', `Succeeded in getting ScanResult by promise, scanResult is ${JSON.stringify(scanResult)}`);
          }).catch((error: BusinessError) => {
            hilog.error(0x0001, '[Scan Sample]',
              `Failed to get ScanResult by promise. Code: ${error.code}, message: ${error.message}`);
          })
        })// 预览流宽、高,默认单位vp,支持px、lpx、vp
        .height(this.cameraHeight)
        .width(this.cameraWidth)
        .position({ x: 0, y: 0 })
    }
    .alignContent(Alignment.Bottom)
    .height('100%')
    .width('100%')
    .position({ x: 0, y: 0 })
  }
}

ScanFrame

相机预览流(YUV)。

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

**起始版本:**5.0.0(12)

名称 类型 只读 可选 说明
byteBuffer ArrayBuffer 相机预览流的ArrayBuffer数组。
width number 相机预览流的宽度,单位:px。
height number 相机预览流的高度,单位:px。
scanCodeRects Array<scanBarcode.ScanCodeRect> 相机预览流的码图检测位置信息。

示例:

javascript 复制代码
import { hilog } from '@kit.PerformanceAnalysisKit';
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
import { scanBarcode, customScan } from '@kit.ScanKit';

const TAG = '[ScanKit ScanFrame]';

@Entry
@Component
struct customScanPage {
  // 设置预览流高度,默认单位:vp
  @State cameraHeight: number = 640;
  // 设置预览流宽度,默认单位:vp
  @State cameraWidth: number = 360;
  private mXComponentController: XComponentController = new XComponentController();
  private callback: AsyncCallback<scanBarcode.ScanResult[]> =
    async (error: BusinessError, result: scanBarcode.ScanResult[]) => {
      if (error) {
        hilog.error(0x0001, '[Scan Sample]', `Failed to get ScanResult by callback. Code: ${error.code}, message: ${error.message}`);
        return;
      }
      hilog.info(0x0001, '[Scan Sample]', `Succeeded in getting ScanResult by callback, result is ${JSON.stringify(result)}`);
    }
  // 回调获取ScanFrame
  private frameCallback: AsyncCallback<customScan.ScanFrame> =
    async (error: BusinessError, frameResult: customScan.ScanFrame) => {
      if (error) {
        hilog.error(0x0001, '[Scan Sample]', `Failed to get ScanFrame by callback. Code: ${error.code}, message: ${error.message}`);
        return;
      }
      // byteBuffer相机YUV图像数组
      hilog.info(0x0001, '[Scan Sample]', `Succeeded in getting ScanFrame.byteBuffer.byteLength:  ${frameResult.byteBuffer.byteLength}`);
      hilog.info(0x0001, '[Scan Sample]', `Succeeded in getting ScanFrame.scanCodeRect: ${JSON.stringify(frameResult.scanCodeRects)}`);
    }

  build() {
    Stack() {
      XComponent({
        id: 'componentId',
        type: 'surface',
        controller: this.mXComponentController
      })
        .onLoad(() => {
          hilog.info(0x0001, '[Scan Sample]', 'Succeeded in loading, onLoad is called.');
          // 获取XComponent的surfaceId
          let surfaceId: string = this.mXComponentController.getXComponentSurfaceId();
          hilog.info(0x0001, '[Scan Sample]', `Succeeded ing getting surfaceId: ${surfaceId}`);
          // 设置ViewControl相应字段
          let viewControl: customScan.ViewControl = {
            width: this.cameraWidth,
            height: this.cameraHeight,
            surfaceId: surfaceId
          };
          customScan.start(viewControl, this.callback, this.frameCallback);
        })// 预览流宽、高,默认单位vp,支持px、lpx、vp
        .height(this.cameraHeight)
        .width(this.cameraWidth)
        .position({ x: 0, y: 0 })
    }
    .alignContent(Alignment.Bottom)
    .height('100%')
    .width('100%')
    .position({ x: 0, y: 0 })
  }
}

说明

  1. scanCodeRects返回为横向预览流中的码图位置信息,需要将位置转换为纵向坐标系,数组中每一个元素left\top\right\bottom参数转换逻辑,以scanCodeRects第一个元素为例:

    javascript 复制代码
    // start接口frameCallback回调返回frameResult数据
    let rect:scanBarcode.ScanCodeRect = frameResult.scanCodeRects[0];
    // 预览流尺寸转换为显示组件Xcomponent尺寸比例
    let ratio = this.scanWidth / frameResult.height;
    left = (frameResult.height - rect.bottom) * ratio;
    top = rect.left * ratio;
    right = (frameResult.height - rect.top) * ratio;
    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.

示例:

javascript 复制代码
import { scanBarcode, scanCore, customScan } from '@kit.ScanKit';

let options: scanBarcode.ScanOptions = {
  scanTypes: [scanCore.ScanType.ALL],
  enableMultiMode: true,
  enableAlbum: true
};
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.

示例:

javascript 复制代码
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { scanBarcode, customScan } from '@kit.ScanKit';

@Entry
@Component
struct customScanPage {
  // 设置预览流高度,默认单位:vp
  @State cameraHeight: number = 640
  // 设置预览流宽度,默认单位:vp
  @State cameraWidth: number = 360
  private mXComponentController: XComponentController = new XComponentController();

  build() {
    Stack() {
      XComponent({
        id: 'componentId',
        type: 'surface',
        controller: this.mXComponentController
      })
        .onLoad(() => {
          hilog.info(0x0001, '[Scan Sample]', 'Succeeded in loading, onLoad is called.');
          // 获取XComponent的surfaceId
          let surfaceId: string = this.mXComponentController.getXComponentSurfaceId();
          hilog.info(0x0001, '[Scan Sample]', `Succeeded in getting surfaceId: ${surfaceId}`);
          // 设置ViewControl相应字段
          let viewControl: customScan.ViewControl = {
            width: this.cameraWidth,
            height: this.cameraHeight,
            surfaceId: surfaceId
          };
          customScan.start(viewControl).then((scanResult: Array<scanBarcode.ScanResult>) => {
            hilog.info(0x0001, '[Scan Sample]', `Succeeded in getting ScanResult by promise, scanResult is ${JSON.stringify(scanResult)}`);
          }).catch((error: BusinessError) => {
            hilog.error(0x0001, '[Scan Sample]', `Failed to get ScanResult by promise. Code: ${error.code}, message: ${error.message}`);
          });
        })// 预览流宽、高,默认单位vp,支持px、lpx、vp
        .height(this.cameraHeight)
        .width(this.cameraWidth)
        .position({ x: 0, y: 0 })
    }
    .alignContent(Alignment.Bottom)
    .height('100%')
    .width('100%')
    .position({ x: 0, y: 0 })
  }
}

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.

示例:

javascript 复制代码
import { hilog } from '@kit.PerformanceAnalysisKit';
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
import { scanBarcode, customScan } from '@kit.ScanKit';

@Entry
@Component
struct customScanPage {
  // 设置预览流高度,默认单位:vp
  @State cameraHeight: number = 640;
  // 设置预览流宽度,默认单位:vp
  @State cameraWidth: number = 360;
  private mXComponentController: XComponentController = new XComponentController();
  // 返回自定义扫描结果的回调
  private callback: AsyncCallback<Array<scanBarcode.ScanResult>> =
    async (error: BusinessError, result: Array<scanBarcode.ScanResult>) => {
      if (error) {
        hilog.error(0x0001, '[Scan Sample]', `Failed to get ScanResult by callback. Code: ${error.code}, message: ${error.message}`);
        return;
      }
      hilog.info(0x0001, '[Scan Sample]', `Succeeded in getting ScanResult by callback, result is ${JSON.stringify(result)}`);
    }
  // 回调获取ScanFrame
  private frameCallback: AsyncCallback<customScan.ScanFrame> =
    async (error: BusinessError, scanFrame: customScan.ScanFrame) => {
      if (error) {
        hilog.error(0x0001, '[Scan Sample]', `Failed to get ScanFrame by callback. Code: ${error.code}, message: ${error.message}`);
        return;
      }
      hilog.info(0x0001, '[Scan Sample]', `Succeeded in getting ScanFrame by callback, scanFrame is ${JSON.stringify(scanFrame)}`);
    }

  build() {
    Stack() {
      XComponent({
        id: 'componentId',
        type: 'surface',
        controller: this.mXComponentController
      })
        .onLoad(() => {
          hilog.info(0x0001, '[Scan Sample]', 'Succeeded in- loading, onLoad is called.');
          // 获取XComponent的surfaceId
          let surfaceId: string = this.mXComponentController.getXComponentSurfaceId();
          hilog.info(0x0001, '[Scan Sample]', `Succeeded in getting surfaceId: ${surfaceId}`);
          // 设置ViewControl相应字段
          let viewControl: customScan.ViewControl = {
            width: this.cameraWidth,
            height: this.cameraHeight,
            surfaceId: surfaceId
          };
          customScan.start(viewControl, this.callback, this.frameCallback);
        })// 预览流宽、高,默认单位vp,支持px、lpx、vp
        .height(this.cameraHeight)
        .width(this.cameraWidth)
        .position({ x: 0, y: 0 })
    }
    .alignContent(Alignment.Bottom)
    .height('100%')
    .width('100%')
    .position({ x: 0, y: 0 })
  }
}

customScan.getFlashLightStatus

getFlashLightStatus(): boolean

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

说明

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

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

**起始版本:**4.1.0(11)

返回值:

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

错误码:

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

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

示例:

javascript 复制代码
import { customScan } from '@kit.ScanKit';

// 根据当前闪光灯状态,选择打开或关闭闪关灯
if (customScan.getFlashLightStatus()) {
  customScan.closeFlashLight();
} else {
  customScan.openFlashLight();
}

customScan.openFlashLight

openFlashLight(): void

开启相机闪光灯。

说明

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

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

**起始版本:**4.1.0(11)

错误码:

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

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

示例:

javascript 复制代码
import { customScan } from '@kit.ScanKit';

// 根据当前闪光灯状态,选择打开或关闭闪关灯
if (customScan.getFlashLightStatus()) {
  customScan.closeFlashLight();
} else {
  customScan.openFlashLight();
}

customScan.closeFlashLight

closeFlashLight(): void

关闭相机闪光灯。

说明

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

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

**起始版本:**4.1.0(11)

错误码:

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

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

示例:

javascript 复制代码
import { customScan } from '@kit.ScanKit';

// 根据当前闪光灯状态,选择打开或关闭闪关灯
if (customScan.getFlashLightStatus()) {
  customScan.closeFlashLight();
} else {
  customScan.openFlashLight();
}

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.

示例:

javascript 复制代码
import { customScan } from '@kit.ScanKit';

// 设置变焦比
let zoomValue = 2.0;
customScan.setZoom(zoomValue);

customScan.getZoom

getZoom(): number

获取当前的变焦比。

说明

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

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

**起始版本:**5.0.0(12)

返回值:

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

错误码:

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

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

示例:

javascript 复制代码
import { hilog } from '@kit.PerformanceAnalysisKit';
import { customScan } from '@kit.ScanKit';

// 获取变焦比
let zoomValue = customScan.getZoom();
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.

示例:

javascript 复制代码
import { customScan } from '@kit.ScanKit';

// 设置对焦点
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.

示例:

javascript 复制代码
import { customScan } from '@kit.ScanKit';

// 设置连续自动对焦模式
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.

示例:

javascript 复制代码
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { customScan } from '@kit.ScanKit';

let callback = (error: BusinessError, bool: boolean) => {
  if (error) {
    hilog.error(0x0001, '[Scan Sample]',
      `Failed to light Flash by callback. Code: ${error.code}, message: ${error.message}`);
    return;
  }
  hilog.info(0x0001, '[Scan Sample]', `Succeeded in lighting Flash by callback, bool is ${bool}`);
}
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.

示例:

javascript 复制代码
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { customScan } from '@kit.ScanKit';

let callback = (error: BusinessError, bool: boolean) => {
  if (error) {
    hilog.error(0x0001, '[Scan Sample]',
      `Failed to cancel Flash by callback. Code: ${error.code}, message: ${error.message}`);
    return;
  }
  hilog.info(0x0001, '[Scan Sample]', `Succeeded in cancelling Flash by callback, bool is ${bool}`);
}
// 可以不填callback,取消lightingFlash所有监听。填写callback,必须保持和customScan.on中监听的事件保持一致
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.

示例:

javascript 复制代码
import { hilog } from '@kit.PerformanceAnalysisKit';
import { customScan } from '@kit.ScanKit';
import { BusinessError } from '@kit.BasicServicesKit';

customScan.stop().then(() => {
  hilog.info(0x0001, '[Scan Sample]', 'Succeeded in stopping scan by promise.');
}).catch((error: BusinessError) => {
  hilog.error(0x0001, '[Scan Sample]', `Failed to stop scan by promise. Code: ${error.code}, message: ${error.message}`);
});

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.

示例:

javascript 复制代码
import { hilog } from '@kit.PerformanceAnalysisKit';
import { customScan } from '@kit.ScanKit';
import { BusinessError } from '@kit.BasicServicesKit';

customScan.stop((error: BusinessError) => {
  if (error) {
    hilog.error(0x0001, '[Scan Sample]', `Failed to stop scan by callback. Code: ${error.code}, message: ${error.message}`);
    return;
  }
  hilog.info(0x0001, '[Scan Sample]', 'Succeeded in stopping scan by callback.');
})

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.

示例:

javascript 复制代码
import { hilog } from '@kit.PerformanceAnalysisKit';
import { customScan } from '@kit.ScanKit';
import { BusinessError } from '@kit.BasicServicesKit';

customScan.release().then(() => {
  hilog.info(0x0001, '[Scan Sample]', 'Succeeded in releasing scan by promise.');
}).catch((error: BusinessError) => {
  hilog.error(0x0001, '[Scan Sample]', `Failed to release scan by promise. Code: ${error.code}, message: ${error.message}`);
})

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.

示例:

javascript 复制代码
import { hilog } from '@kit.PerformanceAnalysisKit';
import { customScan } from '@kit.ScanKit';
import { BusinessError } from '@kit.BasicServicesKit';

customScan.release((error: BusinessError) => {
  if (error) {
    hilog.error(0x0001, '[Scan Sample]', `Failed to release scan by callback. Code: ${error.code}, message: ${error.message}`);
    return;
  }
  hilog.info(0x0001, '[Scan Sample]', 'Succeeded in releasing scan by callback.');
});

内容来源 HarmonyOS NEXT API12 官方文档

相关推荐
helloxmg1 小时前
鸿蒙harmonyos next flutter混合开发之开发package
flutter·华为·harmonyos
zhongcx17 小时前
鸿蒙应用示例:ArkTS UI框架中的文本缩进技巧
harmonyos
东林知识库19 小时前
鸿蒙NEXT开发-自定义构建函数(基于最新api12稳定版)
华为·harmonyos
裴云飞1 天前
鸿蒙性能优化之布局优化
性能优化·harmonyos
zhongcx1 天前
鸿蒙应用示例:ArkTS中设置颜色透明度与颜色渐变方案探讨
harmonyos
真正的醒悟1 天前
华为资源分享
运维·服务器·华为
PlumCarefree1 天前
mp4(H.265编码)转为本地RTSP流
音视频·harmonyos·h.265
鸿蒙自习室2 天前
鸿蒙网络管理模块04——网络连接管理
华为·harmonyos·鸿蒙·媒体
小Q的编程笔记2 天前
HarmonyOS:AVPlayer 与 XComponent 联手打造定制化视频播放器
harmonyos
Yeats_Liao2 天前
华为开源自研AI框架昇思MindSpore应用案例:计算高效的卷积模型ShuffleNet
人工智能·深度学习·华为