質問編集履歴

1

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

2019/01/02 08:52

投稿

xinxin
xinxin

スコア16

test CHANGED
File without changes
test CHANGED
@@ -14,7 +14,267 @@
14
14
 
15
15
 
16
16
 
17
-
17
+ カメラのViewControllerのコードは以下になります。
18
+
19
+ ```swift
20
+
21
+ import UIKit
22
+
23
+ import AVFoundation
24
+
25
+
26
+
27
+ class SecondViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate{
28
+
29
+ // デバイスからの入力と出力を管理するオブジェクトの作成
30
+
31
+ var captureSession = AVCaptureSession()
32
+
33
+ // カメラデバイスそのものを管理するオブジェクトの作成
34
+
35
+ // メインカメラの管理オブジェクトの作成
36
+
37
+ var mainCamera: AVCaptureDevice?
38
+
39
+ // インカメの管理オブジェクトの作成
40
+
41
+ var innerCamera: AVCaptureDevice?
42
+
43
+ // 現在使用しているカメラデバイスの管理オブジェクトの作成
44
+
45
+ var currentDevice: AVCaptureDevice?
46
+
47
+ // キャプチャーの出力データを受け付けるオブジェクト
48
+
49
+ var photoOutput : AVCapturePhotoOutput?
50
+
51
+ // プレビュー表示用のレイヤ
52
+
53
+ var cameraPreviewLayer : AVCaptureVideoPreviewLayer?
54
+
55
+
56
+
57
+ // シャッターボタン
58
+
59
+ @IBOutlet weak var cameraButton: UIButton!
60
+
61
+
62
+
63
+ override func viewDidLoad() {
64
+
65
+ super.viewDidLoad()
66
+
67
+ setupCaptureSession()
68
+
69
+ setupDevice()
70
+
71
+ setupInputOutput()
72
+
73
+ setupPreviewLayer()
74
+
75
+ captureSession.startRunning()
76
+
77
+ styleCaptureButton()
78
+
79
+ // Do any additional setup after loading the view, typically from a nib.
80
+
81
+ }
82
+
83
+
84
+
85
+ override func didReceiveMemoryWarning() {
86
+
87
+ super.didReceiveMemoryWarning()
88
+
89
+ // Dispose of any resources that can be recreated.
90
+
91
+ }
92
+
93
+
94
+
95
+ /// 撮影ボタン押下時に呼ばれる
96
+
97
+ @IBAction func cameraButton_TouchUpInside(_ sender: Any) {
98
+
99
+
100
+
101
+ let settings = AVCapturePhotoSettings()
102
+
103
+ // フラッシュの設定
104
+
105
+ settings.flashMode = .auto
106
+
107
+ // カメラの手ぶれ補正
108
+
109
+ settings.isAutoStillImageStabilizationEnabled = true
110
+
111
+ // 撮影された画像をdelegateメソッドで処理
112
+
113
+ self.photoOutput?.capturePhoto(with: settings, delegate: self as! AVCapturePhotoCaptureDelegate)
114
+
115
+ }
116
+
117
+ }
118
+
119
+
120
+
121
+ //MARK: AVCapturePhotoCaptureDelegateデリゲートメソッド
122
+
123
+ extension SecondViewController: AVCapturePhotoCaptureDelegate{
124
+
125
+ // 撮影した画像データが生成されたときに呼び出されるデリゲートメソッド
126
+
127
+ func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
128
+
129
+ if let imageData = photo.fileDataRepresentation() {
130
+
131
+ // Data型をUIImageオブジェクトに変換
132
+
133
+ let uiImage = UIImage(data: imageData)
134
+
135
+ // 写真ライブラリに画像を保存
136
+
137
+ UIImageWriteToSavedPhotosAlbum(uiImage!, nil,nil,nil)
138
+
139
+ }
140
+
141
+ }
142
+
143
+ }
144
+
145
+
146
+
147
+ //MARK: カメラ設定メソッド
148
+
149
+ extension SecondViewController{
150
+
151
+ // カメラの画質の設定
152
+
153
+ func setupCaptureSession() {
154
+
155
+ captureSession.sessionPreset = AVCaptureSession.Preset.photo
156
+
157
+ }
158
+
159
+
160
+
161
+ // デバイスの設定
162
+
163
+ func setupDevice() {
164
+
165
+ // カメラデバイスのプロパティ設定
166
+
167
+ let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera], mediaType: AVMediaType.video, position: AVCaptureDevice.Position.unspecified)
168
+
169
+ // プロパティの条件を満たしたカメラデバイスの取得
170
+
171
+ let devices = deviceDiscoverySession.devices
172
+
173
+
174
+
175
+ for device in devices {
176
+
177
+ if device.position == AVCaptureDevice.Position.back {
178
+
179
+ mainCamera = device
180
+
181
+ } else if device.position == AVCaptureDevice.Position.front {
182
+
183
+ innerCamera = device
184
+
185
+ }
186
+
187
+ }
188
+
189
+ // 起動時のカメラを設定
190
+
191
+ currentDevice = mainCamera
192
+
193
+ }
194
+
195
+
196
+
197
+ // 入出力データの設定
198
+
199
+ func setupInputOutput() {
200
+
201
+ do {
202
+
203
+ // 指定したデバイスを使用するために入力を初期化
204
+
205
+ let captureDeviceInput = try AVCaptureDeviceInput(device: currentDevice!)
206
+
207
+ // 指定した入力をセッションに追加
208
+
209
+ captureSession.addInput(captureDeviceInput)
210
+
211
+ // 出力データを受け取るオブジェクトの作成
212
+
213
+ photoOutput = AVCapturePhotoOutput()
214
+
215
+ // 出力ファイルのフォーマットを指定
216
+
217
+ photoOutput!.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format: [AVVideoCodecKey : AVVideoCodecType.jpeg])], completionHandler: nil)
218
+
219
+ captureSession.addOutput(photoOutput!)
220
+
221
+ } catch {
222
+
223
+ print(error)
224
+
225
+ }
226
+
227
+ }
228
+
229
+
230
+
231
+ // カメラのプレビューを表示するレイヤの設定
232
+
233
+ func setupPreviewLayer() {
234
+
235
+ // 指定したAVCaptureSessionでプレビューレイヤを初期化
236
+
237
+ self.cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
238
+
239
+ // プレビューレイヤが、カメラのキャプチャーを縦横比を維持した状態で、表示するように設定
240
+
241
+ self.cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
242
+
243
+ // プレビューレイヤの表示の向きを設定
244
+
245
+ self.cameraPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
246
+
247
+
248
+
249
+ self.cameraPreviewLayer?.frame = view.frame
250
+
251
+
252
+
253
+ self.view.layer.insertSublayer(self.cameraPreviewLayer!, at: 0)
254
+
255
+ }
256
+
257
+ // ボタンのスタイルを設定
258
+
259
+ func styleCaptureButton() {
260
+
261
+ cameraButton.layer.borderColor = UIColor.white.cgColor
262
+
263
+ cameraButton.layer.borderWidth = 5
264
+
265
+
266
+
267
+ cameraButton.clipsToBounds = true
268
+
269
+ cameraButton.layer.cornerRadius = min(cameraButton.frame.width, cameraButton.frame.height) / 2
270
+
271
+ }
272
+
273
+ }
274
+
275
+
276
+
277
+ ```
18
278
 
19
279
 
20
280