capacitor-community / barcode-scanner

A fast and efficient (QR) barcode scanner for Capacitor
MIT License
436 stars 172 forks source link

CameraDirection not work on iOS? #224

Open Lyfei opened 1 year ago

Lyfei commented 1 year ago

Describe the bug Set the front camera, but it's always the back camera

To Reproduce

It doesn't work either at the beginning or in the middle

    async startScan() {
        BarcodeScanner.hideBackground();
        const option: ScanOptions = { cameraDirection: CameraDirection.FRONT };
        await BarcodeScanner.prepare(option);
        const result = await BarcodeScanner.startScan(option); // start scanning and wait for a result
        if (result.hasContent) {
            console.log(result.content); // log the raw scanned content
        }
    }

    async toggleCamera() {
        this.frontCamera = !this.frontCamera;
        if (this.frontCamera) {
            await BarcodeScanner.prepare({ cameraDirection: CameraDirection.FRONT }).catch(console.log);
        } else {
            await BarcodeScanner.prepare({ cameraDirection: CameraDirection.BACK }).catch(console.log);;
        }
    }

Expected behavior Use FRONT

Version 3.0.1

Smartphone (please complete the following information):

Ionic:

Ionic CLI : 6.20.9 (/usr/local/lib/node_modules/@ionic/cli) Ionic Framework : @ionic/angular 6.0.10 @angular-devkit/build-angular : 12.2.17 @angular-devkit/schematics : 12.2.17 @angular/cli : 12.2.17 @ionic/angular-toolkit : 5.0.3

Capacitor:

Capacitor CLI : 4.6.1 @capacitor/android : 4.6.1 @capacitor/core : 4.6.1 @capacitor/ios : 4.6.1

Utility:

cordova-res (update available: 0.15.4) : 0.8.1 native-run : 1.7.2

System:

NodeJS : v12.14.1 (/usr/local/bin/node) npm : 6.13.4 OS : macOS Monterey

Device: iPhone 12, iOS 16.1

kumarjitendra-epic commented 1 year ago

And reverse is happening in Android. Camera is set to "back", still it always opens the front camera and no control to switch to back one.

thegnuu commented 1 year ago

This is weird, and of course it is working in my debug environment. Can you guys eventually share your repos so I can have a look at it and test it with a not working example?

kumarjitendra-epic commented 1 year ago

It is working when I open the app as web on my browser. It opens the back camera perfectly then but when I use the apk for use on app on mobile, it opens the front. Also, it is not scanning the barcode, which I think is a known issue

async checkPermission() { return new Promise(async (resolve, reject) => { const status = await BarcodeScanner.checkPermission({ force: true }); if (status.granted) { resolve(true); } else if (status.denied) { alert('Give access to camera'); } else { resolve(false); } }); }

async stopScan() { BarcodeScanner.stopScan(); }

async prepareScan() { await BarcodeScanner.prepare() }

async onScan() { await this.checkPermission(); await BarcodeScanner.hideBackground(); document.querySelector('body').classList.add('scanner-active'); const result = await BarcodeScanner.startScan({ cameraDirection: "back" }); console.log('After Start Scan ' + result.content); await BarcodeScanner.showBackground(); document.querySelector('body').classList.remove('scanner-active'); }

cychongaspevo commented 1 year ago

iphone 11 iOS 16.4.1 is working, i trying to switch camera, what I did was start with "Front" camera, then switch camera, but need to stop the camera first then start camera again. but this step must have a delay on start camera again. Not sure on android.

switchCamera() {
    this.stopScanner();
    if (this.defaultCameraDirection == CameraDirection.FRONT) {
      this.defaultCameraDirection = CameraDirection.BACK;
    } else {
      this.defaultCameraDirection = CameraDirection.FRONT;
    }

    setTimeout(() => {
      this.startScanner();
    }, 100);
  }
Lyfei commented 1 year ago

iphone 11 iOS 16.4.1 is working, i trying to switch camera, what I did was start with "Front" camera, then switch camera, but need to stop the camera first then start camera again. but this step must have a delay on start camera again. Not sure on android.

switchCamera() {
    this.stopScanner();
    if (this.defaultCameraDirection == CameraDirection.FRONT) {
      this.defaultCameraDirection = CameraDirection.BACK;
    } else {
      this.defaultCameraDirection = CameraDirection.FRONT;
    }

    setTimeout(() => {
      this.startScanner();
    }, 100);
  }

Worked!

If my method 'this.startScan();' does include 'BarcodeScanner.prepare()' will fail. It seems that 'prepare' is only available on iOS for getting started.

BTW, you do not need to use 'stopScan' to cause a short white screen. Just start again.

This is my revised version, both native have been tested.

    toggleCamera() {
        this.isDirectionBack = !this.isDirectionBack
        this.startScan(this.isDirectionBack)
    }

    async firstScan() { 
        await BarcodeScanner.hideBackground();
        await BarcodeScanner.prepare()
        this.startScan(this.isDirectionBack);
    }

    async startScan(isDirectionBack = true) {
        const option: ScanOptions = {
            cameraDirection: (isDirectionBack ? CameraDirection.BACK : CameraDirection.FRONT)
        };
        const result: ScanResult = await BarcodeScanner.startScan(option)

        if (result.hasContent) {
        }
    }