use*_*679 5 camera avfoundation ios swift
我使用各种主题和灯光进行了许多测试。每次测试都显示标准的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)
}
Run Code Online (Sandbox Code Playgroud)
请试试!您也会看到差异...
我注意到你所描述的内容并查看你的代码,我没有看到你实现:
backCamera.focusPointOfInterest = focusPoint
backCamera.focusMode = AVCaptureFocusMode.autoFocus
backCamera.exposureMode = AVCaptureExposureMode.autoExpose
Run Code Online (Sandbox Code Playgroud)
我在内部实现了这一点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()
}
}
Run Code Online (Sandbox Code Playgroud)
当相机显示暗图像时,用户只需点击屏幕,它就会调整曝光和焦距,使图片变亮。
| 归档时间: |
|
| 查看次数: |
934 次 |
| 最近记录: |