HarmonyOS 鸿蒙Next文档扫描DocumentScanner

HarmonyOS 鸿蒙Next文档扫描DocumentScanner 如何使用系统文档扫描功能?(问题来源项目案例整理:https://github.com/heqiyuan35-creator/BaitKnows.git

3 回复

使用 @kit.VisionKit 中的 DocumentScanner 组件:

import { DocumentScanner, DocumentScannerConfig, DocType, FilterId } from '@kit.VisionKit';

const config = new DocumentScannerConfig();
config.supportType = [DocType.DOC, DocType.SHEET];  // 支持文档和表格
config.isGallerySupported = true;   // 支持从相册选择
config.maxShotCount = 5;            // 最多拍摄5张
config.defaultFilterId = FilterId.ORIGINAL;

DocumentScanner({
  scannerConfig: config,
  onResult: (code: number, saveType, uris: string[]) => {
    if (code === 0) {
      console.log('扫描成功,图片URI:', uris);
    } else if (code === -1) {
      console.log('用户取消');
    }
  }
})

更多关于HarmonyOS 鸿蒙Next文档扫描DocumentScanner的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


HarmonyOS Next的DocumentScanner是系统提供的文档扫描能力,基于相机和图像处理技术实现。它通过调用系统相机捕获图像,并自动进行边缘检测、透视校正、图像增强等处理,最终输出高质量扫描文档。开发者可通过DocumentScanner API集成此功能,支持自定义扫描界面和处理参数。该能力适用于办公、教育等需要文档数字化的场景。

在HarmonyOS Next中,使用系统文档扫描功能主要通过DocumentScanner接口实现。以下是核心使用步骤:

  1. 权限申请:在module.json5文件中声明必要的权限。

    {
      "module": {
        "requestPermissions": [
          {
            "name": "ohos.permission.CAMERA"
          },
          {
            "name": "ohos.permission.READ_MEDIA"
          },
          {
            "name": "ohos.permission.WRITE_MEDIA"
          }
        ]
      }
    }
    
  2. 创建扫描器并配置:导入@kit.ArkGraphics2D模块,创建DocumentScanner实例并设置参数。

    import { documentScanner } from '@kit.ArkGraphics2D';
    
    // 1. 创建文档扫描器
    let scanner: documentScanner.DocumentScanner = documentScanner.createDocumentScanner();
    
    // 2. (可选)配置扫描参数
    let config: documentScanner.ScanConfig = {
      sourceType: documentScanner.SourceType.CAMERA, // 数据源:相机
      pixelFormat: documentScanner.PixelFormat.RGBA_8888, // 像素格式
      mode: documentScanner.ScanMode.SHOT, // 扫描模式:单张拍摄
    };
    scanner.prepare(config);
    
  3. 执行扫描:调用scan()方法启动扫描流程。这是一个异步操作,需要通过Promise或异步函数处理返回的ScanResult

    // 3. 执行扫描(建议在异步函数或Promise中调用)
    scanner.scan().then((result: documentScanner.ScanResult) => {
      if (result !== null && result.code === documentScanner.ScanResultCode.SUCCESS) {
        // 扫描成功,处理结果
        let image: image.PixelMap = result.image; // 获取扫描生成的图像PixelMap
        // 你可以在此处对image进行保存、显示或进一步处理
        console.info('Document scan succeeded.');
      } else {
        // 扫描失败或用户取消
        console.error(`Document scan failed. Code: ${result?.code}`);
      }
    }).catch((err: BusinessError) => {
      console.error(`Document scan error: ${err.code}, ${err.message}`);
    });
    
  4. 释放资源:扫描完成后,应及时释放扫描器实例。

    // 4. 释放扫描器
    scanner.release();
    

关键说明

  • 流程createDocumentScanner() -> prepare(config) -> scan() -> 处理ScanResult -> release()
  • 结果处理:成功的扫描结果ScanResult中包含一个image.PixelMap对象,这是扫描文档的图像数据核心,可用于保存为图片文件或UI显示。
  • 模式选择ScanMode支持SHOT(单张拍摄)和PLATE(连续拍摄,适用于多页文档)模式,需根据场景配置。
  • 错误处理:务必处理scan()方法可能抛出的异常和返回的错误码,例如用户取消操作(ScanResultCode.CANCEL)或系统错误。

你提供的GitHub项目链接(BaitKnows)可以作为实际项目上下文的参考。按照上述步骤集成,即可在HarmonyOS Next应用中调用系统级文档扫描界面并获取标准化图像。

回到顶部