2014年12月27日 星期六

Manual Camera Controls in iOS 8 With Swift: ISO

since: 2014/12/27
update: 2014/12/27
reference:
1. Taking control of the iPhone camera in iOS 8 with Swift (Part 1) 
2. Taking control of the iPhone camera in iOS 8 with Swift (Part 2)
3. Manual Camera Controls in iOS 8 With Swift: Focus

A. 前言:

    延續上篇 Manual Camera Controls in iOS 8 With Swift: Focus 的專案.
在這篇文章裡, 將會對 API 有更深一點的探究, 並且利用所建立的第二個軸(Y)
的垂直觸控位置, 來控制影像的 ISO.

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

B. 記錄觸控時的水平與垂直方向位置遠近百分比:
    1. 水平方向: 螢幕總寬百分比; 垂直方向: 螢幕總高百分比
        例如:
        觸控點在螢幕的最右上方之值為: (x, y) = (1, 1)
        觸控點在螢幕的最左下方之值為: (x, y) = (0, 0)
        觸控點在螢幕的正中央之值為: (x, y) = (0.5, 0.5)

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

    // take the touched point and divide by the total number of points for either the width or height of the screen.
    func touchPercent(touch : UITouch) -> CGPoint {
        // Get the dimensions of the screen in points
        let screenSize = UIScreen.mainScreen().bounds.size
       
        // Create an empty CGPoint object set to 0, 0
        var touchPer = CGPointZero
       
        // Set the x and y values to be the value of the tapped position, divided by the width/height of the screen
        touchPer.x = touch.locationInView(self.view).x / screenSize.width
        touchPer.y = touch.locationInView(self.view).y / screenSize.height
       
        // Return the populated CGPoint
        return touchPer
    }

      
    /*
    // 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))
    }

    */

    // we need to adjust the touchesBegan and touchesMoved method to use our new function.
    //@@add
    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        let touchPer = touchPercent( touches.anyObject() as UITouch )
        focusTo(Float(touchPer.x))
    }
   
    //@@add
    override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
        let touchPer = touchPercent( touches.anyObject() as UITouch )
        focusTo(Float(touchPer.x))
    }

....
}


    3. 執行後:
        將原本水平方向觸控的手動控制對焦, 調整新函式後, 功能不變.

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

C. 手動控制 ISO 值大小:


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

  
    //@@add
    func updateDeviceSettings(focusValue : Float, isoValue : Float) {
        if let device = captureDevice {
           
            // normal locking and setting of the focus
            if(device.lockForConfiguration(nil)) {
                device.setFocusModeLockedWithLensPosition(focusValue, completionHandler: { (time) -> Void in
                    //
                })
               
                // Adjust the iso to clamp between minIso and maxIso based on the active format
                let minISO = device.activeFormat.minISO
                let maxISO = device.activeFormat.maxISO
                let clampedISO = isoValue * (maxISO - minISO) + minISO
               
                device.setExposureModeCustomWithDuration(AVCaptureExposureDurationCurrent, ISO: clampedISO, completionHandler: { (time) -> Void in
                    //
                })
               
                device.unlockForConfiguration()
            }
        }
    }

   
....
    // we need to adjust the touchesBegan and touchesMoved method to use our new function.
    //@@add
    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        let touchPer = touchPercent( touches.anyObject() as UITouch )
        //focusTo(Float(touchPer.x))
        updateDeviceSettings(Float(touchPer.x), isoValue: Float(touchPer.y))
    }
   
    //@@add
    override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
        let touchPer = touchPercent( touches.anyObject() as UITouch )
        //focusTo(Float(touchPer.x))
        updateDeviceSettings(Float(touchPer.x), isoValue: Float(touchPer.y))
    }
    ....
}


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

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 螢幕上, 從左往右滑, 就可以手動控制對焦了.