Skip to content

Instantly share code, notes, and snippets.

@kuninori
Last active May 7, 2017 04:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuninori/4b8abeed9f0c0036cf6e to your computer and use it in GitHub Desktop.
Save kuninori/4b8abeed9f0c0036cf6e to your computer and use it in GitHub Desktop.
AVFoundation Video
//
// DetailViewController.h
// Sync Camera
//
// Created by kuninorif on 2014/05/22.
// Copyright (c) 2014年 kuninori. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <Accelerate/Accelerate.h>
@interface DetailViewController : UIViewController
@property (strong, nonatomic) id detailItem;
@property (nonatomic) AVCaptureSession *session;
@property (nonatomic) AVCaptureStillImageOutput *imageOutput;
@property (weak, nonatomic) IBOutlet UIView *previewView;
@property (weak, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
@end
//
// DetailViewController.m
// Sync Camera
//
// Created by kuninorif on 2014/05/22.
// Copyright (c) 2014年 kuninori. All rights reserved.
//
#import "DetailViewController.h"
@interface DetailViewController () <AVCaptureVideoDataOutputSampleBufferDelegate>
- (void)configureView;
@end
@implementation DetailViewController
#pragma mark - Managing the detail item
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
}
- (void)configureView
{
}
- (void)viewDidLoad
{
self.session = [[AVCaptureSession alloc] init];
if( [self.session canSetSessionPreset:AVCaptureSessionPresetHigh] ){
self.session.sessionPreset = AVCaptureSessionPresetHigh;
}
// シュミレーターでは動かない
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
AVCaptureDevice *targetdevice;
for(AVCaptureDevice *device in devices){
if( device.position == AVCaptureDevicePositionBack){
targetdevice = device;
}
}
if( targetdevice == nil ){
NSLog(@"not foud device.");
return;
}
NSError *err;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:targetdevice error:&err];
if( err != nil ){
NSLog(@"error: %@", err);
}
if( [self.session canAddInput:input] ){
[self.session addInput:input];
} else {
NSLog(@"not support");
}
AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
[output setAlwaysDiscardsLateVideoFrames:YES];
[output setVideoSettings:@{ (NSString *)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_32BGRA) }];
[output setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
[self.session addOutput:output];
[self.session startRunning];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// video buffer
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer, 0);
uint8_t *base = CVPixelBufferGetBaseAddress(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
size_t bytes = CVPixelBufferGetBytesPerRow(imageBuffer);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef cgContext = CGBitmapContextCreate(base, width, height, 8,
bytes, colorSpace,
kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
// image
CGImageRef cgImage = CGBitmapContextCreateImage(cgContext);
UIImage *image = [UIImage imageWithCGImage:cgImage scale:1.0 orientation:UIImageOrientationRight];
CGImageRelease(cgImage);
CGContextRelease(cgContext);
// unlock
CVPixelBufferUnlockBaseAddress(imageBuffer, false);
self.previewView.image = image;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment