2014年12月26日 星期五

Manual Camera Controls in iOS 8 With Swift: Focus

since: 2014/12/26
update: 2014/12/26
reference:
1. Taking control of the iPhone camera in iOS 8 with Swift (Part 1)

A. 前言:
     AVFoundation API 可以使用所有新增到 iOS 8 的 camera 細密控制功能,
利用建立好的 capture session 來製作一個 app, 包含手動控制: 對焦, 曝光ISO.    

    首先, 我們只需要建立一個基本的 camera preview. 這篇文章結束後, 我們就會有
一個俏皮的方式來控制對焦. 準備好了嗎? 讓我們開始吧......

-----------------------------------------------------------------------------------------------

B. 新增專案:
     1. Xcode > File > New > Project...
         iOS > Application > Single View Application
         > Next

     2. Choose options for your new project:
         Product Name: CameraPi
         Organization Identifier: com.blogspot
         Language: Swift
         Device:
Universal
         > Next > Create


-----------------------------------------------------------------------------------------------

C. 查詢 iPhone 上有哪些影音擷取設備:
1. 開啟 ViewController.swift 檔案, 修改如下:
import UIKit
//@add

import AVFoundation

class ViewController: UIViewController {
   
    //@add: create a AVCaptureSession object
    let captureSession = AVCaptureSession()
    var previewLayer : AVCaptureVideoPreviewLayer?

   
    // If we find a device we'll store it here for later use
    var captureDevice : AVCaptureDevice?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        //@add: set our quality settings
        captureSession.sessionPreset = AVCaptureSessionPresetHigh
       
        //@add: find a device (to record from).
        let devices = AVCaptureDevice.devices()
        println(devices)
    }

....

}

2. 執行後:
    可以看到在 iPhone 5 上, 除了前置鏡頭, 後置鏡頭外, 還有一個麥克風.
    我們的目的是要取得後置鏡頭.

-----------------------------------------------------------------------------------------------

D. 呈現相機的預覽模式:
1. 開啟 ViewController.swift 檔案, 修改如下:
....
class ViewController: UIViewController {
....
    override func viewDidLoad() {
....      
        //@add: find a device (to record from).
        let devices = AVCaptureDevice.devices()

       
        //@add: Loop through all the capture devices on this phone
        for device in devices {
            // Make sure this particular device supports video
            if (device.hasMediaType(AVMediaTypeVideo)) {
                // Finally check the position and confirm we've got the back camera
                if(device.position == AVCaptureDevicePosition.Back) {
                    captureDevice = device as? AVCaptureDevice
                   
                    if captureDevice != nil {
                        println("Capture device found")
                        beginSession()
                    }
                }
            }
        }

        //println(devices)
    }
   
    //@add
    func beginSession() {
             
        var err : NSError? = nil
        captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: &err))
       
        if err != nil {
            println("error: \(err?.localizedDescription)")
        }
       
        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        self.view.layer.addSublayer(previewLayer)
        previewLayer?.frame = self.view.layer.frame
        captureSession.startRunning()
    }

....
}

2. 執行後:
    可以看到標準的 iOS 相機預覽模式

-----------------------------------------------------------------------------------------------

E.相機設為固定對焦模式:
1. 開啟 ViewController.swift 檔案, 修改如下:
....
class ViewController: UIViewController {
....
    //@add

    func configureDevice() {
        if let device = captureDevice {
            // locks the device
            device.lockForConfiguration(nil)
           
            // sets the focus to locked
            device.focusMode = .Locked
           
            // unlocks the device
            device.unlockForConfiguration()
        }
    }

   
    //@add
    func beginSession() {

         //@add
        configureDevice()
        ....
    }
....
}


2. 執行後:
    相機對焦固定, 已經無法自動對焦. 這代表我們可以自行控制對焦的距離.

-----------------------------------------------------------------------------------------------

F.相機設為手動對焦模式

1. 開啟 ViewController.swift 檔案, 修改如下:
....
class ViewController: UIViewController {
....  
    //@add

    func focusTo(value : Float) {
        // validate that the device exists
        if let device = captureDevice {
           
            // lock the device & If the lock is successful
            if(device.lockForConfiguration(nil)) {
                // tell the lens to focus on the point ‘value’
                device.setFocusModeLockedWithLensPosition(value, completionHandler: { (time) -> Void in
                    //
                })
                device.unlockForConfiguration()
            }
        }
    }

   
    // Gets a value from 0.0 to 1.0 based on how far you are touching on the screen horizontally
    //@add

    let screenWidth = UIScreen.mainScreen().bounds.size.width

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        var anyTouch = touches.anyObject() as UITouch
        var touchPercent = anyTouch.locationInView(self.view).x / screenWidth
        focusTo(Float(touchPercent))
    }

   
    //@add
    override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
        var anyTouch = touches.anyObject() as UITouch
        var touchPercent = anyTouch.locationInView(self.view).x / screenWidth
        focusTo(Float(touchPercent))
    }

   ....
}


2. 執行後:
    用手指在 iPhone 螢幕上, 從左往右滑, 就可以手動控制對焦了.

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。