update: 2012/03/07
reference: iOS Camera Overlay Example Using AVCaptureSession | musicalgeometry
A. 說明
1. 直接以目前的專案來初步測試: "相機鋪蓋" 與 "濾鏡矩形" 的效果.
2. 不調整 UI, 而以程式的方式來測試.
3. 不考量設備方向的改變.
----------------------------------------------------------------------------------
B. 開啓 ViewController.h 檔案, 修改如下:
....
@interface ViewController : GLKViewController <AVCaptureVideoDataOutputSampleBufferDelegate>
{
....
//@add for test
UILabel *scanningLabel;
UIImageView *overlayImageView;
UIButton *overlayButton;
}
....
//@add for test
@property (nonatomic, strong)UILabel *scanningLabel;
@property (nonatomic, strong)UIImageView *overlayImageView;
@property (nonatomic, strong)UIButton *overlayButton;
....
//@add for test
- (void)helperTest;
- (CIImage *)filterTest:(CIImage *)sourceImage;
- (void)scanButtonPressedTest;
- (void)hideLabelTest:(UILabel *)label;
- (void)cameraOverlayTest;
@end
----------------------------------------------------------------------------------
C. 開啓 ViewController.m 檔案, 修改如下:
....
@implementation ViewController
....
//@add for test
@synthesize scanningLabel = _scanningLabel;
@synthesize overlayImageView = _overlayImageView;
@synthesize overlayButton = _overlayButton;
....
#pragma mark Test
....
//@test for initialize scanningLabel
- (UILabel *)scanningLabel
{
if (_scanningLabel == nil) {
_scanningLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 50, 120, 30)];
}
return _scanningLabel;
}
//@test for initialize overlayImageView
- (UIImageView *)overlayImageView
{
if (_overlayImageView == nil) {
_overlayImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"overlaygraphic.png"]];
}
return _overlayImageView;
}
//@test for initialize overlayButton
- (UIButton *)overlayButton
{
if (_overlayButton == nil) {
_overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
}
return _overlayButton;
}
//@test for scanButtonPressed
- (void)scanButtonPressedTest {
[self.scanningLabel setHidden:NO];
[self performSelector:@selector(hideLabelTest:) withObject:self.scanningLabel afterDelay:2];
}
//@test for hideLabel
- (void)hideLabelTest:(UILabel *)label {
[label setHidden:YES];
}
....
//@test for cameraOverlay
- (void)cameraOverlayTest
{
//@overlayImageView
[self.overlayImageView setFrame:CGRectMake(30, 100, 260, 200)];
[self.view addSubview:self.overlayImageView];
//@overlayButton
[self.overlayButton setImage:[UIImage imageNamed:@"scanbutton.png"] forState:UIControlStateNormal];
[self.overlayButton setFrame:CGRectMake(130, 320, 60, 30)];
[self.overlayButton addTarget:self action:@selector(scanButtonPressedTest) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.overlayButton];
//@scanningLabel
[self.scanningLabel setBackgroundColor:[UIColor clearColor]];
[self.scanningLabel setFont:[UIFont fontWithName:@"Courier" size: 18.0]];
[self.scanningLabel setTextColor:[UIColor redColor]];
[self.scanningLabel setText:@"Scanning..."];
[self.scanningLabel setHidden:YES];
[self.view addSubview:self.scanningLabel];
}
....
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//@add:helper Test
[self helperTest];
//@add: establish Render
[self establishRender];
//[self setupGL];
//@add: establishCamera
[self establishCamera:kCameraBack];
//@add test for cameraOverlay
[self cameraOverlayTest];
//@add: startRunning
[self startRunningSession];
}
....
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
// 將取得的 sampleBuffer 轉成 CVPixelBuffer
CVPixelBufferRef pixelBuffer = (CVPixelBufferRef)CMSampleBufferGetImageBuffer(sampleBuffer);
// 接著, 將 CVPixelBuffer 利用 Core Image 的初始化方法再轉成 CIImage.
CIImage *ciImage = [CIImage imageWithCVPixelBuffer:pixelBuffer];
//@add:濾鏡測試
//ciImage = [self filterTest:ciImage];
//@add:方向轉變的調整
ciImage = [Filter4CamHelper orientationTransform:ciImage];
// 然後使用 CIContext 物件將其內容畫到 render buffer
[self.coreImageContext drawImage:ciImage atPoint:CGPointZero fromRect:[ciImage extent]];
//@add test for filter Rectangle
//@add:濾鏡測試
ciImage = [self filterTest:ciImage];
[self.coreImageContext drawImage:ciImage inRect:CGRectMake(30, 180, 260, 200) fromRect:[ciImage extent]];
// 最後, 在螢幕上呈現出來.
[self.glContext presentRenderbuffer:GL_RENDERBUFFER];
}
----------------------------------------------------------------------------------
D. 編譯並執行
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。