IntentIntegrator (ZXing 3.3.2 API)

com.google.zxing.integration.android

## Class IntentIntegrator

- java.lang.Object
- - com.google.zxing.integration.android.IntentIntegrator

-     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.

  ## Initiating a barcode scan

  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.

  ## Sharing text via barcode

  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.

  ## Enabling experimental barcode formats

  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.

  Author:
    Sean Owen, Fred Lin, Isaac Potoczny-Jones, Brad Drehmer, gcstang

- - ### Field Summary

    Fields
    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 Summary

    Constructors
    Constructor and Description
    `IntentIntegrator(android.app.Activity activity)`
    `IntentIntegrator(android.app.Fragment fragment)`

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    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.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### REQUEST_CODE

          public static final int REQUEST_CODE

      See Also:
        Constant Field Values

    - #### DEFAULT_TITLE

          public static final String DEFAULT_TITLE

      See Also:
        Constant Field Values

    - #### DEFAULT_MESSAGE

          public static final String DEFAULT_MESSAGE

      See Also:
        Constant Field Values

    - #### DEFAULT_YES

          public static final String DEFAULT_YES

      See Also:
        Constant Field Values

    - #### DEFAULT_NO

          public static final String DEFAULT_NO

      See Also:
        Constant Field Values

    - #### PRODUCT_CODE_TYPES

          public static final Collection<String> PRODUCT_CODE_TYPES

    - #### ONE_D_CODE_TYPES

          public static final Collection<String> ONE_D_CODE_TYPES

    - #### QR_CODE_TYPES

          public static final Collection<String> QR_CODE_TYPES

    - #### DATA_MATRIX_TYPES

          public static final Collection<String> DATA_MATRIX_TYPES

    - #### ALL_CODE_TYPES

          public static final Collection<String> ALL_CODE_TYPES

    - #### TARGET_BARCODE_SCANNER_ONLY

          public static final List<String> TARGET_BARCODE_SCANNER_ONLY

    - #### TARGET_ALL_KNOWN

          public static final List<String> TARGET_ALL_KNOWN

  - ### Constructor Detail

    - #### IntentIntegrator

          public IntentIntegrator(android.app.Activity activity)

      Parameters:
        `activity` - `Activity` invoking the integration

    - #### IntentIntegrator

          public IntentIntegrator(android.app.Fragment fragment)

      Parameters:
        `fragment` - `Fragment` invoking the integration. `startActivityForResult(Intent, int)` will be called on the `Fragment` instead of an `Activity`

  - ### Method Detail

    - #### getTitle

          public String getTitle()

    - #### setTitle

          public void setTitle(String title)

    - #### setTitleByID

          public void setTitleByID(int titleID)

    - #### getMessage

          public String getMessage()

    - #### setMessage

          public void setMessage(String message)

    - #### setMessageByID

          public void setMessageByID(int messageID)

    - #### getButtonYes

          public String getButtonYes()

    - #### setButtonYes

          public void setButtonYes(String buttonYes)

    - #### setButtonYesByID

          public void setButtonYesByID(int buttonYesID)

    - #### getButtonNo

          public String getButtonNo()

    - #### setButtonNo

          public void setButtonNo(String buttonNo)

    - #### setButtonNoByID

          public void setButtonNoByID(int buttonNoID)

    - #### getTargetApplications

          public Collection<String> getTargetApplications()

    - #### setTargetApplications

          public final void setTargetApplications(List<String> targetApplications)

    - #### setSingleTargetApplication

          public void setSingleTargetApplication(String targetApplication)

    - #### getMoreExtras

          public Map<String,?> getMoreExtras()

    - #### addExtra

          public final void addExtra(String key,
                                     Object value)

    - #### initiateScan

          public final android.app.AlertDialog initiateScan()

      Initiates a scan for all known barcode types with the default camera.
      Returns:
        the `AlertDialog` that was shown to the user prompting them to download the app if a prompt was needed, or null otherwise.

    - #### initiateScan

          public final android.app.AlertDialog initiateScan(int cameraId)

      Initiates a scan for all known barcode types with the specified camera.
      Parameters:
        `cameraId` - camera ID of the camera to use. A negative value means "no preference".
      Returns:
        the `AlertDialog` that was shown to the user prompting them to download the app if a prompt was needed, or null otherwise.

    - #### initiateScan

          public final 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". You can supply constants like `PRODUCT_CODE_TYPES` for example.
      Parameters:
        `desiredBarcodeFormats` - names of `BarcodeFormat`s to scan for
      Returns:
        the `AlertDialog` that was shown to the user prompting them to download the app if a prompt was needed, or null otherwise.

    - #### initiateScan

          public final 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". You can supply constants like `PRODUCT_CODE_TYPES` for example.
      Parameters:
        `desiredBarcodeFormats` - names of `BarcodeFormat`s to scan for
        `cameraId` - camera ID of the camera to use. A negative value means "no preference".
      Returns:
        the `AlertDialog` that was shown to the user prompting them to download the app if a prompt was needed, or null otherwise

    - #### startActivityForResult

          protected void startActivityForResult(android.content.Intent intent,
                                                int code)

      Start an activity. This method is defined to allow different methods of activity starting for newer versions of Android and for compatibility library.
      Parameters:
        `intent` - Intent to start.
        `code` - Request code for the activity
      See Also:
        `Activity.startActivityForResult(Intent, int)`, `Fragment.startActivityForResult(Intent, int)`

    - #### parseActivityResult

          public static IntentResult parseActivityResult(int requestCode,
                                                         int resultCode,
                                                         android.content.Intent intent)

      Call this from your `Activity`'s `Activity.onActivityResult(int, int, Intent)` method.

      Parameters:
        `requestCode` - request code from `onActivityResult()`
        `resultCode` - result code from `onActivityResult()`
        `intent` - `Intent` from `onActivityResult()`
      Returns:
        null if the event handled here was not related to this class, or else an `IntentResult` containing the result of the scan. If the user cancelled scanning, the fields will be null.

    - #### shareText

          public final android.app.AlertDialog shareText(CharSequence text)

      Defaults to type "TEXT_TYPE".
      Parameters:
        `text` - the text string to encode as a barcode
      Returns:
        the `AlertDialog` that was shown to the user prompting them to download the app if a prompt was needed, or null otherwise
      See Also:
        `shareText(CharSequence, CharSequence)`

    - #### shareText

          public final 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.
      Parameters:
        `text` - the text string to encode as a barcode
        `type` - type of data to encode. See `com.google.zxing.client.android.Contents.Type` constants.
      Returns:
        the `AlertDialog` that was shown to the user prompting them to download the app if a prompt was needed, or null otherwise

Copyright © 2007–2018. All rights reserved.