-
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed Android Handler
- Loading branch information
Hector Alonso
committed
Apr 9, 2023
1 parent
d1745df
commit 8239450
Showing
22 changed files
with
2,205 additions
and
380 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<CameraInfo> cameras = new(); | ||
public ObservableCollection<CameraInfo> 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<MicrophoneInfo> micros = new(); | ||
public ObservableCollection<MicrophoneInfo> 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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
x:Class="Camera.MAUI.Test.MVVMPage" | ||
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit" | ||
xmlns:local="clr-namespace:Camera.MAUI.Test" | ||
xmlns:cv="clr-namespace:Camera.MAUI;assembly=Camera.MAUI" | ||
Title="MVVMPage" Background="white"> | ||
<ContentPage.BindingContext> | ||
<local:CameraViewModel /> | ||
</ContentPage.BindingContext> | ||
<ScrollView> | ||
<Grid> | ||
<VerticalStackLayout HorizontalOptions="Center" VerticalOptions="Center"> | ||
<cv:CameraView x:Name="cameraView" WidthRequest="300" HeightRequest="200" BarCodeOptions="{Binding BarCodeOptions}" BarCodeResults="{Binding BarCodeResults, Mode=OneWayToSource}" | ||
Cameras="{Binding Cameras, Mode=OneWayToSource}" Camera="{Binding Camera}" AutoStartPreview="{Binding AutoStartPreview}" NumCamerasDetected="{Binding NumCameras, Mode=OneWayToSource}" | ||
AutoSnapShotAsImageSource="True" AutoSnapShotFormat="PNG" TakeAutoSnapShot="{Binding TakeSnapshot}" AutoSnapShotSeconds="{Binding SnapshotSeconds}" | ||
Microphones="{Binding Microphones, Mode=OneWayToSource}" Microphone="{Binding Microphone}" NumMicrophonesDetected="{Binding NumMicrophones, Mode=OneWayToSource}" | ||
AutoRecordingFile="{Binding RecordingFile}" AutoStartRecording="{Binding AutoStartRecording}"/> | ||
<HorizontalStackLayout HorizontalOptions="Center" Margin="5"> | ||
<Label Text="Select a camera:" VerticalOptions="Center" BackgroundColor="White" TextColor="Black"/> | ||
<Picker VerticalOptions="Center" TextColor="Black" ItemsSource="{Binding Cameras}" SelectedItem="{Binding Camera,Mode=TwoWay}"/> | ||
</HorizontalStackLayout> | ||
<HorizontalStackLayout HorizontalOptions="Center" Margin="5"> | ||
<Label Text="Select a microphone:" VerticalOptions="Center" BackgroundColor="White" TextColor="Black"/> | ||
<Picker VerticalOptions="Center" TextColor="Black" ItemsSource="{Binding Microphones}" SelectedItem="{Binding Microphone,Mode=TwoWay}"/> | ||
</HorizontalStackLayout> | ||
<HorizontalStackLayout HorizontalOptions="Center"> | ||
<Label Text="Mirrored" VerticalOptions="Center" TextColor="Black"/> | ||
<CheckBox BindingContext="{x:Reference cameraView}" VerticalOptions="Center" Color="Black" IsChecked="{Binding MirroredImage}"/> | ||
<Label Text="Torch" VerticalOptions="Center" TextColor="Black"/> | ||
<CheckBox BindingContext="{x:Reference cameraView}" VerticalOptions="Center" Color="Black" IsChecked="{Binding TorchEnabled}"/> | ||
<Label Text="QR Detec." VerticalOptions="Center" TextColor="Black"/> | ||
<CheckBox BindingContext="{x:Reference cameraView}" VerticalOptions="Center" Color="Black" IsChecked="{Binding BarCodeDetectionEnabled}"/> | ||
</HorizontalStackLayout> | ||
<HorizontalStackLayout HorizontalOptions="Center"> | ||
<Label Text="AutoSnap freq: " VerticalOptions="Center" TextColor="Black"/> | ||
<Entry WidthRequest="20" Keyboard="Numeric" TextColor="Black" Text="{Binding Seconds, Mode=TwoWay}" /> | ||
<Label Text="Take Autosnap" VerticalOptions="Center" TextColor="Black"/> | ||
<CheckBox VerticalOptions="Center" Color="Black" IsChecked="{Binding TakeSnapshot, Mode=OneWayToSource}"/> | ||
<Label Text="As ISource" VerticalOptions="Center" TextColor="Black"/> | ||
<CheckBox BindingContext="{x:Reference cameraView}" IsChecked="{Binding AutoSnapShotAsImageSource}" VerticalOptions="Center" Color="Black"/> | ||
</HorizontalStackLayout> | ||
<HorizontalStackLayout HorizontalOptions="Center" Margin="2" Spacing="2"> | ||
<Label Text="Zoom" VerticalOptions="Center" TextColor="Black"/> | ||
<Stepper BindingContext="{x:Reference cameraView}" Minimum="{Binding MinZoomFactor}" Maximum="7" Increment="0.5" Value="{Binding ZoomFactor,Mode=TwoWay}" /> | ||
</HorizontalStackLayout> | ||
<HorizontalStackLayout HorizontalOptions="Center" Margin="5"> | ||
<Button Text="Start Camera" Command="{Binding StartCamera}" /> | ||
<Button Text="Stop Camera" Command="{Binding StopCamera}" /> | ||
<Button Text="Take Photo" Command="{Binding TakeSnapshotCmd}" /> | ||
</HorizontalStackLayout> | ||
<HorizontalStackLayout HorizontalOptions="Center" Margin="5"> | ||
<Button Text="Start Record" Command="{Binding StartRecording}" /> | ||
<Button Text="Stop Record" Command="{Binding StopRecording}" /> | ||
</HorizontalStackLayout> | ||
<Label Text="{Binding BarcodeText}" TextColor="Black" FontAttributes="Bold" HorizontalOptions="Center" /> | ||
<Image BindingContext="{x:Reference cameraView}" Source="{Binding SnapShot}" Aspect="AspectFit" WidthRequest="250" HeightRequest="100" HorizontalOptions="Center" /> | ||
<toolkit:MediaElement Source="{Binding VideoSource}" ShouldAutoPlay="False" ShouldShowPlaybackControls="True" HeightRequest="300" WidthRequest="200" /> | ||
</VerticalStackLayout> | ||
</Grid> | ||
</ScrollView> | ||
|
||
</ContentPage> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Camera.MAUI.Test; | ||
|
||
public partial class MVVMPage : ContentPage | ||
{ | ||
public MVVMPage() | ||
{ | ||
InitializeComponent(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.