simple_barcode_scanner icon indicating copy to clipboard operation
simple_barcode_scanner copied to clipboard

Delay the scanning

Open Sanya-L opened this issue 2 years ago • 8 comments

Hi, is that possible to delay the scanning? Like i dont want the to scan immediately after the function is call. Instead, i want it to wait for few second for the end user to hold for the right position, otherwise it the possibility of scanning wrong barcode in bunch of barcode is very high.

Sanya-L avatar Jul 24 '23 07:07 Sanya-L

For now no, there is no such API, but what you just said make sense. I will probably add another field for delay.

CodingWithTashi avatar Jul 24 '23 10:07 CodingWithTashi

Thanks for your prompt reply. I'm new to flutter and I need this kind of logic in my project, can you suggest any alternative solution/idea for this?

Sanya-L avatar Jul 24 '23 17:07 Sanya-L

yes. that is what i just want to suggest too.

minbokang avatar Oct 26 '23 05:10 minbokang

The underlying package that I used for simple_barcode_scanner does not support same feature as well. either I will have to look into native code or retry with different native package. I am working on the same. Will keep you guys posted.

CodingWithTashi avatar Oct 26 '23 07:10 CodingWithTashi

To introduce a delay in the processing of the barcode scan results, you can modify the stream that you receive from the getBarcodeStreamReceiver method. Since Dart streams don't have a built-in delay between events, you will need to create a new Stream and manage the timing yourself.

Here's an updated version of your getBarcodeStreamReceiver method that introduces a delay between scan results:

dart Copy code import 'dart:async';

// ... (rest of your imports)

class FlutterBarcodeScanner { // ... (existing properties)

static Stream<String>? getBarcodeStreamReceiver( String lineColor, String cancelButtonText, bool isShowFlashIcon, ScanMode scanMode) { if (cancelButtonText.isEmpty) { cancelButtonText = 'Cancel'; }

// ... (existing params setup)

// Open the scanner and get the raw stream
_channel.invokeMethod('scanBarcode', params);

// Create a new StreamController
var controller = StreamController<String>();

// If there's no active receiver, create one.
if (_onBarcodeReceiver == null) {
  _onBarcodeReceiver = _eventChannel.receiveBroadcastStream();
  
  // Listen to the raw stream, add a delay, and push events to the new stream.
  _onBarcodeReceiver!.listen((barcode) {
    Future.delayed(const Duration(seconds: 1), () {
      // After the delay, add the barcode to the stream controller.
      controller.add(barcode);
    });
  }).onDone(() {
    // Close the stream controller when the raw stream is done.
    controller.close();
  });
}

// Return the delayed stream.
return controller.stream;

} } With this approach, you're creating a new StreamController to control the flow of data. When a barcode is scanned, the listen method receives the barcode, waits for the specified delay, and then adds the barcode to the stream controller. This way, you will not miss any scans, but each will be delayed as per the Future.delayed function.

Remember, this approach assumes that scans won't come in more often than the delay; otherwise, they will start to stack up. If you need to handle scans more frequently than the delay, you will have to implement additional logic to decide which scans to keep or discard.....from chatGPT

minbokang avatar Nov 02 '23 08:11 minbokang

another answer from chatGPT..

If you want to introduce a delay before the barcode scanning begins, you will need to delay the display of the barcode scanning UI. You can achieve this by using a Future.delayed in combination with a Timer to start the scanning process after a specified delay.

Here is an example of how you could introduce a delay before the scanner starts:

dart Copy code import 'dart:async';

// ... (rest of your imports)

class SimpleBarcodeScannerPage extends StatefulWidget { // ... (existing properties)

const SimpleBarcodeScannerPage({ Key? key, // ... (rest of your properties) }) : super(key: key);

@override _SimpleBarcodeScannerPageState createState() => _SimpleBarcodeScannerPageState(); }

class _SimpleBarcodeScannerPageState extends State<SimpleBarcodeScannerPage> { bool isDelayOver = false;

@override void initState() { super.initState(); // Delay the scanner start Future.delayed(Duration(seconds: 1), () { if (mounted) { setState(() { isDelayOver = true; // Set this flag to true to start scanning }); } }); }

@override Widget build(BuildContext context) { if (!isDelayOver) { // You can show a loader or a message indicating the delay return Scaffold( body: Center( child: CircularProgressIndicator(), ), ); }

// Start scanning only after the delay is over
return BarcodeScanner(
  lineColor: widget.lineColor,
  cancelButtonText: widget.cancelButtonText,
  isShowFlashIcon: widget.isShowFlashIcon,
  scanType: widget.scanType,
  appBarTitle: widget.appBarTitle,
  centerTitle: widget.centerTitle,
  onScanned: (res) {
    Navigator.pop(context, res);
  },
);

} } In the _SimpleBarcodeScannerPageState class, the initState function sets a delay before the scanner UI appears. The isDelayOver state determines whether the scanner should start or not. This state is only set to true after the delay is completed. Before that, you could show a loading indicator or some placeholder content.

The scanner UI will only be built if isDelayOver is true, ensuring that there is a pause before the scanning starts. Adjust the duration in Duration(seconds: 1) to set how long the delay should be.

minbokang avatar Nov 02 '23 08:11 minbokang

I see you have shared updated code for FlutterBarcodeScanner as well. Since flutter_barcode_scanner is too old. I was planning to use different package but if this is working I will update same package or you can create PR on same.

Thanks

CodingWithTashi avatar Nov 02 '23 08:11 CodingWithTashi

@CodingWithTashi Did you add the delay feature in simple barcode scanner

ramaatamai avatar Oct 09 '24 12:10 ramaatamai

Hi @ramaatamai , Thanks for using our package. I have added delay for android for now, Please note that I am working on ios now. It may take time since I don't have resource to test out myself. Will keep this issue posted

CodingWithTashi avatar Oct 11 '24 17:10 CodingWithTashi

@CodingWithTashi Thanks for the reply ,Once iOS completed let us know.

ramaatamai avatar Oct 14 '24 04:10 ramaatamai

Hi @ramaatamai Issue fixed https://github.com/CodingWithTashi/simple_barcode_scanner/pull/74, closing now

CodingWithTashi avatar Oct 19 '24 00:10 CodingWithTashi

@CodingWithTashi Thanks for reply

ramaatamai avatar Oct 23 '24 09:10 ramaatamai

@CodingWithTashi Did you release the latest version of simple barcode scanner?

ramaatamai avatar Oct 23 '24 09:10 ramaatamai

Yes, this issue is closed now.

CodingWithTashi avatar Oct 23 '24 13:10 CodingWithTashi

@CodingWithTashi If we scanning half Barcode in Android Qr page closed, But in IOS we scan half Barcode screen is not closing .We need to scan full qr code . Why different behaviour.

ramaatamai avatar Nov 11 '24 14:11 ramaatamai

I believe you are referring to this issue https://github.com/CodingWithTashi/simple_barcode_scanner/issues/78. Please track it here

CodingWithTashi avatar Nov 11 '24 15:11 CodingWithTashi

@CodingWithTashi Yes and one more i want the scanner in landscape only.

ramaatamai avatar Nov 11 '24 15:11 ramaatamai

@CodingWithTashi
Scanner supports in Landscape? If yes, Can you please check once below snippet

SystemChrome.setPreferredOrientations([ DeviceOrientation.landscapeRight, DeviceOrientation.landscapeLeft, ]);

String? res = await SimpleBarcodeScanner.scanBarcode(
  context,
  barcodeAppBar: const BarcodeAppBar(
    appBarTitle: 'Test',
    centerTitle: false,
    enableBackButton: true,
    backButtonIcon: Icon(Icons.arrow_back_ios),
  ),
  isShowFlashIcon: true,
  delayMillis: milliseconds,
  cameraFace: CameraFace.front,
);

SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);  in Landscape preview  looks weird Can you Please check once?

ramaatamai avatar Nov 11 '24 15:11 ramaatamai