用AVFoundation自訂相機拍照,

來源:互聯網
上載者:User

用AVFoundation自訂相機拍照,

自訂拍照或者錄視頻的功能,就需要用到AVFoundation架構,目前我只用到了拍照,所以記錄下自訂拍照用法,視頻用法等用上了再補充,應該是大同小異demo在這裡:https://github.com/Phelthas/TEST_XMLCommon  以拍照過程為例,實現主要包括以下幾個部分:1,首先要判斷使用者授權:let authorizationStatus = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo)switch authorizationStatus {
        case.NotDetermined:
            AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (granted:  Bool) -> Void in
                if granted {
                    self.configureCamera()
                } else {
                    self.showErrorAlertView()
                }
            })
        case.Authorized:
            self.configureCamera()
        default:
            self.showErrorAlertView()        } 2,AVCaptureInput     AVCaptureInput是抽象出來拍照過程的輸入端,這個可以是自拍,後置網路攝影機或者麥克風     需要通過 public init(device: AVCaptureDevice!) throws這個方法來初始化獲得而AVCaptureDevice 是蘋果定義的擷取裝置的抽象,用 public class func devicesWithMediaType(mediaType: String!) -> [AnyObject]! 方法來獲得。這個mediaType: String 又是定義在 AVMediaFormat.h.中的字串,這裡用到的是 AVMediaTypeVideo 3,AVCaptureOutputAVCaptureOutput是抽象出來的拍照過程的輸出端,可以是圖片,視頻,音頻等,主要有:AVCaptureAudioDataOutput、AVCaptureAudioPreviewOutput、AVCaptureFileOutput、AVCaptureStillImageOutput、AVCaptureVideoDataOutput、AVCaptureAudioFileOutput、AVCaptureMovieFileOutput其中AVCaptureFileOutput依然代表輸出到檔案的輸出端,這裡用到的是AVCaptureStillImageOutput 4,AVCaptureSessionAVCaptureSession就是抽象出來的拍照過程,它需要一個輸入:AVCaptureInput,一個輸出:AVCaptureOutput, if session.canAddInput(cameraInput) {                session.addInput(cameraInput) }  if session.canAddInput(cameraInput) {                session.addInput(cameraInput) }  然後調用session的startRunning方法,就開始捕獲映像資訊了,一般這裡會用到AVCaptureVideoPreviewLayer,用來做映像的預覽:用session初始化previewLayerpreviewLayer = AVCaptureVideoPreviewLayer(session: self.session)然後設定layer的frame,把該layer添加到你想要顯示預覽的地方就可以了 5,在調用session的startRunning之前,還可以對相機進行聚焦,白平衡等設定          if let tempDevice = self.currentCameraDevice {                try tempDevice.lockForConfiguration()
                if tempDevice.hasFlash {
                    tempDevice.flashMode = self.flashMode
                }
                if tempDevice.isFocusModeSupported(.AutoFocus) {
                    tempDevice.focusMode = .AutoFocus
                }
                if tempDevice.isWhiteBalanceModeSupported(.AutoWhiteBalance) {
                    tempDevice.whiteBalanceMode = .AutoWhiteBalance
                }
                tempDevice.unlockForConfiguration()
                           } 6,擷取圖片到指定裝置就是調用stillImageOutput的方法來擷取圖片,方法如下let connection = self.stillImageOutput.connectionWithMediaType(AVMediaTypeVideo)
        self.stillImageOutput.captureStillImageAsynchronouslyFromConnection(connection) { [unowned self] (imageDataSampleBuffer: CMSampleBuffer!, error) -> Void in            if error == nil {                // 如果使用 session .Photo 預設,或者在裝置輸出設定中明確進行了設定,就能獲得已經壓縮為JPEG的資料                let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)                              // 樣本緩衝區也包含中繼資料               //let metadata:NSDictionary = CMCopyDictionaryOfAttachments(nil, imageDataSampleBuffer, CMAttachmentMode(kCMAttachmentMode_ShouldPropagate))!                if let image = UIImage(data: imageData) {                    //這個image就是擷取到的拍照獲得的image,這裡可以儲存到相簿,或者修改,或者想幹什麼幹什麼                    //注意這個image還是網路攝影機拍下來時的解析度,並不是你設定的layer大小的,如果還需要剪裁,就剪裁之後在儲存                }
            } else {
                DLog("error while capturing still image: \(error)")
            }
                   }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.