3

我目前正在开发一个使用 ZBarSDK 读取 QR 码的 iPad 应用程序。

我正在使用ZBarReaderView (NOT ),所以我正在使用绘制UIViewController的前置摄像头(参见下面的代码)。UIViewCGRect

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

现在出现UIView时,图像在它的一侧,我不知道如何修复它。我尝试使用CGAffineTransformwhich 成功地稍微改变了角度,但是如果 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;
}
4

3 回答 3

6

我遇到了同样的问题并简单地解决了!

我只需要打电话:

[reader willRotateToInterfaceOrientation: toInterfaceOrientation duration: duration];

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

于 2013-04-03T17:46:25.827 回答
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,我只需要修复旋转的东西。

于 2012-12-30T08:03:30.583 回答
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);
}
于 2013-03-01T07:55:48.060 回答