chore: Simplify format sorting/filtering (#140)

* Simplify format sorting/filtering

* Update useCameraFormat.ts

* Also check photo HDR

* Simplify double tap

* Remove snapshot

* Remove custom `useCameraDevice` hook

* Update Podfile.lock
This commit is contained in:
Marc Rousavy
2021-05-14 11:52:28 +02:00
committed by GitHub
parent f839bc23ac
commit 310ad5fc4c
7 changed files with 37 additions and 200 deletions

View File

@@ -1,7 +1,6 @@
import { useMemo } from 'react';
import type { CameraDevice, CameraDeviceFormat } from '../CameraDevice';
import { filterFormatsByAspectRatio, sortFormatsByResolution } from '../utils/FormatFilter';
import type { Size } from '../utils/FormatFilter';
import { sortFormats } from '../utils/FormatFilter';
/**
* Returns the best format for the given camera device.
@@ -9,27 +8,9 @@ import type { Size } from '../utils/FormatFilter';
* This function tries to choose a format with the highest possible photo-capture resolution and best matching aspect ratio.
*
* @param {CameraDevice} device The Camera Device
* @param {Size} cameraViewSize The Camera View's size. This can be an approximation and **must be memoized**! Default: `SCREEN_SIZE`
*
* @returns The best matching format for the given camera device, or `undefined` if the camera device is `undefined`.
*/
export function useCameraFormat(device?: CameraDevice, cameraViewSize?: Size): CameraDeviceFormat | undefined {
const formats = useMemo(() => {
if (device?.formats == null) return [];
const filtered = filterFormatsByAspectRatio(device.formats, cameraViewSize);
const sorted = filtered.sort(sortFormatsByResolution);
const bestFormat = sorted[0];
if (bestFormat == null) return [];
const bestFormatResolution = bestFormat.photoHeight * bestFormat.photoWidth;
return sorted.filter((f) => {
// difference in resolution in percent (e.g. 100x100 is 0.5 of 200x200)
const resolutionDiff = (bestFormatResolution - f.photoHeight * f.photoWidth) / bestFormatResolution;
// formats that are less than 25% of the bestFormat's resolution are dropped. They are too much quality loss
return resolutionDiff <= 0.25;
});
}, [device?.formats, cameraViewSize]);
return formats[0];
export function useCameraFormat(device?: CameraDevice): CameraDeviceFormat | undefined {
return useMemo(() => device?.formats.sort(sortFormats)[0], [device?.formats]);
}