openharmony 鸿蒙 js-apis-scan

2026-08-25 浏览 (1)

@ohos.scan (Scan)

This module provides JavaScript APIs of the scan framework for discovering and connecting to scanners.

NOTE The initial APIs of this module are supported since API version 20. This topic describes only public APIs provided by the module.

Modules to Import

import { scan } from '@kit.BasicServicesKit';

ScanErrorCode

Enumerates the scan error codes.

System capability: SystemCapability.Print.PrintFramework

NameValueDescription
SCAN_ERROR_NO_PERMISSION201No permission.
SCAN_ERROR_NOT_SYSTEM_APPLICATION202Non-system application.
SCAN_ERROR_INVALID_PARAMETER401Invalid parameter.
SCAN_ERROR_GENERIC_FAILURE13100001Generic failure.
SCAN_ERROR_RPC_FAILURE13100002RPC failure.
SCAN_ERROR_SERVER_FAILURE13100003Service failure.
SCAN_ERROR_UNSUPPORTED13100004Unsupported operation.
SCAN_ERROR_CANCELED13100005Operation canceled.
SCAN_ERROR_DEVICE_BUSY13100006Device busy.
SCAN_ERROR_INVALID13100007Invalid operation.
SCAN_ERROR_JAMMED13100008Paper jammed.
SCAN_ERROR_NO_DOCS13100009Out of paper.
SCAN_ERROR_COVER_OPEN13100010Cover open.
SCAN_ERROR_IO_ERROR13100011I/O error.
SCAN_ERROR_NO_MEMORY13100012Insufficient memory.

ConstraintType

Enumerates the parameter constraint types.

System capability: SystemCapability.Print.PrintFramework

NameValueDescription
SCAN_CONSTRAINT_NONE0No constraint.
SCAN_CONSTRAINT_RANGE1Range.
SCAN_CONSTRAINT_WORD_LIST2Number list.
SCAN_CONSTRAINT_STRING_LIST3String list.

PhysicalUnit

Enumerates the physical units.

System capability: SystemCapability.Print.PrintFramework

NameValueDescription
SCAN_UNIT_NONE0No unit.
SCAN_UNIT_PIXEL1Pixel unit.
SCAN_UNIT_BIT2Bit unit.
SCAN_UNIT_MM3Millimeter unit.
SCAN_UNIT_DPI4DPI unit.
SCAN_UNIT_PERCENT5Percentage unit.
SCAN_UNIT_MICROSECOND6Microsecond unit.

OptionValueType

Enumerates the option value types.

System capability: SystemCapability.Print.PrintFramework

NameValueDescription
SCAN_TYPE_BOOL0Boolean.
SCAN_TYPE_INT1Integer.
SCAN_TYPE_FIXED2Fixed-point number.
SCAN_TYPE_STRING3String.

ScannerSyncMode

Enumerates the scanner sync codes.

System capability: SystemCapability.Print.PrintFramework

NameValueDescription
UPDATE_STR'update'Update code, which indicates that the scanner ID changes.
DELETE_STR'delete'Deletion code, which indicates that the scanner is offline.

ScannerDiscoveryMode

Enumerates the scanner discovery modes.

System capability: SystemCapability.Print.PrintFramework

NameValueDescription
TCP_STR'TCP'Discovery mode of the network scanner.
USB_STR'USB'Discovery mode of the USB scanner.

Range

Defines the range.

System capability: SystemCapability.Print.PrintFramework

Properties

NameTypeRead-OnlyOptionalDescription
minValuenumberNoNoMinimum value.
maxValuenumberNoNoMaximum value.
quantValuenumberNoNoQuantized value.

ScannerParameter

Defines the scanner parameters.

System capability: SystemCapability.Print.PrintFramework

Properties

NameTypeRead-OnlyOptionalDescription
optionNamestringNoNoOption name.
optionIndexnumberNoNoOption index.
optionTitlestringNoNoOption title.
optionDescstringNoNoOption description.
optionTypeOptionValueTypeNoNoOption value type.
optionUnitPhysicalUnitNoNoPhysical unit of the option.
optionConstraintTypeConstraintTypeNoNoConstraint type of the option.
optionConstraintStringstring[]NoYesString constraints of the option.
optionConstraintIntnumber[]NoYesInteger constraints of the option.
optionConstraintRangeRangeNoYesRange constraint of the option.

ScannerOptionValue

Defines the scanner option value.

System capability: SystemCapability.Print.PrintFramework

Properties

NameTypeRead-OnlyOptionalDescription
valueTypeOptionValueTypeNoNoValue type.
numValuenumberNoYesValue of the number type.
strValuestringNoYesValue of the string type.
boolValuebooleanNoYesValue of the Boolean type.

PictureScanProgress

Defines the progress of scanning pictures.

System capability: SystemCapability.Print.PrintFramework

Properties

NameTypeRead-OnlyOptionalDescription
progressnumberNoNoProgress percentage, whose value ranges from 0 to 100. Unit: %
pictureFdnumberNoNoFile descriptor of the scanned picture.
isFinalbooleanNoNoWhether the picture is the last one to be scanned. The value true indicates that the picture is the last one to be scanned, and false indicates that the picture is not the last one.

ScannerDevice

Defines the scanner.

System capability: SystemCapability.Print.PrintFramework

Properties

NameTypeRead-OnlyOptionalDescription
scannerIdstringNoNoUnique identifier of the scanner.
discoveryModeScannerDiscoveryModeNoNoDiscovery mode of the scanner.
uniqueIdstringNoNoUnique ID of the scanner.
manufacturerstringNoNoManufacturer of the scanner.
modelstringNoNoModel of the scanner.
deviceNamestringNoNoName of the scanner.

ScannerSyncDevice

Defines the device to be synced from the scanner.

System capability: SystemCapability.Print.PrintFramework

Properties

NameTypeRead-OnlyOptionalDescription
scannerIdstringNoNoScanner ID.
discoveryModeScannerDiscoveryModeNoNoDiscovery mode.
uniqueIdstringNoNoUnique ID.
syncModeScannerSyncModeNoNoSync mode.
oldScannerIdstringNoYesOld scanner ID, which is valid only when syncMode is set to update.

scan.init

init(): Promise<void>

Initializes the scan service. This API uses a promise to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

IDError Message
201Permission denied.

Example

import { scan } from '@kit.BasicServicesKit';
import { BusinessError } from '@kit.BasicServicesKit';

scan.init().then(() => {
    console.info('scan init success');
}).catch((error: BusinessError) => {
    console.error('scan init failed: ' + JSON.stringify(error));
})

scan.exit

exit(): Promise<void>

Exits the scan service. This API uses a promise to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

IDError Message
201Permission denied.

Example

import { scan } from '@kit.BasicServicesKit';
import { BusinessError } from '@kit.BasicServicesKit';

scan.exit().then(() => {
    console.info('scan exit success');
}).catch((error: BusinessError) => {
    console.error('scan exit failed: ' + JSON.stringify(error));
})

scan.startScannerDiscovery

startScannerDiscovery(): Promise<void>

Starts scanner discovery. This API uses a promise to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

IDError Message
201Permission denied.

Example

import { scan } from '@kit.BasicServicesKit';
import { BusinessError } from '@kit.BasicServicesKit';

scan.startScannerDiscovery().then(() => {
    console.info('start scanner discovery success');
}).catch((error: BusinessError) => {
    console.error('start scanner discovery failed: ' + JSON.stringify(error));
})

scan.openScanner

openScanner(scannerId: string): Promise<void>

Opens a scanner. This API uses a promise to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
scannerIdstringYesID of the scanner to be opened.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

IDError Message
201Permission denied.

Example

import { scan } from '@kit.BasicServicesKit';
import { BusinessError } from '@kit.BasicServicesKit';

let scannerId: string = 'scanner_001';
scan.openScanner(scannerId).then(() => {
    console.info('open scanner success');
}).catch((error: BusinessError) => {
    console.error('open scanner failed: ' + JSON.stringify(error));
})

scan.closeScanner

closeScanner(scannerId: string): Promise<void>

Closes a scanner. This API uses a promise to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
scannerIdstringYesID of the scanner to be closed.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

IDError Message
201Permission denied.

Example

import { scan } from '@kit.BasicServicesKit';
import { BusinessError } from '@kit.BasicServicesKit';

let scannerId: string = 'scanner_001';
scan.closeScanner(scannerId).then(() => {
    console.info('close scanner success');
}).catch((error: BusinessError) => {
    console.error('close scanner failed: ' + JSON.stringify(error));
})

scan.getScannerParameter

getScannerParameter(scannerId: string): Promise<ScannerParameter[]>

Obtains scanner parameters. This API uses a promise to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
scannerIdstringYesScanner ID.

Return value

TypeDescription
Promise<ScannerParameter[]>Promise used to return the scanner parameters.

Error codes

IDError Message
201Permission denied.

Example

import { scan } from '@kit.BasicServicesKit';
import { BusinessError } from '@kit.BasicServicesKit';

let scannerId: string = 'scanner_001';
scan.getScannerParameter(scannerId).then((parameters: scan.ScannerParameter[]) => {
    console.info('get scanner parameters success: ' + JSON.stringify(parameters));
}).catch((error: BusinessError) => {
    console.error('get scanner parameters failed: ' + JSON.stringify(error));
})

scan.setScannerParameter

setScannerParameter(scannerId: string, optionIndex: number, value: ScannerOptionValue): Promise<void>

Sets scanner parameters. This API uses a promise to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
scannerIdstringYesScanner ID.
optionIndexnumberYesIndex of the option to be set.
valueScannerOptionValueYesValue to be set.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

IDError Message
201Permission denied.

Example

import { scan } from '@kit.BasicServicesKit';
import { BusinessError } from '@kit.BasicServicesKit';

let scannerId: string = 'scanner_001';
let optionIndex: number = 1;
let value: scan.ScannerOptionValue = {
    valueType: scan.OptionValueType.SCAN_TYPE_INT,
    numValue: 100
};
scan.setScannerParameter(scannerId, optionIndex, value).then(() => {
    console.info('set scanner parameter success');
}).catch((error: BusinessError) => {
    console.error('set scanner parameter failed: ' + JSON.stringify(error));
})

scan.setScanAutoOption

setScanAutoOption(scannerId: string, optionIndex: number): Promise<void>

Sets the scan option to auto mode. This API uses a promise to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
scannerIdstringYesScanner ID.
optionIndexnumberYesIndex of the option to be set to auto mode.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

IDError Message
201Permission denied.

Example

import { scan } from '@kit.BasicServicesKit';
import { BusinessError } from '@kit.BasicServicesKit';

let scannerId: string = 'scanner_001';
let optionIndex: number = 1;
scan.setScanAutoOption(scannerId, optionIndex).then(() => {
    console.info('set scan auto option success');
}).catch((error: BusinessError) => {
    console.error('set scan auto option failed: ' + JSON.stringify(error));
})

scan.getScannerCurrentSetting

getScannerCurrentSetting(scannerId: string, optionIndex: number): Promise<ScannerOptionValue>

Obtains the current scanner settings. This API uses a promise to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
scannerIdstringYesScanner ID.
optionIndexnumberYesIndex of the option to be obtained.

Return value

TypeDescription
Promise<ScannerOptionValue>Promise used to return the scanner option value.

Error codes

IDError Message
201Permission denied.

Example

import { scan } from '@kit.BasicServicesKit';
import { BusinessError } from '@kit.BasicServicesKit';

let scannerId: string = 'scanner_001';
let optionIndex: number = 1;
scan.getScannerCurrentSetting(scannerId, optionIndex).then((value: scan.ScannerOptionValue) => {
    console.info('get scanner current setting success: ' + JSON.stringify(value));
}).catch((error: BusinessError) => {
    console.error('get scanner current setting failed: ' + JSON.stringify(error));
})

scan.startScan

startScan(scannerId: string, batchMode: boolean): Promise<void>

Starts scanning. This API uses a promise to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
scannerIdstringYesScanner ID.
batchModebooleanYesWhether to use the batch processing mode. The value true indicates that the batch processing mode is used, and false indicates the opposite.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

IDError Message
201Permission denied.

Example

import { scan } from '@kit.BasicServicesKit';
import { BusinessError } from '@kit.BasicServicesKit';

let scannerId: string = 'scanner_001';
let batchMode: boolean = true;
scan.startScan(scannerId, batchMode).then(() => {
    console.info('start scan success');
}).catch((error: BusinessError) => {
    console.error('start scan failed: ' + JSON.stringify(error));
})

scan.cancelScan

cancelScan(scannerId: string): Promise<void>

Cancels scanning. This API uses a promise to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
scannerIdstringYesScanner ID.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

IDError Message
201Permission denied.

Example

import { scan } from '@kit.BasicServicesKit';
import { BusinessError } from '@kit.BasicServicesKit';

let scannerId: string = 'scanner_001';
scan.cancelScan(scannerId).then(() => {
    console.info('cancel scan success');
}).catch((error: BusinessError) => {
    console.error('cancel scan failed: ' + JSON.stringify(error));
})

scan.getPictureScanProgress

getPictureScanProgress(scannerId: string): Promise<PictureScanProgress>

Obtains the progress of scanning a picture. This API uses a promise to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
scannerIdstringYesScanner ID.

Return value

TypeDescription
Promise<PictureScanProgress>Promise used to return the progress.

Error codes

IDError Message
201Permission denied.

Example

import { scan } from '@kit.BasicServicesKit';
import { BusinessError } from '@kit.BasicServicesKit';

let scannerId: string = 'scanner_001';
scan.getPictureScanProgress(scannerId).then((progress: scan.PictureScanProgress) => {
    console.info('get picture scan progress success: ' + JSON.stringify(progress));
}).catch((error: BusinessError) => {
    console.error('get picture scan progress failed: ' + JSON.stringify(error));
})

scan.on

on(type: 'scanDeviceFound', callback: Callback<ScannerDevice>): void

Registers a callback used to listen for the scanner discovery event. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
type'scanDeviceFound'YesEvent type.
callbackCallback<ScannerDevice>YesCallback used to return the discovered scanner.

Error codes

IDError Message
201Permission denied.

Example

import { scan } from '@kit.BasicServicesKit';

scan.on('scanDeviceFound', (device: scan.ScannerDevice) => {
    console.info('scan device found: ' + JSON.stringify(device));
})

scan.off

off(type: 'scanDeviceFound', callback?: Callback<ScannerDevice>): void

Unregisters a callback used to listen for the scanner discovery event. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.PRINT

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
type'scanDeviceFound'YesEvent type.
callbackCallback<ScannerDevice>NoCallback to unregister.

Error codes

IDError Message
201Permission denied.

Example

import { scan } from '@kit.BasicServicesKit';

let callback = (device: scan.ScannerDevice) => {
    console.info('scan device found: ' + JSON.stringify(device));
};
scan.on('scanDeviceFound', callback);
// Unregister the callback.
scan.off('scanDeviceFound', callback);

scan.on

on(type: 'scanDeviceSync', callback: Callback<ScannerSyncDevice>): void

Registers a callback used to listen for the scanner sync event. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
type'scanDeviceSync'YesEvent type.
callbackCallback<ScannerSyncDevice>YesCallback used to return the synced scanner.

Error codes

IDError Message
201Permission denied.

Example

import { scan } from '@kit.BasicServicesKit';

scan.on('scanDeviceSync', (device: scan.ScannerSyncDevice) => {
    console.info('scan device sync: ' + JSON.stringify(device));
})

scan.off

off(type: 'scanDeviceSync', callback?: Callback<ScannerSyncDevice>): void

Unregisters a callback used to listen for the scanner sync event. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.MANAGE_PRINT_JOB

System capability: SystemCapability.Print.PrintFramework

Parameters

NameTypeMandatoryDescription
type'scanDeviceSync'YesEvent type.
callbackCallback<ScannerSyncDevice>NoCallback to unregister.

Error codes

IDError Message
201Permission denied.

Example

import { scan } from '@kit.BasicServicesKit';

let callback = (device: scan.ScannerSyncDevice) => {
    console.info('scan device sync: ' + JSON.stringify(device));
};
scan.on('scanDeviceSync', callback);
// Unregister the callback.
scan.off('scanDeviceSync', callback);

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 errorcode-zlib

openharmony 鸿蒙 capi-oh-scan-scan-picturescanprogress

openharmony 鸿蒙 js-apis-screen-lock

openharmony 鸿蒙 capi-os-account-common-h

openharmony 鸿蒙 js-apis-screen-lock-sys

openharmony 鸿蒙 capi-oh-print-print-propertylist

openharmony 鸿蒙 capi-oh-scan

openharmony 鸿蒙 js-apis-system-device

openharmony 鸿蒙 errorcode-intelligentVoice

openharmony 鸿蒙 js-apis-request-sys

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/qJFESuF5