public class IntentIntegrator extends Object
A utility class which helps ease integration with Barcode Scanner via Intent
s. This is a simple
way to invoke barcode scanning and receive the result, without any need to integrate, modify, or learn the
project's source code.
To integrate, create an instance of IntentIntegrator
and call initiateScan()
and wait
for the result in your app.
It does require that the Barcode Scanner (or work-alike) application is installed. The
initiateScan()
method will prompt the user to download the application, if needed.
There are a few steps to using this integration. First, your Activity
must implement
the method Activity.onActivityResult(int, int, Intent)
and include a line of code like this:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanResult != null) {
// handle scan result
}
// else continue with any other code you need in the method
...
}
This is where you will handle a scan result.
Second, just call this in response to a user action somewhere to begin the scan process:
IntentIntegrator integrator = new IntentIntegrator(yourActivity);
integrator.initiateScan();
Note that initiateScan()
returns an AlertDialog
which is non-null if the
user was prompted to download the application. This lets the calling app potentially manage the dialog.
In particular, ideally, the app dismisses the dialog if it's still active in its Activity.onPause()
method.
You can use setTitle(String)
to customize the title of this download prompt dialog (or, use
setTitleByID(int)
to set the title by string resource ID.) Likewise, the prompt message, and
yes/no button labels can be changed.
Finally, you can use addExtra(String, Object)
to add more parameters to the Intent used
to invoke the scanner. This can be used to set additional options not directly exposed by this
simplified API.
By default, this will only allow applications that are known to respond to this intent correctly
do so. The apps that are allowed to response can be set with setTargetApplications(List)
.
For example, set to TARGET_BARCODE_SCANNER_ONLY
to only target the Barcode Scanner app itself.
To share text, encoded as a QR Code on-screen, similarly, see shareText(CharSequence)
.
Some code, particularly download integration, was contributed from the Anobiit application.
Some formats are not enabled by default even when scanning with ALL_CODE_TYPES
, such as
PDF417. Use initiateScan(Collection)
with
a collection containing the names of formats to scan for explicitly, like "PDF_417", to use such
formats.
Modifier and Type | Field and Description |
---|---|
static Collection<String> |
ALL_CODE_TYPES |
static Collection<String> |
DATA_MATRIX_TYPES |
static String |
DEFAULT_MESSAGE |
static String |
DEFAULT_NO |
static String |
DEFAULT_TITLE |
static String |
DEFAULT_YES |
static Collection<String> |
ONE_D_CODE_TYPES |
static Collection<String> |
PRODUCT_CODE_TYPES |
static Collection<String> |
QR_CODE_TYPES |
static int |
REQUEST_CODE |
static List<String> |
TARGET_ALL_KNOWN |
static List<String> |
TARGET_BARCODE_SCANNER_ONLY |
Constructor and Description |
---|
IntentIntegrator(android.app.Activity activity) |
IntentIntegrator(android.app.Fragment fragment) |
Modifier and Type | Method and Description |
---|---|
void |
addExtra(String key,
Object value) |
String |
getButtonNo() |
String |
getButtonYes() |
String |
getMessage() |
Map<String,?> |
getMoreExtras() |
Collection<String> |
getTargetApplications() |
String |
getTitle() |
android.app.AlertDialog |
initiateScan()
Initiates a scan for all known barcode types with the default camera.
|
android.app.AlertDialog |
initiateScan(Collection<String> desiredBarcodeFormats)
Initiates a scan, using the default camera, only for a certain set of barcode types, given as strings corresponding
to their names in ZXing's
BarcodeFormat class like "UPC_A". |
android.app.AlertDialog |
initiateScan(Collection<String> desiredBarcodeFormats,
int cameraId)
Initiates a scan, using the specified camera, only for a certain set of barcode types, given as strings corresponding
to their names in ZXing's
BarcodeFormat class like "UPC_A". |
android.app.AlertDialog |
initiateScan(int cameraId)
Initiates a scan for all known barcode types with the specified camera.
|
static IntentResult |
parseActivityResult(int requestCode,
int resultCode,
android.content.Intent intent)
Call this from your
Activity 's
Activity.onActivityResult(int, int, Intent) method. |
void |
setButtonNo(String buttonNo) |
void |
setButtonNoByID(int buttonNoID) |
void |
setButtonYes(String buttonYes) |
void |
setButtonYesByID(int buttonYesID) |
void |
setMessage(String message) |
void |
setMessageByID(int messageID) |
void |
setSingleTargetApplication(String targetApplication) |
void |
setTargetApplications(List<String> targetApplications) |
void |
setTitle(String title) |
void |
setTitleByID(int titleID) |
android.app.AlertDialog |
shareText(CharSequence text)
Defaults to type "TEXT_TYPE".
|
android.app.AlertDialog |
shareText(CharSequence text,
CharSequence type)
Shares the given text by encoding it as a barcode, such that another user can
scan the text off the screen of the device.
|
protected void |
startActivityForResult(android.content.Intent intent,
int code)
Start an activity.
|
public static final int REQUEST_CODE
public static final String DEFAULT_TITLE
public static final String DEFAULT_MESSAGE
public static final String DEFAULT_YES
public static final String DEFAULT_NO
public static final Collection<String> PRODUCT_CODE_TYPES
public static final Collection<String> ONE_D_CODE_TYPES
public static final Collection<String> QR_CODE_TYPES
public static final Collection<String> DATA_MATRIX_TYPES
public static final Collection<String> ALL_CODE_TYPES
public IntentIntegrator(android.app.Activity activity)
activity
- Activity
invoking the integrationpublic IntentIntegrator(android.app.Fragment fragment)
fragment
- Fragment
invoking the integration.
startActivityForResult(Intent, int)
will be called on the Fragment
instead
of an Activity
public String getTitle()
public void setTitle(String title)
public void setTitleByID(int titleID)
public String getMessage()
public void setMessage(String message)
public void setMessageByID(int messageID)
public String getButtonYes()
public void setButtonYes(String buttonYes)
public void setButtonYesByID(int buttonYesID)
public String getButtonNo()
public void setButtonNo(String buttonNo)
public void setButtonNoByID(int buttonNoID)
public Collection<String> getTargetApplications()
public void setSingleTargetApplication(String targetApplication)
public final android.app.AlertDialog initiateScan()
AlertDialog
that was shown to the user prompting them to download the app
if a prompt was needed, or null otherwise.public final android.app.AlertDialog initiateScan(int cameraId)
cameraId
- camera ID of the camera to use. A negative value means "no preference".AlertDialog
that was shown to the user prompting them to download the app
if a prompt was needed, or null otherwise.public final android.app.AlertDialog initiateScan(Collection<String> desiredBarcodeFormats)
BarcodeFormat
class like "UPC_A". You can supply constants
like PRODUCT_CODE_TYPES
for example.desiredBarcodeFormats
- names of BarcodeFormat
s to scan forAlertDialog
that was shown to the user prompting them to download the app
if a prompt was needed, or null otherwise.public final android.app.AlertDialog initiateScan(Collection<String> desiredBarcodeFormats, int cameraId)
BarcodeFormat
class like "UPC_A". You can supply constants
like PRODUCT_CODE_TYPES
for example.desiredBarcodeFormats
- names of BarcodeFormat
s to scan forcameraId
- camera ID of the camera to use. A negative value means "no preference".AlertDialog
that was shown to the user prompting them to download the app
if a prompt was needed, or null otherwiseprotected void startActivityForResult(android.content.Intent intent, int code)
intent
- Intent to start.code
- Request code for the activityActivity.startActivityForResult(Intent, int)
,
Fragment.startActivityForResult(Intent, int)
public static IntentResult parseActivityResult(int requestCode, int resultCode, android.content.Intent intent)
Call this from your Activity
's
Activity.onActivityResult(int, int, Intent)
method.
requestCode
- request code from onActivityResult()
resultCode
- result code from onActivityResult()
intent
- Intent
from onActivityResult()
IntentResult
containing the result of the scan. If the user cancelled scanning,
the fields will be null.public final android.app.AlertDialog shareText(CharSequence text)
text
- the text string to encode as a barcodeAlertDialog
that was shown to the user prompting them to download the app
if a prompt was needed, or null otherwiseshareText(CharSequence, CharSequence)
public final android.app.AlertDialog shareText(CharSequence text, CharSequence type)
text
- the text string to encode as a barcodetype
- type of data to encode. See com.google.zxing.client.android.Contents.Type
constants.AlertDialog
that was shown to the user prompting them to download the app
if a prompt was needed, or null otherwiseCopyright © 2007–2018. All rights reserved.