Barcode Detection API - Web APIs 编辑

Draft

This page is not complete.

Secure context

This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

 

Note:

This feature is available in Web Workers.

The Barcode Detection API detects linear and two-dimensional barcodes in images.

Concepts and usage

Support for barcode recognition within web apps unlocks a variety of use cases through supported barcode formats. QR codes can be used for online payments, web navigation or establishing social media connections, aztec codes can be used to scan boarding passes and shopping apps can use EAN or UPC barcodes to compare prices of physical items.

Detection is achieved through the detect() method, which takes an image object; either an element, a Blob, ImageData or a CanvasImageSource. Optional parameters can be passed to the BarcodeDetector constructor to provide hints on which barcode formats to detect.

Supported barcode formats

The Barcode Detection API supports the following barcode formats:

FormatDescriptionImage
aztecA square two-dimensional matrix following iso24778 and with a square bullseye pattern at their centre, thus resembling an Aztec pyramid. Does not require a surrounding blank zone.A sample image of an aztec barcode. A square with smaller black and white squares inside
code_128A linear (one-dimensional), bidirectionally-decodable, self-checking barcode following iso15417 and able to encode all 128 characters of ASCII (hence the naming).An image of a code-128 barcode. A horizontal distribution of vertical black and white lines
code_39A linear (one-dimensional), self-checking barcode following iso16388. It is a discrete and variable-length barcode type.An image of a code-39 barcode. A horizontal distribution of vertical black and white lines
code_93A linear, continuous symbology with a variable length following bc5. It offers a larger information density than Code 128 and the visually similar Code 39. Code 93 is used primarily by Canada Post to encode supplementary delivery information.An image of a code 93 format barcode. A horizontal distribution of white and black horizontal lines
codabarA linear barcode representing characters 0-9, A-D and symbols - . $ / +An image of a codabar format barcode. A horizontal distribution of black and white vertical lines
data_matrixAn orientation-independent two-dimensional barcode composed of black and white modules arranged in either a square or rectangular pattern following iso16022.An example of a data matrix barcode. A square filled with smaller black and white squares
ean_13A linear barcode based on the UPC-A standard and defined in iso15420.An image of an EAN-13 format barcode. A horizontal distribution of white and black lines
ean_8A linear barcode defined in iso15420 and derived from EAN-13.An image of an EAN-8 format barcode. A horizontal distribution of vertical black and white lines
itfA continuous, self-checking, bidirectionally decodable barcode. It will always encode 14 digits.An image of an ITF Barcode. A horizontal distribution of white and black lines
pdf417A continuous two-dimensional barcode symbology format with multiple rows and columns. It's bi-directionally decodable and uses the iso15438 standard.An example of a pdf417 barcode format. A rectangle of smaller black and white squares
qr_codeA two-dimensional barcode that uses the iso18004 standard. The information encoded can be text, URL or other data.An example of a QR code. A square of smaller black and white squares
upc_aOne of the most common linear barcode types and is widely applied to retail in the United States. Defined in iso15420, it represents digits by strips of bars and spaces, each digit being associated to a unique pattern of 2 bars and 2 spaces, both of variable width. UPC-A can encode 12 digits that are uniquely assigned to each trade item, and it’s technically a subset of EAN-13 (UPC-A codes are represented in EAN-13 with the first character set to 0).An image of a upc-a barcode. A rectangle of black and white vertical lines with numbers underneath
upc_eA variation of UPC-A defined in iso15420, compressing out unnecessary zeros for a more compact barcode.An image of a upc-e barcode. A rectangle of black and white vertical lines
unknownThis value is used by the platform to signify that it does not know or specify which barcode format is being detected or supported.

You can check for formats supported by the user agent via the getSupportedFormats() method.

Interfaces

BarcodeDetector
The BarcodeDetector interface of the Barcode Detection API allows detection of linear and two dimensional barcodes in images.

Examples

Creating A Detector

This example tests for browser compatibility and creates a new barcode detector object, with specified supported formats.

// check compatibility
if (!('BarcodeDetector' in window)) {
  console.log('Barcode Detector is not supported by this browser.');
} else {
  console.log('Barcode Detector supported!');

  // create new detector
  var barcodeDetector = new BarcodeDetector({formats: ['code_39', 'codabar', 'ean_13']});
}

Getting Supported Formats

The following example calls the getSupportFormat() method and logs the results to the console.

// check supported types
BarcodeDetector.getSupportedFormats()
  .then(formats => {
    supportedFormats.forEach(format => console.log(format));
  });

Detect Barcodes

This example uses the detect() method to detect the barcodes within the given image. These are iterated over and the barcode data is logged to the console.

  barcodeDetector.detect(imageEl)
    .then(barcodes => {
      barcodes.forEach(barcode => console.log(barcode.rawData));
    }
    .catch(err => {
      console.log(err);
    })

Specifications

SpecificationStatusComment
Accelerated Shape Detection in ImagesDraftInitial definition.

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:141 次

字数:11234

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文