AVCaptureSessionとAVCaptureDeviceInputでカメラ入力表示
カメラアプリ作成の為に、まず画面にカメラからの入力を表示させてみる。
AVCaptureSessionにAVCaptureDeviceInputをくっつけて、AVCaptureVideoPreviewLayerでViewに表示する。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
- (void)viewDidLoad {
[super viewDidLoad];
// キャプチャセッションを作る
_captureSession = [[AVCaptureSession alloc]init];
// ビデオの解像度 ミドル
if ([_captureSession canSetSessionPreset:AVCaptureSessionPresetMedium]) {
_captureSession.sessionPreset = AVCaptureSessionPresetMedium;
}
// ビデオ入力の設定
NSError *error = nil;
// ビデオデバイスを取って
AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// デバイス入力を
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
if (!input) {
NSLog(@"input error");
}
// キャプチャセッションに追加する
[_captureSession addInput:input];
// プレビューレイヤーを作って
AVCaptureVideoPreviewLayer *videoPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_captureSession];
// リサイズ形式を設定して
videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
// フレームサイズを設定して
videoPreviewLayer.frame = self.view.bounds;
// サブレイヤーとして追加する
[self.view.layer addSublayer:videoPreviewLayer];
// キャプチャセッションを開始
[_captureSession startRunning];
} |
こんな感じでカメラからの入力が表示される。動画アップのテストも兼ねて。