@@ -186,6 +186,10 @@ class CameraPickerState extends State<CameraPicker>
186
186
/// 可用的相机实例
187
187
List <CameraDescription > cameras;
188
188
189
+ /// 当前曝光值
190
+ final ValueNotifier <double > _currentExposureOffset =
191
+ ValueNotifier <double >(0.0 );
192
+
189
193
/// The maximum available value for exposure.
190
194
/// 最大可用曝光值
191
195
double _maxAvailableExposureOffset = 0.0 ;
@@ -194,8 +198,6 @@ class CameraPickerState extends State<CameraPicker>
194
198
/// 最小可用曝光值
195
199
double _minAvailableExposureOffset = 0.0 ;
196
200
197
- double _currentExposureOffset = 0.0 ;
198
-
199
201
/// The maximum available value for zooming.
200
202
/// 最大可用缩放值
201
203
double _maxAvailableZoom;
@@ -343,6 +345,21 @@ class CameraPickerState extends State<CameraPicker>
343
345
}
344
346
}
345
347
348
+ /// Adjust the proper scale type according to the [controller] .
349
+ /// 通过 [controller] 的预览大小,判断相机预览适用的缩放类型。
350
+ _PreviewScaleType get _effectiveScaleType {
351
+ assert (controller != null );
352
+ final Size _size = controller.value.previewSize;
353
+ final Size _scaledSize = _size * (Screens .widthPixels / _size.height);
354
+ if (_scaledSize.width > Screens .heightPixels) {
355
+ return _PreviewScaleType .width;
356
+ } else if (_scaledSize.width < Screens .heightPixels) {
357
+ return _PreviewScaleType .height;
358
+ } else {
359
+ return _PreviewScaleType .none;
360
+ }
361
+ }
362
+
346
363
/// Initialize cameras instances.
347
364
/// 初始化相机实例
348
365
Future <void > initCameras ([CameraDescription cameraDescription]) async {
@@ -783,7 +800,9 @@ class CameraPickerState extends State<CameraPicker>
783
800
}
784
801
785
802
Widget _cameraPreview (BuildContext context) {
786
- return Listener (
803
+ assert (controller != null );
804
+
805
+ Widget _preview = Listener (
787
806
onPointerDown: (_) => _pointers++ ,
788
807
onPointerUp: (_) => _pointers-- ,
789
808
child: GestureDetector (
@@ -794,6 +813,41 @@ class CameraPickerState extends State<CameraPicker>
794
813
child: CameraPreview (controller),
795
814
),
796
815
);
816
+
817
+ if (_effectiveScaleType == _PreviewScaleType .none) {
818
+ return _preview;
819
+ }
820
+
821
+ double _width;
822
+ double _height;
823
+ switch (_effectiveScaleType) {
824
+ case _PreviewScaleType .width:
825
+ _width = Screens .width;
826
+ _height = Screens .width / controller.value.aspectRatio;
827
+ break ;
828
+ case _PreviewScaleType .height:
829
+ _width = Screens .height * controller.value.aspectRatio;
830
+ _height = Screens .height;
831
+ break ;
832
+ default :
833
+ _width = Screens .width;
834
+ _height = Screens .height;
835
+ break ;
836
+ }
837
+ final double _offsetHorizontal = (_width - Screens .width).abs () / - 2 ;
838
+ final double _offsetVertical = (_height - Screens .height).abs () / - 2 ;
839
+ _preview = Stack (
840
+ children: < Widget > [
841
+ Positioned (
842
+ left: _offsetHorizontal,
843
+ right: _offsetHorizontal,
844
+ top: _offsetVertical,
845
+ bottom: _offsetVertical,
846
+ child: _preview,
847
+ ),
848
+ ],
849
+ );
850
+ return _preview;
797
851
}
798
852
799
853
Widget _initializeWrapper ({
@@ -817,25 +871,24 @@ class CameraPickerState extends State<CameraPicker>
817
871
color: Colors .black,
818
872
child: Stack (
819
873
fit: StackFit .expand,
874
+ alignment: Alignment .center,
820
875
children: < Widget > [
821
876
if (isInitialized)
822
- Center (
823
- child: RotatedBox (
824
- quarterTurns: widget.cameraQuarterTurns ?? 0 ,
825
- child: AspectRatio (
826
- aspectRatio: controller.value.aspectRatio,
827
- child: Stack (
828
- children: < Widget > [
829
- Positioned .fill (child: _cameraPreview (context)),
830
- _focusingAreaWidget,
831
- ],
832
- ),
877
+ RotatedBox (
878
+ quarterTurns: widget.cameraQuarterTurns ?? 0 ,
879
+ child: AspectRatio (
880
+ aspectRatio: controller.value.aspectRatio,
881
+ child: Stack (
882
+ children: < Widget > [
883
+ Positioned .fill (child: _cameraPreview (context)),
884
+ _focusingAreaWidget,
885
+ ],
833
886
),
834
887
),
835
888
),
836
889
SafeArea (
837
890
child: Padding (
838
- padding: const EdgeInsets .symmetric (vertical : 20.0 ),
891
+ padding: const EdgeInsets .only (bottom : 20.0 ),
839
892
child: Column (
840
893
children: < Widget > [
841
894
settingsAction,
@@ -852,3 +905,5 @@ class CameraPickerState extends State<CameraPicker>
852
905
);
853
906
}
854
907
}
908
+
909
+ enum _PreviewScaleType { none, width, height }
0 commit comments