googlesamples / ios-vision

Apache License 2.0
94 stars 40 forks source link

Barcode Detector Example: Scanned barcode Shape(purpleColor) shows in wrong location. #5

Open iosnatarajan opened 7 years ago

iosnatarajan commented 7 years ago

When I try to scan a linear and 2D barcodes, the scanned area(purple shape) shows in wrong location on preview layer. Herewith I have attached the screenshots which reflects this issue.

Note: This issue only occurs when I hold the device horizontally at the top of barcode.

Thanks!

img_1857 img_1858

TomSteward commented 6 years ago

Hi there! Thank you for trying out the API. Let's see can I help you solve the issue.

First, I want to confirm how to reproduce the issue you're having:

  1. hold the phone in portrait mode in upright position
  2. rotate the phone so the device is parallel to the ground with the screen facing upwards
  3. rotate the phone while having the device parallel to the ground with the screen facing upwards
  4. you are seeing barcode overlay in the wrong location

If the above steps reproduced the issue you're seeing, then this is due to the incorrect GMVDetectorImageOrientation value is passing to the featuresInImage:options: method on GMVDetector.

Here's some more detail about what's going on.

When the device orientation is UIDeviceOrientationFaceUp or UIDeviceOrientationFaceDown, the application doesn't understand it's rotated. This is also why the screenshot you took has portrait mode UI instead of landscape mode UI.

If you try to scan a barcode in UIDeviceOrientationLandscapeLeft or UIDeviceOrientationLandscapeRight position, then the overlay should show up correctly.

The barcode API is doing the right thing. This is more of an issue in the sample app. Below is the code snippet from the sample app where the barcode API is called.

  UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
  GMVImageOrientation orientation = [GMVUtility
      imageOrientationFromOrientation:deviceOrientation
            withCaptureDevicePosition:devicePosition
             defaultDeviceOrientation:self.lastKnownDeviceOrientation];
  NSDictionary *options = @{
    GMVDetectorImageOrientation : @(orientation)
  };

  NSArray<GMVBarcodeFeature *> *barcodes = [self.barcodeDetector featuresInImage:image
                                                                         options:options];

The sample code uses lastKnownDeviceOrientation as the orientation to use when the device orientation is either UIDeviceOrientationFaceUp or UIDeviceOrientationFaceDown. You can read more details on the GMVUtility imageOrientationFromOrientation:withCaptureDevicePosition:defaultDeviceOrientation: method in the API reference.

To fix the issue, instead of using the lastKnownDeviceOrientation, it should be replaced with a UIDeviceOrientation derived from UIInterfaceOrientation.

Please feel free to submit a patch to the issue. Otherwise we can update the sample code in the next update.

ethras commented 6 years ago

any update on this issue?