initialize method

int initialize(
  1. double width,
  2. double height,
  3. int refreshDelayMillis,
  4. int minimalResultCount,
)

Initialize the scanner :

  • request user permission
  • request camera stream
  • initialize video
  • start video streaming
  • start picture snapshot timer scheduling

Implementation

int initialize(
  double width,
  double height,
  int refreshDelayMillis,
  int minimalResultCount,
) {
  completer = Completer<String>();
  _enumerateDevicesCompleter = Completer<List<String>>();
  _barcodeResults.singleShot = minimalResultCount == 1;
  _barcodeResults.minimalResultCount = minimalResultCount;
  _barcodeResults.clear();

  // Create a video element which will be provided with stream source
  _webcamVideoElement = VideoElement()
    ..width = 1920
    ..height = 1080
    ..style.width = '100%'
    ..style.height = '100%'
    ..style.objectFit = 'contain'
    ..autoplay = true
    ..muted = true;
  _webcamVideoElement.setAttribute('playsinline', 'true');

  imageElement = ImageElement()
    ..width = 1920
    ..height = 1080
    ..style.width = '100%'
    ..style.height = '100%';

  _canvasElement = CanvasElement(
    width: 1920,
    height: 1080,
  );

  final time = DateTime.now().microsecondsSinceEpoch;

  // ignore: undefined_prefixed_name
  ui.platformViewRegistry.registerViewFactory(
    'webcamVideoElement$time',
    (int viewId) => _webcamVideoElement,
  );
  // ignore: undefined_prefixed_name
  ui.platformViewRegistry.registerViewFactory(
    'imageElement',
    (int viewId) => imageElement,
  );

  // Access the webcam stream
  _setupMediaStream(width, height);

  Future.delayed(Duration(seconds: 1), () {
    _scan(refreshDelayMillis);
  });

  return time;
}