2012年9月3日 星期一

Filter4Cam 實作: 29. 影像視窗大小調整

since: 2012/09/03
update: 2012/09/03


A. 說明:
     在 實作 28  中, 於 captureOutput:didOutputSampleBuffer: fromConnection:
     方法裡, 我們利用了 CGRectMake 來建立要畫到 render buffer 的目標矩形大小.
     寬度固定為: self.view.frame.size.width
     高度固定為: self.view.frame.size.height  
     當時並沒有考慮到設備方向改變時的情況, 因此當設備水平擺放時, 就會發生
     寬度被切掉的情況, 因此要做一些調整.
    說明: 影像被切掉的地方顯示為黑色.

------------------------------------------------------------------------

B. 開啟 ViewController.h 檔案, 修改如下:
....
@interface ViewController : GLKViewController <AVCaptureVideoDataOutputSampleBufferDelegate, UITableViewDelegate, UITableViewDataSource>
{
....
    // 目前使用的濾鏡物件
    FilterBase *currentFilter;
   
    //@add: 影像視窗的大小
    CGRect destRect;

}
....


@property (nonatomic, strong) FilterBase *currentFilter;

//@add
@property (assign) CGRect destRect;

....

------------------------------------------------------------------------

C. 開啟 ViewController.m 檔案, 修改如下:
....
// step 01:
@synthesize dataObj = _dataObj;
@synthesize currentFilter = _currentFilter;


//@add
@synthesize destRect = _destRect;


....
// step 02:
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
....
    //@update: comment it
    /*
    //@add
    CGRect destRect = CGRectMake(self.view.frame.origin.x,
                                 self.view.frame.origin.y,
                                 self.view.frame.size.width,
                                 self.view.frame.size.height);
   
    */
   
    //@add: 根據設備擺放方向來設定 "影像視窗大小" 的長寬
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
   
    // 設備垂直擺放
    if (orientation == UIDeviceOrientationPortrait ||
        orientation == UIDeviceOrientationPortraitUpsideDown)
    {
        self.destRect = CGRectMake(self.view.frame.origin.x,
                                   self.view.frame.origin.y,
                                   self.view.frame.size.width,
                                   self.view.frame.size.height);
    }
    // 設備水平擺放
    else if (orientation == UIDeviceOrientationLandscapeRight ||
             orientation == UIDeviceOrientationLandscapeLeft)
    {
        self.destRect = CGRectMake(self.view.frame.origin.x,
                                   self.view.frame.origin.y,
                                   self.view.frame.size.height,
                                   self.view.frame.size.width);
    }

    // 設備: 面朝上, 面朝下, 未知
    else
    {
        // Do Nothing
    }


    // 然後使用 CIContext 物件將其內容畫到 render buffer
    //[self.coreImageContext drawImage:self.ciImage inRect:destRect fromRect:[self.ciImage extent]];
    //@update
    [self.coreImageContext drawImage:self.ciImage inRect:self.destRect fromRect:[self.ciImage extent]];


    // 最後, 在螢幕上呈現出來.
    [self.glContext presentRenderbuffer:GL_RENDERBUFFER];
}

....

// step 03:
- (void)viewDidLoad
{
....
    //@add
    self.destRect = CGRectMake(self.view.frame.origin.x,
                                                         self.view.frame.origin.y,
                                                         self.view.frame.size.width,
                                                         self.view.frame.size.height);

   
    //@add: establish Render
    [self establishRender];

    //@add: establishCamera
    [self establishCamera:kCameraBack];
   
    //@add test for cameraOverlay
    //[self cameraOverlayTest];
   
    //@add for setting camera Overlay
    [self cameraOverlay];
   
    //@add: startRunning
    [self startRunningSession];
   
    //@add for initialize reusableCells
    [self initReusableCells];
}

....

------------------------------------------------------------------------

D. 編譯並執行:

      拍照畫面(水平擺放)

      拍照結果(水平擺放: 720 x 960)

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。