From 4826ee2b73d951e664a9ff751ff31788fdacc21a Mon Sep 17 00:00:00 2001 From: Rob Farthing Date: Wed, 10 Nov 2021 02:08:02 +0000 Subject: [PATCH] feat: allow the default camera resolution to be set (#347) * Fix IOS Bundle ID issue for Xcode Archive/App uploads * -Fix flipped front camera on mobile -Allow default resolution of camera to be set to 1280x720 for devices that have high resolution cameras (prevents crash behaviour on mobile) * Revert "Fix IOS Bundle ID issue for Xcode Archive/App uploads" This reverts commit a13a7229e2ded5588968cf102150e10c73efb516. * Revert "-Fix flipped front camera on mobile" This reverts commit fea330cc5c53285e189ba2159ab10b5c2c361a03. * Use a manually set default resolution within 300px to choose the nearest default resolution on mobile -Prevents huge performance issues on phones with high resolution cameras when using the sample app * Apply suggestions from code review Co-authored-by: Junrou Nishida * Refactor to improve the default width functionality - tested * Check if default resolution matches first, if not fall back to use a resolution within 300px, else return the first resolution found * Update Assets/Mediapipe/Samples/Common/Scripts/ImageSource/WebCamSource.cs Co-authored-by: Junrou Nishida * Move resolution switching from ImageSource to WebCamSource exclusively * Revert ImageConfig changes to allow default active resolution to populate resolution dropdown * Implemented resolution comparer * Ensure lf file ending again * Remove redundant boolean * Update tooltip text - Update Assets/Mediapipe/Samples/Common/Scripts/ImageSource/WebCamSource.cs Co-authored-by: Junrou Nishida * Make _preferableDefaultWidth private - Update Assets/Mediapipe/Samples/Common/Scripts/ImageSource/WebCamSource.cs Co-authored-by: Junrou Nishida * Remove unnecessary UnityEngine import * Move ResolutionStructComparer to the end of WebCamSource.cs * Ensure all lf maintained Co-authored-by: RobFarthing_EPM Co-authored-by: Junrou Nishida --- .../Scripts/ImageSource/WebCamSource.cs | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/Assets/Mediapipe/Samples/Common/Scripts/ImageSource/WebCamSource.cs b/Assets/Mediapipe/Samples/Common/Scripts/ImageSource/WebCamSource.cs index e902fdd67..b4a7e4af9 100644 --- a/Assets/Mediapipe/Samples/Common/Scripts/ImageSource/WebCamSource.cs +++ b/Assets/Mediapipe/Samples/Common/Scripts/ImageSource/WebCamSource.cs @@ -6,6 +6,7 @@ using System; using System.Collections; +using System.Collections.Generic; using System.Linq; using UnityEngine; @@ -17,6 +18,9 @@ namespace Mediapipe.Unity { public class WebCamSource : ImageSource { + [Tooltip("For the default resolution, the one whose width is closest to this value will be chosen")] + [SerializeField] private int _preferableDefaultWidth = 1280; + private const string _TAG = nameof(WebCamSource); [SerializeField] private ResolutionStruct[] _defaultAvailableResolutions; @@ -227,8 +231,7 @@ public override Texture GetCurrentTexture() private ResolutionStruct GetDefaultResolution() { var resolutions = availableResolutions; - - return (resolutions == null || resolutions.Length == 0) ? new ResolutionStruct() : resolutions[0]; + return resolutions == null || resolutions.Length == 0 ? new ResolutionStruct() : resolutions.OrderBy(resolution => resolution, new ResolutionStructComparer(_preferableDefaultWidth)).First(); } private void InitializeWebCamTexture() @@ -254,5 +257,32 @@ private IEnumerator WaitForWebCamTexture() throw new TimeoutException("Failed to start WebCam"); } } + + private class ResolutionStructComparer : IComparer + { + private readonly int _preferableDefaultWidth; + + public ResolutionStructComparer(int preferableDefaultWidth) + { + _preferableDefaultWidth = preferableDefaultWidth; + } + + public int Compare(ResolutionStruct a, ResolutionStruct b) + { + var aDiff = Mathf.Abs(a.width - _preferableDefaultWidth); + var bDiff = Mathf.Abs(b.width - _preferableDefaultWidth); + if (aDiff != bDiff) + { + return aDiff - bDiff; + } + if (a.height != b.height) + { + // prefer smaller height + return a.height - b.height; + } + // prefer smaller frame rate + return (int)(a.frameRate - b.frameRate); + } + } } }