3
3
import android .content .Intent ;
4
4
import android .hardware .Camera ;
5
5
import android .os .Bundle ;
6
- import androidx .fragment .app .Fragment ;
7
- import androidx .appcompat .app .AppCompatActivity ;
8
6
import android .util .Log ;
9
7
import android .view .LayoutInflater ;
10
8
import android .view .View ;
13
11
import android .widget .Toast ;
14
12
15
13
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 ;
18
16
import com .mikepenz .aboutlibraries .LibsBuilder ;
19
17
18
+ import androidx .activity .result .ActivityResultLauncher ;
19
+ import androidx .appcompat .app .AppCompatActivity ;
20
+ import androidx .fragment .app .Fragment ;
21
+
20
22
21
23
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
+ });
22
40
23
- public final int CUSTOMIZED_REQUEST_CODE = 0x0000ffff ;
24
41
25
42
@ Override
26
43
protected void onCreate (Bundle savedInstanceState ) {
@@ -29,49 +46,45 @@ protected void onCreate(Bundle savedInstanceState) {
29
46
}
30
47
31
48
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 ());
37
50
}
38
51
39
52
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 );
43
56
}
44
57
45
58
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 );
49
62
}
50
63
51
64
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 );
59
72
}
60
73
61
74
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 );
68
81
}
69
82
70
83
71
84
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 );
75
88
}
76
89
77
90
public void scanContinuous (View view ) {
@@ -80,24 +93,26 @@ public void scanContinuous(View view) {
80
93
}
81
94
82
95
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 );
84
98
}
85
99
86
100
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 );
88
103
}
89
104
90
105
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 );
95
110
}
96
111
97
112
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 );
101
116
}
102
117
103
118
public void tabs (View view ) {
@@ -109,45 +124,24 @@ public void about(View view) {
109
124
new LibsBuilder ().start (this );
110
125
}
111
126
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
-
145
127
/**
146
128
* Sample of scanning from a Fragment
147
129
*/
148
130
public static class ScanFragment extends Fragment {
149
131
private String toast ;
150
132
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
+
151
145
public ScanFragment () {
152
146
}
153
147
@@ -168,7 +162,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
168
162
}
169
163
170
164
public void scanFromFragment () {
171
- IntentIntegrator . forSupportFragment ( this ). initiateScan ( );
165
+ fragmentLauncher . launch ( new ScanOptions () );
172
166
}
173
167
174
168
private void displayToast () {
@@ -177,20 +171,5 @@ private void displayToast() {
177
171
toast = null ;
178
172
}
179
173
}
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
- }
195
174
}
196
175
}
0 commit comments