2022-12-14 17:33:38 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:io';
|
|
|
|
import 'dart:math';
|
|
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:camera/camera.dart';
|
|
|
|
import 'package:exif/exif.dart';
|
2022-12-21 19:31:22 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2022-12-14 17:33:38 +00:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2023-01-26 09:10:23 +00:00
|
|
|
import 'package:lightmeter/interactors/metering_interactor.dart';
|
2022-12-18 09:03:41 +00:00
|
|
|
import 'package:lightmeter/screens/metering/ev_source/ev_source_bloc.dart';
|
2022-12-14 17:33:38 +00:00
|
|
|
import 'package:lightmeter/screens/metering/communication/bloc_communication_metering.dart';
|
2023-01-26 15:03:48 +00:00
|
|
|
import 'package:lightmeter/screens/metering/communication/event_communication_metering.dart'
|
|
|
|
as communication_event;
|
|
|
|
import 'package:lightmeter/screens/metering/communication/state_communication_metering.dart'
|
|
|
|
as communication_states;
|
2022-12-14 17:33:38 +00:00
|
|
|
import 'package:lightmeter/utils/log_2.dart';
|
|
|
|
|
|
|
|
import 'event_camera.dart';
|
|
|
|
import 'state_camera.dart';
|
|
|
|
|
2022-12-15 11:00:28 +00:00
|
|
|
class CameraBloc extends EvSourceBloc<CameraEvent, CameraState> {
|
2023-01-26 09:10:23 +00:00
|
|
|
final MeteringInteractor _meteringInteractor;
|
2022-12-14 17:33:38 +00:00
|
|
|
late final _WidgetsBindingObserver _observer;
|
|
|
|
CameraController? _cameraController;
|
|
|
|
CameraController? get cameraController => _cameraController;
|
|
|
|
|
2022-12-21 19:31:22 +00:00
|
|
|
static const _maxZoom = 7.0;
|
|
|
|
RangeValues? _zoomRange;
|
|
|
|
double _currentZoom = 0.0;
|
|
|
|
|
|
|
|
static const _exposureMaxRange = RangeValues(-4, 4);
|
|
|
|
RangeValues? _exposureOffsetRange;
|
|
|
|
double _exposureStep = 0.0;
|
|
|
|
double _currentExposureOffset = 0.0;
|
|
|
|
|
2023-01-21 10:37:49 +00:00
|
|
|
CameraBloc(
|
|
|
|
MeteringCommunicationBloc communicationBloc,
|
2023-01-26 09:10:23 +00:00
|
|
|
this._meteringInteractor,
|
2023-01-21 10:37:49 +00:00
|
|
|
) : super(
|
2022-12-15 11:00:28 +00:00
|
|
|
communicationBloc,
|
|
|
|
const CameraInitState(),
|
|
|
|
) {
|
2022-12-14 17:33:38 +00:00
|
|
|
_observer = _WidgetsBindingObserver(_appLifecycleStateObserver);
|
|
|
|
WidgetsBinding.instance.addObserver(_observer);
|
|
|
|
|
2022-12-15 11:00:28 +00:00
|
|
|
on<InitializeEvent>(_onInitialize);
|
2022-12-21 19:31:22 +00:00
|
|
|
on<ZoomChangedEvent>(_onZoomChanged);
|
|
|
|
on<ExposureOffsetChangedEvent>(_onExposureOffsetChanged);
|
2023-01-21 10:37:49 +00:00
|
|
|
on<ExposureOffsetResetEvent>(_onExposureOffsetResetEvent);
|
2022-12-14 17:33:38 +00:00
|
|
|
|
|
|
|
add(const InitializeEvent());
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> close() async {
|
|
|
|
WidgetsBinding.instance.removeObserver(_observer);
|
|
|
|
_cameraController?.dispose();
|
|
|
|
super.close();
|
|
|
|
}
|
|
|
|
|
2022-12-15 11:00:28 +00:00
|
|
|
@override
|
|
|
|
void onCommunicationState(communication_states.SourceState communicationState) {
|
2022-12-14 17:33:38 +00:00
|
|
|
if (communicationState is communication_states.MeasureState) {
|
|
|
|
_takePhoto().then((ev100) {
|
|
|
|
if (ev100 != null) {
|
2023-01-26 15:03:48 +00:00
|
|
|
communicationBloc.add(
|
|
|
|
communication_event.MeasuredEvent(ev100 + _meteringInteractor.cameraEvCalibration),
|
|
|
|
);
|
2022-12-14 17:33:38 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 11:00:28 +00:00
|
|
|
Future<void> _onInitialize(_, Emitter emit) async {
|
2022-12-14 17:33:38 +00:00
|
|
|
emit(const CameraLoadingState());
|
|
|
|
try {
|
|
|
|
final cameras = await availableCameras();
|
|
|
|
_cameraController = CameraController(
|
|
|
|
cameras.firstWhere(
|
|
|
|
(camera) => camera.lensDirection == CameraLensDirection.back,
|
|
|
|
orElse: () => cameras.last,
|
|
|
|
),
|
|
|
|
ResolutionPreset.medium,
|
|
|
|
enableAudio: false,
|
|
|
|
);
|
|
|
|
|
|
|
|
await _cameraController!.initialize();
|
|
|
|
await _cameraController!.setFlashMode(FlashMode.off);
|
2022-12-21 19:31:22 +00:00
|
|
|
|
|
|
|
_zoomRange = await Future.wait<double>([
|
|
|
|
_cameraController!.getMinZoomLevel(),
|
|
|
|
_cameraController!.getMaxZoomLevel(),
|
|
|
|
]).then((levels) => RangeValues(levels[0], min(_maxZoom, levels[1])));
|
|
|
|
_currentZoom = _zoomRange!.start;
|
|
|
|
|
|
|
|
_exposureOffsetRange = await Future.wait<double>([
|
|
|
|
_cameraController!.getMinExposureOffset(),
|
|
|
|
_cameraController!.getMaxExposureOffset(),
|
2023-01-26 15:03:48 +00:00
|
|
|
]).then(
|
|
|
|
(levels) => RangeValues(
|
|
|
|
max(_exposureMaxRange.start, levels[0]),
|
|
|
|
min(_exposureMaxRange.end, levels[1]),
|
|
|
|
),
|
|
|
|
);
|
2022-12-21 19:31:22 +00:00
|
|
|
await _cameraController!.getExposureOffsetStepSize().then((value) {
|
|
|
|
_exposureStep = value == 0 ? 0.1 : value;
|
|
|
|
});
|
2023-01-22 19:40:44 +00:00
|
|
|
_currentExposureOffset = 0.0;
|
2022-12-21 19:31:22 +00:00
|
|
|
|
|
|
|
emit(CameraInitializedState(_cameraController!));
|
|
|
|
|
|
|
|
_emitActiveState(emit);
|
2022-12-15 09:25:52 +00:00
|
|
|
_takePhoto().then((ev100) {
|
|
|
|
if (ev100 != null) {
|
2022-12-15 11:00:28 +00:00
|
|
|
communicationBloc.add(communication_event.MeasuredEvent(ev100));
|
2022-12-15 09:25:52 +00:00
|
|
|
}
|
|
|
|
});
|
2022-12-14 17:33:38 +00:00
|
|
|
} catch (e) {
|
|
|
|
emit(const CameraErrorState());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-21 19:31:22 +00:00
|
|
|
Future<void> _onZoomChanged(ZoomChangedEvent event, Emitter emit) async {
|
|
|
|
_cameraController!.setZoomLevel(event.value);
|
|
|
|
_currentZoom = event.value;
|
|
|
|
_emitActiveState(emit);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _onExposureOffsetChanged(ExposureOffsetChangedEvent event, Emitter emit) async {
|
|
|
|
_cameraController!.setExposureOffset(event.value);
|
|
|
|
_currentExposureOffset = event.value;
|
|
|
|
_emitActiveState(emit);
|
|
|
|
}
|
|
|
|
|
2023-01-21 10:37:49 +00:00
|
|
|
Future<void> _onExposureOffsetResetEvent(ExposureOffsetResetEvent event, Emitter emit) async {
|
2023-01-26 09:10:23 +00:00
|
|
|
_meteringInteractor.quickVibration();
|
2023-01-21 10:37:49 +00:00
|
|
|
add(const ExposureOffsetChangedEvent(0));
|
|
|
|
}
|
|
|
|
|
2022-12-21 19:31:22 +00:00
|
|
|
void _emitActiveState(Emitter emit) {
|
|
|
|
emit(CameraActiveState(
|
|
|
|
minZoom: _zoomRange!.start,
|
|
|
|
maxZoom: _zoomRange!.end,
|
|
|
|
currentZoom: _currentZoom,
|
|
|
|
minExposureOffset: _exposureOffsetRange!.start,
|
|
|
|
maxExposureOffset: _exposureOffsetRange!.end,
|
|
|
|
exposureOffsetStep: _exposureStep,
|
|
|
|
currentExposureOffset: _currentExposureOffset,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2022-12-14 17:33:38 +00:00
|
|
|
Future<double?> _takePhoto() async {
|
|
|
|
if (_cameraController == null ||
|
|
|
|
!_cameraController!.value.isInitialized ||
|
|
|
|
_cameraController!.value.isTakingPicture) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
final file = await _cameraController!.takePicture();
|
|
|
|
final Uint8List bytes = await file.readAsBytes();
|
|
|
|
Directory(file.path).deleteSync(recursive: true);
|
|
|
|
|
|
|
|
final tags = await readExifFromBytes(bytes);
|
|
|
|
final iso = double.parse("${tags["EXIF ISOSpeedRatings"]}");
|
|
|
|
final apertureValueRatio = (tags["EXIF FNumber"]!.values as IfdRatios).ratios.first;
|
|
|
|
final aperture = apertureValueRatio.numerator / apertureValueRatio.denominator;
|
|
|
|
final speedValueRatio = (tags["EXIF ExposureTime"]!.values as IfdRatios).ratios.first;
|
|
|
|
final speed = speedValueRatio.numerator / speedValueRatio.denominator;
|
|
|
|
|
|
|
|
return log2(pow(aperture, 2)) - log2(speed) - log2(iso / 100);
|
|
|
|
} on CameraException catch (e) {
|
|
|
|
debugPrint('Error: ${e.code}\nError Message: ${e.description}');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _appLifecycleStateObserver(AppLifecycleState state) async {
|
|
|
|
switch (state) {
|
|
|
|
case AppLifecycleState.resumed:
|
|
|
|
add(const InitializeEvent());
|
|
|
|
break;
|
|
|
|
case AppLifecycleState.paused:
|
|
|
|
case AppLifecycleState.inactive:
|
|
|
|
_cameraController?.dispose();
|
|
|
|
_cameraController = null;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This is needed only because we cannot use `with` with mixins
|
|
|
|
class _WidgetsBindingObserver with WidgetsBindingObserver {
|
|
|
|
final ValueChanged<AppLifecycleState> onLifecycleStateChanged;
|
|
|
|
|
|
|
|
_WidgetsBindingObserver(this.onLifecycleStateChanged);
|
|
|
|
|
|
|
|
@override
|
|
|
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
|
|
|
onLifecycleStateChanged(state);
|
|
|
|
}
|
|
|
|
}
|