质量:自定义 AVFoundation 相机应用程序 VS。 iOS 标准相机应用
Quality: Custom AVFoundation Camera App VS. iOS standard Camera App
我使用各种主题和灯光进行了多项测试。每个测试都显示标准 iOS 相机应用程序质量明显优于我自定义的基于 AVFoundation 的应用程序(颜色未褪色、更好的对焦、更好的照明、更少的颗粒感)。我无法解释 巨大的 差异。下面是使用这两种方法(使用前置摄像头)拍摄的视频的屏幕截图示例。
iOS 标准相机应用
自定义 AVFoundation 录制的视频
自定义实现代码:
let chosenCameraType = AVCaptureDevicePosition.Front
//get camera
let devices = AVCaptureDevice.devices()
for device in devices
{
if (!device.hasMediaType(AVMediaTypeVideo))
{
continue
}
if (device.position != chosenCameraType)
{
continue
}
camera = (device as? AVCaptureDevice)!
}
do
{
captureSession = AVCaptureSession()
captureSession!.sessionPreset = AVCaptureSessionPresetHigh
let video = try AVCaptureDeviceInput(device: camera) as AVCaptureDeviceInput
captureSession!.addInput(video)
let audio = try AVCaptureDeviceInput(device: AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeAudio)) as AVCaptureDeviceInput
captureSession!.addInput(audio)
fileOutput = AVCaptureMovieFileOutput()
captureSession?.addOutput(fileOutput)
captureSession!.startRunning()
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
let name = String(UInt64(NSDate().timeIntervalSince1970 * 1000))
fileOutput?.startRecordingToOutputFileURL(NSURL(fileURLWithPath: "\(documentsPath)/" + name + ".mov"), recordingDelegate: self)
}
catch let error as NSError
{
print(error)
}
请尝试!您也会看到不同之处...
我注意到你在描述什么并查看了你的代码我没有看到你实现:
backCamera.focusPointOfInterest = focusPoint
backCamera.focusMode = AVCaptureFocusMode.autoFocus
backCamera.exposureMode = AVCaptureExposureMode.autoExpose
我在 touchesBegan
中实现了这个,这样相机就会聚焦在用户触摸屏幕的区域。这是那段代码:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touchPoint = touches.first
let x = (touchPoint?.location(in: self.view).x)! / self.view.bounds.width
let y = (touchPoint?.location(in: self.view).y)! / self.view.bounds.height
let realX = (touchPoint?.location(in: self.view).x)!
let realY = (touchPoint?.location(in: self.view).y)!
let focusPoint = CGPoint(x: x, y: y)
let k = DrawSquare(frame: CGRect(
origin: CGPoint(x: realX - 75, y: realY - 75),
size: CGSize(width: 150, height: 150)))
if backCamera != nil {
do {
try backCamera.lockForConfiguration()
self.previewView.addSubview(k)
}
catch {
print("Can't lock back camera for configuration")
}
if backCamera.isFocusPointOfInterestSupported {
backCamera.focusPointOfInterest = focusPoint
}
if backCamera.isFocusModeSupported(AVCaptureDevice.FocusMode.autoFocus) {
backCamera.focusMode = AVCaptureDevice.FocusMode.autoFocus
}
if backCamera.isExposureModeSupported(AVCaptureDevice.ExposureMode.autoExpose) {
backCamera.exposureMode = AVCaptureDevice.ExposureMode.autoExpose
}
backCamera.unlockForConfiguration()
}
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(500)) {
k.removeFromSuperview()
}
}
当相机显示较暗的图像时,用户只需点击屏幕即可调整曝光和对焦,从而使画面变亮。
我使用各种主题和灯光进行了多项测试。每个测试都显示标准 iOS 相机应用程序质量明显优于我自定义的基于 AVFoundation 的应用程序(颜色未褪色、更好的对焦、更好的照明、更少的颗粒感)。我无法解释 巨大的 差异。下面是使用这两种方法(使用前置摄像头)拍摄的视频的屏幕截图示例。
iOS 标准相机应用
自定义 AVFoundation 录制的视频
自定义实现代码:
let chosenCameraType = AVCaptureDevicePosition.Front
//get camera
let devices = AVCaptureDevice.devices()
for device in devices
{
if (!device.hasMediaType(AVMediaTypeVideo))
{
continue
}
if (device.position != chosenCameraType)
{
continue
}
camera = (device as? AVCaptureDevice)!
}
do
{
captureSession = AVCaptureSession()
captureSession!.sessionPreset = AVCaptureSessionPresetHigh
let video = try AVCaptureDeviceInput(device: camera) as AVCaptureDeviceInput
captureSession!.addInput(video)
let audio = try AVCaptureDeviceInput(device: AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeAudio)) as AVCaptureDeviceInput
captureSession!.addInput(audio)
fileOutput = AVCaptureMovieFileOutput()
captureSession?.addOutput(fileOutput)
captureSession!.startRunning()
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
let name = String(UInt64(NSDate().timeIntervalSince1970 * 1000))
fileOutput?.startRecordingToOutputFileURL(NSURL(fileURLWithPath: "\(documentsPath)/" + name + ".mov"), recordingDelegate: self)
}
catch let error as NSError
{
print(error)
}
请尝试!您也会看到不同之处...
我注意到你在描述什么并查看了你的代码我没有看到你实现:
backCamera.focusPointOfInterest = focusPoint
backCamera.focusMode = AVCaptureFocusMode.autoFocus
backCamera.exposureMode = AVCaptureExposureMode.autoExpose
我在 touchesBegan
中实现了这个,这样相机就会聚焦在用户触摸屏幕的区域。这是那段代码:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touchPoint = touches.first
let x = (touchPoint?.location(in: self.view).x)! / self.view.bounds.width
let y = (touchPoint?.location(in: self.view).y)! / self.view.bounds.height
let realX = (touchPoint?.location(in: self.view).x)!
let realY = (touchPoint?.location(in: self.view).y)!
let focusPoint = CGPoint(x: x, y: y)
let k = DrawSquare(frame: CGRect(
origin: CGPoint(x: realX - 75, y: realY - 75),
size: CGSize(width: 150, height: 150)))
if backCamera != nil {
do {
try backCamera.lockForConfiguration()
self.previewView.addSubview(k)
}
catch {
print("Can't lock back camera for configuration")
}
if backCamera.isFocusPointOfInterestSupported {
backCamera.focusPointOfInterest = focusPoint
}
if backCamera.isFocusModeSupported(AVCaptureDevice.FocusMode.autoFocus) {
backCamera.focusMode = AVCaptureDevice.FocusMode.autoFocus
}
if backCamera.isExposureModeSupported(AVCaptureDevice.ExposureMode.autoExpose) {
backCamera.exposureMode = AVCaptureDevice.ExposureMode.autoExpose
}
backCamera.unlockForConfiguration()
}
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(500)) {
k.removeFromSuperview()
}
}
当相机显示较暗的图像时,用户只需点击屏幕即可调整曝光和对焦,从而使画面变亮。