BarcodeScanner
Stay organized with collections
Save and categorize content based on your preferences.
Recognizes barcodes (in a variety of 1D and 2D formats) in a supplied InputImage
.
A BarcodeScanner
is created via
BarcodeScanning.getClient(BarcodeScannerOptions)
or BarcodeScanning.getClient()
.
The default option is not recommended because it tries to scan all barcode formats, which is
slow. For example, the code below creates a barcode scanner for Barcode.FORMAT_PDF417
.
BarcodeScanner barcodeScanner =
BarcodeScanning.getClient(
new BarcodeScannerOptions.Builder()
.setBarcodeFormats(Barcode.FORMAT_PDF417)
.build());
To perform barcode scanning in an image, you first need to create an instance of
InputImage
from a Bitmap
,
ByteBuffer
, etc. See
InputImage
documentation for more details. For example, the code below creates an InputImage
from an Image
.
InputImage image = InputImage.fromMediaImage(mediaImage, rotationDegrees);
Then the code below can scan barcodes in the supplied InputImage
.
Task<List<Barcode>> task = barcodeScanner.process(image);
task.addOnSuccessListener(...).addOnFailureListener(...);
Inherited Method Summary
From interface java.io.Closeable
From interface java.lang.AutoCloseable
Public Methods
public abstract void close ()
Closes the scanner and releases its resources.
public abstract Task<List<Barcode>>
process (MlImage
image)
Detects barcodes from the supplied image.
This is an experimental API in beta version.
Create an MlImage
object using one of MlImage
's
builder methods. See MlImage
documentation for more details.
To get the best detection result, we recommend the following:
Returns
- a
Task
that
asynchronously returns a List
of scanned
Barcode
s.
An empty list is returned by the Task
if
nothing is found.
Detects barcodes from the supplied image.
Create an InputImage
object using one of InputImage
's
factory methods. See InputImage
documentation for more details.
To get the best detection result, we recommend the following:
Returns
- a
Task
that
asynchronously returns a List
of scanned
Barcode
s.
An empty list is returned by the Task
if
nothing is found.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-10-31 UTC.
[null,null,["Last updated 2024-10-31 UTC."],[[["\u003cp\u003e\u003ccode\u003eBarcodeScanner\u003c/code\u003e recognizes various 1D and 2D barcodes within images provided as \u003ccode\u003eInputImage\u003c/code\u003e objects.\u003c/p\u003e\n"],["\u003cp\u003eIt's recommended to create \u003ccode\u003eBarcodeScanner\u003c/code\u003e instances using \u003ccode\u003eBarcodeScanning.getClient()\u003c/code\u003e with specific barcode formats for optimal performance.\u003c/p\u003e\n"],["\u003cp\u003eBarcode detection is performed using the \u003ccode\u003eprocess()\u003c/code\u003e method, which returns a \u003ccode\u003eTask\u003c/code\u003e containing a list of detected \u003ccode\u003eBarcode\u003c/code\u003e objects.\u003c/p\u003e\n"],["\u003cp\u003eFor best results, ensure the barcode occupies a significant portion of the input image and specify the target barcode formats during scanner creation.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eBarcodeScanner\u003c/code\u003e instances should be closed using the \u003ccode\u003eclose()\u003c/code\u003e method to release resources when they are no longer needed.\u003c/p\u003e\n"]]],[],null,["# BarcodeScanner\n\npublic interface **BarcodeScanner** extends [Detector](/android/reference/com/google/mlkit/vision/interfaces/Detector)\\\u003c[List](//developer.android.com/reference/java/util/List.html)\\\u003c[Barcode](/android/reference/com/google/mlkit/vision/barcode/common/Barcode)\\\u003e\\\u003e [OptionalModuleApi](/android/reference/com/google/android/gms/common/api/OptionalModuleApi) \nRecognizes barcodes (in a variety of 1D and 2D formats) in a supplied [InputImage](/android/reference/com/google/mlkit/vision/common/InputImage).\n\nA [BarcodeScanner](/android/reference/com/google/mlkit/vision/barcode/BarcodeScanner)\nis created via [BarcodeScanning.getClient(BarcodeScannerOptions)](/android/reference/com/google/mlkit/vision/barcode/BarcodeScanning#getClient(com.google.mlkit.vision.barcode.BarcodeScannerOptions)) or [BarcodeScanning.getClient()](/android/reference/com/google/mlkit/vision/barcode/BarcodeScanning#getClient()).\nThe default option is not recommended because it tries to scan all barcode formats, which is\nslow. For example, the code below creates a barcode scanner for [Barcode.FORMAT_PDF417](/android/reference/com/google/mlkit/vision/barcode/common/Barcode#FORMAT_PDF417). \n\n BarcodeScanner barcodeScanner =\n BarcodeScanning.getClient(\n new BarcodeScannerOptions.Builder()\n .setBarcodeFormats(Barcode.FORMAT_PDF417)\n .build());\n \nTo perform barcode scanning in an image, you first need to create an instance of\n[InputImage](/android/reference/com/google/mlkit/vision/common/InputImage)\nfrom a [Bitmap](//developer.android.com/reference/android/graphics/Bitmap.html),\n[ByteBuffer](//developer.android.com/reference/java/nio/ByteBuffer.html), etc. See\n[InputImage](/android/reference/com/google/mlkit/vision/common/InputImage)\ndocumentation for more details. For example, the code below creates an [InputImage](/android/reference/com/google/mlkit/vision/common/InputImage)\nfrom an [Image](//developer.android.com/reference/android/media/Image.html). \n\n InputImage image = InputImage.fromMediaImage(mediaImage, rotationDegrees);\n \nThen the code below can scan barcodes in the supplied [InputImage](/android/reference/com/google/mlkit/vision/common/InputImage). \n\n Task\u003cList\u003cBarcode\u003e\u003e task = barcodeScanner.process(image);\n task.addOnSuccessListener(...).addOnFailureListener(...);\n \n### Public Method Summary\n\n|----------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract void | [close](/android/reference/com/google/mlkit/vision/barcode/BarcodeScanner#close())() Closes the scanner and releases its resources. |\n| abstract Task\\\u003c[List](//developer.android.com/reference/java/util/List.html)\\\u003c[Barcode](/android/reference/com/google/mlkit/vision/barcode/common/Barcode)\\\u003e\\\u003e | [process](/android/reference/com/google/mlkit/vision/barcode/BarcodeScanner#process(com.google.android.odml.image.MlImage))([MlImage](/android/reference/com/google/android/odml/image/MlImage) image) Detects barcodes from the supplied image. |\n| abstract Task\\\u003c[List](//developer.android.com/reference/java/util/List.html)\\\u003c[Barcode](/android/reference/com/google/mlkit/vision/barcode/common/Barcode)\\\u003e\\\u003e | [process](/android/reference/com/google/mlkit/vision/barcode/BarcodeScanner#process(com.google.mlkit.vision.common.InputImage))([InputImage](/android/reference/com/google/mlkit/vision/common/InputImage) image) Detects barcodes from the supplied image. |\n\n### Inherited Method Summary\n\nFrom interface java.io.Closeable \n\n|---------------|---------|\n| abstract void | close() |\n\nFrom interface java.lang.AutoCloseable \n\n|---------------|---------|\n| abstract void | close() |\n\nPublic Methods\n--------------\n\n#### public abstract void **close** ()\n\nCloses the scanner and releases its resources. \n\n#### public abstract Task\\\u003c[List](//developer.android.com/reference/java/util/List.html)\\\u003c[Barcode](/android/reference/com/google/mlkit/vision/barcode/common/Barcode)\\\u003e\\\u003e\n**process** ([MlImage](/android/reference/com/google/android/odml/image/MlImage) image)\n\nDetects barcodes from the supplied image.\n\nThis is an experimental API in beta version.\n\nCreate an [MlImage](/android/reference/com/google/android/odml/image/MlImage)\nobject using one of [MlImage](/android/reference/com/google/android/odml/image/MlImage)'s\nbuilder methods. See [MlImage](/android/reference/com/google/android/odml/image/MlImage)\ndocumentation for more details.\n\nTo get the best detection result, we recommend the following:\n\n- The barcode covers most of the image.\n- Narrow down the barcode formats in [BarcodeScannerOptions](/android/reference/com/google/mlkit/vision/barcode/BarcodeScannerOptions). \n\n##### Returns\n\n- a [Task](/android/reference/com/google/android/gms/tasks/Task) that asynchronously returns a [List](//developer.android.com/reference/java/util/List.html) of scanned [Barcode](/android/reference/com/google/mlkit/vision/barcode/common/Barcode)s. An empty list is returned by the [Task](/android/reference/com/google/android/gms/tasks/Task) if nothing is found. \n\n#### public abstract Task\\\u003c[List](//developer.android.com/reference/java/util/List.html)\\\u003c[Barcode](/android/reference/com/google/mlkit/vision/barcode/common/Barcode)\\\u003e\\\u003e\n**process** ([InputImage](/android/reference/com/google/mlkit/vision/common/InputImage) image)\n\nDetects barcodes from the supplied image.\n\nCreate an [InputImage](/android/reference/com/google/mlkit/vision/common/InputImage)\nobject using one of [InputImage](/android/reference/com/google/mlkit/vision/common/InputImage)'s\nfactory methods. See [InputImage](/android/reference/com/google/mlkit/vision/common/InputImage)\ndocumentation for more details.\n\nTo get the best detection result, we recommend the following:\n\n- The barcode covers most of the image.\n- Narrow down the barcode formats in [BarcodeScannerOptions](/android/reference/com/google/mlkit/vision/barcode/BarcodeScannerOptions). \n\n##### Returns\n\n- a [Task](/android/reference/com/google/android/gms/tasks/Task) that asynchronously returns a [List](//developer.android.com/reference/java/util/List.html) of scanned [Barcode](/android/reference/com/google/mlkit/vision/barcode/common/Barcode)s. An empty list is returned by the [Task](/android/reference/com/google/android/gms/tasks/Task) if nothing is found."]]