Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delay the scanning #34

Closed
Sanya-L opened this issue Jul 24, 2023 · 18 comments
Closed

Delay the scanning #34

Sanya-L opened this issue Jul 24, 2023 · 18 comments
Assignees
Labels
enhancement New feature or request

Comments

@Sanya-L
Copy link

Sanya-L commented Jul 24, 2023

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.

@CodingWithTashi
Copy link
Owner

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

@CodingWithTashi CodingWithTashi self-assigned this Jul 24, 2023
@CodingWithTashi CodingWithTashi added the enhancement New feature or request label Jul 24, 2023
@Sanya-L
Copy link
Author

Sanya-L commented Jul 24, 2023

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?

@minbokang
Copy link

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

@CodingWithTashi
Copy link
Owner

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.

@minbokang
Copy link

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? 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
Copy link

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 {
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.

@CodingWithTashi
Copy link
Owner

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

@ramaatamai
Copy link

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

@CodingWithTashi
Copy link
Owner

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

@ramaatamai
Copy link

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

@CodingWithTashi
Copy link
Owner

Hi @ramaatamai Issue fixed #74, closing now

@ramaatamai
Copy link

@CodingWithTashi Thanks for reply

@ramaatamai
Copy link

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

@CodingWithTashi
Copy link
Owner

Yes, this issue is closed now.

@ramaatamai
Copy link

@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.

@CodingWithTashi
Copy link
Owner

I believe you are referring to this issue #78. Please track it here

@ramaatamai
Copy link

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

@ramaatamai
Copy link

ramaatamai commented Nov 11, 2024

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants