Skip to content

android-password-store/zxing-android-embedded

 
 

Repository files navigation

ZXing Android Embedded

Barcode scanning library for Android, using ZXing for decoding.

The project is loosely based on the ZXing Android Barcode Scanner application, but is not affiliated with the official ZXing project.

Features:

  1. Can be used via Intents (little code required).
  2. Can be embedded in an Activity, for advanced customization of UI and logic.
  3. Scanning can be performed in landscape or portrait mode.
  4. Camera is managed in a background thread, for fast startup time.

A sample application is available in Releases.

By default, Android SDK 24+ is required because of zxing:core 3.4.0. To support SDK 14+, see Older SDK versions.

Adding aar dependency with Gradle

From version 4.x, only Android SDK 24+ is supported by default, and androidx is required.

Add the following to your build.gradle file:

repositories {
    mavenCentral()
}

dependencies {
    implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
}

Older SDK versions

By default, only SDK 24+ is supported, even though the library specifies 19 as the minimum version. No guarantees are made on support for SDK versions below 24 - you'll have to test to make sure it's compatible.

SDK versions 19 - 23 should also work, but one of the changes changes below are required, and this is not routinely tested.

Option 1. Downgrade zxing:core to 3.3.0

repositories {
    mavenCentral()
}

dependencies {
    implementation('com.journeyapps:zxing-android-embedded:4.3.0') { transitive = false }
    implementation 'com.google.zxing:core:3.3.0'
}

Option 2: Desugaring (Advanced)

This option does not require changing library versions, but may complicate the build process.

See Java 8+ API desugaring support.

android {
    defaultConfig {
        // Important: multidex must be enabled
        // https://developer.android.com/studio/build/multidex#mdex-gradle
        multiDexEnabled true
        minSdkVersion 19
    }

    compileOptions {
        // Flag to enable support for the new language APIs
        coreLibraryDesugaringEnabled true
        // Sets Java compatibility to Java 8
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
    implementation "androidx.multidex:multidex:2.0.1"
}

Hardware Acceleration

Hardware acceleration is required since TextureView is used.

Make sure it is enabled in your manifest file:

    <application android:hardwareAccelerated="true" ... >

Usage with ScanContract

Note: startActivityForResult is deprecated, so this example uses registerForActivityResult instead. See for details: https://developer.android.com/training/basics/intents/result

startActivityForResult can still be used via IntentIntegrator, but that is not recommended anymore.

// Register the launcher and result handler
private final ActivityResultLauncher<ScanOptions> barcodeLauncher = registerForActivityResult(new ScanContract(),
        result -> {
            if(result.getContents() == null) {
                Toast.makeText(MyActivity.this, "Cancelled", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(MyActivity.this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
            }
        });

// Launch
public void onButtonClick(View view) {
    barcodeLauncher.launch(new ScanOptions());
}

Customize options:

ScanOptions options = new ScanOptions();
options.setDesiredBarcodeFormats(ScanOptions.ONE_D_CODE_TYPES);
options.setPrompt("Scan a barcode");
options.setCameraId(0);  // Use a specific camera of the device
options.setBeepEnabled(false);
options.setBarcodeImageEnabled(true);
barcodeLauncher.launch(options);

See BarcodeOptions for more options.

Generate Barcode example

While this is not the primary purpose of this library, it does include basic support for generating some barcode types:

try {
  BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
  Bitmap bitmap = barcodeEncoder.encodeBitmap("content", BarcodeFormat.QR_CODE, 400, 400);
  ImageView imageViewQrCode = (ImageView) findViewById(R.id.qrCode);
  imageViewQrCode.setImageBitmap(bitmap);
} catch(Exception e) {

}

No customization of the image is currently supported, including changing colors or padding. If you
require more customization, copy and modify the source for the encoder.

Changing the orientation

To change the orientation, specify the orientation in your AndroidManifest.xml and let the ManifestMerger to update the Activity's definition.

Sample:

<activity
		android:name="com.journeyapps.barcodescanner.CaptureActivity"
		android:screenOrientation="fullSensor"
		tools:replace="screenOrientation" />
ScanOptions options = new ScanOptions();
options.setOrientationLocked(false);
barcodeLauncher.launch(options);

Customization and advanced options

See EMBEDDING.

For more advanced options, look at the Sample Application, and browse the source code of the library.

This is considered advanced usage, and is not well-documented or supported.

Android Permissions

The camera permission is required for barcode scanning to function. It is automatically included as part of the library. On Android 6 it is requested at runtime when the barcode scanner is first opened.

When using BarcodeView directly (instead of via IntentIntegrator / CaptureActivity), you have to request the permission manually before calling BarcodeView#resume(), otherwise the camera will fail to open.

Building locally

./gradlew assemble

To deploy the artifacts the your local Maven repository:

./gradlew publishToMavenLocal

You can then use your local version by specifying in your build.gradle file:

repositories {
    mavenLocal()
}

Sponsored by

JourneyApps - Creating business solutions with mobile apps. Fast.

License

Licensed under the Apache License 2.0

Copyright (C) 2012-201 ZXing authors, Journey Mobile

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

Barcode scanner library for Android, based on the ZXing decoder

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%