使用 AVCaptureSession 以编程方式捕获最大分辨率图像
Programmatically Capture Maximum Resolution Image using AVCaptureSession
我的目标是使用 AVCaptureSession
以编程方式锁定焦点,拍摄一张图像,激活闪光灯,然后在延迟一段时间后拍摄第二张图像。
我已经设法使用 AVCaptureSession
实例和 AVCaptureStillImageOutput
进行捕获。但是,我调用 captureStillImageAsynchronouslyFromConnection(_:completionHandler:)
时得到的图像是 1920 x 1080,而不是我的 iPhone 6S 相机能够拍摄的完整 12 兆像素图像。
这是我的捕获函数:
func captureImageFromStream(completion: (result: UIImage) -> Void)
{
if let stillOutput = self.stillImageOutput {
var videoConnection : AVCaptureConnection?
for connection in stillOutput.connections {
for port in connection.inputPorts! {
if port.mediaType == AVMediaTypeVideo {
videoConnection = connection as? AVCaptureConnection
break
}
}
if videoConnection != nil {
break
}
}
if videoConnection != nil {
stillOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) {
(imageDataSampleBuffer, error) -> Void in
if error == nil {
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)
if let image = UIImage(data: imageData) {
completion(result: image)
}
}
else {
NSLog("ImageCapture Error: \(error)")
}
}
}
}
}
我应该做哪些修改才能捕捉到我正在寻找的图像?我是 Swift 的新手,所以请原谅我犯的任何初学者错误。
在 addOutput
stillImageOutput
和 startRunning
之前,您需要将拍摄会话预设设置为照片:
captureSession.sessionPreset = AVCaptureSessionPresetPhoto
我的目标是使用 AVCaptureSession
以编程方式锁定焦点,拍摄一张图像,激活闪光灯,然后在延迟一段时间后拍摄第二张图像。
我已经设法使用 AVCaptureSession
实例和 AVCaptureStillImageOutput
进行捕获。但是,我调用 captureStillImageAsynchronouslyFromConnection(_:completionHandler:)
时得到的图像是 1920 x 1080,而不是我的 iPhone 6S 相机能够拍摄的完整 12 兆像素图像。
这是我的捕获函数:
func captureImageFromStream(completion: (result: UIImage) -> Void)
{
if let stillOutput = self.stillImageOutput {
var videoConnection : AVCaptureConnection?
for connection in stillOutput.connections {
for port in connection.inputPorts! {
if port.mediaType == AVMediaTypeVideo {
videoConnection = connection as? AVCaptureConnection
break
}
}
if videoConnection != nil {
break
}
}
if videoConnection != nil {
stillOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) {
(imageDataSampleBuffer, error) -> Void in
if error == nil {
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)
if let image = UIImage(data: imageData) {
completion(result: image)
}
}
else {
NSLog("ImageCapture Error: \(error)")
}
}
}
}
}
我应该做哪些修改才能捕捉到我正在寻找的图像?我是 Swift 的新手,所以请原谅我犯的任何初学者错误。
在 addOutput
stillImageOutput
和 startRunning
之前,您需要将拍摄会话预设设置为照片:
captureSession.sessionPreset = AVCaptureSessionPresetPhoto