Last active
October 28, 2016 08:27
-
-
Save Lewuathe/5152338 to your computer and use it in GitHub Desktop.
How to get movie with AVFoundation ref: http://qiita.com/Lewuathe/items/838b47c36be0e1d368da
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @interface VideoCameraViewController (){ | |
| AVCaptureSession *_session; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -(void)viewDidLoad{ | |
| AVCaptureDevice* device; | |
| device = [AVCaptureDevice | |
| defaultDeviceWithMediaType:AVMediaTypeVideo]; | |
| // デバイス入力の取得 | |
| // カメラからのデータはここから入る | |
| AVCaptureDeviceInput* deviceInput; | |
| deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:NULL]; | |
| // ビデオデータ出力の作成 | |
| // アプリからはここからデータを取得する | |
| NSMutableDictionary* settings; | |
| AVCaptureVideoDataOutput* dataOutput; | |
| settings = [NSMutableDictionary dictionary]; | |
| [settings setObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] | |
| forKey:(id)kCVPixelBufferPixelFormatTypeKey]; | |
| dataOutput = [[AVCaptureVideoDataOutput alloc] init]; | |
| dataOutput.videoSettings = settings; | |
| [dataOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()]; | |
| // セッションの作成 | |
| _session = [[AVCaptureSession alloc] init]; | |
| [_session addInput:deviceInput]; | |
| [_session addOutput:dataOutput]; | |
| // セッションの開始 | |
| // ここから撮影が始まる感じ | |
| [_session startRunning]; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // イメージバッファの取得 | |
| CVImageBufferRef buffer; | |
| buffer = CMSampleBufferGetImageBuffer(sampleBuffer); | |
| // イメージバッファのロック | |
| // これをしないbufferがカメラから送られてくるデータが書き換えられていってしまう | |
| CVPixelBufferLockBaseAddress(buffer, 0); | |
| // イメージバッファ情報の取得 | |
| // 画像の情報を取得する。これが得られればCoreGraphicsを使ってUIImageに変換することができる。 | |
| uint8_t* base; | |
| size_t width, height, bytesPerRow; | |
| base = CVPixelBufferGetBaseAddress(buffer); | |
| width = CVPixelBufferGetWidth(buffer); | |
| height = CVPixelBufferGetHeight(buffer); | |
| bytesPerRow = CVPixelBufferGetBytesPerRow(buffer); | |
| // ビットマップコンテキストの作成 | |
| CGColorSpaceRef colorSpace; | |
| CGContextRef cgContext; | |
| colorSpace = CGColorSpaceCreateDeviceRGB(); | |
| cgContext = CGBitmapContextCreate( | |
| base, width, height, 8, bytesPerRow, colorSpace, | |
| kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); | |
| CGColorSpaceRelease(colorSpace); | |
| // 画像の作成 | |
| CGImageRef cgImage; | |
| UIImage* image; | |
| cgImage = CGBitmapContextCreateImage(cgContext); | |
| image = [UIImage imageWithCGImage:cgImage scale:1.0f | |
| orientation:UIImageOrientationRight]; | |
| CGImageRelease(cgImage); | |
| CGContextRelease(cgContext); | |
| // イメージバッファのアンロック | |
| // カメラからまた送られてくるようにするため、忘れずに | |
| CVPixelBufferUnlockBaseAddress(buffer, 0); | |
| // 画像の表示 | |
| // 適当に貼っつけたUIImageViewで表示 | |
| imageView.image = image; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment