【问题标题】:Take photo after autofocusing automatically AVCapture自动对焦后拍照 AVCapture
【发布时间】:2018-09-20 15:21:32
【问题描述】:

我想这样做:当相机对焦时,应用程序会自动拍照。但我发现没有办法检测相机是否对焦。 我正在使用 AVFoundation 拍照 我是iOS新手,不知道如何编码 谁能帮我解决这个问题

【问题讨论】:

标签: ios swift4


【解决方案1】:
import UIKit
import AVFoundation
class ViewController: UIViewController {
    let captureSession = AVCaptureSession()
    let stillImageOutput = AVCaptureStillImageOutput()
    var error: NSError?
    override func viewDidLoad() {
        super.viewDidLoad()
        let devices = AVCaptureDevice.devices().filter{ $0.hasMediaType(AVMediaTypeVideo) && $0.position == AVCaptureDevicePosition.Back }
        if let captureDevice = devices.first as? AVCaptureDevice  {

            captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: &error))
            captureSession.sessionPreset = AVCaptureSessionPresetPhoto
            captureSession.startRunning()
            stillImageOutput.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]
            if captureSession.canAddOutput(stillImageOutput) {
                captureSession.addOutput(stillImageOutput)
            }
            if let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) {
                previewLayer.bounds = view.bounds
                previewLayer.position = CGPointMake(view.bounds.midX, view.bounds.midY)
                previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
                let cameraPreview = UIView(frame: CGRectMake(0.0, 0.0, view.bounds.size.width, view.bounds.size.height))
                cameraPreview.layer.addSublayer(previewLayer)
                cameraPreview.addGestureRecognizer(UITapGestureRecognizer(target: self, action:"saveToCamera:"))
//get instance of phone camera
    let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
     //try to enable auto focus
     if(captureDevice!.isFocusModeSupported(.continuousAutoFocus)) {
         try! captureDevice!.lockForConfiguration()
         captureDevice!.focusMode = .continuousAutoFocus
         captureDevice!.unlockForConfiguration()
        view.addSubview(cameraPreview)

     }
  }
        }
    }
    func saveToCamera(sender: UITapGestureRecognizer) {
        if let videoConnection = stillImageOutput.connectionWithMediaType(AVMediaTypeVideo) {
            stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) {
                (imageDataSampleBuffer, error) -> Void in
                let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)
                 UIImageWriteToSavedPhotosAlbum(UIImage(data: imageData), nil, nil, nil)
            }
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

【讨论】:

  • 我必须调用这个 photoOutput.capturePhoto(with: photoSettings, delegate: self)
  • 那是用来拍照片的,我给你的代码只是为了持续对焦
  • 我只想在相机自动对焦物体(例如信用卡)时自动拍照
  • 对焦完成后调用抓图函数
  • 如何知道焦点已经完成以及我必须在哪里调用捕获照片功能
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-30
相关资源
最近更新 更多