文档管理中心
您当前浏览的HarmonyOS 5.0.0(API 12)文档归档不再维护,推荐您使用最新版本。详细请参考文档维护策略变更

文档扫描

场景介绍

文档扫描控件提供拍摄文档并转换为高清扫描件的服务。仅需使用手机拍摄文档,即可自动裁剪和优化,并支持图片保存和分享;同时支持拍摄或从图库选择图片识别表格,生成表格文档。

可广泛用于教育办公场景,扫描文档、票据、课堂PPT和书籍等输出图片供用户完成发送、存档等操作。

图1 文档扫描示意图

约束与限制

该能力当前不支持模拟器。

开发步骤

  1. 将文档扫描控件相关的类添加至工程。

    收起
    自动换行
    深色代码主题
    复制
    1. import { DocType, DocumentScanner, DocumentScannerConfig, SaveOption, FilterId, ShootingMode } from "@kit.VisionKit";

  2. 配置布局,根据业务场景配置文档扫描控件的相关属性,获取返回的文档图片uri列表。

    收起
    自动换行
    深色代码主题
    复制
    1. import { hilog } from '@kit.PerformanceAnalysisKit';
    2. const TAG = 'DocumentScanner'
    3. @Entry
    4. @Component
    5. struct Index {
    6. private docScanConfig = new DocumentScannerConfig()
    7. aboutToAppear() {
    8. this.docScanConfig.supportType = [DocType.DOC, DocType.SHEET]
    9. this.docScanConfig.isGallerySupported = true
    10. this.docScanConfig.editTabs = []
    11. this.docScanConfig.maxShotCount = 3
    12. this.docScanConfig.defaultFilterId = FilterId.ORIGINAL
    13. this.docScanConfig.defaultShootingMode = ShootingMode.MANUAL
    14. this.docScanConfig.isShareable = true
    15. this.docScanConfig.originalUris = []
    16. }
    17. build() {
    18. Column() {
    19. DocumentScanner({
    20. scannerConfig: this.docScanConfig,
    21. onResult: (code: number, saveType: SaveOption, uris: string[]) => {
    22. hilog.info(0x0001, TAG, `result code: ${code}, save: ${saveType}`)
    23. uris.forEach(uriString => {
    24. hilog.info(0x0001, TAG, `uri: ${uriString}`)
    25. })
    26. }
    27. }).size({ width: '100%', height: '100%' })
    28. }
    29. .height('100%')
    30. .width('100%')
    31. }
    32. }

开发实例

收起
自动换行
深色代码主题
复制
  1. //开发实例分两页实现,一页为文档扫描入口页,一页为文档扫描实现页
  2. //文档扫描入口页,需引入文档扫描实现页,以下文实例为例,实现页文件名为DocDemoPage
  3. import { DocDemoPage } from './DocDemoPage'
  4. @Entry
  5. @Component
  6. struct MainPage {
  7. @Provide('pathStack') pathStack: NavPathStack = new NavPathStack()
  8. @Builder
  9. PageMap(name: string) {
  10. if (name === 'documentScanner') {
  11. DocDemoPage()
  12. }
  13. }
  14. //文档扫描入口按钮,可替换为业务入口
  15. build() {
  16. Navigation(this.pathStack) {
  17. Button('DocumentScanner', { stateEffect: true, type: ButtonType.Capsule })
  18. .width('50%')
  19. .height(40)
  20. .onClick(() => {
  21. this.pathStack.pushPath({ name: 'documentScanner' })
  22. })
  23. }.title('文档扫描控件demo').navDestination(this.PageMap)
  24. .mode(NavigationMode.Stack)
  25. }
  26. }

收起
自动换行
深色代码主题
复制
  1. //文档扫描实现页,文件名为DocDemoPage,需被引入至入口页
  2. import {
  3. DocType,
  4. DocumentScanner,
  5. DocumentScannerConfig,
  6. SaveOption,
  7. FilterId,
  8. ShootingMode
  9. } from "@kit.VisionKit"
  10. import { hilog } from '@kit.PerformanceAnalysisKit';
  11. const TAG: string = 'DocDemoPage'
  12. //文档扫描页,用于加载uiExtensionAbility
  13. @Entry
  14. @Component
  15. export struct DocDemoPage {
  16. @State docImageUris: string[] = []
  17. @Consume('pathStack') pathStack: NavPathStack
  18. private docScanConfig = new DocumentScannerConfig()
  19. aboutToAppear() {
  20. this.docScanConfig.supportType = [DocType.DOC, DocType.SHEET]
  21. this.docScanConfig.isGallerySupported = true
  22. this.docScanConfig.editTabs = []
  23. this.docScanConfig.maxShotCount = 3
  24. this.docScanConfig.defaultFilterId = FilterId.ORIGINAL
  25. this.docScanConfig.defaultShootingMode = ShootingMode.MANUAL
  26. this.docScanConfig.isShareable = true
  27. this.docScanConfig.originalUris = []
  28. }
  29. build() {
  30. NavDestination() {
  31. Stack({ alignContent: Alignment.Top }) {
  32. //展示文档扫描结果
  33. List() {
  34. ForEach(this.docImageUris, (uri: string) => {
  35. ListItem() {
  36. Image(uri)
  37. .objectFit(ImageFit.Contain)
  38. .width(100)
  39. .height(100)
  40. }
  41. })
  42. }
  43. .listDirection(Axis.Vertical)
  44. .alignListItem(ListItemAlign.Center)
  45. .margin({
  46. top: 50
  47. })
  48. .width('80%')
  49. .height('80%')
  50. //文档扫描
  51. DocumentScanner({
  52. scannerConfig: this.docScanConfig,
  53. onResult: (code: number, saveType: SaveOption, uris: string[]) => {
  54. hilog.info(0x0001, TAG, `result code: ${code}, save: ${saveType}`)
  55. if (code === -1) {
  56. this.pathStack.pop()
  57. }
  58. uris.forEach(uriString => {
  59. hilog.info(0x0001, TAG, `uri: ${uriString}`)
  60. })
  61. this.docImageUris = uris
  62. }
  63. })
  64. .size({ width: '100%', height: '100%' })
  65. }
  66. .width('100%')
  67. .height('100%')
  68. }
  69. .width('100%')
  70. .height('100%')
  71. .hideTitleBar(true)
  72. }
  73. }
在 指南 中进行搜索
请输入您想要搜索的关键词