Android integration guide
Scan a payment card with CardScan for Android.

Requirements

  • Android API level 21 or higher
  • AndroidX compatibility
  • Kotlin coroutine compatibility
Note: Your app does not have to be written in kotlin to integrate this library, but must be able to depend on kotlin functionality.

Demo

The cardscan repository contains a demonstration app for the CardScan product. To build and install this library follow the following steps:
  1. 1.
    Clone the repository from github
    1
    git clone --recursive https://github.com/getbouncer/cardscan-android
    Copied!
  2. 2.
    Build the library using gradle or android studio. a. Using android studio, open the directory cardscan-android. Install the app on your device or an emulator by clicking the play button in the top right of android studio.
    b. Using gradle, build the demo app by executing the following command:
    1
    ./gradlew cardscan-demo:assembleRelease
    Copied!
    This will create a release APK in the cardscan-demo/build/outputs/apk directory. Copy this file to your device and install it.

SDK Size

We try to keep our SDK as small as possible while maintaining good performance. The size impact including our SDK into your app varies depending on some features of your app:
We also provide custom implementations of the TensorFlow Lite library that are stripped-down to only support the functions our SDK uses. These custom implementations can drastically reduce the size of the SDK overall.
TF Flavor
Size (not bundled)
Size (bundled)
Dependency
TensorFlow official release
4.00MB
1.00MB
org.tensorflow:tensorflow-lite:2.4.0
Bouncer TF all architectures
2.12MB
0.62MB
com.getbouncer:tensorflow-lite:2.1.0004
Bouncer TF arm only
1.06MB
0.62MB
com.getbouncer:tensorflow-lite-arm-only:2.1.0004
There are two variants of our ML models that you can choose to include in your application; the larger size, faster performing and smaller size, slower performing models. If selecting the slower performing models, the SDK will still attempt to download the faster models over the internet once your app has launched. This helps reduce the size of your app while still guaranteeing performance.
Flavor
Size (not bundled)
Size (bundled)
Dependency
full size
2.9 MB
2.7 MB
com.getbouncer:scan-payment-full:2.1.0004
minimal
1.6 MB
1.2 MB
com.getbouncer:scan-payment-minimal:2.1.0004
download only
0.0 MB
0.0 MB
no dependency required
Given the above table, select the framework you'll be using to calculate the impact that the bouncer SDK will have on the size of your application:
Using download only models
Base SDK
TFLite Framework
Total
App supports all architectures and not bundled
1.0 MB
2.1 MB
3.1 MB
App supports ARM only and not bundled
1.0 MB
1.1 MB
2.1 MB
App released as bundle
1.0 MB
0.6 MB
1.6 MB
App already uses TFLite
1.0 MB
0.0 MB
1.0 MB
Using minimal models
Base SDK
TFLite Framework
Total
App supports all architectures and not bundled
2.99 MB
2.12 MB
5.11 MB
App supports ARM only and not bundled
2.99 MB
1.06 MB
4.05 MB
App already uses TFLite and not bundled
2.99 MB
0.0 MB
2.99 MB
App released as bundle
2.04 MB
0.62 MB
2.66 MB
App already uses TFLite and bundled
2.04 MB
0.0 MB
2.04 MB
Using full size models
Base SDK
TFLite Framework
Total
App supports all architectures and not bundled
3.9 MB
2.1 MB
6.0 MB
App supports ARM only and not bundled
3.9 MB
1.1 MB
5.0 MB
App released as bundle
3.9 MB
0.6 MB
4.5 MB
App already uses TFLite
3.9 MB
0.0 MB
3.9 MB
Bouncer provides a method Scan.isDeviceArchitectureArm() which will return true if the device is running an ARM architecture. This can be used to determine if the device supports scanning when the -arm-only TFLite framework is in use.
The SDK also provides methods CardScanActivity.isScanReady() and CardScanActivity.isNameAndExpiryScanReady() which return true if the ML models used for scanning are downloaded or available through the SDK.

Installation

These libraries are published in the maven central repository, so for most gradle configurations you only need to add the dependencies to your app's build.gradle file:
1
dependencies {
2
implementation 'com.getbouncer:cardscan-ui:2.1.0004'
3
4
// you must select one of the following tensorflow-lite libraries. See the
5
// above chart to understand how each will affect the size of your app.
6
7
// If you're already using tensorflow lite elsewhere in your project, make
8
// sure you depend on the TFLite framework.
9
implementation 'org.tensorflow:tensorflow-lite:2.4.0'
10
11
// If you need to support both ARM and x86 devices (< 1% of all android
12
// devices), include this dependency.
13
implementation 'com.getbouncer:tensorflow-lite:2.1.0004'
14
15
// If you only plan to support ARM devices, use this library
16
implementation 'com.getbouncer:tensorflow-lite-arm-only:2.1.0004'
17
}
Copied!
By default, all of the ML models used to scan are downloaded during the call to CardScanActivity.warmUp. However, if you're concerned about scanning in areas with poor network connectivity, you may also want to include default versions of the scanning ML models in your app. Doing so will guarantee that scan will be available regardless of download speed or network connectivity.
There are two options for including ML models by default into your app: 1. minimal models 2. full models
See the charts above to determine how each of these dependencies will impact the size of your app.

Full Models

The full models are the same as those downloaded over the network. By adding this dependency to your app, you can ensure the maximum performance of the SDK regardless of network connection, but at the cost of a larger SDK.
1
dependencies {
2
implementation "com.getbouncer:scan-payment-full:2.1.0004"
3
}
Copied!

Minimal Models

The minimal models perform slightly slower than the full ML models. The SDK will attempt to download the full versions of the models during CardScanActivity.warmUp, which will override the minimal models. However, in the event the download takes too long or fails, the minimal ML models ensure that the scan will still function with only a small impact to performance.
1
dependencies {
2
implementation "com.getbouncer:scan-payment-minimal:2.1.0004"
3
}
Copied!

Alternative camera implementations

By default, bouncer uses the Android Camera 1 API. To use Camera2 or CameraX, add one of the following imports:
1
implementation "com.getbouncer:scan-camerax:2.1.0004"
2
3
// OR
4
5
implementation "com.getbouncer:scan-camera2:2.1.0004"
Copied!

Using

This library provides a user interface through which payment cards can be scanned. API keys can be created through the Bouncer API console.
Kotlin
Java
1
class LaunchActivity : AppCompatActivity, CardScanActivityResultHandler {
2
3
private const val API_KEY = "<your_api_key_here>"
4
5
override fun onCreate(savedInstanceState: Bundle?) {
6
super.onCreate(savedInstanceState)
7
setContentView(R.layout.activity_launch)
8
9
findViewById(R.id.scanCardButton).setOnClickListener { _ ->
10
CardScanActivity.start(
11
activity = LaunchActivity.this,
12
apiKey = API_KEY,
13
enableEnterCardManually = true,
14
15
// expiry extraction is in beta. See the comment below.
16
enableExpiryExtraction = true,
17
18
// name extraction is in beta. See the comment below.
19
enableNameExtraction = true
20
)
21
}
22
23
/*
24
* To test name and/or expiry extraction, please first provision an API
25
* key, then reach out to [email protected] with details about your
26
* use case and estimated volumes.
27
*
28
* If you are not planning to use name or expiry extraction, you can
29
* omit the line below.
30
*/
31
CardScanActivity.warmUp(
32
context = this,
33
apiKey = API_KEY,
34
initializeNameAndExpiryExtraction = true
35
)
36
}
37
38
override fun onActivityResult(
39
requestCode: Int,
40
resultCode: Int,
41
data: Intent?
42
) {
43
super.onActivityResult(requestCode, resultCode, data)
44
45
if (CardScanActivity.isScanResult(requestCode)) {
46
CardScanActivity.parseScanResult(resultCode, data, this)
47
}
48
}
49
50
override fun cardScanned(scanId: String?, scanResult: ScanResult) {
51
// a payment card was scanned successfully
52
}
53
54
override fun enterManually(scanId: String?) {
55
// the user wants to enter a card manually
56
}
57
58
override fun userCanceled(scanId: String?) {
59
// the user canceled the scan
60
}
61
62
override fun cameraError(scanId: String?) {
63
// scan was canceled due to a camera error
64
}
65
66
override fun analyzerFailure(scanId: String?) {
67
// scan was canceled due to a failure to analyze camera images
68
}
69
70
override fun canceledUnknown(scanId: String?) {
71
// scan was canceled for an unknown reason
72
}
73
}
Copied!
1
class LaunchActivity extends AppCompatActivity
2
implements CardScanActivityResultHandler {
3
4
private static final String API_KEY = "<your_api_key_here>";
5
6
@Override
7
protected void onCreate(@Nullable Bundle savedInstanceState) {
8
super.onCreate(savedInstanceState);
9
setContentView(R.layout.activity_launch);
10
11
findViewById(R.id.scanCardButton).setOnClickListener(v ->
12
CardScanActivity.start(
13
activity = LaunchActivity.this,
14
apiKey = API_KEY,
15
enableEnterCardManually = true,
16
17
// expiry extraction is in beta. See the comment below.
18
enableExpiryExtraction = true,
19
20
// name extraction is in beta. See the comment below.
21
enableNameExtraction = true
22
)
23
);
24
25
/*
26
* To test name and/or expiry extraction, please first provision an API
27
* key, then reach out to [email protected] with details about your
28
* use case and estimated volumes.
29
*
30
* If you are not planning to use name or expiry extraction, you can
31
* omit the line below.
32
*/
33
CardScanActivity.warmUp(
34
this,
35
API_KEY,
36
/* enableNameAndExpiryExtraction */ true
37
);
38
}
39
40
@Override
41
protected void onActivityResult(
42
int requestCode,
43
int resultCode,
44
@Nullable Intent data
45
) {
46
super.onActivityResult(requestCode, resultCode, data);
47
48
if (CardScanActivity.isScanResult(requestCode)) {
49
CardScanActivity.parseScanResult(resultCode, data, this);
50
}
51
}
52
53
@Override
54
public void cardScanned(
55
@Nullable String scanId,
56
@NotNull CardScanActivityResult scanResult
57
) {
58
// a payment card was scanned successfully
59
}
60
61
@Override
62
public void enterManually(@Nullable String scanId) {
63
// the user wants to enter a card manually
64
}
65
66
@Override
67
public void userCanceled(@Nullable String scanId) {
68
// the user canceled the scan
69
}
70
71
@Override
72
public void cameraError(@Nullable String scanId) {
73
// scan was canceled due to a camera error
74
}
75
76
@Override
77
public void analyzerFailure(@Nullable String scanId) {
78
// scan was canceled due to a failure to analyze camera images
79
}
80
81
@Override
82
public void canceledUnknown(@Nullable String scanId) {
83
// scan was canceled for an unknown reason
84
}
85
}
Copied!

Library warm up

CardScan will attempt to update the ML models used to scan payment cards. To ensure these models are upgraded by the time CardScan runs, please make sure to call the warmUp method on CardScan as early in the app flow as possible. In most cases, this can be done in the onApplicationCreate method of your app. Note that warmUp processes on a background thread and will not affect your app's startup time.

Adding an application lifecycle listener

If your app doesn't already listen to application lifecycle events, you can extend the Application object and connect it using your manifest by setting android:name on your application node:
1
<application
2
android:icon="..."
3
android:label="..."
4
android:roundIcon="..."
5
android:name=".MyApplication">
6
7
...
8
9
</application>
Copied!
Create a class with the same name:
Kotlin
Java
1
const val API_KEY = "<your_api_key_here>";
2
3
class MyApplication : Application() {
4
5
override fun onCreate() {
6
super.onCreate();
7
8
/*
9
* CardScan will attempt to update the ML models used to scan payment
10
* cards. By placing the call to the `warmUp` method in the
11
* `onApplicationCreate` method of your app, you allow the most time
12
* possible for models to upgrade. Note that `warmUp` processes on a
13
* background thread and will not affect your app's startup time.
14
*
15
* Note: the last parameter, `initializeNameAndExpiryExtraction`,
16
* determines if name and expiry extraction (beta feature) should be
17
* turned on.
18
*/
19
CardScanActivity.warmUp(this, API_KEY, initializeNameAndExpiryExtraction = false)
20
}
21
}
Copied!
1
public class MyApplication extends Application {
2
public static final String API_KEY = "<your_api_key_here>";
3
4
@Override
5
public void onCreate() {
6
super.onCreate();
7
8
/*
9
* CardScan will attempt to update the ML models used to scan payment
10
* cards. By placing the call to the `warmUp` method in the
11
* `onApplicationCreate` method of your app, you allow the most time
12
* possible for models to upgrade. Note that `warmUp` processes on a
13
* background thread and will not affect your app's startup time.
14
*
15
* Note: the last parameter, `false`, determines if name and expiry
16
* extraction (beta feature) should be turned on.
17
*/
18
CardScanActivity.warmUp(this, API_KEY, false);
19
}
20
}
Copied!

Name and Expiry Extraction

Name and expiry extraction are in beta, and must be manually enabled. The following steps enable them:
  1. 1.
    In the warmUp method, set the optional initializeNameAndExpiryExtraction parameter to true.
  2. 2.
    In the start method, set the optional enableNameExtraction and/or enableExpiryExtraction parameters to true.
The scanner will now attempt to extract the cardholder name and card expiry during scan, and will return the values in the cardScanned method.
Note that name extraction requires additional permissions on your API key. Please contact us as [email protected] to add that permission.

Model Downloading

By default, the SDK will download updates to ML models used to extract card information. To disable model downloading, set the following flag to false before calling warmup or starting the scan:
Kotlin
Java
1
com.getbouncer.scan.framework.Config.downloadModels = false
Copied!
1
com.getbouncer.scan.framework.Config.setDownloadModels(false);
Copied!

UI Customizing

This library is built to be customized to fit your UI.

Basic modifications

To modify text, colors, or padding of the default UI, see the customization documentation.

Extensive modifications

To modify arrangement or UI functionality, you can create a custom implementation of this library. See the example single-activity demo app.

Supporting more cards

Though CardScan supports several cards, you may need to add support for cards specific to your business, instructions can be found in the card support docs.

Developing

The basics of the CardScan architecture are outlined in the architecture docs.
See the development docs for details on developing for this library.

Authors

Adam Wushensky, Sam King, Zain ul Abi Din, and Sven Kuhne

License

This library is available under paid and free licenses. See the LICENSE file for the full license text.

Quick summary

In short, this library will remain free forever for non-commercial applications, but use by commercial applications is limited to 90 days, after which time a licensing agreement is required. We're also adding some legal liability protections.
After this period commercial applications need to convert to a licensing agreement to continue to use this library.

More detailed summary

What’s allowed under the license:
  • Free use for any app for 90 days (for demos, evaluations, hackathons, etc).
  • Contributions (contributors must agree to the Contributor License Agreement)
  • Any modifications as needed to work in your app
What’s not allowed under the license:
  • Commercial applications using the license for longer than 90 days without a license agreement.
  • Using us now in a commercial app today? No worries! Just email [email protected] and we’ll get you set up.
  • Redistribution under a different license
  • Removing attribution
  • Modifying logos
  • Indemnification: using this free software is ‘at your own risk’, so you can’t sue Bouncer Technologies, Inc. for problems caused by this library
Questions? Concerns? Please email us at [email protected] or ask us on slack.
Last modified 3mo ago