ngx-scanner icon indicating copy to clipboard operation
ngx-scanner copied to clipboard

Poor sharpness on iPhone Pro

Open wirthti opened this issue 1 year ago • 1 comments

Hi!

I had an issue where the sharpness of the image was really poor on the newest iPhone (13 and up). I fixed it by adding this to my code, overriding the behaviour of some components:

ZXingScannerComponent.prototype.getAnyVideoDevice = (): Promise<MediaStream> => {
            return navigator.mediaDevices.getUserMedia({
                audio: false,
                video: {
                    width: { min: 640, ideal: 1920 },
                    height: { min: 400, ideal: 1080 },
                    aspectRatio: { ideal: 1.7777777778 }
                }
            });
        };


       BrowserCodeReader.prototype.decodeFromVideoDevice = async function(
            deviceId: string | undefined,
            previewElem: string | HTMLVideoElement | undefined,
            callbackFn: any,
        ): Promise<IScannerControls> {
            // We had to comment this out because it is a private method...
            // BrowserCodeReader.checkCallbackFnOrThrow(callbackFn);

            let videoConstraints: MediaTrackConstraints;

            if (!deviceId) {
                videoConstraints = { facingMode: 'environment' };
            } else {
                videoConstraints = {
                    deviceId: { exact: deviceId },
                    width: { min: 640, ideal: 1920 },
                    height: { min: 400, ideal: 1080 },
                    aspectRatio: { ideal: 1.7777777778 }
                };
            }

            const constraints: MediaStreamConstraints = { video: videoConstraints };

            return await this.decodeFromConstraints(constraints, previewElem, callbackFn);
        }

This forces the camera to use full HD resolution. The problem is, when used in iPhones with 3 back cameras, where one of them is specifically used for close up pictures, it does not work. The image is blurry, because it does not automatically change to this camera. The main camera in those iPhones is not projected for close up pictures, so the image when trying to read a barcode is very blurry, so it doesn't work.

Is there any way to force the phone to use this special close up camera when present?

wirthti avatar Sep 11 '23 12:09 wirthti

not so sure just give it a try may be it works for you it did for me

just decrease the values

width: { min: 640, ideal: 1080}, height: { min: 400, ideal: 480},

jaiminpatelmicrotech avatar Sep 28 '23 13:09 jaiminpatelmicrotech