update: 2012/01/07
reference: Saving a Photo With AVFoundation and Core Image | Indie Ambitions
A.調整 UI
1. 接續之前的專案, 點選 MainStoryboard.storyboard 檔案, 加入一個 button,
並對應到 -(void)snap:(id)sender 方法.
這是可用來調整濾鏡的(在此尚未實作)
B. 修改 ViewController.h
新增一個 CIImage 實體變數.
@interface ViewController : GLKViewController <AVCaptureVideoDataOutputSampleBufferDelegate>
{
//@add
AVCaptureSession *session;
CIContext *coreImageContext;
EAGLContext *context;
GLuint _renderBuffer;
IBOutlet UISlider *sldr;
//@add
CIImage *ciimg;
}
//@add
@property (strong, nonatomic) EAGLContext *context;
- (IBAction)snap:(id)sender;
@end
---------------------------------------------------------------------------------------------
C. 將照片儲存到相簿
1. 將 AssetsLibrary framework 加入專案中.
2. 修改 ViewController.h 檔案, 如下:
....
#import <ImageIO/ImageIO.h>
//@add
#import <AssetsLibrary/AssetsLibrary.h>
....
3. 開啓 ViewController.m 檔案, 修改如下:
- (IBAction)snap:(id)sender {
//@add
CGImageRef cgimg = [coreImageContext createCGImage:ciimg fromRect:[ciimg extent]];
ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
[lib writeImageToSavedPhotosAlbum:cgimg metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
CGImageRelease(cgimg);
}];
}
// 將 CIImage *image 都改成 ciimg
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
CVPixelBufferRef pixelBuffer = (CVPixelBufferRef)CMSampleBufferGetImageBuffer(sampleBuffer);
//CIImage *image = [CIImage imageWithCVPixelBuffer:pixelBuffer];
//@update
ciimg = [CIImage imageWithCVPixelBuffer:pixelBuffer];
//@add filter
/*
image = [CIFilter filterWithName:@"CIFalseColor" keysAndValues:
kCIInputImageKey, image,
@"inputColor0", [CIColor colorWithRed:0.0 green:0.2 blue:0.0],
@"inputColor1", [CIColor colorWithRed:0.0 green:0.0 blue:1.0],
nil].outputImage;
*/
//@update
ciimg = [CIFilter filterWithName:@"CIFalseColor" keysAndValues:
kCIInputImageKey, ciimg,
@"inputColor0", [CIColor colorWithRed:0.0 green:0.2 blue:0.0],
@"inputColor1", [CIColor colorWithRed:0.0 green:0.0 blue:1.0], nil].outputImage;
//[coreImageContext drawImage:image atPoint:CGPointZero fromRect:[image extent] ];
//@update
[coreImageContext drawImage:ciimg atPoint:CGPointZero fromRect:[ciimg extent] ];
[self.context presentRenderbuffer:GL_RENDERBUFFER];
}
4. 編譯並執行
D. 調整方向
1. 修改 ViewController.m 檔案如下
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
CVPixelBufferRef pixelBuffer = (CVPixelBufferRef)CMSampleBufferGetImageBuffer(sampleBuffer);
ciimg = [CIImage imageWithCVPixelBuffer:pixelBuffer];
//update: orientation
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
CGAffineTransform t;
if (orientation == UIDeviceOrientationPortrait) {
t = CGAffineTransformMakeRotation(-M_PI / 2);
} else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
t = CGAffineTransformMakeRotation(M_PI / 2);
} else if (orientation == UIDeviceOrientationLandscapeRight) {
t = CGAffineTransformMakeRotation(M_PI);
} else {
t = CGAffineTransformMakeRotation(0);
}
ciimg = [ciimg imageByApplyingTransform:t];
//@add filter
ciimg = [CIFilter filterWithName:@"CIFalseColor" keysAndValues:
kCIInputImageKey, ciimg,
@"inputColor0", [CIColor colorWithRed:0.0 green:0.2 blue:0.0],
@"inputColor1", [CIColor colorWithRed:0.0 green:0.0 blue:1.0], nil].outputImage;
[coreImageContext drawImage:ciimg atPoint:CGPointZero fromRect:[ciimg extent] ];
[self.context presentRenderbuffer:GL_RENDERBUFFER];
}
2. 編譯並執行
E. 滑動(Slider)
1. 修改 ViewController.m 檔案如下:
//@add
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
....
//@add filter
/*
ciimg = [CIFilter filterWithName:@"CIFalseColor" keysAndValues:
kCIInputImageKey, ciimg,
@"inputColor0", [CIColor colorWithRed:0.0 green:0.2 blue:0.0],
@"inputColor1", [CIColor colorWithRed:0.0 green:0.0 blue:1.0], nil].outputImage;
*/
//@add filter & slider
ciimg = [CIFilter filterWithName:@"CIFalseColor" keysAndValues:
kCIInputImageKey, ciimg,
@"inputColor0", [CIColor colorWithRed:0.0 green:sldr.value blue:0.0],
@"inputColor1", [CIColor colorWithRed:0.0 green:sldr.value blue:1.0], nil].outputImage;
[coreImageContext drawImage:ciimg atPoint:CGPointZero fromRect:[ciimg extent] ];
[self.context presentRenderbuffer:GL_RENDERBUFFER];
}
2. 編譯並執行
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。