capture method

request a photo from Frame

Implementation

Future<(Uint8List, ImageMetadata)> capture() async {
  try {
    // save a snapshot of the image metadata (camera settings) to show under the image
    ImageMetadata meta;

    if (_isAutoExposure) {
      meta = AutoExpImageMetadata(qualityValues[qualityIndex].toInt(), autoExpGainTimes, autoExpInterval, meteringValues[meteringIndex], exposure, exposureSpeed, shutterLimit, analogGainLimit, whiteBalanceSpeed);
    }
    else {
      meta = ManualExpImageMetadata(qualityValues[qualityIndex].toInt(), manualShutter, manualAnalogGain, manualRedGain, manualGreenGain, manualBlueGain);
    }

    // send the lua command to request a photo from the Frame based on the current settings
    _stopwatch.reset();
    _stopwatch.start();
    // Send the respective settings for autoexposure or manual
    if (_isAutoExposure) {
      await frame!.sendMessage(TxCameraSettings(
        msgCode: 0x0d,
        qualityIndex: qualityIndex,
        autoExpGainTimes: autoExpGainTimes,
        autoExpInterval: autoExpInterval,
        meteringIndex: meteringIndex,
        exposure: exposure,
        exposureSpeed: exposureSpeed,
        shutterLimit: shutterLimit,
        analogGainLimit: analogGainLimit,
        whiteBalanceSpeed: whiteBalanceSpeed,
      ));
    }
    else {
      await frame!.sendMessage(TxCameraSettings(
        msgCode: 0x0d,
        qualityIndex: qualityIndex,
        autoExpGainTimes: 0,
        manualShutter: manualShutter,
        manualAnalogGain: manualAnalogGain,
        manualRedGain: manualRedGain,
        manualGreenGain: manualGreenGain,
        manualBlueGain: manualBlueGain,
      ));
    }
    // synchronously await the image response
    Uint8List imageData = await RxPhoto(qualityLevel: qualityValues[qualityIndex].toInt()).attach(frame!.dataResponse).first;

    // received a whole-image Uint8List with jpeg header and footer included
    _stopwatch.stop();

    // add the size and elapsed time to the image metadata widget
    if (meta is AutoExpImageMetadata) {
      meta.size = imageData.length;
      meta.elapsedTimeMs = _stopwatch.elapsedMilliseconds;
    }
    else if (meta is ManualExpImageMetadata) {
      meta.size = imageData.length;
      meta.elapsedTimeMs = _stopwatch.elapsedMilliseconds;
    }

    _log.fine(() => 'Image file size in bytes: ${imageData.length}, elapsedMs: ${_stopwatch.elapsedMilliseconds}, ${((imageData.length / 1024.0) / (_stopwatch.elapsedMilliseconds / 1000.0)).toStringAsFixed(2)} kB/s');

    return (imageData, meta);

  } catch (e) {
    _log.severe('Error executing application: $e');
    rethrow;
  }
}