曝光
Flutter上CupertinoSlider
组件的样式是iOS上的Slider
,使用该组件控制曝光量,
Camera插件提供的API是CameraController
的
Future<double> setExposureOffset(double offset) async {
...
}
最后调用iOS端的系统方法控制曝光值
- (void)setExposureTargetBias:(float)bias completionHandler:(nullable void (^)(CMTime syncTime))handler API_AVAILABLE(ios(8.0), macCatalyst(14.0), tvos(17.0)) API_UNAVAILABLE(macos, visionos);
class TakePictureScreenState extends State<TakePictureScreen> {
/// 设置默认值
double currentExposure = 0.0;
/// 是否显示曝光Slider组件
bool _showedExposure = false;
...
/// 使用CupertinoSlider
Widget showExposure() {
if (_showedExposure) {
return SizedBox(
height: 44,
width: MediaQuery.of(context).size.width,
child: CupertinoSlider(
/// 滑动Slider时触发的事件
onChanged: (value) {
setState(() {
/// 调整相机的曝光值
_controller.setExposureOffset(value);
currentExposure = value;
});
},
min: -3, /// 设置作用范围
max: 3,
value: currentExposure, /// 当前Slider显示的值
),
);
}
return SizedBox.shrink();
}
}

两指手势缩放
系统的相机可以双指进行缩放操作,在Flutter中可以在GestureDetector来实现
/// 最小缩放比例
double _minAvailableZoom = 1.0;
/// 最大缩放比例
double _maxAvailableZoom = 1.0;
/// 记录当前的缩放比例
double _currentScale = 1.0;
/// 当前的基础值
double _baseScale = 1.0;
Listener(
onPointerDown: (_) => _pointers++,
onPointerUp: (_) => _pointers--, // 用来判断是否双指
child: CameraPreview(
_controller,
// Creates a widget that defers its building until layout.
// 布局完成再创建
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return GestureDetector(
// Opaque targets can be hit by hit tests, causing them to both receive events within their bounds and prevent targets visually behind them from also receiving events.
// 相机Widget能收到手势
behavior: HitTestBehavior.opaque,
// 设置开始缩放事件
onScaleStart: _handleScaleStart,
// 设置缩放值变化事件