2012-12-24 12 views
4

我正在使用zbarsdk來讀取條形碼。它工作正常,但我的問題是我已經添加覆蓋視圖到ZBarReaderViewController(讀者在我的代碼中)。所以現在我試着添加水龍頭來關注功能。但它正在崩潰。以下是我的代碼。提前致謝 。任何想法都會很感激。添加水龍頭以將功能集中到ZBarReaderViewController

-(IBAction)scanBarCode:(id)sender 
{ 
    barcodeClicked = 1; 

    NSLog(@"TBD: scan barcode here..."); 
    // ADD: present a barcode reader that scans from the camera feed 
    reader = [ZBarReaderViewController new]; 
    reader.readerDelegate = self; 
    reader.supportedOrientationsMask = ZBarOrientationMaskAll; 

    ZBarImageScanner *scanner = reader.scanner; 
    // TODO: (optional) additional reader configuration here 

    // EXAMPLE: disable rarely used I2/5 to improve performance 
    [scanner setSymbology: ZBAR_I25 
        config: ZBAR_CFG_ENABLE 
         to: 0]; 
    reader.showsZBarControls = NO; 

    UIView *ovlView = [[UIView alloc] init]; 
    [ovlView setFrame:CGRectMake(0, 0, 320, 480)]; 
    [ovlView setBackgroundColor:[UIColor clearColor]]; 

    UIImageView *leftBracket = [[UIImageView alloc] init]; 
    [leftBracket setFrame:CGRectMake(21, 100, 278, 15)]; 
    [leftBracket setImage:[UIImage imageNamed:@"TopBracket.png"]]; 

    UIImageView *rightBracket = [[UIImageView alloc] init]; 
    [rightBracket setFrame:CGRectMake(21, 240, 278, 15)]; 
    [rightBracket setImage:[UIImage imageNamed:@"BottomBracket.png"]]; 

    UIToolbar *bottomBar = [[UIToolbar alloc] init]; 
    [bottomBar setBarStyle:UIBarStyleBlackOpaque]; 

    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height * [UIScreen mainScreen].scale >= 1136) 
    { 
     [bottomBar setFrame:CGRectMake(0, 524, 320, 44)]; 
    } 
    else 
     [bottomBar setFrame:CGRectMake(0, 436, 320, 44)]; 

    UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelCamera)]; 
    /*UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                       target:nil 
                       action:nil];*/ 
    //UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithTitle:@" Info " style:UIBarButtonItemStyleBordered target:self action:@selector(infoButton)]; 
    /*UIButton *info = [UIButton buttonWithType:UIButtonTypeInfoLight]; 
    [info addTarget:self action:@selector(infoButton) forControlEvents:UIControlEventTouchUpInside]; 
    UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithCustomView:info]; 

    [bottomBar setItems:[NSArray arrayWithObjects:cancel,flexItem,infoButton, nil]];*/ 

    [bottomBar setItems:[NSArray arrayWithObjects:cancel, nil]]; 

    [ovlView addSubview:leftBracket]; 
    [ovlView addSubview:rightBracket]; 
    [ovlView addSubview:bottomBar]; 

    reader.cameraOverlayView = ovlView; 

    // present and release the controller 
    [self presentModalViewController:reader 
          animated: YES]; 
    [reader release]; 
} 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UIView * previewView = [[[[[[[[[[ 
            reader.view // UILayoutContainerView 
            subviews] objectAtIndex:0] // UINavigationTransitionView 
            subviews] objectAtIndex:0] // UIViewControllerWrapperView 
           subviews] objectAtIndex:0] // UIView 
           subviews] objectAtIndex:0] // PLCameraView 
          subviews] objectAtIndex:0]; // PLPreviewView 
    [previewView touchesBegan:touches withEvent:event]; 
} 
+0

嗨,斯里,你最終成功了嗎?如果是的話,你可以談談如何做到這一點? – Mathieu

回答

1

查看「對焦模式」一節中該文件由蘋果:https://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/04_MediaCapture.html它討論所有關於如何實現水龍頭正確聚焦。我會嘗試通過

CGRect screenRect = [[UIScreen mainScreen] bounds]; 
    screenWidth = screenRect.size.width; 
    screenHeight = screenRect.size.height; 
    double focus_x = thisFocusPoint.center.x/screenWidth; 
    double focus_y = thisFocusPoint.center.y/screenHeight; 

    [[self captureManager].videoDevice lockForConfiguration:&error]; 
    [[self captureManager].videoDevice setFocusPointOfInterest:CGPointMake(focus_x,focus_y)]; 

那麼實現這一點,如果你正在使用的視圖控制器,增加一個(無效),應該是OK的barcodeviewcontroller實施方式。

- (void) focusAtPoint:(CGPoint)point 

{ 

    AVCaptureDevice *device = [[self videoInput] device]; 

    if ([device isFocusPointOfInterestSupported] && [device isFocusModeSupported:AVCaptureFocusModeAutoFocus]) { 

     NSError *error; 

     if ([device lockForConfiguration:&error]) { 

      [device setFocusPointOfInterest:point]; 

      [device setFocusMode:AVCaptureFocusModeAutoFocus]; 

      [device unlockForConfiguration]; 

     } else { 

      id delegate = [self delegate]; 

      if ([delegate respondsToSelector:@selector(acquiringDeviceLockFailedWithError:)]) { 

       [delegate acquiringDeviceLockFailedWithError:error]; 

      } 

     }   

    } 

} 
+0

我已經使用了ZBarReaderViewController類。我必須修改這個控制器 – sri

+0

用新代碼編輯 – MacN00b

10

感謝您對MacN00b的回答!這爲我指出了正確的方向。我已經爲zbarViewController實現了tap-focus。這是想法:

您可以通過將自定義視圖分配給其cameraOverlayView來將自定義視圖添加到zbarViewController。然後將TapGestureRecagonizer添加到自定義視圖以捕捉水龍頭。然後,獲取觸點並使相機對焦於觸點。你可能會在觸摸點周圍添加一個矩形(這就是我所做的)。

這裏不用代碼(分配自定義視圖cameraOverlayView:

UIView *view = [[UIView alloc] init]; 
UITapGestureRecognizer* tapScanner = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(focusAtPoint:)]; 
[view addGestureRecognizer:tapScanner];  
reader.cameraOverlayView = view; 
在選擇 focusAtPoint

然後:

- (void)focusAtPoint:(id) sender{ 
    CGPoint touchPoint = [(UITapGestureRecognizer*)sender locationInView:_reader.cameraOverlayView]; 
    double focus_x = touchPoint.x/_reader.cameraOverlayView.frame.size.width; 
    double focus_y = (touchPoint.y+66)/_reader.cameraOverlayView.frame.size.height; 
    NSError *error; 
    NSArray *devices = [AVCaptureDevice devices]; 
    for (AVCaptureDevice *device in devices){ 
     NSLog(@"Device name: %@", [device localizedName]); 
     if ([device hasMediaType:AVMediaTypeVideo]) { 
      if ([device position] == AVCaptureDevicePositionBack) { 
       NSLog(@"Device position : back"); 
       CGPoint point = CGPointMake(focus_y, 1-focus_x); 
       if ([device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus] && [device lockForConfiguration:&error]){ 
        [device setFocusPointOfInterest:point]; 
        CGRect rect = CGRectMake(touchPoint.x-30, touchPoint.y-30, 60, 60); 
        UIView *focusRect = [[UIView alloc] initWithFrame:rect]; 
        focusRect.layer.borderColor = [UIColor whiteColor].CGColor; 
        focusRect.layer.borderWidth = 2; 
        focusRect.tag = 99; 
        [_reader.cameraOverlayView addSubview:focusRect]; 
        [NSTimer scheduledTimerWithTimeInterval: 1 
           target: self 
           selector: @selector(dismissFocusRect) 
           userInfo: nil 
           repeats: NO]; 
        [device setFocusMode:AVCaptureFocusModeAutoFocus]; 
        [device unlockForConfiguration]; 
       } 
      } 
     } 
    } 
} 

我已經添加了白色矩形圍繞觸摸點,然後使用選擇器dismissFocusRect關閉此矩形。以下是代碼:

- (void) dismissFocusRect{ 
    for (UIView *subView in _reader.cameraOverlayView.subviews) 
    { 
     if (subView.tag == 99) 
     { 
      [subView removeFromSuperview]; 
     } 
    } 
} 

我希望這可以幫助!

+0

不要忘了使用'AVFoundation.framework'和'#import #import TomWayne