@@ -27,44 +27,61 @@ repositories {
2727}
2828
2929dependencies {
30- implementation 'com.journeyapps:zxing-android-embedded:4.2.0'
31- implementation 'androidx.appcompat:appcompat:1.0.2'
30+ implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
3231}
33-
34- android {
35- buildToolsVersion '28.0.3' // Older versions may give compile errors
36- }
37-
3832```
3933
4034## Older SDK versions
4135
42- For Android SDK versions < 24, you can downgrade ` zxing:core ` to 3.3.0 or earlier for Android 14+ support:
36+ By default, only SDK 24+ is supported, even though the library specifies 19 as the minimum version.
37+ No guarantees are made on support for SDK versions below 24 - you'll have to test to make sure it's compatible.
38+
39+ SDK versions 19 - 23 should also work, but one of the changes changes below are required,
40+ and this is not routinely tested.
41+
42+ ### Option 1. Downgrade zxing: core to 3.3.0
4343
4444``` groovy
4545repositories {
4646 mavenCentral()
4747}
4848
4949dependencies {
50- implementation('com.journeyapps:zxing-android-embedded:4.2.0') { transitive = false }
51- implementation 'androidx.appcompat:appcompat:1.0.2'
50+ implementation('com.journeyapps:zxing-android-embedded:4.3.0') { transitive = false }
5251 implementation 'com.google.zxing:core:3.3.0'
5352}
53+ ```
5454
55+ ### Option 2: Desugaring (Advanced)
56+
57+ This option does not require changing library versions, but may complicate the build process.
58+
59+ See [ Java 8+ API desugaring support] ( https://developer.android.com/studio/write/java8-support#library-desugaring ) .
60+
61+ ``` groovy
5562android {
56- buildToolsVersion '28.0.3'
57- }
63+ defaultConfig {
64+ // Important: multidex must be enabled
65+ // https://developer.android.com/studio/build/multidex#mdex-gradle
66+ multiDexEnabled true
67+ minSdkVersion 19
68+ }
5869
59- ```
60- You'll also need this in your Android manifest:
70+ compileOptions {
71+ // Flag to enable support for the new language APIs
72+ coreLibraryDesugaringEnabled true
73+ // Sets Java compatibility to Java 8
74+ sourceCompatibility JavaVersion.VERSION_1_8
75+ targetCompatibility JavaVersion.VERSION_1_8
76+ }
77+ }
6178
62- ``` xml
63- <uses-sdk tools : overrideLibrary =" com.google.zxing.client.android" />
79+ dependencies {
80+ coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
81+ implementation "androidx.multidex:multidex:2.0.1"
82+ }
6483```
6584
66- No guarantees are made on support for older SDK versions - you'll have to test to make sure it's compatible.
67-
6885## Hardware Acceleration
6986
7087Hardware acceleration is required since TextureView is used.
@@ -75,48 +92,42 @@ Make sure it is enabled in your manifest file:
7592 <application android : hardwareAccelerated =" true" ... >
7693```
7794
78- ## Usage with IntentIntegrator
95+ ## Usage with ScanContract
7996
80- Launch the intent with the default options:
81- ``` java
82- new IntentIntegrator (this ). initiateScan(); // `this` is the current Activity
83-
84-
85- // Get the results:
86- @Override
87- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
88- IntentResult result = IntentIntegrator . parseActivityResult(requestCode, resultCode, data);
89- if (result != null ) {
90- if (result. getContents() == null ) {
91- Toast . makeText(this , " Cancelled" , Toast . LENGTH_LONG ). show();
92- } else {
93- Toast . makeText(this , " Scanned: " + result. getContents(), Toast . LENGTH_LONG ). show();
94- }
95- } else {
96- super . onActivityResult(requestCode, resultCode, data);
97- }
98- }
99- ```
97+ Note: ` startActivityForResult ` is deprecated, so this example uses ` registerForActivityResult ` instead.
98+ See for details: https://developer.android.com/training/basics/intents/result
10099
101- Use from a Fragment:
102- ``` java
103- IntentIntegrator . forFragment(this ). initiateScan(); // `this` is the current Fragment
100+ ` startActivityForResult ` can still be used via ` IntentIntegrator ` , but that is not recommended anymore.
104101
105- // If you're using the support library, use IntentIntegrator.forSupportFragment(this) instead.
102+ ``` java
103+ // Register the launcher and result handler
104+ private final ActivityResultLauncher<ScanOptions > barcodeLauncher = registerForActivityResult(new ScanContract (),
105+ result - > {
106+ if (result. getContents() == null ) {
107+ Toast . makeText(MyActivity . this , " Cancelled" , Toast . LENGTH_LONG ). show();
108+ } else {
109+ Toast . makeText(MyActivity . this , " Scanned: " + result. getContents(), Toast . LENGTH_LONG ). show();
110+ }
111+ });
112+
113+ // Launch
114+ public void onButtonClick(View view) {
115+ barcodeLauncher. launch(new ScanOptions ());
116+ }
106117```
107118
108119Customize options:
109120``` java
110- IntentIntegrator integrator = new IntentIntegrator ( this );
111- integrator . setDesiredBarcodeFormats(IntentIntegrator . ONE_D_CODE_TYPES );
112- integrator . setPrompt(" Scan a barcode" );
113- integrator . setCameraId(0 ); // Use a specific camera of the device
114- integrator . setBeepEnabled(false );
115- integrator . setBarcodeImageEnabled(true );
116- integrator . initiateScan( );
121+ ScanOptions options = new ScanOptions ( );
122+ options . setDesiredBarcodeFormats(ScanOptions . ONE_D_CODE_TYPES );
123+ options . setPrompt(" Scan a barcode" );
124+ options . setCameraId(0 ); // Use a specific camera of the device
125+ options . setBeepEnabled(false );
126+ options . setBarcodeImageEnabled(true );
127+ barcodeLauncher . launch(options );
117128```
118129
119- See [ IntentIntegrator ] [ 5 ] for more options.
130+ See [ BarcodeOptions ] [ 5 ] for more options.
120131
121132### Generate Barcode example
122133
@@ -152,9 +163,9 @@ Sample:
152163```
153164
154165``` java
155- IntentIntegrator integrator = new IntentIntegrator ( this );
156- integrator . setOrientationLocked(false );
157- integrator . initiateScan( );
166+ ScanOptions options = new ScanOptions ( );
167+ options . setOrientationLocked(false );
168+ barcodeLauncher . launch(options );
158169```
159170
160171### Customization and advanced options
@@ -198,7 +209,7 @@ You can then use your local version by specifying in your `build.gradle` file:
198209
199210Licensed under the [ Apache License 2.0] [ 7 ]
200211
201- Copyright (C) 2012-2018 ZXing authors, Journey Mobile
212+ Copyright (C) 2012-201 ZXing authors, Journey Mobile
202213
203214 Licensed under the Apache License, Version 2.0 (the "License");
204215 you may not use this file except in compliance with the License.
@@ -216,7 +227,5 @@ Licensed under the [Apache License 2.0][7]
216227
217228[ 1 ] : http://journeyapps.com
218229[ 2 ] : https://github.com/zxing/zxing/
219- [ 3 ] : https://github.com/zxing/zxing/wiki/Scanning-Via-Intent
220- [ 4 ] : https://github.com/journeyapps/zxing-android-embedded/blob/2.x/README.md
221- [ 5 ] : zxing-android-embedded/src/com/google/zxing/integration/android/IntentIntegrator.java
230+ [ 5 ] : zxing-android-embedded/src/com/journeyapps/barcodescanner/ScanOptions.java
222231[ 7 ] : http://www.apache.org/licenses/LICENSE-2.0
0 commit comments