From 8239450a2bc45e2e2f73c6459afa779347e3e09a Mon Sep 17 00:00:00 2001 From: Hector Alonso Date: Mon, 10 Apr 2023 00:44:18 +0200 Subject: [PATCH] Added recrod video/audio. Changed Android Handler --- Camera.MAUI.Test/Camera.MAUI.Test.csproj | 5 + Camera.MAUI.Test/FullScreenPage.xaml.cs | 2 + Camera.MAUI.Test/MVVM/CameraViewModel.cs | 183 ++++ Camera.MAUI.Test/MVVM/MVVMPage.xaml | 64 ++ Camera.MAUI.Test/MVVM/MVVMPage.xaml.cs | 9 + Camera.MAUI.Test/MainPage.xaml | 1 + Camera.MAUI.Test/MainPage.xaml.cs | 5 + Camera.MAUI.Test/MauiProgram.cs | 7 +- .../Platforms/Android/AndroidManifest.xml | 2 + Camera.MAUI.Test/Platforms/iOS/Info.plist | 2 + Camera.MAUI.Test/SizedPage.xaml | 23 +- Camera.MAUI.Test/SizedPage.xaml.cs | 53 +- Camera.MAUI/Apple/MauiCameraView.cs | 147 ++- Camera.MAUI/Camera.MAUI.csproj | 9 +- Camera.MAUI/CameraResult.cs | 3 +- Camera.MAUI/CameraView.cs | 222 +++- Camera.MAUI/CameraViewHandler.cs | 32 +- Camera.MAUI/MicrophoneInfo.cs | 11 + .../Platforms/Android/MauiCameraView.cs | 642 +++++++----- .../Platforms/Windows/MauiCameraView.cs | 120 ++- MauiCameraView.backup | 980 ++++++++++++++++++ Readme.md | 63 +- 22 files changed, 2205 insertions(+), 380 deletions(-) create mode 100644 Camera.MAUI.Test/MVVM/CameraViewModel.cs create mode 100644 Camera.MAUI.Test/MVVM/MVVMPage.xaml create mode 100644 Camera.MAUI.Test/MVVM/MVVMPage.xaml.cs create mode 100644 Camera.MAUI/MicrophoneInfo.cs create mode 100644 MauiCameraView.backup diff --git a/Camera.MAUI.Test/Camera.MAUI.Test.csproj b/Camera.MAUI.Test/Camera.MAUI.Test.csproj index 6451e30..a656ee8 100644 --- a/Camera.MAUI.Test/Camera.MAUI.Test.csproj +++ b/Camera.MAUI.Test/Camera.MAUI.Test.csproj @@ -53,6 +53,8 @@ + + @@ -67,6 +69,9 @@ MSBuild:Compile + + MSBuild:Compile + MSBuild:Compile diff --git a/Camera.MAUI.Test/FullScreenPage.xaml.cs b/Camera.MAUI.Test/FullScreenPage.xaml.cs index 0094673..8a0d312 100644 --- a/Camera.MAUI.Test/FullScreenPage.xaml.cs +++ b/Camera.MAUI.Test/FullScreenPage.xaml.cs @@ -13,6 +13,7 @@ private void CameraView_CamerasLoaded(object sender, EventArgs e) if (cameraView.Cameras.Count > 0) { cameraView.Camera = cameraView.Cameras.First(); + /* MainThread.BeginInvokeOnMainThread(async () => { if (await cameraView.StartCameraAsync() == CameraResult.Success) @@ -21,6 +22,7 @@ private void CameraView_CamerasLoaded(object sender, EventArgs e) playing = true; } }); + */ } } private async void Button_Clicked(object sender, EventArgs e) diff --git a/Camera.MAUI.Test/MVVM/CameraViewModel.cs b/Camera.MAUI.Test/MVVM/CameraViewModel.cs new file mode 100644 index 0000000..8b04dce --- /dev/null +++ b/Camera.MAUI.Test/MVVM/CameraViewModel.cs @@ -0,0 +1,183 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ZXing; +using System.Windows.Markup; +using System.Collections.Specialized; +using Camera.MAUI.ZXingHelper; +using CommunityToolkit.Maui.Views; + +namespace Camera.MAUI.Test; + +public class CameraViewModel : INotifyPropertyChanged +{ + private CameraInfo camera = null; + public CameraInfo Camera + { + get => camera; + set + { + camera = value; + OnPropertyChanged(nameof(Camera)); + AutoStartPreview = false; + OnPropertyChanged(nameof(AutoStartPreview)); + AutoStartPreview = true; + OnPropertyChanged(nameof(AutoStartPreview)); + } + } + private ObservableCollection cameras = new(); + public ObservableCollection Cameras + { + get => cameras; + set + { + cameras = value; + OnPropertyChanged(nameof(Cameras)); + } + } + public int NumCameras + { + set + { + if (value > 0) + Camera = Cameras.First(); + } + } + private MicrophoneInfo micro = null; + public MicrophoneInfo Microphone + { + get => micro; + set + { + micro = value; + OnPropertyChanged(nameof(Microphone)); + } + } + private ObservableCollection micros = new(); + public ObservableCollection Microphones + { + get => micros; + set + { + micros = value; + OnPropertyChanged(nameof(Microphones)); + } + } + public int NumMicrophones + { + set + { + if (value > 0) + Microphone = Microphones.First(); + } + } + public MediaSource VideoSource { get; set; } + public BarcodeDecodeOptions BarCodeOptions { get; set; } + public string BarcodeText { get; set; } = "No barcode detected"; + public bool AutoStartPreview { get; set; } = false; + public bool AutoStartRecording { get; set; } = false; + private Result[] barCodeResults; + public Result[] BarCodeResults + { + get => barCodeResults; + set + { + barCodeResults = value; + if (barCodeResults != null && barCodeResults.Length > 0) + BarcodeText = barCodeResults[0].Text; + else + BarcodeText = "No barcode detected"; + OnPropertyChanged(nameof(BarcodeText)); + } + } + private bool takeSnapshot = false; + public bool TakeSnapshot + { + get => takeSnapshot; + set + { + takeSnapshot = value; + OnPropertyChanged(nameof(TakeSnapshot)); + } + } + public float SnapshotSeconds { get; set; } = 0f; + public string Seconds + { + get => SnapshotSeconds.ToString(); + set + { + if (float.TryParse(value, out float seconds)) + { + SnapshotSeconds = seconds; + OnPropertyChanged(nameof(SnapshotSeconds)); + } + } + } + public Command StartCamera { get; set; } + public Command StopCamera { get; set; } + public Command TakeSnapshotCmd { get; set; } + public string RecordingFile { get; set; } + public Command StartRecording { get; set; } + public Command StopRecording { get; set; } + + public event PropertyChangedEventHandler PropertyChanged; + + protected virtual void OnPropertyChanged(string propertyName) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + public CameraViewModel() + { + BarCodeOptions = new ZXingHelper.BarcodeDecodeOptions + { + AutoRotate = true, + PossibleFormats = { ZXing.BarcodeFormat.QR_CODE }, + ReadMultipleCodes = false, + TryHarder = true, + TryInverted = true + }; + OnPropertyChanged(nameof(BarCodeOptions)); + StartCamera = new Command(() => + { + AutoStartPreview = true; + OnPropertyChanged(nameof(AutoStartPreview)); + }); + StopCamera = new Command(() => + { + AutoStartPreview = false; + OnPropertyChanged(nameof(AutoStartPreview)); + }); + TakeSnapshotCmd = new Command(() => + { + TakeSnapshot = false; + TakeSnapshot = true; + }); +#if IOS + RecordingFile = Path.Combine(FileSystem.Current.CacheDirectory, "Video.mov"); +#else + RecordingFile = Path.Combine(FileSystem.Current.CacheDirectory, "Video.mp4"); +#endif + OnPropertyChanged(nameof(RecordingFile)); + StartRecording = new Command(() => + { + AutoStartRecording = true; + OnPropertyChanged(nameof(AutoStartRecording)); + }); + StopRecording = new Command(() => + { + AutoStartRecording = false; + OnPropertyChanged(nameof(AutoStartRecording)); + VideoSource = MediaSource.FromFile(RecordingFile); + OnPropertyChanged(nameof(VideoSource)); + }); + OnPropertyChanged(nameof(StartCamera)); + OnPropertyChanged(nameof(StopCamera)); + OnPropertyChanged(nameof(TakeSnapshotCmd)); + OnPropertyChanged(nameof(StartRecording)); + OnPropertyChanged(nameof(StopRecording)); + } +} diff --git a/Camera.MAUI.Test/MVVM/MVVMPage.xaml b/Camera.MAUI.Test/MVVM/MVVMPage.xaml new file mode 100644 index 0000000..453d222 --- /dev/null +++ b/Camera.MAUI.Test/MVVM/MVVMPage.xaml @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + + + + + + + +