2012年9月3日 星期一

Filter4Cam 實作: 28. 影像解析度調整

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


A. 說明:
     調整內容如下:
     1. 移除濾鏡的顯示範圍限制, 讓濾鏡效果呈現在整個螢幕上.
     2. 相機前置鏡頭的解析度為: 640x480; 後置鏡頭的解析度為: 720 x 960.

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

B. 開啟 ViewController.h 檔案, 修改如下:
....
@interface ViewController : GLKViewController <AVCaptureVideoDataOutputSampleBufferDelegate, UITableViewDelegate, UITableViewDataSource>
{
....
    //@add for Camera Overlay UI
    //@update: comment it
    //UIImageView *filterLensImageView;
    UITableView *filterListTableView;
    UIView *buttonView;       // 用來放所有按鈕的 View
    UIButton *saveButton;     // "拍照" 按鈕
    UIButton *switchButton;   // "鏡頭切換" 按鈕
    UIButton *torchButton;    // "閃光燈切換" 按鈕
    UIButton *observerButton; // "觀察者模式" 按鈕
....
}

....
//@add for Camera Overlay UI
//@update: comment it
//@property (nonatomic, strong) UIImageView *filterLensImageView;
@property (nonatomic, strong) UITableView *filterListTableView;
@property (nonatomic, strong) UIView *buttonView;
@property (nonatomic, strong) UIButton *saveButton;
@property (nonatomic, strong) UIButton *switchButton;
@property (nonatomic, strong) UIButton *torchButton;
@property (nonatomic, strong) UIButton *observerButton;

....
//@add for orientation Transform
- (CIImage *)orientationTransform:(CIImage *)sourceImage;

//@update: comment it
//- (void)drawFilteredImage:(CIImage *)filteredImage;
....

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

C. 移除 "界定濾鏡顯示範圍的圖" (filterLensImageView):
      開啟 ViewController.m 檔案, 修改如下:
 ....
// step 01:
//@add for Camera Overlay UI
//@update: comment it
//@synthesize filterLensImageView = _filterLensImageView;
@synthesize filterListTableView = _filterListTableView;
....


// step 02:
//@update: comment it
/*
//@add for Camera Overlay UI
- (UIImageView *)filterLensImageView
{  
    if (_filterLensImageView == nil) {
        _filterLensImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"filterLens.png"]];
    }
   
    return _filterLensImageView;
}
*/

....

// step 03:
- (void)cameraOverlay
{  
    //@update: comment it
    //[self.filterLensImageView setFrame:CGRectMake(20, 80, 280, 280)];

    //@update: comment it
    //[self.view addSubview:self.filterLensImageView];
....
}
....


// step 04:
- (void)viewDidUnload
{   
....

    //@update: comment it
    //self.filterLensImageView = nil;
....
}

....

// step 05:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
....
        //@update: Tune UI

        //@update: comment it (共有 5 個地方)
        //[self.filterLensImageView setFrame:CGRectMake(20, 120, 280, 280)];
....
}
....


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

D. 相機解析度調整:
      開啟 ViewController.m 檔案, 修改如下:
....
// step 01:
- (void)establishCamera:(uint)whichCamera
{
....   
    // begin
    [self.session beginConfiguration];
    //[self.session setSessionPreset:AVCaptureSessionPreset640x480];
    //[self.session setSessionPreset:AVCaptureSessionPreset1280x720];
    //[self.session setSessionPreset:AVCaptureSessionPresetHigh];

    //@update: 預設後置鏡頭的解析度: 720 x 960
    [self.session setSessionPreset:AVCaptureSessionPresetPhoto];
// 720 x 960
    //[self.session setSessionPreset:AVCaptureSessionPresetiFrame1280x720];
    //[self.session setSessionPreset:AVCaptureSessionPresetiFrame960x540];   
....

....


// step 02:
//@update: comment it
/*
// 畫出 "濾鏡範圍" 內的影像(含方向轉變的調整)
- (void)drawFilteredImage:(CIImage *)filteredImage
{
....
}

*/
....

// step 03:
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
....   
    // 然後使用 CIContext 物件將其內容畫到 render buffer
    //@update: comment it
    //[self.coreImageContext drawImage:self.ciImage atPoint:CGPointZero fromRect:[self.ciImage extent]];
   
    //@update
    if(self.dataObj.isUsingFilter == YES)
    {       
        // check if key "filterID" exists:
        // objectForKey will return nil if a key doesn't exists.
        if (self.currentFilter = [self.dataObj.filterDictionary objectForKey:self.dataObj.filterID])
        {
            self.ciImage = [self.currentFilter filterImage:self.ciImage];
            //@update: comment it
            //[self drawFilteredImage:self.ciImage];
        }
    }
   
    //@add
    CGRect destRect = CGRectMake(self.view.frame.origin.x,
                                                                 self.view.frame.origin.y,
                                                                 self.view.frame.size.width,
                                                                 self.view.frame.size.height);

   
    // 然後使用 CIContext 物件將其內容畫到 render buffer
    [self.coreImageContext drawImage:self.ciImage inRect:destRect fromRect:[self.ciImage extent]];
   
    // 最後, 在螢幕上呈現出來.
    [self.glContext presentRenderbuffer:GL_RENDERBUFFER];
}
....


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

E. 調整 "切換鏡頭" 的解析度:
     開啟 ViewController.m 檔案, 修改如下:
....
- (void)switchCameras
{
....  
    // Change the input
    AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput deviceInputWithDevice:newDevice error:nil];
    [self.session addInput:captureInput];
   
    //@add
    // 使用前置鏡頭: 640x480
    if (self.isUsingFrontCamera)
    {
        [self.session setSessionPreset:AVCaptureSessionPreset640x480];
    }
    else
    {
        // 使用後置鏡頭: 720 x 960
        [self.session setSessionPreset:AVCaptureSessionPresetPhoto];
    }

   
    [self.session commitConfiguration];
}
....


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

F. 拍照 "影像範圍" 調整:

     開啟 ViewController.m 檔案, 修改如下:
....
//@update: "拍照": 截取相機鏡頭全部範圍的影像
- (void)savePhoto
{   
....
            //cgImage = [self.coreImageContext createCGImage:self.ciImage fromRect:CGRectMake(23, -557.5, 274, 274)];
            //@update (總共有 6 個地方)
            cgImage = [self.coreImageContext createCGImage:self.ciImage fromRect:[self.ciImage extent]];

....
}
....


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

G. 編譯並執行:
      拍照畫面

      拍照結果(720 x 960)

沒有留言:

張貼留言

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