1616package com .amolg .flutterbarcodescanner ;
1717
1818import android .content .Context ;
19+ import android .os .Handler ;
20+ import android .os .Looper ;
21+ import android .util .Log ;
22+
1923import androidx .annotation .UiThread ;
2024
2125import com .amolg .flutterbarcodescanner .camera .GraphicOverlay ;
3236public 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+ }
0 commit comments