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 了.

沒有留言:

張貼留言

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