Skip to content

Commit 0c97cf7

Browse files
committed
ScanOptions and ScanContract.
1 parent 9beeac7 commit 0c97cf7

File tree

9 files changed

+549
-140
lines changed

9 files changed

+549
-140
lines changed

Diff for: README.md

+35-40
Original file line numberDiff line numberDiff line change
@@ -100,48 +100,45 @@ Make sure it is enabled in your manifest file:
100100
<application android:hardwareAccelerated="true" ... >
101101
```
102102

103-
## Usage with IntentIntegrator
103+
## Usage with ScanContract
104104

105-
Launch the intent with the default options:
106-
```java
107-
new IntentIntegrator(this).initiateScan(); // `this` is the current Activity
108-
109-
110-
// Get the results:
111-
@Override
112-
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
113-
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
114-
if(result != null) {
115-
if(result.getContents() == null) {
116-
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
117-
} else {
118-
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
119-
}
120-
} else {
121-
super.onActivityResult(requestCode, resultCode, data);
122-
}
123-
}
124-
```
105+
Note: `startActivityForResult` is deprecated, so this example uses `registerForActivityResult` instead.
106+
See for details: https://developer.android.com/training/basics/intents/result
125107

126-
Use from a Fragment:
127-
```java
128-
IntentIntegrator.forFragment(this).initiateScan(); // `this` is the current Fragment
108+
`startActivityForResult` can still be used via `IntentIntegrator`, but that is not recommended anymore.
129109

130-
// If you're using the support library, use IntentIntegrator.forSupportFragment(this) instead.
110+
```java
111+
// Note: startActivityForResult is deprecated, so this example uses registerForActivityResult instead.
112+
// See for details: https://developer.android.com/training/basics/intents/result
113+
114+
// Register the launcher and result handler
115+
private final ActivityResultLauncher<ScanOptions> barcodeLauncher = registerForActivityResult(new ScanContract(),
116+
result -> {
117+
if(result.getContents() == null) {
118+
Toast.makeText(MyActivity.this, "Cancelled", Toast.LENGTH_LONG).show();
119+
} else {
120+
Toast.makeText(MyActivity.this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
121+
}
122+
});
123+
124+
// Launch
125+
public void onButtonClick(View view) {
126+
barcodeLauncher.launch(new ScanOptions());
127+
}
131128
```
132129

133130
Customize options:
134131
```java
135-
IntentIntegrator integrator = new IntentIntegrator(this);
136-
integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
137-
integrator.setPrompt("Scan a barcode");
138-
integrator.setCameraId(0); // Use a specific camera of the device
139-
integrator.setBeepEnabled(false);
140-
integrator.setBarcodeImageEnabled(true);
141-
integrator.initiateScan();
132+
ScanOptions options = new ScanOptions();
133+
options.setDesiredBarcodeFormats(ScanOptions.ONE_D_CODE_TYPES);
134+
options.setPrompt("Scan a barcode");
135+
options.setCameraId(0); // Use a specific camera of the device
136+
options.setBeepEnabled(false);
137+
options.setBarcodeImageEnabled(true);
138+
barcodeLauncher.launch(options);
142139
```
143140

144-
See [IntentIntegrator][5] for more options.
141+
See [BarcodeOptions][5] for more options.
145142

146143
### Generate Barcode example
147144

@@ -177,9 +174,9 @@ Sample:
177174
```
178175

179176
```java
180-
IntentIntegrator integrator = new IntentIntegrator(this);
181-
integrator.setOrientationLocked(false);
182-
integrator.initiateScan();
177+
ScanOptions options = new ScanOptions();
178+
options.setOrientationLocked(false);
179+
barcodeLauncher.launch(options);
183180
```
184181

185182
### Customization and advanced options
@@ -223,7 +220,7 @@ You can then use your local version by specifying in your `build.gradle` file:
223220

224221
Licensed under the [Apache License 2.0][7]
225222

226-
Copyright (C) 2012-2018 ZXing authors, Journey Mobile
223+
Copyright (C) 2012-201 ZXing authors, Journey Mobile
227224

228225
Licensed under the Apache License, Version 2.0 (the "License");
229226
you may not use this file except in compliance with the License.
@@ -241,7 +238,5 @@ Licensed under the [Apache License 2.0][7]
241238

242239
[1]: http://journeyapps.com
243240
[2]: https://github.com/zxing/zxing/
244-
[3]: https://github.com/zxing/zxing/wiki/Scanning-Via-Intent
245-
[4]: https://github.com/journeyapps/zxing-android-embedded/blob/2.x/README.md
246-
[5]: zxing-android-embedded/src/com/google/zxing/integration/android/IntentIntegrator.java
241+
[5]: zxing-android-embedded/src/com/journeyapps/barcodescanner/ScanOptions.java
247242
[7]: http://www.apache.org/licenses/LICENSE-2.0

Diff for: build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ subprojects {
1919
version = '4.2.0'
2020
group = 'com.journeyapps'
2121

22-
ext.androidTargetSdk = 28
22+
ext.androidTargetSdk = 30
2323
ext.zxingCore = 'com.google.zxing:core:3.4.1'
2424
}

Diff for: sample/build.gradle

+4-3
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,17 @@ dependencies {
6565
implementation project(':zxing-android-embedded')
6666

6767
implementation 'androidx.appcompat:appcompat:1.3.1'
68-
implementation "androidx.legacy:legacy-support-v13:1.0.0"
68+
implementation 'androidx.legacy:legacy-support-v13:1.0.0'
69+
implementation "androidx.activity:activity:1.3.1"
6970

7071
// Desugaring and multidex is required for API < 21.
7172
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
72-
implementation "androidx.multidex:multidex:2.0.1"
73+
implementation 'androidx.multidex:multidex:2.0.1'
7374

7475
// leakcanary is for development purposes only
7576
// https://github.com/square/leakcanary
7677
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.7'
7778

7879
// AboutLibraries
79-
implementation "com.mikepenz:aboutlibraries:6.2.3"
80+
implementation 'com.mikepenz:aboutlibraries:6.2.3'
8081
}

Diff for: sample/src/main/java/example/zxing/MainActivity.java

+69-90
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import android.content.Intent;
44
import android.hardware.Camera;
55
import android.os.Bundle;
6-
import androidx.fragment.app.Fragment;
7-
import androidx.appcompat.app.AppCompatActivity;
86
import android.util.Log;
97
import android.view.LayoutInflater;
108
import android.view.View;
@@ -13,14 +11,33 @@
1311
import android.widget.Toast;
1412

1513
import com.google.zxing.client.android.Intents;
16-
import com.google.zxing.integration.android.IntentIntegrator;
17-
import com.google.zxing.integration.android.IntentResult;
14+
import com.journeyapps.barcodescanner.ScanContract;
15+
import com.journeyapps.barcodescanner.ScanOptions;
1816
import com.mikepenz.aboutlibraries.LibsBuilder;
1917

18+
import androidx.activity.result.ActivityResultLauncher;
19+
import androidx.appcompat.app.AppCompatActivity;
20+
import androidx.fragment.app.Fragment;
21+
2022

2123
public class MainActivity extends AppCompatActivity {
24+
private final ActivityResultLauncher<ScanOptions> barcodeLauncher = registerForActivityResult(new ScanContract(),
25+
result -> {
26+
if(result.getContents() == null) {
27+
Intent originalIntent = result.getOriginalIntent();
28+
if (originalIntent == null) {
29+
Log.d("MainActivity", "Cancelled scan");
30+
Toast.makeText(MainActivity.this, "Cancelled", Toast.LENGTH_LONG).show();
31+
} else if(originalIntent.hasExtra(Intents.Scan.MISSING_CAMERA_PERMISSION)) {
32+
Log.d("MainActivity", "Cancelled scan due to missing camera permission");
33+
Toast.makeText(MainActivity.this, "Cancelled due to missing camera permission", Toast.LENGTH_LONG).show();
34+
}
35+
} else {
36+
Log.d("MainActivity", "Scanned");
37+
Toast.makeText(MainActivity.this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
38+
}
39+
});
2240

23-
public final int CUSTOMIZED_REQUEST_CODE = 0x0000ffff;
2441

2542
@Override
2643
protected void onCreate(Bundle savedInstanceState) {
@@ -29,49 +46,45 @@ protected void onCreate(Bundle savedInstanceState) {
2946
}
3047

3148
public void scanBarcode(View view) {
32-
new IntentIntegrator(this).initiateScan();
33-
}
34-
35-
public void scanBarcodeWithCustomizedRequestCode(View view) {
36-
new IntentIntegrator(this).setRequestCode(CUSTOMIZED_REQUEST_CODE).initiateScan();
49+
barcodeLauncher.launch(new ScanOptions());
3750
}
3851

3952
public void scanBarcodeInverted(View view){
40-
IntentIntegrator integrator = new IntentIntegrator(this);
41-
integrator.addExtra(Intents.Scan.SCAN_TYPE, Intents.Scan.INVERTED_SCAN);
42-
integrator.initiateScan();
53+
ScanOptions options = new ScanOptions();
54+
options.addExtra(Intents.Scan.SCAN_TYPE, Intents.Scan.INVERTED_SCAN);
55+
barcodeLauncher.launch(options);
4356
}
4457

4558
public void scanMixedBarcodes(View view){
46-
IntentIntegrator integrator = new IntentIntegrator(this);
47-
integrator.addExtra(Intents.Scan.SCAN_TYPE, Intents.Scan.MIXED_SCAN);
48-
integrator.initiateScan();
59+
ScanOptions options = new ScanOptions();
60+
options.addExtra(Intents.Scan.SCAN_TYPE, Intents.Scan.MIXED_SCAN);
61+
barcodeLauncher.launch(options);
4962
}
5063

5164
public void scanBarcodeCustomLayout(View view) {
52-
IntentIntegrator integrator = new IntentIntegrator(this);
53-
integrator.setCaptureActivity(AnyOrientationCaptureActivity.class);
54-
integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
55-
integrator.setPrompt("Scan something");
56-
integrator.setOrientationLocked(false);
57-
integrator.setBeepEnabled(false);
58-
integrator.initiateScan();
65+
ScanOptions options = new ScanOptions();
66+
options.setCaptureActivity(AnyOrientationCaptureActivity.class);
67+
options.setDesiredBarcodeFormats(ScanOptions.ONE_D_CODE_TYPES);
68+
options.setPrompt("Scan something");
69+
options.setOrientationLocked(false);
70+
options.setBeepEnabled(false);
71+
barcodeLauncher.launch(options);
5972
}
6073

6174
public void scanPDF417(View view) {
62-
IntentIntegrator integrator = new IntentIntegrator(this);
63-
integrator.setDesiredBarcodeFormats(IntentIntegrator.PDF_417);
64-
integrator.setPrompt("Scan something");
65-
integrator.setOrientationLocked(false);
66-
integrator.setBeepEnabled(false);
67-
integrator.initiateScan();
75+
ScanOptions options = new ScanOptions();
76+
options.setDesiredBarcodeFormats(ScanOptions.PDF_417);
77+
options.setPrompt("Scan something");
78+
options.setOrientationLocked(false);
79+
options.setBeepEnabled(false);
80+
barcodeLauncher.launch(options);
6881
}
6982

7083

7184
public void scanBarcodeFrontCamera(View view) {
72-
IntentIntegrator integrator = new IntentIntegrator(this);
73-
integrator.setCameraId(Camera.CameraInfo.CAMERA_FACING_FRONT);
74-
integrator.initiateScan();
85+
ScanOptions options = new ScanOptions();
86+
options.setCameraId(Camera.CameraInfo.CAMERA_FACING_FRONT);
87+
barcodeLauncher.launch(options);
7588
}
7689

7790
public void scanContinuous(View view) {
@@ -80,24 +93,26 @@ public void scanContinuous(View view) {
8093
}
8194

8295
public void scanToolbar(View view) {
83-
new IntentIntegrator(this).setCaptureActivity(ToolbarCaptureActivity.class).initiateScan();
96+
ScanOptions options = new ScanOptions().setCaptureActivity(ToolbarCaptureActivity.class);
97+
barcodeLauncher.launch(options);
8498
}
8599

86100
public void scanCustomScanner(View view) {
87-
new IntentIntegrator(this).setOrientationLocked(false).setCaptureActivity(CustomScannerActivity.class).initiateScan();
101+
ScanOptions options = new ScanOptions().setOrientationLocked(false).setCaptureActivity(CustomScannerActivity.class);
102+
barcodeLauncher.launch(options);
88103
}
89104

90105
public void scanMarginScanner(View view) {
91-
IntentIntegrator integrator = new IntentIntegrator(this);
92-
integrator.setOrientationLocked(false);
93-
integrator.setCaptureActivity(SmallCaptureActivity.class);
94-
integrator.initiateScan();
106+
ScanOptions options = new ScanOptions();
107+
options.setOrientationLocked(false);
108+
options.setCaptureActivity(SmallCaptureActivity.class);
109+
barcodeLauncher.launch(options);
95110
}
96111

97112
public void scanWithTimeout(View view) {
98-
IntentIntegrator integrator = new IntentIntegrator(this);
99-
integrator.setTimeout(8000);
100-
integrator.initiateScan();
113+
ScanOptions options = new ScanOptions();
114+
options.setTimeout(8000);
115+
barcodeLauncher.launch(options);
101116
}
102117

103118
public void tabs(View view) {
@@ -109,45 +124,24 @@ public void about(View view) {
109124
new LibsBuilder().start(this);
110125
}
111126

112-
@Override
113-
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
114-
if (requestCode != CUSTOMIZED_REQUEST_CODE && requestCode != IntentIntegrator.REQUEST_CODE) {
115-
// This is important, otherwise the result will not be passed to the fragment
116-
super.onActivityResult(requestCode, resultCode, data);
117-
return;
118-
}
119-
switch (requestCode) {
120-
case CUSTOMIZED_REQUEST_CODE: {
121-
Toast.makeText(this, "REQUEST_CODE = " + requestCode, Toast.LENGTH_LONG).show();
122-
break;
123-
}
124-
default:
125-
break;
126-
}
127-
128-
IntentResult result = IntentIntegrator.parseActivityResult(resultCode, data);
129-
130-
if(result.getContents() == null) {
131-
Intent originalIntent = result.getOriginalIntent();
132-
if (originalIntent == null) {
133-
Log.d("MainActivity", "Cancelled scan");
134-
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
135-
} else if(originalIntent.hasExtra(Intents.Scan.MISSING_CAMERA_PERMISSION)) {
136-
Log.d("MainActivity", "Cancelled scan due to missing camera permission");
137-
Toast.makeText(this, "Cancelled due to missing camera permission", Toast.LENGTH_LONG).show();
138-
}
139-
} else {
140-
Log.d("MainActivity", "Scanned");
141-
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
142-
}
143-
}
144-
145127
/**
146128
* Sample of scanning from a Fragment
147129
*/
148130
public static class ScanFragment extends Fragment {
149131
private String toast;
150132

133+
private final ActivityResultLauncher<ScanOptions> fragmentLauncher = registerForActivityResult(new ScanContract(),
134+
result -> {
135+
if(result.getContents() == null) {
136+
toast = "Cancelled from fragment";
137+
} else {
138+
toast = "Scanned from fragment: " + result.getContents();
139+
}
140+
141+
// At this point we may or may not have a reference to the activity
142+
displayToast();
143+
});
144+
151145
public ScanFragment() {
152146
}
153147

@@ -168,7 +162,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
168162
}
169163

170164
public void scanFromFragment() {
171-
IntentIntegrator.forSupportFragment(this).initiateScan();
165+
fragmentLauncher.launch(new ScanOptions());
172166
}
173167

174168
private void displayToast() {
@@ -177,20 +171,5 @@ private void displayToast() {
177171
toast = null;
178172
}
179173
}
180-
181-
@Override
182-
public void onActivityResult(int requestCode, int resultCode, Intent data) {
183-
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
184-
if(result != null) {
185-
if(result.getContents() == null) {
186-
toast = "Cancelled from fragment";
187-
} else {
188-
toast = "Scanned from fragment: " + result.getContents();
189-
}
190-
191-
// At this point we may or may not have a reference to the activity
192-
displayToast();
193-
}
194-
}
195174
}
196175
}

Diff for: sample/src/main/res/layout/activity_main.xml

-6
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@
2525
android:text="Scan PDF417"
2626
android:onClick="scanPDF417"/>
2727

28-
<Button
29-
android:layout_width="wrap_content"
30-
android:layout_height="wrap_content"
31-
android:text="@string/scan_barcode_with_request_code"
32-
android:onClick="scanBarcodeWithCustomizedRequestCode"/>
33-
3428
<Button
3529
android:layout_width="wrap_content"
3630
android:layout_height="wrap_content"

0 commit comments

Comments
 (0)