initialize method

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

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) {
  completer = Completer<String>();
  _enumerateDevicesCompleter = Completer<List<String>>();
  _barcodeResults.clear();

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

  imageElement = ImageElement()
    ..width = width.toInt()
    ..height = (height * .2).toInt()
    ..style.width = '100%'
    ..style.height = (height * .2).toInt().toString() + 'px';

  _canvasElement = CanvasElement(
    width: width.toInt(),
    height: height.toInt(),
  );

  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;
}