-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
MainActivity.java
156 lines (131 loc) · 5.7 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package example.zxing;
import android.content.Intent;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import com.google.zxing.client.android.Intents;
import com.journeyapps.barcodescanner.ScanContract;
import com.journeyapps.barcodescanner.ScanOptions;
import com.mikepenz.aboutlibraries.LibsBuilder;
import androidx.activity.result.ActivityResultLauncher;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
public class MainActivity extends AppCompatActivity {
private final ActivityResultLauncher<ScanOptions> barcodeLauncher = registerForActivityResult(new ScanContract(),
result -> {
if(result.getContents() == null) {
Intent originalIntent = result.getOriginalIntent();
if (originalIntent == null) {
Log.d("MainActivity", "Cancelled scan");
Toast.makeText(MainActivity.this, "Cancelled", Toast.LENGTH_LONG).show();
} else if(originalIntent.hasExtra(Intents.Scan.MISSING_CAMERA_PERMISSION)) {
Log.d("MainActivity", "Cancelled scan due to missing camera permission");
Toast.makeText(MainActivity.this, "Cancelled due to missing camera permission", Toast.LENGTH_LONG).show();
}
} else {
Log.d("MainActivity", "Scanned");
Toast.makeText(MainActivity.this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void scanBarcode(View view) {
barcodeLauncher.launch(new ScanOptions());
}
public void scanBarcodeInverted(View view){
ScanOptions options = new ScanOptions();
options.addExtra(Intents.Scan.SCAN_TYPE, Intents.Scan.INVERTED_SCAN);
barcodeLauncher.launch(options);
}
public void scanMixedBarcodes(View view){
ScanOptions options = new ScanOptions();
options.addExtra(Intents.Scan.SCAN_TYPE, Intents.Scan.MIXED_SCAN);
barcodeLauncher.launch(options);
}
public void scanBarcodeCustomLayout(View view) {
ScanOptions options = new ScanOptions();
options.setCaptureActivity(AnyOrientationCaptureActivity.class);
options.setDesiredBarcodeFormats(ScanOptions.ONE_D_CODE_TYPES);
options.setPrompt("Scan something");
options.setOrientationLocked(false);
options.setBeepEnabled(false);
barcodeLauncher.launch(options);
}
public void scanPDF417(View view) {
ScanOptions options = new ScanOptions();
options.setDesiredBarcodeFormats(ScanOptions.PDF_417);
options.setPrompt("Scan something");
options.setOrientationLocked(false);
options.setBeepEnabled(false);
barcodeLauncher.launch(options);
}
public void scanBarcodeFrontCamera(View view) {
ScanOptions options = new ScanOptions();
options.setCameraId(Camera.CameraInfo.CAMERA_FACING_FRONT);
barcodeLauncher.launch(options);
}
public void scanContinuous(View view) {
Intent intent = new Intent(this, ContinuousCaptureActivity.class);
startActivity(intent);
}
public void scanToolbar(View view) {
ScanOptions options = new ScanOptions().setCaptureActivity(ToolbarCaptureActivity.class);
barcodeLauncher.launch(options);
}
public void scanCustomScanner(View view) {
ScanOptions options = new ScanOptions().setOrientationLocked(false).setCaptureActivity(CustomScannerActivity.class);
barcodeLauncher.launch(options);
}
public void scanMarginScanner(View view) {
ScanOptions options = new ScanOptions();
options.setOrientationLocked(false);
options.setCaptureActivity(SmallCaptureActivity.class);
barcodeLauncher.launch(options);
}
public void scanWithTimeout(View view) {
ScanOptions options = new ScanOptions();
options.setTimeout(8000);
barcodeLauncher.launch(options);
}
public void tabs(View view) {
Intent intent = new Intent(this, TabbedScanning.class);
startActivity(intent);
}
public void about(View view) {
new LibsBuilder().start(this);
}
/**
* Sample of scanning from a Fragment
*/
public static class ScanFragment extends Fragment {
private final ActivityResultLauncher<ScanOptions> fragmentLauncher = registerForActivityResult(new ScanContract(),
result -> {
if(result.getContents() == null) {
Toast.makeText(getContext(), "Cancelled from fragment", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getContext(), "Scanned from fragment: " + result.getContents(), Toast.LENGTH_LONG).show();
}
});
public ScanFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_scan, container, false);
Button scan = view.findViewById(R.id.scan_from_fragment);
scan.setOnClickListener(v -> scanFromFragment());
return view;
}
public void scanFromFragment() {
fragmentLauncher.launch(new ScanOptions());
}
}
}