2012-12-30 1 views
3

Je développe actuellement une application iPad qui utilise ZBarSDK pour lire des codes QR.ZBarReaderView UIView Problème d'orientation de la caméra

J'utilise ZBarReaderView (pas UIViewController), donc je suis montrant la caméra face à l'avant dans une UIView tracée à l'aide CGRect (voir code ci-dessous).

Mon problème est que l'appareil photo se place sur le côté dans la vue. L'application iPad sera uniquement développée pour fonctionner en mode paysage.

Lorsque le UIView apparaît maintenant, l'image est sur le côté et je ne sais pas comment la réparer. J'ai essayé d'utiliser CGAffineTransform qui change avec succès l'angle un peu mais comment cela va-t-il gérer si l'iPad est inversé dans l'autre sens?

J'ai vu d'autres articles sur ce problème similaire, mais aucune résolution qui a fonctionné pour moi. À noter que j'utilise IOS6 et s'il vous plaît rappelez-vous que j'utilise ZBarReaderView, de sorte que le supportedOrientationsMask n'est pas disponible.

Je souhaite que l'utilisateur voit la vue de la caméra en position verticale, que le paysage soit à gauche ou à droite. En ce moment, peu importe de quel côté il est tourné, la vidéo est de côté (donc je peux me voir à 90 degrés sur le côté). Le UIView ne change pas du tout lorsque l'écran est activé. Je suis un peu perdu.

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

Répondre

3

D'accord, je suis l'orientation travaille maintenant. Aucune idée pourquoi cela fonctionne maintenant, mais voici le code de fin pour la partie 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]; 

avis, la seule différence il y a cette ligne:

ZBarReaderView * readerView = [ZBarReaderView new]; 

Changer cette ligne a résolu le problème d'orientation initiale. Aucune idée pourquoi mais il l'a fait. Maintenant, j'ai juste besoin de réparer les trucs de rotation pour si l'utilisateur tourne l'iPad.

+0

Merci beaucoup pour cette réponse, mais puis-je savoir ce qu'est readerView.device? comment mettre en œuvre la caméra flip qui permettent à l'utilisateur de choisir si vous souhaitez utiliser une caméra avant ou arrière? –

1

Cela a bien fonctionné pour moi:

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

// Pour soutenir l'orientation paysage

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

Je suis le même problème et l'a résolu Symply!

Je avais juste besoin d'appeler:

[reader willRotateToInterfaceOrientation: toInterfaceOrientation duration: duration]; 

dans le willRotate ... du contrôleur de vue qui contient le ZbarReaderView.

+1

C'est la manière correcte de gérer le changement d'orientation de ZBar. – Deminetix