2012-12-30 49 views
3

我目前正在开发一款使用ZBarSDK读取QR码的iPad应用程序。ZBarReaderView UIView相机方向问题

我使用ZBarReaderView(NOT UIViewController),所以我示出被使用CGRect取用一个UIView置摄像头的前部(参见下面的代码)。

我的问题是相机在视图内出现在它的一侧。 iPad应用程序只能开发为在横向模式下工作。

UIView现在出现时,图像是在它的一边,我不知道如何解决它。我尝试使用CGAffineTransform,它成功地改变了角度,但如果iPad以另一种方式翻转,这将如何处理?

我看到一些关于这个类似问题的其他文章,但没有解决方案已经为我工作。值得注意的是我使用的IOS6,请记住我使用ZBarReaderView,所以supportedOrientationsMask不可用。

我希望用户能够以直立的姿势看到相机视图,而不管风景是向左还是向右。不过现在,无论它转向哪个方向,视频都是横向的(所以我可以看到自己与侧面成90度角)。当屏幕转动时,UIView根本不会改变。我有点失落。

代码:

- (void) viewDidAppear: (BOOL) animated { 

    ZBarReaderView * readerView = [ZBarReaderView new]; 
    ZBarImageScanner * scanner = [ZBarImageScanner new]; 
    [scanner setSymbology: ZBAR_I25 
        config: ZBAR_CFG_ENABLE 
         to: 0]; 

    [readerView initWithImageScanner:scanner]; 
    readerView.readerDelegate = self; 
    readerView.tracksSymbols = YES; 

    readerView.frame = CGRectMake(20,220,230,230); 
    readerView.torchMode = 0; 
    readerView.device = [self frontFacingCameraIfAvailable]; 
    [readerView start]; 

    [self.view addSubview: readerView]; 

} 

-(void)relocateReaderPopover:(UIInterfaceOrientation)toInterfaceOrientation { 
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) { 
     readerView.previewTransform = CGAffineTransformMakeRotation(M_PI_2); 
    } else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) { 
     readerView.previewTransform = CGAffineTransformMakeRotation(-M_PI_2); 
    } else if (toInterfaceOrientation== UIInterfaceOrientationPortraitUpsideDown) { 
     readerView.previewTransform = CGAffineTransformMakeRotation(M_PI); 
    } else { 
     readerView.previewTransform = CGAffineTransformIdentity; 
    } 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) { 
     return YES; 
    } else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) { 
     return YES; 
    } else { 
     return NO; 
    } 
} 

- (void) willRotateToInterfaceOrientation: (UIInterfaceOrientation) orient 
           duration: (NSTimeInterval) duration 
{ 
    if (orient == UIInterfaceOrientationLandscapeLeft) { 
     [readerView willRotateToInterfaceOrientation: orient 
              duration: duration]; 

    } 
    if (orient == UIInterfaceOrientationLandscapeRight) { 
     [readerView willRotateToInterfaceOrientation: orient 
              duration: duration]; 

    } 
} 

- (BOOL)shouldAutorotate { 

    UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation]; 

    if (orientation == UIInterfaceOrientationLandscapeLeft) { 
     return YES; 
    } else if (orientation == UIInterfaceOrientationLandscapeRight) { 
     return YES; 
    } else { 
     return NO; 
    } 
} 

-(NSUInteger) supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskLandscape; 
} 

回答

3

好吧,我得到了现在的方向工作。不知道为什么,现在的工作,但这里是为readerView部分结束代码:

readerView = [ZBarReaderView new]; 
ZBarImageScanner * scanner = [ZBarImageScanner new]; 
[scanner setSymbology: ZBAR_I25 
       config: ZBAR_CFG_ENABLE 
        to: 0]; 
[readerView initWithImageScanner:scanner]; 
readerView.readerDelegate = self; 

readerView.tracksSymbols = YES; 

readerView.frame = CGRectMake(20,220,230,230); 
readerView.torchMode = 0; 
readerView.device = [self frontFacingCameraIfAvailable]; 

[self relocateReaderPopover:[self interfaceOrientation]]; 

[readerView start]; 

[self.view addSubview: readerView]; 

注意到的唯一真正的区别有这一行:

ZBarReaderView * readerView = [ZBarReaderView new]; 

更改该行有固定的初始取向问题。不知道为什么,但它确实如此。现在我只需要修复用户旋转iPad的旋转东西。

+0

非常感谢你回答这个问题,但是我可以知道什么是readerView.device?如何实现相机翻转,让用户选择是否要使用前置或后置摄像头? –

1

这个工作对我罚款:

// ADD: present a barcode reader that scans from the camera feed 
ZBarReaderViewController *reader = [ZBarReaderViewController new]; 
reader.readerDelegate = self; 
//reader.supportedOrientationsMask = ZBarOrientationMaskAll; 
ScanOverlay *overlayController = [[ScanOverlay alloc] initWithNibName:@"ScanOverlay" bundle:nil]; 
reader.cameraOverlayView = overlayController.view; 
ZBarImageScanner *scanner = reader.scanner; 
// TODO: (optional) additional reader configuration here 
reader.supportedOrientationsMask = ZBarOrientationMask(UIInterfaceOrientationLandscapeRight||UIInterfaceOrientationLandscapeLeft); 
reader.wantsFullScreenLayout = YES; 
reader.showsZBarControls = NO; //If we don't set this to NO, the overlay may not display at all 
reader.tracksSymbols = YES; 

//为了支持横向方向

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; 
if (UIDeviceOrientationLandscapeLeft == orientation) { 
    //Rotate 90 
    reader.cameraViewTransform = CGAffineTransformMakeRotation (3*M_PI/2.0); 
} else if (UIDeviceOrientationLandscapeRight == orientation) { 
    //Rotate 270 
    reader.cameraViewTransform = CGAffineTransformMakeRotation (M_PI/2.0); 
} 
6

我得到了同样的问题,解决它symply!

我只需要拨打:

[reader willRotateToInterfaceOrientation: toInterfaceOrientation duration: duration]; 

的willRotate内...包含ZbarReaderView视图控制器。

+1

这是处理ZBar方向变化的正确方法。 – Deminetix