问题
前面写了拍照、扫码、录视频的功能,前面要求功能实现即可,后面发现画质不够
思路
可能原因分析:
1.AVCaptureSession设置输出格式会影响画质清晰度
2.拍照中并未做聚焦/曝光处理, 或 聚焦/曝光设置参数导致模糊
解决
针对原因1,罗列如下sessionPreset对应的像素(height *width):
AVCaptureSessionPresetHigh???????????????? 1920*1080
AVCaptureSessionPresetMedium? ? ? ????? 480*360
AVCaptureSessionPresetLow? ? ? ? ? ? ????? 192*144
AVCaptureSessionPresetPhoto???????????????? 4032*3024
AVCaptureSessionPreset352x288? ? ? ? ???? 352*288
AVCaptureSessionPreset640x480? ? ? ? ???? 640*480
AVCaptureSessionPreset1280x720? ? ? ? ? 1280*720
AVCaptureSessionPreset1920x1080? ? ? ? ? 1920*1080
AVCaptureSessionPreset3840x2160? ? ? ? ? 3840*2160
AVCaptureSessionPresetiFrame960x540? ? 960*540
AVCaptureSessionPresetiFrame1280x720 1280*720
AVCaptureSessionPresetInputPriority? ? ? ? ?1920*1080
按需来设置该参数
针对原因2,聚焦/曝光设置:
/* 必须先设置聚焦位置,再设定聚焦方式 */
CGRect rectLayer = self.preView.frame;
CGPoint ccenter = CGPointMake((rectLayer.origin.x + rectLayer.size.width) / 2, (rectLayer.origin.y + rectLayer.size.height) / 2);
CGPoint cpoint = [self.preView captureDevicePointForPoint:ccenter];
AVCaptureDevice *devices= [self.dInput device];
[self.session beginConfiguration];
[devices lockForConfiguration:nil];
// 设置聚焦点的位置
if ([devices isFocusPointOfInterestSupported]) {
[devices setFocusPointOfInterest:cpoint];
}
// 设置聚焦模式
if ([devices isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]) {
[devices setFocusMode:AVCaptureFocusModeContinuousAutoFocus];
}
// 设置曝光点的位置
if ([devices isExposurePointOfInterestSupported]) {
[devices setExposurePointOfInterest:cpoint];
}
// 设置曝光模式
if ([devices isExposureModeSupported:AVCaptureExposureModeContinuousAutoExposure]) {
[devices setExposureMode:AVCaptureExposureModeContinuousAutoExposure];
}
[device unlockForConfiguration];
[self.session commitConfiguration];
其中聚焦模式 跟曝光模式的设置很有意思:
typedef NS_ENUM(NSInteger, AVCaptureFocusMode) {
// 将焦点锁定在镜头的当前位置
AVCaptureFocusModeLocked = 0,
// 设备应自动对焦一次,然后将对焦模式更改为AVCaptureFocusModeLocked
AVCaptureFocusModeAutoFocus = 1,
// 设备应在需要时自动对焦
AVCaptureFocusModeContinuousAutoFocus = 2,
}
typedef NS_ENUM(NSInteger, AVCaptureExposureMode) {
// 将曝光锁定在其当前值
AVCaptureExposureModeLocked = 0,
// 自动调整一次曝光,然后将曝光模式更改为AVCaptureExposureModeLocked
AVCaptureExposureModeAutoExpose = 1,
// 在需要时自动调整曝光
AVCaptureExposureModeContinuousAutoExposure = 2,
// 仅根据用户提供的ISO,DurationDuration值来调整曝光
AVCaptureExposureModeCustom API_AVAILABLE(macos(10.15), ios(8.0)) = 3,
}
当我设置聚焦模式 = AVCaptureFocusModeAutoFocus,曝光模式 = AVCaptureExposureModeAutoExpose,会有聚焦的过程,但跟系统拍照有差距。
调试下参数分别为ContinuousAuto模式后,画质清晰度跟系统拍照差不多了。