AVCaptureStillImageOutputを実装して静止画を撮る
iPhoneで静止画キャプチャを撮るために、AVCpatureStillImageOutputを使ってみる。
AVCaptureSessionの出力にAVCaptureStillImageOutputを設定してやればいい。
|
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
- (void)viewDidLoad {
[super viewDidLoad];
// キャプチャセッションを作る
_captureSession = [[AVCaptureSession alloc]init];
// ビデオの解像度 Midium
if ([_captureSession canSetSessionPreset:AVCaptureSessionPresetMedium]) {
_captureSession.sessionPreset = AVCaptureSessionPresetMedium;
}
// ビデオデバイスを取って
AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// 入力を作る
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
// セッションに入力を追加
[_captureSession addInput:input];
// AVCaptureStillImageOutputで静止画出力を作る
_stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:
AVVideoCodecJPEG, AVVideoCodecKey, nil];
_stillImageOutput.outputSettings = outputSettings;
[outputSettings release];
// セッションに出力を追加
[_captureSession addOutput:_stillImageOutput];
// プレビューレイヤーを作って
AVCaptureVideoPreviewLayer *videoPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_captureSession];
// リサイズ形式を設定して
videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
// フレームサイズを設定して
videoPreviewLayer.frame = self.view.bounds;
// ビューのサブレイヤーにビデオ出力レイヤーを追加
[self.view.layer addSublayer:videoPreviewLayer];
// 4×4個のボタンを作る
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
int width = [UIScreen mainScreen].bounds.size.width / 4;
int height = [UIScreen mainScreen].bounds.size.height / 4;
int x = width * j;
int y = height * i;
// ボタンを作る
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(x, y, width, height);
// キャプチャイベント
[button addTarget:self action:@selector(capture:) forControlEvents:UIControlEventTouchDown];
// ビューにサブビューとして追加
[self.view addSubview:button];
}
}
// セッションを開始
[_captureSession startRunning];
} |
今回は、画面に4×4個のボタンを配置して、各ボタンが押されたときにそのボタン自身にキャプチャした静止画を貼り付けるようにする。
|
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 |
- (void)capture:(id)sender {
// コネクションを検索
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in _stillImageOutput.connections) {
for (AVCaptureInputPort *port in [connection inputPorts]) {
if ([[port mediaType] isEqual:AVMediaTypeVideo] ) {
videoConnection = connection;
break;
}
}
if (videoConnection)
break;
}
// 静止画をキャプチャする
[_stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection
completionHandler:
^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
if (imageSampleBuffer != NULL) {
// キャプチャしたデータを取る
NSData *data = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
// 押されたボタンにキャプチャした静止画を設定する
UIButton *button = (UIButton*)sender;
[button setBackgroundImage:[UIImage imageWithData:data] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageWithData:data] forState:UIControlStateHighlighted];
}
}];
} |
動かしてみた感じは以下。