Description
Use the chrome.documentScan API to discover and retrieve images from attached document scanners.
The Document Scan API is designed to allow apps and extensions to view the content of paper documents on an attached document scanner.
Permissions
documentScanAvailability
Concepts and usage
This API supports two means of scanning documents. If your use case can work
with any scanner and doesn't require control of the configuration, use the
scan() method. More complicated use cases require a combination of methods,
which are only supported in Chrome 124 and later.
Simple scanning
For simple use cases, meaning those that can work with any scanner and don't
require control of configuration, call scan(). This method takes a
ScanOptions object and returns a Promise that resolves with a ScanResults
object. The capabilities of this option are limited to the number of scans and
the MIME types that will be accepted by the caller. Scans are returned as URLs
for display in an <img> tag for a user interface.
Complex scanning
Complex scans are accomplished in three phases as described in this section. This outline does not describe every method argument or every property returned in a response. It is only intended to give you a general guide to writing scanner code.
Discovery
Call
getScannerList(). Available scanners are returned in a Promise that resolves with aGetScannerListResponse.- The response object contains an array of
ScannerInfoobjects. - The array may contain multiple entries for a single scanner if that scanner supports multiple protocols or connection methods.
- The response object contains an array of
Select a scanner from the returned array and save the value of its
scannerIdproperty.Use the properties of individual
ScannerInfoobjects to distinguish among multiple objects for the same scanner. Objects from the same scanner will have the same value for thedeviceUuidproperty.ScannerInfoalso contains animageFormatsproperty containing an array of supported image types.
Scanner configuration
Call
openScanner(), passing in the saved scanner ID. It returns a Promise that resolves with anOpenScannerResponse. The response object contains:A
scannerHandleproperty, which you'll need to save.An options property containing scanner-specific properties, which you'll need to set. See Retrieve scanner options for more information.
(Optional) If you need the user to provide values for scanner options, construct a user interface. You will need the scanner options provided by the previous step, and you'll need to retrieve option groups provided by the scanner. See Construct a user interface for more information.
Construct an array of
OptionSettingobjects using programmatic or user-provided values. See Set scanner options for more information.Pass the array of
OptionSettingobjects tosetOptions()to set options for the scanner. It returns a Promise that resolves with aSetOptionsResponse. This object contains an updated version of the scanner options retrieved in step 1 of scanner configuration.Since changing one option can alter constraints on another option, you may need to repeat these steps several times.
Scanning
Construct a
StartScanOptionsobject and pass it tostartScan(). It returns a Promise that resolves with aStartScanResponse. Itsjobproperty is a handle that you will use to either read scan data or cancel the scan.Pass the job handle to
readScanData(). It returns a Promise that resolves with aReadScanDataResponseobject. If data was read successfully, itsresultproperty equalsSUCCESSand itsdataproperty contains anArrayBufferwith part of the scan. Note thatestimatedCompletioncontains an estimated percentage of the total data that has been delivered so far.Repeat the previous step until the
resultproperty equalsEOFor an error.
When the end of the scan is reached, call
closeScanner() with the scanner handle saved in step
3. It returns a Promise that resolves with a
CloseScannerResponse. Calling
cancelScan() at any time after the job is created will
end scanning.
Response objects
All methods return a Promise that resolves with a response object of some kind.
Most of these contain a result property whose value is a member of
OperationResult. Some properties of response objects
won't contain values unless the value of result has a specific value. These
relationships are described in the reference for each response object.
For example,