Dragon warrior
Dragon warrior

Reputation: 1644

How can I make the BarcodeScanner plugin works in the landscape mode?

I am developing a iOS app using Cordova, and I download the Cordova Barcode Scanner Plugin from this link.

However, it only works in portrait mode.

I make some changes in CDVBarcodeScanner.mm.

#pragma mark CDVBarcodeScannerOrientationDelegate

- (BOOL)shouldAutorotate
{   
    return YES;// NO;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll; // UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ((self.orientationDelegate != nil) && [self.orientationDelegate respondsToSelector:@selector(shouldAutorotateToInterfaceOrientation:)]) {
        return [self.orientationDelegate shouldAutorotateToInterfaceOrientation:interfaceOrientation];
    }

    return YES;
}

- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration
{
//    [CATransaction begin];
//    
//    self.processor.previewLayer.orientation = orientation;
//    [self.processor.previewLayer layoutSublayers];
//    self.processor.previewLayer.frame = self.view.bounds;
//    
//    [CATransaction commit];
//    [super willAnimateRotationToInterfaceOrientation:orientation duration:duration];

    [UIView setAnimationsEnabled:NO];
    AVCaptureVideoPreviewLayer* previewLayer = self.processor.previewLayer;
    previewLayer.frame = self.view.bounds;

    if (orientation == UIInterfaceOrientationLandscapeLeft) {
        [previewLayer setOrientation:AVCaptureVideoOrientationLandscapeLeft];
    } else if (orientation == UIInterfaceOrientationLandscapeRight) {
        [previewLayer setOrientation:AVCaptureVideoOrientationLandscapeRight];
    } else if (orientation == UIInterfaceOrientationPortrait) {
        [previewLayer setOrientation:AVCaptureVideoOrientationPortrait];
    } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
        [previewLayer setOrientation:AVCaptureVideoOrientationPortraitUpsideDown];
    }

    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [UIView setAnimationsEnabled:YES];
}

I can rotate to the Landscape mode now, but it still only work in the portrait mode. How can I fix it?


According to the solution, I delete if (result.empty() && hints.getTryHarder() && image->isRotateSupported()) {} in zxing-all-in-one.cpp.

However, it only works in landscape now.

Upvotes: 2

Views: 1885

Answers (2)

Robin
Robin

Reputation: 27

You can also modify the CDVBarcodeScanner.mm. Just replace all ***Portraits with LandscapeLeft/LandscapeRight -> I prefer LandscapeLeft.

Upvotes: 0

Dragon warrior
Dragon warrior

Reputation: 1644

On zxing-all-in-one.cpp file,

change

if (result.empty() && hints.getTryHarder() && image->isRotateSupported()) {}

to

if (result.empty()) {}

Upvotes: 1

Related Questions