先週くらいからswiftの勉強をしています。
カメラを起動して撮影し、画像を保存するところまではできたのですが、
これにevernoteのカメラのように台形補正(斜めに写しても四角形を補正してくれる機能)
や四角形を感知して自動的に撮影する機能を追加したいと考えております。
しかし、現時点ではどのように実装していったらいいのか方向性も全く見えない状況です・・・
もしサンプルコードなどをお持ちの方、このサイトを参考にするといいよなどの情報を
お持ちの方がいれば是非アドバイス願います。
ちなみに現段階でのコードは下記のようになっております。
サンプルをコピーして、エラーが出る箇所を修正したコードになっております。
swift
1// 2// ViewController.swift 3// AVFoundation002 4// 5 6import UIKit 7import AVFoundation 8 9class ViewController: UIViewController { 10 11 // セッション. 12 var mySession : AVCaptureSession! 13 // デバイス. 14 var myDevice : AVCaptureDevice! 15 // 画像のアウトプット. 16 var myImageOutput : AVCaptureStillImageOutput! 17 18 19 override func viewDidLoad() { 20 super.viewDidLoad() 21 22 // セッションの作成. 23 mySession = AVCaptureSession() 24 25 // デバイス一覧の取得. 26 let devices = AVCaptureDevice.devices() 27 28 // バックカメラをmyDeviceに格納. 29 for device in devices{ 30 if(device.position == AVCaptureDevicePosition.Back){ 31 myDevice = device as! AVCaptureDevice 32 } 33 } 34 35 // バックカメラからVideoInputを取得. 36 mySession.addInput(try! AVCaptureDeviceInput(device: myDevice)) 37 38 // 出力先を生成. 39 myImageOutput = AVCaptureStillImageOutput() 40 41 // セッションに追加. 42 mySession.addOutput(myImageOutput) 43 44 // 画像を表示するレイヤーを生成. 45 let myVideoLayer : AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer(session: mySession) 46 myVideoLayer.frame = self.view.bounds 47 myVideoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill 48 49 // Viewに追加. 50 self.view.layer.addSublayer(myVideoLayer) 51 52 // セッション開始. 53 mySession.startRunning() 54 55 // UIボタンを作成. 56 let myButton = UIButton(frame: CGRectMake(0,0,120,50)) 57 myButton.backgroundColor = UIColor.redColor(); 58 myButton.layer.masksToBounds = true 59 myButton.setTitle("撮影", forState: .Normal) 60 myButton.layer.cornerRadius = 20.0 61 myButton.layer.position = CGPoint(x: self.view.bounds.width/2, y:self.view.bounds.height-50) 62 myButton.addTarget(self, action: "onClickMyButton:", forControlEvents: .TouchUpInside) 63 64 // UIボタンをViewに追加. 65 self.view.addSubview(myButton); 66 67 } 68 69 // ボタンイベント. 70 func onClickMyButton(sender: UIButton){ 71 72 // ビデオ出力に接続. 73 let myVideoConnection = myImageOutput.connectionWithMediaType(AVMediaTypeVideo) 74 75 // 接続から画像を取得. 76 self.myImageOutput.captureStillImageAsynchronouslyFromConnection(myVideoConnection, completionHandler: { (imageDataBuffer, error) -> Void in 77 78 // 取得したImageのDataBufferをJpegに変換. 79 let myImageData : NSData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataBuffer) 80 81 // JpegからUIIMageを作成. 82 let myImage : UIImage = UIImage(data: myImageData)! 83 84 // アルバムに追加. 85 UIImageWriteToSavedPhotosAlbum(myImage, self, nil, nil) 86 87 }) 88 } 89}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/08/23 13:34
2015/08/23 14:31 編集
2015/08/24 00:48