2015-04-17 10 views
8

Tôi chỉ mới bắt đầu trong Objective-C và tôi đang cố gắng tạo một ứng dụng đơn giản, nơi nó hiển thị chế độ xem máy ảnh với hiệu ứng mờ ảo trên đó. Tôi nhận được đầu ra Camera hoạt động với khung công tác AVFoundation. Bây giờ, tôi đang cố gắng để treo lên khung hình ảnh cốt lõi nhưng không có kiến ​​thức làm thế nào để, tài liệu của Apple là khó hiểu cho tôi và tìm kiếm các hướng dẫn và hướng dẫn trực tuyến dẫn đến không có kết quả. Xin được cảm ơn trước về sự giúp đỡ.Làm cách nào để xuất CIFilter sang chế độ xem Camera?

#import "ViewController.h" 
#import <AVFoundation/AVFoundation.h> 
@interface ViewController() 

@property (strong ,nonatomic) CIContext *context; 

@end 

@implementation ViewController 
AVCaptureSession *session; 
AVCaptureStillImageOutput *stillImageOutput; 

-(CIContext *)context 
{ 
    if(!_context) 
    { 
     _context = [CIContext contextWithOptions:nil]; 
    } 
    return _context; 
} 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

-(void)viewWillAppear:(BOOL)animated{ 
    session = [[AVCaptureSession alloc] init]; 
    [session setSessionPreset:AVCaptureSessionPresetPhoto]; 

    AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 
    NSError *error; 
    AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&error]; 

    if ([session canAddInput:deviceInput]) { 
     [session addInput:deviceInput]; 
    } 

    AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session]; 
    [previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill]; 
    CALayer *rootLayer = [[self view] layer]; 
    [rootLayer setMasksToBounds:YES]; 
    CGRect frame = self.imageView.frame; 

    [previewLayer setFrame:frame]; 

    [previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight]; 


    [rootLayer insertSublayer:previewLayer atIndex:0]; 

    stillImageOutput = [[AVCaptureStillImageOutput alloc] init]; 
    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG, AVVideoCodecKey, nil]; 
    [stillImageOutput setOutputSettings:outputSettings]; 

    [session addOutput:stillImageOutput]; 

    [session startRunning];  
} 
@end 

Trả lời

10

Đây là thứ để bạn bắt đầu. Đây là phiên bản cập nhật của mã từ liên kết sau.
https://gist.github.com/eladb/9662102

Bí quyết là sử dụng AVCaptureVideoDataOutputSampleBufferDelegate.
Với đại biểu này, bạn có thể sử dụng imageWithCVPixelBuffer để tạo CIImage từ bộ đệm máy ảnh của mình.

Ngay bây giờ mặc dù tôi đang cố gắng tìm ra cách giảm độ trễ. Tôi sẽ cập nhật càng sớm càng tốt.


Cập nhật: Độ trễ hiện ở mức tối thiểu và một số hiệu ứng không đáng kể. Thật không may, có vẻ như mờ là một trong những chậm nhất. Bạn có thể muốn xem xét vImage.


#import "ViewController.h" 
#import <CoreImage/CoreImage.h> 
#import <AVFoundation/AVFoundation.h> 

@interface ViewController() { 

} 

@property (strong, nonatomic) CIContext *coreImageContext; 
@property (strong, nonatomic) AVCaptureSession *cameraSession; 
@property (strong, nonatomic) AVCaptureVideoDataOutput *videoOutput; 
@property (strong, nonatomic) UIView *blurCameraView; 
@property (strong, nonatomic) CIFilter *filter; 
@property BOOL cameraOpen; 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.blurCameraView = [[UIView alloc]initWithFrame:[[UIScreen mainScreen] bounds]]; 
    [self.view addSubview:self.blurCameraView]; 

    //setup filter 
    self.filter = [CIFilter filterWithName:@"CIGaussianBlur"]; 
    [self.filter setDefaults]; 
    [self.filter setValue:@(3.0f) forKey:@"inputRadius"]; 

    [self setupCamera]; 
    [self openCamera]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (void)setupCamera 
{ 
    self.coreImageContext = [CIContext contextWithOptions:@{kCIContextUseSoftwareRenderer : @(YES)}]; 

    // session 
    self.cameraSession = [[AVCaptureSession alloc] init]; 
    [self.cameraSession setSessionPreset:AVCaptureSessionPresetLow]; 
    [self.cameraSession commitConfiguration]; 

    // input 
    AVCaptureDevice *shootingCamera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 
    AVCaptureDeviceInput *shootingDevice = [AVCaptureDeviceInput deviceInputWithDevice:shootingCamera error:NULL]; 
    if ([self.cameraSession canAddInput:shootingDevice]) { 
     [self.cameraSession addInput:shootingDevice]; 
    } 

    // video output 
    self.videoOutput = [[AVCaptureVideoDataOutput alloc] init]; 
    self.videoOutput.alwaysDiscardsLateVideoFrames = YES; 
    [self.videoOutput setSampleBufferDelegate:self queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)]; 
    if ([self.cameraSession canAddOutput:self.videoOutput]) { 
     [self.cameraSession addOutput:self.videoOutput]; 
    } 

    if (self.videoOutput.connections.count > 0) { 
     AVCaptureConnection *connection = self.videoOutput.connections[0]; 
     connection.videoOrientation = AVCaptureVideoOrientationPortrait; 
    } 

    self.cameraOpen = NO; 
} 

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection { 
    // Get a CMSampleBuffer's Core Video image buffer for the media data 
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 

    // turn buffer into an image we can manipulate 
    CIImage *result = [CIImage imageWithCVPixelBuffer:imageBuffer]; 

    // filter 
    [self.filter setValue:result forKey:@"inputImage"]; 

    // render image 
    CGImageRef blurredImage = [self.coreImageContext createCGImage:self.filter.outputImage fromRect:result.extent]; 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     self.blurCameraView.layer.contents = (__bridge id)blurredImage; 
     CGImageRelease(blurredImage); 
    }); 
} 

- (void)openCamera { 
    if (self.cameraOpen) { 
     return; 
    } 

    self.blurCameraView.alpha = 0.0f; 
    [self.cameraSession startRunning]; 
    [self.view layoutIfNeeded]; 

    [UIView animateWithDuration:3.0f animations:^{ 

     self.blurCameraView.alpha = 1.0f; 

    }]; 

    self.cameraOpen = YES; 
} 
+0

Wow. Đuợc! sẽ kiểm tra cái này. Cảm ơn! –

+0

Cách thực hiện việc này để Ghi cùng một Video? –

Các vấn đề liên quan