質問するログイン新規登録

質問編集履歴

1

カメラのコードを追加しました。

2019/01/02 08:52

投稿

xinxin
xinxin

スコア16

title CHANGED
File without changes
body CHANGED
@@ -6,7 +6,137 @@
6
6
  それができるためにはどんな関数を使い、どこに挿入すればいいでしょうか。できれば参考でくるサイトをご掲示してくだされば嬉しいです。
7
7
  お願いします。
8
8
 
9
+ カメラのViewControllerのコードは以下になります。
10
+ ```swift
11
+ import UIKit
12
+ import AVFoundation
9
13
 
14
+ class SecondViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate{
15
+ // デバイスからの入力と出力を管理するオブジェクトの作成
16
+ var captureSession = AVCaptureSession()
17
+ // カメラデバイスそのものを管理するオブジェクトの作成
18
+ // メインカメラの管理オブジェクトの作成
19
+ var mainCamera: AVCaptureDevice?
20
+ // インカメの管理オブジェクトの作成
21
+ var innerCamera: AVCaptureDevice?
22
+ // 現在使用しているカメラデバイスの管理オブジェクトの作成
23
+ var currentDevice: AVCaptureDevice?
24
+ // キャプチャーの出力データを受け付けるオブジェクト
25
+ var photoOutput : AVCapturePhotoOutput?
26
+ // プレビュー表示用のレイヤ
27
+ var cameraPreviewLayer : AVCaptureVideoPreviewLayer?
28
+
29
+ // シャッターボタン
30
+ @IBOutlet weak var cameraButton: UIButton!
10
31
 
32
+ override func viewDidLoad() {
33
+ super.viewDidLoad()
34
+ setupCaptureSession()
35
+ setupDevice()
36
+ setupInputOutput()
37
+ setupPreviewLayer()
38
+ captureSession.startRunning()
39
+ styleCaptureButton()
40
+ // Do any additional setup after loading the view, typically from a nib.
41
+ }
42
+
43
+ override func didReceiveMemoryWarning() {
44
+ super.didReceiveMemoryWarning()
45
+ // Dispose of any resources that can be recreated.
46
+ }
47
+
48
+ /// 撮影ボタン押下時に呼ばれる
49
+ @IBAction func cameraButton_TouchUpInside(_ sender: Any) {
50
+
51
+ let settings = AVCapturePhotoSettings()
52
+ // フラッシュの設定
53
+ settings.flashMode = .auto
54
+ // カメラの手ぶれ補正
55
+ settings.isAutoStillImageStabilizationEnabled = true
56
+ // 撮影された画像をdelegateメソッドで処理
57
+ self.photoOutput?.capturePhoto(with: settings, delegate: self as! AVCapturePhotoCaptureDelegate)
58
+ }
59
+ }
60
+
61
+ //MARK: AVCapturePhotoCaptureDelegateデリゲートメソッド
62
+ extension SecondViewController: AVCapturePhotoCaptureDelegate{
63
+ // 撮影した画像データが生成されたときに呼び出されるデリゲートメソッド
64
+ func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
65
+ if let imageData = photo.fileDataRepresentation() {
66
+ // Data型をUIImageオブジェクトに変換
67
+ let uiImage = UIImage(data: imageData)
68
+ // 写真ライブラリに画像を保存
69
+ UIImageWriteToSavedPhotosAlbum(uiImage!, nil,nil,nil)
70
+ }
71
+ }
72
+ }
73
+
74
+ //MARK: カメラ設定メソッド
75
+ extension SecondViewController{
76
+ // カメラの画質の設定
77
+ func setupCaptureSession() {
78
+ captureSession.sessionPreset = AVCaptureSession.Preset.photo
79
+ }
80
+
81
+ // デバイスの設定
82
+ func setupDevice() {
83
+ // カメラデバイスのプロパティ設定
84
+ let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera], mediaType: AVMediaType.video, position: AVCaptureDevice.Position.unspecified)
85
+ // プロパティの条件を満たしたカメラデバイスの取得
86
+ let devices = deviceDiscoverySession.devices
87
+
88
+ for device in devices {
89
+ if device.position == AVCaptureDevice.Position.back {
90
+ mainCamera = device
91
+ } else if device.position == AVCaptureDevice.Position.front {
92
+ innerCamera = device
93
+ }
94
+ }
95
+ // 起動時のカメラを設定
96
+ currentDevice = mainCamera
97
+ }
98
+
99
+ // 入出力データの設定
100
+ func setupInputOutput() {
101
+ do {
102
+ // 指定したデバイスを使用するために入力を初期化
103
+ let captureDeviceInput = try AVCaptureDeviceInput(device: currentDevice!)
104
+ // 指定した入力をセッションに追加
105
+ captureSession.addInput(captureDeviceInput)
106
+ // 出力データを受け取るオブジェクトの作成
107
+ photoOutput = AVCapturePhotoOutput()
108
+ // 出力ファイルのフォーマットを指定
109
+ photoOutput!.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format: [AVVideoCodecKey : AVVideoCodecType.jpeg])], completionHandler: nil)
110
+ captureSession.addOutput(photoOutput!)
111
+ } catch {
112
+ print(error)
113
+ }
114
+ }
115
+
116
+ // カメラのプレビューを表示するレイヤの設定
117
+ func setupPreviewLayer() {
118
+ // 指定したAVCaptureSessionでプレビューレイヤを初期化
119
+ self.cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
120
+ // プレビューレイヤが、カメラのキャプチャーを縦横比を維持した状態で、表示するように設定
121
+ self.cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
122
+ // プレビューレイヤの表示の向きを設定
123
+ self.cameraPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
124
+
125
+ self.cameraPreviewLayer?.frame = view.frame
126
+
127
+ self.view.layer.insertSublayer(self.cameraPreviewLayer!, at: 0)
128
+ }
129
+ // ボタンのスタイルを設定
130
+ func styleCaptureButton() {
131
+ cameraButton.layer.borderColor = UIColor.white.cgColor
132
+ cameraButton.layer.borderWidth = 5
133
+
134
+ cameraButton.clipsToBounds = true
135
+ cameraButton.layer.cornerRadius = min(cameraButton.frame.width, cameraButton.frame.height) / 2
136
+ }
137
+ }
138
+
139
+ ```
140
+
11
141
  ちなみにカメラのコードは以下のサイトを参考にしました。
12
142
  [リンク内容](https://qiita.com/t_okkan/items/b2dd11426eab107c5d15)