Skip to content

Commit 1c60e3c

Browse files
feat: added delay
1 parent 7e50acb commit 1c60e3c

10 files changed

Lines changed: 65 additions & 13 deletions

android/src/main/java/com/amolg/flutterbarcodescanner/BarcodeCaptureActivity.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,15 @@ public boolean onTouchEvent(MotionEvent e) {
203203
private void createCameraSource(boolean autoFocus, boolean useFlash, int cameraFacing) {
204204
Context context = getApplicationContext();
205205

206+
int delayMillis = (int) getIntent().getIntExtra("delayMillis", 0);
207+
206208
// A barcode detector is created to track barcodes. An associated multi-processor instance
207209
// is set to receive the barcode detection results, track the barcodes, and maintain
208210
// graphics for each barcode on screen. The factory is used by the multi-processor to
209211
// create a separate tracker instance for each barcode.
210212
BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context).build();
211-
BarcodeTrackerFactory barcodeFactory = new BarcodeTrackerFactory(mGraphicOverlay, this);
213+
Log.d("BarcodeCaptureActivity", "Barcode detector set up:"+delayMillis);
214+
BarcodeTrackerFactory barcodeFactory = new BarcodeTrackerFactory(mGraphicOverlay, this,delayMillis);
212215
barcodeDetector.setProcessor(
213216
new MultiProcessor.Builder<>(barcodeFactory).build());
214217

android/src/main/java/com/amolg/flutterbarcodescanner/BarcodeGraphicTracker.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
package com.amolg.flutterbarcodescanner;
1717

1818
import android.content.Context;
19+
import android.os.Handler;
20+
import android.os.Looper;
21+
import android.util.Log;
22+
1923
import androidx.annotation.UiThread;
2024

2125
import com.amolg.flutterbarcodescanner.camera.GraphicOverlay;
@@ -32,8 +36,10 @@
3236
public class BarcodeGraphicTracker extends Tracker<Barcode> {
3337
private GraphicOverlay<BarcodeGraphic> mOverlay;
3438
private BarcodeGraphic mGraphic;
35-
39+
private int delayMillis;
3640
private BarcodeUpdateListener mBarcodeUpdateListener;
41+
private Handler handler;
42+
private boolean isWaiting = false;
3743

3844
/**
3945
* Consume the item instance detected from an Activity or Fragment level by implementing the
@@ -44,9 +50,11 @@ public interface BarcodeUpdateListener {
4450
void onBarcodeDetected(Barcode barcode);
4551
}
4652

47-
BarcodeGraphicTracker(GraphicOverlay<BarcodeGraphic> mOverlay, BarcodeGraphic mGraphic, Context context) {
53+
BarcodeGraphicTracker(GraphicOverlay<BarcodeGraphic> mOverlay, BarcodeGraphic mGraphic, Context context, int delayMillis) {
4854
this.mOverlay = mOverlay;
4955
this.mGraphic = mGraphic;
56+
this.delayMillis = delayMillis;
57+
this.handler = new Handler(Looper.getMainLooper());
5058
if (context instanceof BarcodeUpdateListener) {
5159
this.mBarcodeUpdateListener = (BarcodeUpdateListener) context;
5260
} else {
@@ -60,7 +68,7 @@ public interface BarcodeUpdateListener {
6068
@Override
6169
public void onNewItem(int id, Barcode item) {
6270
mGraphic.setId(id);
63-
mBarcodeUpdateListener.onBarcodeDetected(item);
71+
processDetection(item);
6472
}
6573

6674
/**
@@ -70,6 +78,24 @@ public void onNewItem(int id, Barcode item) {
7078
public void onUpdate(Detector.Detections<Barcode> detectionResults, Barcode item) {
7179
mOverlay.add(mGraphic);
7280
mGraphic.updateItem(item);
81+
processDetection(item);
82+
}
83+
84+
private void processDetection(final Barcode item) {
85+
if (!isWaiting) {
86+
isWaiting = true;
87+
Log.d("BarcodeGraphicTracker", "Barcode detected, waiting for " + delayMillis + "ms");
88+
handler.postDelayed(new Runnable() {
89+
@Override
90+
public void run() {
91+
mBarcodeUpdateListener.onBarcodeDetected(item);
92+
isWaiting = false;
93+
Log.d("BarcodeGraphicTracker", "Delay completed, barcode processed");
94+
}
95+
}, delayMillis);
96+
} else {
97+
Log.d("BarcodeGraphicTracker", "Still waiting, ignoring new detection");
98+
}
7399
}
74100

75101
/**
@@ -90,4 +116,4 @@ public void onMissing(Detector.Detections<Barcode> detectionResults) {
90116
public void onDone() {
91117
mOverlay.remove(mGraphic);
92118
}
93-
}
119+
}

android/src/main/java/com/amolg/flutterbarcodescanner/BarcodeTrackerFactory.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,17 @@
2929
class BarcodeTrackerFactory implements MultiProcessor.Factory<Barcode> {
3030
private GraphicOverlay<BarcodeGraphic> mGraphicOverlay;
3131
private Context mContext;
32+
private int delayMillis;
3233

33-
public BarcodeTrackerFactory(GraphicOverlay<BarcodeGraphic> mGraphicOverlay, Context mContext) {
34+
public BarcodeTrackerFactory(GraphicOverlay<BarcodeGraphic> mGraphicOverlay, Context mContext, int delayMillis) {
3435
this.mGraphicOverlay = mGraphicOverlay;
3536
this.mContext = mContext;
37+
this.delayMillis = delayMillis;
3638
}
3739

3840
@Override
3941
public Tracker<Barcode> create(Barcode barcode) {
4042
BarcodeGraphic graphic = new BarcodeGraphic(mGraphicOverlay);
41-
return new BarcodeGraphicTracker(mGraphicOverlay, graphic, mContext);
43+
return new BarcodeGraphicTracker(mGraphicOverlay, graphic, mContext,delayMillis);
4244
}
4345
}

android/src/main/java/com/amolg/flutterbarcodescanner/FlutterBarcodeScannerPlugin.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class FlutterBarcodeScannerPlugin implements MethodCallHandler, ActivityR
4848
public static String lineColor = "";
4949
public static boolean isShowFlashIcon = false;
5050
public static boolean isContinuousScan = false;
51+
public static int delayMillis = 0;
5152
static EventChannel.EventSink barcodeStream;
5253
private EventChannel eventChannel;
5354

@@ -115,6 +116,10 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
115116

116117
isContinuousScan = (boolean) arguments.get("isContinuousScan");
117118

119+
if (null != arguments.get("delayMillis"))
120+
delayMillis = (int) arguments.get("delayMillis");
121+
122+
118123
startBarcodeScannerActivityView((String) arguments.get("cancelButtonText"), isContinuousScan);
119124
}
120125
} catch (Exception e) {
@@ -124,7 +129,8 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
124129

125130
private void startBarcodeScannerActivityView(String buttonText, boolean isContinuousScan) {
126131
try {
127-
Intent intent = new Intent(activity, BarcodeCaptureActivity.class).putExtra("cancelButtonText", buttonText);
132+
Intent intent = new Intent(activity, BarcodeCaptureActivity.class).putExtra("cancelButtonText", buttonText)
133+
.putExtra("delayMillis", delayMillis);
128134
if (isContinuousScan) {
129135
activity.startActivity(intent);
130136
} else {

lib/flutter_barcode_scanner.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class FlutterBarcodeScanner {
2626
/// displayed if [isShowFlashIcon] is true. The text of the cancel button can
2727
/// be customized with the [cancelButtonText] string.
2828
static Future<String> scanBarcode(String lineColor, String cancelButtonText,
29-
bool isShowFlashIcon, ScanMode scanMode) async {
29+
bool isShowFlashIcon, ScanMode scanMode, int? delayMillis) async {
3030
if (cancelButtonText.isEmpty) {
3131
cancelButtonText = 'Cancel';
3232
}
@@ -37,7 +37,8 @@ class FlutterBarcodeScanner {
3737
'cancelButtonText': cancelButtonText,
3838
'isShowFlashIcon': isShowFlashIcon,
3939
'isContinuousScan': false,
40-
'scanMode': scanMode.index
40+
'scanMode': scanMode.index,
41+
'delayMillis': delayMillis ?? 0
4142
};
4243

4344
/// Get barcode scan result

lib/screens/io_device.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class BarcodeScanner extends StatelessWidget {
1818
final bool? centerTitle;
1919
final Widget? child;
2020
final BarcodeAppBar? barcodeAppBar;
21+
final int? delayMillis;
2122

2223
const BarcodeScanner({
2324
super.key,
@@ -30,6 +31,7 @@ class BarcodeScanner extends StatelessWidget {
3031
this.appBarTitle,
3132
this.centerTitle,
3233
this.barcodeAppBar,
34+
this.delayMillis,
3335
});
3436

3537
@override
@@ -44,6 +46,7 @@ class BarcodeScanner extends StatelessWidget {
4446
onScanned: onScanned,
4547
appBarTitle: appBarTitle,
4648
centerTitle: centerTitle,
49+
delayMillis: delayMillis,
4750
);
4851
} else {
4952
/// Scan Android and ios barcode scanner with flutter_barcode_scanner
@@ -71,7 +74,7 @@ class BarcodeScanner extends StatelessWidget {
7174
break;
7275
}
7376
String barcode = await FlutterBarcodeScanner.scanBarcode(
74-
lineColor, cancelButtonText, isShowFlashIcon, scanMode);
77+
lineColor, cancelButtonText, isShowFlashIcon, scanMode, delayMillis);
7578
onScanned(barcode);
7679
}
7780
}

lib/screens/unsupported.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class BarcodeScanner extends StatelessWidget {
1212
final bool? centerTitle;
1313
final Widget? child;
1414
final BarcodeAppBar? barcodeAppBar;
15+
final int? delayMillis;
1516
const BarcodeScanner(
1617
{super.key,
1718
this.lineColor = "#ff6666",
@@ -22,7 +23,8 @@ class BarcodeScanner extends StatelessWidget {
2223
this.appBarTitle,
2324
this.child,
2425
this.centerTitle,
25-
this.barcodeAppBar});
26+
this.barcodeAppBar,
27+
this.delayMillis});
2628

2729
@override
2830
Widget build(BuildContext context) {

lib/screens/web.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class BarcodeScanner extends StatelessWidget {
1919
final bool? centerTitle;
2020
final Widget? child;
2121
final BarcodeAppBar? barcodeAppBar;
22+
final int? delayMillis;
2223

2324
const BarcodeScanner({
2425
super.key,
@@ -31,6 +32,7 @@ class BarcodeScanner extends StatelessWidget {
3132
this.centerTitle,
3233
this.child,
3334
this.barcodeAppBar,
35+
this.delayMillis,
3436
});
3537

3638
@override

lib/screens/window.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class WindowBarcodeScanner extends StatelessWidget {
1919
final String? appBarTitle;
2020
final bool? centerTitle;
2121
final BarcodeAppBar? barcodeAppBar;
22+
final int? delayMillis;
2223

2324
const WindowBarcodeScanner({
2425
super.key,
@@ -30,6 +31,7 @@ class WindowBarcodeScanner extends StatelessWidget {
3031
this.appBarTitle,
3132
this.centerTitle,
3233
this.barcodeAppBar,
34+
this.delayMillis,
3335
});
3436

3537
@override

lib/simple_barcode_scanner.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class SimpleBarcodeScannerPage extends StatelessWidget {
3030

3131
final BarcodeAppBar? barcodeAppBar;
3232

33+
/// Delay in milliseconds to start the scanner
34+
final int? delayMillis;
35+
3336
/// Specify a child widget to be positioned beneath the scanner.
3437
/// This is beneficial when you need to include a customized text field
3538
/// for manual entry of barcode/QR code.
@@ -66,7 +69,8 @@ class SimpleBarcodeScannerPage extends StatelessWidget {
6669
'Use BarcodeAppBar instead. This field will be removed in future versions.')
6770
this.centerTitle,
6871
this.child,
69-
this.barcodeAppBar});
72+
this.barcodeAppBar,
73+
this.delayMillis});
7074

7175
@override
7276
Widget build(BuildContext context) {
@@ -85,6 +89,7 @@ class SimpleBarcodeScannerPage extends StatelessWidget {
8589
appBarTitle: appBarTitle,
8690
centerTitle: centerTitle,
8791
barcodeAppBar: barcodeAppBar,
92+
delayMillis: delayMillis,
8893
child: child,
8994
onScanned: (res) {
9095
Navigator.pop(context, res);

0 commit comments

Comments
 (0)