鸿蒙Harmony 自定义扫一扫组件

816 阅读2分钟

鸿蒙扫一扫组件(多码识别)


  • 利用鸿蒙系统customScan能力,实现二维码识别
  • 支持多码识别
  • 支持选择图库扫码能力
  • 支持开启关闭闪光灯
  • 支持UI定制
  • 手指撑开缩小控制镜头缩放倍数

截屏

使用示例

单码识别

  • 导入
import { Scanner, ScannerController } from '@coner/Scanner';
  • 使用
scannerController: ScannerController = new ScannerController()
Scanner({
  controller: this.scannerController,
  onScanResult: (code: ResultState, value: string) => {
    if (code == ResultState.Success) {
      promptAction.showToast({ message: value })
    }
  },
  onCameraReject: () => {
    promptAction.showToast({ message: '摄像头权限被拒绝' })
  }
}).layoutWeight(1)

多码识别

  • 导入
import { ProScanner, ProScannerController } from '@ohos/Scanner'
  • 使用
controller: ProScannerController = new ProScannerController()
ProScanner({
  controller: this.controller,
  onFindMultipleCode: (result: scanBarcode.ScanResult[]) => {
    promptAction.showToast({ message: '发现了' + result.length + '个二维码' })
  },
  onScanResult: (code: ResultState, value: string) => {
    if (code == ResultState.Success) {
      promptAction.showToast({ message: value })
    }
  }
})
  .layoutWeight(1)

安装使用


ohpm i @coner/scanner

Scanner 属性


字段名类型默认值说明
albumsShowbooleantrue相册是否显示
albumsIconResourceStr$r('app.media.scanner_albums')相册图标
albumsIconSizeLength64相册图标大小
albumsTextstring'相册'相册文案
albumsTextSizeLength16相册文案文字大小
albumsTextColorResourceColorColor.White相册文案颜色
lightShowbooleantrue手电筒是否显示
lightCloseIconResourceStr$r('app.media.scanner_light_close')手电筒关闭图标
lightOpenIconResourceStr$r('app.media.scanner_light_open')手电筒开启图标
lightIconSizeLength64手电筒图标大小
lightOpenTextstring'开灯'手电筒开启文案
lightCloseTextstring'关灯'手电筒关闭文案
lightTextSizeLength16手电筒文字大小
lightTextColorResourceColorColor.White手电筒文案颜色
tipsShowbooleantrue提示词是否显示
tipsstring'将条码、二维码放入框内,即可自动扫描'提示词内容
tipsTextColorResourceColorColor.White提示词文字颜色
tipsTextSizeLength14提示词文字大小
tipsTopMarginLength10提示词距离上面的间距
maskColorResourceColor'#7f000000'遮罩颜色
scannerSizenumber256扫描框宽高
cornerLineWidthnumber3角上的框宽度
cornerLineLengthnumber30角上的框长度
cornerLineColorResourceColorColor.White角上的框颜色
cornerLineShowbooleantrue四个角是否显示
scanTopMarginnumber100扫描框距离上面的间距
scanLineWidthLength1扫描线宽度
scanLineLengthLength'100%'扫描线长度
scanLineColorResourceColorColor.White扫描线颜色
scanLineShowbooleantrue扫描线是否显示
scanAnimTimenumber1500动画时间
scanIntervalTimenumber1000扫码间隔时间
disableCheckAreabooleanfalse是否禁止检查二维码在框中
areaOffsetnumber100二维码在框中的偏移量
controllerScannerControllerthis.scannerController扫码控制类
onScanResult(code: ResultState, value: string) => voidundefined扫码结果回调函数
onCameraGrant() => voidundefined摄像头权限同意回调
onCameraReject() => voidundefined摄像头权限拒绝回调

ScannerController控制器


方法入参返回值说明
openLightvoidvoid打开闪光灯
closeLightvoidvoid关闭闪光灯
toggleLightvoidvoid闪光点开关
pickPhotovoidvoid选择图片识别二维码
setZoomzoom: numbervoid设置扫码镜头放大比例
getZoomvoidnumber获取扫码镜头放大比例
getLightStatusvoidboolean获取闪光灯开启状态
releaseScanvoidvoid释放相机资源
startScanvoidvoid启动扫码
rescanvoidvoid重启相机扫码
scanUriuri: stringvoid扫描图片资源uri
scanUrlurl: stringvoid扫描网络图片url
scanPixelMappixelMap: PixelMapvoid扫描图片PixelMap

使用方法

this.scannerController.scanUrl(url) // 扫码结果回调到Scanner组件的onScanResult回调方法

ProScanner 属性-多码识别


字段名类型默认值说明
albumsShowbooleantrue相册是否显示
albumsIconResourceStr$r('app.media.scanner_albums')相册图标
albumsIconSizeLength64相册图标大小
albumsTextstring'相册'相册文案
albumsTextSizeLength16相册文案文字大小
albumsTextColorResourceColorColor.White相册文案颜色
lightShowbooleantrue手电筒是否显示
lightCloseIconResourceStr$r('app.media.scanner_light_close')手电筒关闭图标
lightOpenIconResourceStr$r('app.media.scanner_light_open')手电筒开启图标
lightIconSizeLength64手电筒图标大小
lightOpenTextstring'开灯'手电筒开启文案
lightCloseTextstring'关灯'手电筒关闭文案
lightTextSizeLength16手电筒文字大小
lightTextColorResourceColorColor.White手电筒文案颜色
scanIntervalTimenumber1000扫码间隔时间
pointViewSizenumber40多码标志点大小
pointViewColorResourceColor'#4AA4F9'多码标志点颜色
pointViewBorderWidthnumber3多码标志点边框宽度
pointIconResourceStr$r('app.media.scanner_arrow_right')多码标志点图片
pointIconSizenumber26多码标志点图片大小
pointViewBorderColorResourceColorColor.White多码标志点边框颜色
pointViewLeftOffsetnumber20多码标志点向左的偏移量
pointViewTopOffsetnumber20多码标志点向上的偏移量
pointViewCustomBuilder组件默认样式多码标志点自定义UI
controllerProScannerControllerthis.scannerController扫码控制类
onFindMultipleCode(result: scanBarcode.ScanResult[]) => voidundefined发现多码回调函数
onScanResult(code: ResultState, value: string) => voidundefined扫码结果回调函数
onCameraGrant() => voidundefined摄像头权限同意回调
onCameraReject() => voidundefined摄像头权限拒绝回调

ProScannerController控制器

与ScannerController一致,此处省略

ScanUtil


  • 在非扫码页面(未使用Scanner组件)时调用扫描网络图片、图片uri、图片PixelMap能力
方法入参返回值说明
scanUriuri: stringPromise扫描图片资源uri
scanUrlurl: stringPromise扫描网络图片url
scanPixelMappixelMap: PixelMapPromise扫描图片PixelMap

使用方法

ScanUtil.scanUrl(url)
  .then((res) => {
    promptAction.showToast({ message: res })
  })
  .catch((err: string) => {
    promptAction.showToast({ message: '失败了:' + err })
  })

声明权限

entry module下的module.json5中新增如下配置

{
  "module": {
    // ...
    'requestPermissions': [
      {
        "name": "ohos.permission.INTERNET",
      },{
        "name": "ohos.permission.CAMERA",
        "reason": "$string:reasonRequestCamera",
        "usedScene": {
          "abilities": [
            "EntryAbility"
          ],
          "when": "inuse"
        }
      }
    ]
  }
}

单码识别自定义UI


Scanner({
  cornerLineShow: false, // 隐藏四个角
  scanLineShow: false, // 隐藏扫描线
  albumsShow: false, // 隐藏图库按钮和文字
  lightShow: false, // 隐藏闪光点按钮和文字
  tipsShow: false, // 隐藏提示词
  maskColor: Color.Transparent, // 蒙层透明
  onScanResult: (code: ResultState, value: string) => {
    if (code == ResultState.Success) {
      promptAction.showToast({ message: value })
    }
  },
  onCameraReject: () => {
    promptAction.showToast({ message: '摄像头权限被拒绝' })
  }
})

多码识别自定义UI


调整默认的多码标志点UI

ProScanner({
  pointViewSize: 40,
  pointViewColor: '#4AA4F9',
  pointViewBorderWidth: 3,
  pointIcon: $r('app.media.xxxxx'),
  pointIconSize: 26,
  pointViewBorderColor: Color.White,
  pointViewLeftOffset: 20,
  pointViewTopOffset: 20,
})

对默认样式不满意可以直接重写标志点UI

ProScanner({
  pointView: this.PointView()
})
@Builder
PointView() {
  // 自定义UI样式
}

交流催更

QQ君羊: 571144615

891723038699_.pic.jpg

感谢支持