3

I'm currently developing an iPad application that is using ZBarSDK for reading QR Codes.

I am using ZBarReaderView (NOT UIViewController), so i'm showing the front facing camera in a UIView that is drawn using CGRect (see code below).

My problem is that the camera comes up on it's side within the view. The iPad app will ONLY be developed to work in Landscape mode.

When the UIView comes up now, the image is on it's side and i'm not sure how to fix it. I tried using CGAffineTransform which successfully changes the angle somewhat but how will this handle if the iPad is flipped the other way?

I saw some other posts about this similar issue but no resolution that has worked for me. Worth noting i'm using IOS6 and please remember i'm using ZBarReaderView, so the supportedOrientationsMask is NOT available.

I want the user to see the camera view in the upright position regardless of whether landscape is left or right. Right now though, regardless of which way it is turned, the video is sideways (so I can see myself at 90 degrees to the side). The UIView doesn't change at all when the screen is turned either. I'm a bit lost.

Code:

- (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;
}
Guillaume Algis
  • 10,705
  • 6
  • 44
  • 72
Bobster4300
  • 254
  • 4
  • 15

3 Answers3

6

I got the same issue and resolved it symply !

I just needed to call :

[reader willRotateToInterfaceOrientation: toInterfaceOrientation duration: duration];

within the willRotate... of the view controller that contains the ZbarReaderView.

user2241649
  • 81
  • 1
  • 3
3

Okay I got the orientation working now. No idea why it works now but here's the end code for the readerView part:

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];

Notice the only real difference there is this line:

ZBarReaderView * readerView = [ZBarReaderView new];

Changing that line has fixed the initial orientation issue. No idea why but it did. Now I just need to fix the rotation stuff for if the user rotates the iPad.

Bobster4300
  • 254
  • 4
  • 15
  • Thank you so much on this answer, but may I know what is readerView.device? how to implement the camera flip that allow user to choose whether want to use front or back camera? – Js Lim Mar 15 '13 at 08:27
1

This worked fine for me:

// 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;

//To support Landscape orientation

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);
}
Joe M
  • 669
  • 6
  • 9