@@ -27,44 +27,61 @@ repositories {
27
27
}
28
28
29
29
dependencies {
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'
32
31
}
33
-
34
- android {
35
- buildToolsVersion '28.0.3' // Older versions may give compile errors
36
- }
37
-
38
32
```
39
33
40
34
## Older SDK versions
41
35
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
43
43
44
44
``` groovy
45
45
repositories {
46
46
mavenCentral()
47
47
}
48
48
49
49
dependencies {
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 }
52
51
implementation 'com.google.zxing:core:3.3.0'
53
52
}
53
+ ```
54
54
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
55
62
android {
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
+ }
58
69
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
+ }
61
78
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
+ }
64
83
```
65
84
66
- No guarantees are made on support for older SDK versions - you'll have to test to make sure it's compatible.
67
-
68
85
## Hardware Acceleration
69
86
70
87
Hardware acceleration is required since TextureView is used.
@@ -75,48 +92,42 @@ Make sure it is enabled in your manifest file:
75
92
<application android : hardwareAccelerated =" true" ... >
76
93
```
77
94
78
- ## Usage with IntentIntegrator
95
+ ## Usage with ScanContract
79
96
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
100
99
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.
104
101
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
+ }
106
117
```
107
118
108
119
Customize options:
109
120
``` 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 );
117
128
```
118
129
119
- See [ IntentIntegrator ] [ 5 ] for more options.
130
+ See [ BarcodeOptions ] [ 5 ] for more options.
120
131
121
132
### Generate Barcode example
122
133
@@ -152,9 +163,9 @@ Sample:
152
163
```
153
164
154
165
``` 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 );
158
169
```
159
170
160
171
### Customization and advanced options
@@ -198,7 +209,7 @@ You can then use your local version by specifying in your `build.gradle` file:
198
209
199
210
Licensed under the [ Apache License 2.0] [ 7 ]
200
211
201
- Copyright (C) 2012-2018 ZXing authors, Journey Mobile
212
+ Copyright (C) 2012-201 ZXing authors, Journey Mobile
202
213
203
214
Licensed under the Apache License, Version 2.0 (the "License");
204
215
you may not use this file except in compliance with the License.
@@ -216,7 +227,5 @@ Licensed under the [Apache License 2.0][7]
216
227
217
228
[ 1 ] : http://journeyapps.com
218
229
[ 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
222
231
[ 7 ] : http://www.apache.org/licenses/LICENSE-2.0
0 commit comments