2012年9月6日 星期四

Filter4Cam 實作: 30. 濾鏡轉場特效

since: 2012/09/06
update: 2012/09/06

reference:
I touchs: Filter4Cam 學習之 Core Image Filter With CATransition

A. 說明
      1. 使用轉場特效的時機:
           a. 原始影像 -> 套用濾鏡.
           b. 套用濾鏡的影像 -> 恢復成原始影像
           c. 套用 A 濾鏡 -> 套用 B 濾鏡

      2. 在這邊有二個時間要處理: 一是 "執行轉場特效" 所花的時間(將設為 1.5 秒),
          另外是, 開始執行轉場特效多久後, 會去作濾鏡的套用.(將設為 0.75 秒)

      3. 在轉場的過程中, 當轉場還未完全結束時(即使看起來好像結束了), 此時去點選
           濾鏡應該是不能發生作用的.
           a. 因此需要在 ViewController 裡宣告一個可用來決定是否可點選濾鏡的
               自定 protocol 變數: id<TableSelectionDelegate> tsDelegate;

                並且將此工作委派(delegate)給存放濾鏡 Cell 的 Table (HorizontalTableCell):
                // ViewController => tableView:cellForRowAtIndexPath:
                self.tsDelegate = cell;

           b. 接著, 在存放濾鏡 Cell 的 Table 類別 (HorizontalTableCell) 裡, 宣告依循
                <TableSelectionDelegate> 協定, 並實作 "是否可點選濾鏡" 的功能:

                - (void)allowsTableSelection:(BOOL)allows
                {
                     self.currentTableView.allowsSelection = allows;
                }


           c. 備註:
                原本在 ViewController.h 裡, 直接定義 <TableSelectionDelegate> 協定,
                但是 HorizontalTableCell 類別卻無法參照到, 可能是 Xcode 本身的問題,
                因此將 <TableSelectionDelegate> 協定, 放到一個新增的 header file 來處理.

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

B. 新增 "可否點選濾鏡表單之代理" 協定
      1. Xcode > File > New > File...
          iOS > Cocoa Touch > Objective-C protocol > Next
          Protocol: TableSelectionDelegate
          > Next

          Targets: Filter4Cam (checked)
          > Create

       2. 開啟 TableSelectionDelegate.h 檔案, 修改如下:
#import <Foundation/Foundation.h>

// "可否點選濾鏡表單之代理" 協定
@protocol TableSelectionDelegate <NSObject>

//@add

- (void)allowsTableSelection:(BOOL)allows;

@end

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

C. 新增用來標示 "是否要作轉場特效" 的全域變數
      1. 說明: (以下將在之後一一處理)
          a. 在 HorizontalTableCell.m 的 tableView:didSelectRowAtIndexPath: 方法裡,
               無論是 "選取濾鏡" 或 "取消濾鏡",  都會標示要作 "轉場特效":
               self.dataObj.startTransition = YES;

          b. 接著, 在 ViewController.m 的
               captureOutput:didOutputSampleBuffer:fromConnection: 方法裡, 
               會檢查是否要作轉場特效: [self checkTransition];

          c. 承上, 如果是的話, 就會開始執行轉場特效: [self startAnimation];
              當轉場特效結束時, 會自動呼叫: animationDidStop:finished: 方法,
              並且將 "是否要作轉場特效" 標示為否: self.dataObj.startTransition = NO;

      2. 開啟 GlobalDataClass.h 檔案, 修改如下:
....
@interface GlobalDataClass : NSObject
{   
    //@add: array for storage filterCells
    NSMutableArray *filterCellArray;
    BOOL isUsingFilter;        // 有使用濾鏡?
    BOOL startTransition;    // 標示: 是否啟用轉場特效?
....
}

//@add
@property (nonatomic, strong) NSMutableArray *filterCellArray;
@property (assign) BOOL isUsingFilter;
@property (assign) BOOL startTransition;
....


      3. 開啟 GlobalDataClass.m 檔案, 修改如下:
....
//@add
@synthesize filterCellArray = _filterCellArray;
@synthesize isUsingFilter = _isUsingFilter;

@synthesize startTransition = _startTransition;
....

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

D.  轉場特效

       1. 開啟 ViewController.h 檔案, 修改如下:
           (檢查: 必須要 #import <QuartzCore/QuartzCore.h>)
....
//@add
#import "Filter4CamHelper.h"
#import "ConstantDefined.h"
#import "HorizontalTableCell.h"
#import "GlobalDataClass.h"

#import "TableSelectionDelegate.h"
....
@interface ViewController : GLKViewController <AVCaptureVideoDataOutputSampleBufferDelegate, UITableViewDelegate, UITableViewDataSource>
{
....   

    //@add for CATransition
    BOOL transitioning; // 是否正在作轉場特效?
   
    //@add for delegate used
    id<TableSelectionDelegate> tsDelegate;

}
....

@property (assign) BOOL isUsingFrontCamera;
@property (assign) BOOL isLastFrontCamera;

@property (assign) BOOL transitioning;

@property (assign) CGRect destRect;
@property (assign) /* weak ref */ id<TableSelectionDelegate> tsDelegate;
....
//@add: functional buttons 
- (void)savePhoto; // "拍照"
- (void)switchCameras; // "鏡頭切換"
- (void)switchLight; // "閃光燈切換"
- (void)switchObserver; // "觀察者模式"


- (void)checkTransition; // 檢查是否要作轉場特效
- (void)startAnimation;  // 開始執行轉場特效


@end

       2. 開啟 ViewController.m 檔案, 修改如下:
....
// step 01:
@synthesize isUsingFrontCamera = _isUsingFrontCamera;
@synthesize isLastFrontCamera = _isLastFrontCamera;

@synthesize transitioning = _transitioning;
....
//@add
@synthesize destRect = _destRect;

@synthesize tsDelegate = _tsDelegate;
....
// step 02:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //@update: use reusableCells
    HorizontalTableCell *cell = [self.reusableCells objectAtIndex:indexPath.section];

   
    // 說明: 一般是在 AppDelegate 中, 設定 delegate 的關係, 在此處剛好可利用
    self.tsDelegate = cell;
   
    return cell;
}
....


// step 03:
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
....
    //@add: 檢查是否要作轉場特效
    [self checkTransition];

    
    if(self.dataObj.isUsingFilter == YES)
    {
        if (self.currentFilter = [self.dataObj.filterDictionary objectForKey:self.dataObj.filterID])
        {
            self.ciImage = [self.currentFilter filterImage:self.ciImage];
        }
    }
....
}
....


// step 04:
- (void)viewDidLoad
{
    [super viewDidLoad];

    self.dataObj.isUsingFilter = NO;

    self.dataObj.startTransition = NO; // 預設: 不進行轉場特效

    self.isUsingFrontCamera = NO; // 預設為後置鏡頭
    self.isLastFrontCamera = NO;  // 預設上次為後置鏡頭

    self.transitioning = NO;      // 目前沒有正在作轉場特效
   
    lastOrientation = Unknown; // 設備上次的擺放方向: 未知
....
}


....
// step 05:
- (void)viewDidUnload
{   
....
    self.observerButton = nil;
    self.dataObj = nil;
    self.currentFilter = nil;

    self.tsDelegate = nil;
}
....


// step 06:

// 檢查是否要作轉場特效
- (void)checkTransition
{
    // 要開始進行轉場特效
    if (self.dataObj.startTransition)
    {
        [self startAnimation];
    }
}

....

// step 07:
// 開始執行轉場特效
- (void)startAnimation
{
   
// 先讓濾鏡表單無法選取
    [self.tsDelegate allowsTableSelection:NO];
   
    // 如果, 目前不是正在進行 "轉場特效" 的話, 就開始進行
    if(!self.transitioning)
    {
        CATransition *transition = [CATransition animation];
        transition.duration = 1.50; // 轉場的時間 (秒)
       
        transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
       
        transition.type = @"pageCurl"; // 翻頁(由下往上)
       
        self.transitioning = YES;
        transition.delegate = self;
       
        //[self.view.layer addAnimation:transition forKey:nil]; // 亦可
        [self.glView.layer addAnimation:transition forKey:nil];
    }
}

....

// step 08:
//@add for CATransition(轉場特效結束時, 會自動呼叫)
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    // "轉場特效" 進行完畢
    self.transitioning = NO;
   
    // 停止進行轉場特效(標示為否)
    self.dataObj.startTransition = NO;

    // 恢復濾鏡表單可選取狀態
    [self.tsDelegate allowsTableSelection:YES];
}

....

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

E. 延緩濾鏡套用

      1. 開啟 HorizontalTableCell.h 檔案, 修改如下:
....
#import "GlobalDataClass.h"

#import "TableSelectionDelegate.h"

//@interface HorizontalTableCell : UITableViewCell <UITableViewDelegate, UITableViewDataSource>
//@update
@interface HorizontalTableCell : UITableViewCell <UITableViewDelegate, UITableViewDataSource, TableSelectionDelegate>
{
....
    NSIndexPath *lastIndexPath;
    NSIndexPath *currentIndexPath; // 目前所點選的 table 之 IndexPath
    UITableView *currentTableView; // 目前所點選的 Table
   
    GlobalDataClass *dataObj;
}

....
@property (nonatomic, strong) NSIndexPath *lastIndexPath;

@property (nonatomic, strong) NSIndexPath *currentIndexPath;
@property (nonatomic, strong) UITableView *currentTableView;

@property (nonatomic, strong) GlobalDataClass *dataObj;

//@add: 設定 "選取 / 取消選取 濾鏡 Cell" 的行程
- (void)setSelectProgress;

//@add: 進行 "選取 / 取消選取 濾鏡 Cell" 的行程
- (void)goSelectProgress:(NSTimer *)theTimer;

@end


      2. 開啟 HorizontalTableCell.m 檔案, 修改如下:
....
// step 01:
@synthesize lastIndexPath = _lastIndexPath;
@synthesize currentIndexPath = _currentIndexPath;
@synthesize currentTableView = _currentTableView;

@synthesize dataObj = _dataObj;
....


// step 02:
#pragma mark Getter
....

- (NSIndexPath *)currentIndexPath
{
    if (_currentIndexPath == nil) {
        _currentIndexPath = [NSIndexPath indexPathForRow:-1 inSection:0];
    }
   
    return _currentIndexPath;
}

- (UITableView *)currentTableView
{
    if (_currentTableView == nil) {
        _currentTableView = [[UITableView alloc] init];
    }
   
    return _currentTableView;
}

....

// step 03:

- (void)dealloc
{
    self.filterTableView = nil;
    self.filters = nil;
    self.lastIndexPath = nil;
    self.dataObj = nil;

    self.currentIndexPath = nil;
    self.currentTableView = nil;

}
....


// step 04:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 說明:
    // 1. GlobalDataClass 的變數 startTransition, 最初在 ViewController 的
    //    viewDidLoad 裡, 設值為 NO (即預設: 不進行轉場特效)
    //
    // 2. 當第一次點選濾鏡時, 會進入到本方法裡,
    //    a. 先設定: self.dataObj.startTransition = YES; 標示要作 "轉場特效".
    //        在 ViewController 的
    //        captureOutput:didOutputSampleBuffer:fromConnection: 裡,
    //        先檢查是否要作轉場特效(checkTransition), 接著開始執行轉場特效
    //        (startAnimation), 轉場特效結束時, 會自動呼叫 CATransition 的
    //        animationDidStop:finished: 方法
    //
    //    b. 接著設定: "選取 / 取消選取 濾鏡 Cell" 的行程 (setSelectProgress),
    //        來延遲套用濾鏡的時間.
    //
    // 3. 時間的設定: 轉場特效 1.50 秒 (ViewController -> startAnimation)
    //     套用濾鏡的延遲時間: 0.75 秒 ([self setSelectProgress])
   
    // 如果在進行的 "轉場特效" 尚未結束, 又點選了濾鏡
    // (實際上, 此時是無法點選的, 只能對上層的 Table 作捲動)

    if (self.dataObj.startTransition)
    {

        // 說明:
        // 當 startTransition = YES, 在 ViewController 的 startAnimation 中,
        // 呼叫了: [self.tsDelegate allowsTableSelection:NO];
        //
        // 即在此設定了 self.currentTableView.allowsSelection = NO;
        // 就算去點選濾鏡 Cell, 也不會呼叫 tableView:didSelectRowAtIndexPath: 方法,
        // 所以此區塊永遠不會執行到.

        NSLog(@"You will never see this message ~");
       
        return;
    }

   
    self.currentIndexPath = indexPath;
    self.currentTableView = tableView;


    // 標示要作 "轉場特效"
    self.dataObj.startTransition = YES;
   
    // 設定 "選取 / 取消選取 濾鏡 Cell" 的行程
    [self setSelectProgress];
}

....

// step 05:
//@add: 設定 "選取 / 取消選取 濾鏡 Cell" 的行程
- (void)setSelectProgress
{
    [NSTimer scheduledTimerWithTimeInterval:0.75f
                                     target:self
                                   selector:@selector(goSelectProgress:)
                                   userInfo:nil
                                    repeats:YES];
}
....
.
// step 06:
//@add: 進行 "選取 / 取消選取 濾鏡 Cell" 的行程
- (void)goSelectProgress:(NSTimer *)theTimer {
   

    // 設定 timer 過期 (所以, 只會執行此一次)
    // 或可將 setSelectProgress 裡的 repeats: 設成 NO

    [theTimer invalidate];
   
    int newRow = [self.currentIndexPath row];
    int oldRow = [self.lastIndexPath row];

    //NSLog(@"oldRow = %d", oldRow);
    //NSLog(@"newRow = %d", newRow);

   
    // 選取濾鏡
    if (newRow != oldRow)
    {
        self.lastIndexPath = self.currentIndexPath;

       
        // 套用濾鏡功能
        /* (此方式亦可)
         FilterCell *cell = (FilterCell *)[tableView cellForRowAtIndexPath:indexPath];
         NSLog(@"FilterID = %@, Title = %@", cell.filterID, cell.titleLabel.text);
         */

       
        NSDictionary *currentFilter = [self.filters objectAtIndex:self.currentIndexPath.row];
        /*
         NSString *FilterID = [currentFilter objectForKey:@"FilterID"];
         NSString *Title = [currentFilter objectForKey:@"Title"];
         NSString *ImageName = [currentFilter objectForKey:@"ImageName"];
         NSLog(@"FilterID = %@, Title = %@, ImageName = %@", FilterID, Title, ImageName);
         */

       
        self.dataObj.isUsingFilter = YES;
        self.dataObj.filterID = [NSMutableString stringWithString:[currentFilter objectForKey:@"FilterID"]];
    }

    // 取消選取濾鏡
    else
    {

        //self.lastIndexPath = nil; // -> not work at section:0 row:0
        self.lastIndexPath = [NSIndexPath indexPathForRow:-1 inSection:0];
        [self.currentTableView deselectRowAtIndexPath:self.currentIndexPath animated:NO];

       
        // 恢復原始影像
        self.dataObj.isUsingFilter = NO;
        self.dataObj.filterID = [NSMutableString stringWithString:@"NoID"];
    }
   
    NSLog(@"filterID = %@", self.dataObj.filterID);
}

....

// step 07:
//@add for <TableSelectionDelegate> protocol method
- (void)allowsTableSelection:(BOOL)allows
{

    // 說明: 當 allows = NO 時, 即使去點選濾鏡 Cell, 也不會執行:
    // tableView:didSelectRowAtIndexPath: 方法

    self.currentTableView.allowsSelection = allows;
}

....

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

F. 編譯並執行:




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)

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)

2012年9月2日 星期日

iPod touch 3rd Jailbreak 5.1.1


since: 2012/09/02
update: 2012/09/02

reference:
1.
iOS 5.1.1 Absinthe 2.0.4 全 JB 過程教學
2. I touchs: iPod touch 3rd Jailbreak 4.3.3

A. 說明:
     由於即將到來的 iOS 6 不支援: iPhone 3G, iPad 1 與 iPod touch 3,
     因此決定將手邊已不常用到的(較常用 iPad 2) iPod touch 3rd 作 JB.  

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

B. 前置作業:
    1. 確認 iPod touch 目前為最新版本: iOS 5.1.1 .
        => 設定 > 一般 > 關於本機 > 版本: 5.1.1

    2. 備份 ECID:
        a. 在 Mac OS X 下, 連結 iPod touch 並且下載 Umbrella:
             The Firmware Umbrella - TinyUmbrella         
             p.s. 在此下載的檔案為: TinyUmbrella-5.11.01.pkg

        b. (目前版本: 5.11.01). 安裝好 TinyUmbrella 後, 點開進入並選擇你的 iPod touch.

        c. 接著按下右上方的 "Save SHSH" 按鈕.

        d. 儲存好的 SHSH 會各放一份在 CYDIA 的 server 和本機(Mac) 上的
            /Users/(your username)/.shsh 目錄下.

    3. 備份 iPod Touch 資料:
        使用 iTunes 同步並備份 iPod touch 的資料.

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

C. 進行 JB:

     1. 下載 JB 軟體
          網址: Greenpois0n.com
          連結: Absinthe v2.0.4 MacOSX (10.5, 10.6, 10.7)
          檔名: absinthe-mac-2.0.4.dmg
          備註: 由於目前已升級為 OS X 10.8, 經測試後無法 JB,
                    因此改下載 Linux 版本: Absinthe v2.0.4 Linux (x86/x86_64)
                    檔名: absinthe-linux-2.0.4.tar.bz2
                     (亦可用 Windows 的版本)

     2. 重置內容與設定:
         JB 官網建議如此作, 可讓 JB 過程更加快速.
        => 設定 > 一般 > 重置: 清除所有內容和設定

     3. 將 iPod touch 的 "自動鎖定" 功能, 設成 "永不".
        => 設定 > 一般 > 自動鎖定: 永不


     4. 將 iPod touch 連接到 Linux 上, 並解壓縮 JB 軟體後, 執行 absinthe.x86_64

     5. 點選 "Jailbreak" 開始 JB.

     6. 在 JB 的過程中, iPod touch 會重新啓動, 完成後直接關閉視窗即可.

     7. JB 成功的話, 可以在 iPod touch 的第二頁桌面上看到 "Cydia" app

     8. 將  iPod touch 連接回 Mac 上, 並作資料回復. 

     9. 將 iPod touch 的 "自動鎖定" 功能, 設定回 "5分鐘".
         => 設定 > 一般 > 自動鎖定: 5分鐘 // 依個人需求而定

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

D. Cydia 設定
     1. Cydia 開啟後, 選擇 開發者 > 完成.

     2. 切換到 "變更" 項目, 並按下左上方的 "更新":

     3. Cydia 安裝軟體: 先切換到 "搜尋" 項目
         a. OpenSSH:
             可在 Mac 上使用 sftp client 的軟體連入 iPod touch 系統中.

         b. AppSync for iOS 5.0+:
             讓應用程式 (.ipa / .app) 可直接透過 iTunes 安裝.
             (1). 開啟 Cydia > 管理 > 軟體源 > 編輯 > 添加
             (2). 輸入 http://cydia.hackulo.us/ > 按下 添加源 按鈕 > 若彈出提示框,
                    選擇 仍然添加 即可.
             (3). 完成 Source Update 後, 按下 Return To Cydia, 返回 Source, 再按下
                    剛加入的 Sources, 選擇 AppSync for iOS 5.0+ 安裝.

         c. CyDelete:
             可直接在桌面刪除從 Cydia 下載的軟體.

         d. USB Drive:
             可當成 USB 硬碟來使用.
             (1). 新增安裝源: http://apt.dmytro.me/
             (2). 安裝 USB Drive 軟體.

         e. afc2add:
             可讓應用軟體( iPhone Explorer / DiskAid )瀏覽到 iPod touch
             的根目錄.

             說明: 再執行一次 "變更" 項目, 並按下左上方的 "更新".

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

E. 更改 iPod touch 的 root 密碼:
     1. 確定已用 Cydia 安裝了 OpenSSH.

     2. 共享 Mac Book Pro 的網路給 iPod touch:
         => Mac OS X > 系統偏好設定 > 共享 > Internet 共享
              (待都設定好時再勾選 "Internet 共享")
              > 共享來源: 乙太網路; 對使用以下傳輸埠的電腦: Wi-Fi

    3. iPod touch > 設定 > Wi-Fi > 停用 > 啟用
        (說明: Mac 所分享的仍然是原本所選擇的網路名稱)
        查看 IP, ex: 192.168.1.11

    4. Mac OS X > 終端機 > ssh root@192.168.1.11 (default password: alpine)
        => 如果出現以下訊息:
        WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
        (代表, 之前有 jailbreak 過不同的版本並曾登入過 iPod touch,)
        解決辦法: 先移除(這裡用搬移檔案) Mac 上 .ssh 目錄下的檔案, 再登入一次:
        $ cd /Users/lanli/.ssh
        $ mv known_hosts known_hosts_back_20120902

     5. 承上, 鍵入 passwd 來輸入新密碼.

     6. 亦可在 Mac 上使用 sftp client 的軟體連入 iPod touch 系統中.

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

F. 備註:
    1. 如果想幫 iBook 增加 "英漢字典" 功能的話, 請參考:
        I touchs: 幫 iBooks 增加 英漢字典

    2. iTunes 備份檔案的位置:
        (your home) > 資源庫 > Application Support > MobileSync > Backup

2012年8月18日 星期六

Running OpenGL on Linux Mint - 3

since: 2012/08/18
update: 2012/09/06

reference:
1. OpenGL Video Tutorial - Getting OpenGL Set Up on Linux
2. Howto Install OpenGL Development Environment
3. Computing Reference Wikispace - geany_settings
4. GLEW: The OpenGL Extension Wrangler Library

開發環境設定與繪圖測試

A. 檢查 OpenGL 與 GLUT 開發函式庫
     1. 說明:
          要讓 OpenGL 在 Linux 上正常運作, 至少要在檔案中找到 gl.h 與 glut.h 檔案,
          它們通常位於 /usr/include/GL/ 目錄下.

     2. 檢查:
         $ cd /usr/include/GL/
         $ ls

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

B. 安裝 IDE(整合開發環境)
     1. Menu > Software Manager

     2. > Programming

     3. > geany

     4. > Install

     5. 備註:
         或直接執行
         $ sudo apt-get install geany

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

C. 開發環境設定與繪圖測試
     1. 開啟 Geany
         Menu > All applications (選單右上方)
         > Programming > Geany

     2. 查看/設定 專案的檔案目錄
          Edit > Preferences
         General > Startup > Project files:
         /home/lanli/projects

     3. 建立專案目錄:
         $ cd ~
         $ mkdir projects

     4. 新增 OpenGL 測試檔案
         開啓新檔案, 寫入以下程式碼, 存檔至 /home/lanli/projects/teapot.c
#include <GL/glut.h>

void init();
void display();

int main(int argc, char* argv[])
{
 glutInit(&argc, argv);
 glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
 glutInitWindowPosition(0, 0);
 glutInitWindowSize(300, 300);
 
 glutCreateWindow("OpenGL 3D View");
 
 init();
 glutDisplayFunc(display);
 
 glutMainLoop();

 return 0;
}

void init()
{
 glClearColor(0.0, 0.0, 0.0, 0.0);
 glMatrixMode(GL_PROJECTION);
 glOrtho(-5, 5, -5, 5, 5, 15);
 glMatrixMode(GL_MODELVIEW);
 gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);
}

void display()
{
 glClear(GL_COLOR_BUFFER_BIT);
  
 glColor3f(1.0, 0, 0);
 glutWireTeapot(3);
 
 glFlush();
}

     5. 查看編譯設定
         Build > Set Build Commands

         (update: 2012/08/22)
     6. 調整編譯設定
         a. 將 Build 的 Command 改成:
             gcc -Wall -o "%e.o" "%f" -lglut -lGL -lGLU -lSDL // -lSDL 非必要

         b. 將 Execute 的 Command 改成: "./%e.o"        // 添加 .o

     7. 編譯並執行
         a. Build > Build
         b. Build > Execute

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

D. 使用 GLEW 的方式: (update: 2012/08/30)
     1. 說明:
         a. 調整編譯設定:
             將 Build 的 Command 改成: 
             gcc -Wall -o "%e.o" "%f" -lglut -lGLEW -lGL -lGLU

         b. 如果 GLEW 要跟 GLUT 一起使用的話, 必須要先 include glew.h, 然後才
     
include glut.h, 並且 glew.h 已經有 include glu.h 了.

         c. GLEW 的初始化必須要在全部的 glutInitxxx 及  glutCreateWindow 之後進行.
             (可以在註冊 callback functions 動作之前)

*******************************************************

     2. 將 teapot.c 修改如下:
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glut.h>

void init();
void display();
void initGlew();

int main(int argc, char* argv[])
{
 glutInit(&argc, argv);
 glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
 glutInitWindowPosition(0, 0);
 glutInitWindowSize(300, 300);
 
 glutCreateWindow("OpenGL 3D View"); 
 initGlew();
 init();
 glutDisplayFunc(display);
 
 glutMainLoop();

 return 0;
}

void init()
{
 glClearColor(0.0, 0.0, 0.0, 0.0);
 glMatrixMode(GL_PROJECTION);
 glOrtho(-5, 5, -5, 5, 5, 15);
 glMatrixMode(GL_MODELVIEW);
 gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);
}

void display()
{
 glClear(GL_COLOR_BUFFER_BIT);
  
 glColor3f(1.0, 0, 0);
 glutWireTeapot(3);
 
 glFlush();
}

void initGlew()
{
    GLenum err = glewInit();
   
    if(err !=  GLEW_OK)
    {
        fprintf(stderr, "glewInitError\n");
        exit(1);
    }
   
    fprintf(stderr, "GLEW initialized OK\n");
    fprintf(stderr, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
}


*******************************************************

     3. 編譯並執行
         a. Build > Build
         b. Build > Execute

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

E. 備註: (update: 2012/09/06)
     1. 後來在 geany 下使用 extern 變數時, 會造成編譯失敗的情況.
         ....
         extern void checkGLErrors(const char* caller);
         ....
         checkGLErrors("main here");
         ....

         *****************************************************

         main.c:(.text+0x70): undefined reference to `checkGLErrors'

         編譯失敗


     2. 試過安裝 codeblocks 來建立 OpenGL 專案, 編譯時也會出問題
         (可能是我不太熟  codeblocks). 

     3. 所以, 之後會改用 eclipse 來開發 OpenGL.