diff --git a/CameraExplorer/App.xaml.cs b/CameraExplorer/App.xaml.cs index 916d56c..0048192 100644 --- a/CameraExplorer/App.xaml.cs +++ b/CameraExplorer/App.xaml.cs @@ -17,8 +17,6 @@ namespace CameraExplorer { public partial class App : Application { - private CameraExplorer.DataContext _dataContext = CameraExplorer.DataContext.Singleton; - /// /// Provides easy access to the root frame of the Phone Application. /// @@ -77,7 +75,10 @@ namespace CameraExplorer // This code will not execute when the application is closing private void Application_Deactivated(object sender, DeactivatedEventArgs e) { - _dataContext.UnitializeCamera(); + CameraExplorer.DataContext d = CameraExplorer.DataContext.Singleton; + + d.Device.Dispose(); + d.Device = null; } // Code to execute when the application is closing (eg, user hit Back) diff --git a/CameraExplorer/ArrayParameter.cs b/CameraExplorer/ArrayParameter.cs index f3b8320..69c8073 100644 --- a/CameraExplorer/ArrayParameter.cs +++ b/CameraExplorer/ArrayParameter.cs @@ -1,14 +1,8 @@ using Microsoft.Devices; -using Microsoft.Phone.Shell; using System; using System.Collections; using System.Collections.Generic; -using System.ComponentModel; using System.Linq; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using System.Windows.Media.Imaging; using Windows.Phone.Media.Capture; namespace CameraExplorer @@ -107,7 +101,7 @@ namespace CameraExplorer { private List _options = new List(); private ArrayParameterOption _selectedOption; - private Guid _guid; + private Guid _propertyId; private bool _refreshing = false; public ArrayParameter(PhotoCaptureDevice device, string name) @@ -115,10 +109,10 @@ namespace CameraExplorer { } - public ArrayParameter(PhotoCaptureDevice device, Guid guid, string name) + public ArrayParameter(PhotoCaptureDevice device, Guid propertyId, string name) : base(device, name) { - _guid = guid; + _propertyId = propertyId; } public override void Refresh() @@ -208,11 +202,11 @@ namespace CameraExplorer return new ArrayParameterEnumerator(this, _options.Count); } - protected Guid Guid + protected Guid PropertyId { get { - return _guid; + return _propertyId; } } @@ -289,7 +283,7 @@ namespace CameraExplorer Options.Add(option); CameraCapturePropertyRange range = PhotoCaptureDevice.GetSupportedPropertyRange(Device.SensorLocation, KnownCameraPhotoProperties.ExposureTime); - object value = Device.GetProperty(Guid); + object value = Device.GetProperty(PropertyId); UInt32[] standardValues = { /* 16000, 8000, 4000,*/ 2000, 1000, 500, 250, 125, 60, 30, 15, 8, 4, 2, 1 }; UInt32 min = (UInt32)range.Min; @@ -317,7 +311,7 @@ namespace CameraExplorer protected override void SetOption(ArrayParameterOption option) { - Device.SetProperty(Guid, option.Value); + Device.SetProperty(PropertyId, option.Value); } public override void SetDefault() @@ -340,8 +334,8 @@ namespace CameraExplorer Options.Add(option); - CameraCapturePropertyRange range = PhotoCaptureDevice.GetSupportedPropertyRange(Device.SensorLocation, Guid); - object value = Device.GetProperty(Guid); + CameraCapturePropertyRange range = PhotoCaptureDevice.GetSupportedPropertyRange(Device.SensorLocation, PropertyId); + object value = Device.GetProperty(PropertyId); UInt32[] standardValues = { 100, 200, 400, 800, 1600, 3200 }; UInt32 min = (UInt32)range.Min; @@ -367,64 +361,7 @@ namespace CameraExplorer protected override void SetOption(ArrayParameterOption option) { - Device.SetProperty(Guid, option.Value); - } - - public override void SetDefault() - { - if (Options.Count > 0) - { - SelectedOption = Options.First(); - } - else - { - SelectedOption = null; - } - } - } - - public class ManualWhiteBalanceParameter : ArrayParameter - { - public ManualWhiteBalanceParameter(PhotoCaptureDevice device) - : base(device, KnownCameraPhotoProperties.ManualWhiteBalance, "Manual white balance") - { - } - - protected override void PopulateOptions() - { - ArrayParameterOption option = new ArrayParameterOption(null, "Auto"); - ArrayParameterOption selectedOption = option; - - Options.Add(option); - - CameraCapturePropertyRange range = PhotoCaptureDevice.GetSupportedPropertyRange(Device.SensorLocation, Guid); - object value = Device.GetProperty(Guid); - UInt32[] standardValues = { 2700, 3000, 5200, 5400, 6000, 6500, 7200, 8000 }; - - UInt32 min = (UInt32)range.Min; - UInt32 max = (UInt32)range.Max; - - foreach (UInt32 i in standardValues) - { - if (i >= min && i <= max) - { - option = new ArrayParameterOption(i, i.ToString() + " K"); - - Options.Add(option); - - if (i.Equals(value)) - { - selectedOption = option; - } - } - } - - SelectedOption = selectedOption; - } - - protected override void SetOption(ArrayParameterOption option) - { - Device.SetProperty(Guid, option.Value); + Device.SetProperty(PropertyId, option.Value); } public override void SetDefault() @@ -449,8 +386,8 @@ namespace CameraExplorer protected override void PopulateOptions() { - IReadOnlyList supportedValues = PhotoCaptureDevice.GetSupportedPropertyValues(Device.SensorLocation, Guid); - object value = Device.GetProperty(Guid); + IReadOnlyList supportedValues = PhotoCaptureDevice.GetSupportedPropertyValues(Device.SensorLocation, PropertyId); + object value = Device.GetProperty(PropertyId); foreach (dynamic i in supportedValues) { @@ -469,7 +406,7 @@ namespace CameraExplorer protected override void SetOption(ArrayParameterOption option) { - Device.SetProperty(Guid, option.Value); + Device.SetProperty(PropertyId, option.Value); } public override void SetDefault() @@ -502,8 +439,8 @@ namespace CameraExplorer protected override void PopulateOptions() { - IReadOnlyList supportedValues = PhotoCaptureDevice.GetSupportedPropertyValues(Device.SensorLocation, Guid); - object value = Device.GetProperty(Guid); + IReadOnlyList supportedValues = PhotoCaptureDevice.GetSupportedPropertyValues(Device.SensorLocation, PropertyId); + object value = Device.GetProperty(PropertyId); foreach (dynamic i in supportedValues) { @@ -522,7 +459,7 @@ namespace CameraExplorer protected override void SetOption(ArrayParameterOption option) { - Device.SetProperty(Guid, (FlashMode)option.Value); + Device.SetProperty(PropertyId, (FlashMode)option.Value); } public override void SetDefault() @@ -555,8 +492,8 @@ namespace CameraExplorer protected override void PopulateOptions() { - IReadOnlyList supportedValues = PhotoCaptureDevice.GetSupportedPropertyValues(Device.SensorLocation, Guid); - object value = Device.GetProperty(Guid); + IReadOnlyList supportedValues = PhotoCaptureDevice.GetSupportedPropertyValues(Device.SensorLocation, PropertyId); + object value = Device.GetProperty(PropertyId); foreach (dynamic i in supportedValues) { @@ -575,7 +512,7 @@ namespace CameraExplorer protected override void SetOption(ArrayParameterOption option) { - Device.SetProperty(Guid, option.Value); + Device.SetProperty(PropertyId, option.Value); } public override void SetDefault() @@ -613,8 +550,8 @@ namespace CameraExplorer Options.Add(option); - IReadOnlyList supportedValues = PhotoCaptureDevice.GetSupportedPropertyValues(Device.SensorLocation, Guid); - object value = Device.GetProperty(Guid); + IReadOnlyList supportedValues = PhotoCaptureDevice.GetSupportedPropertyValues(Device.SensorLocation, PropertyId); + object value = Device.GetProperty(PropertyId); foreach (dynamic i in supportedValues) { @@ -635,7 +572,7 @@ namespace CameraExplorer protected override void SetOption(ArrayParameterOption option) { - Device.SetProperty(Guid, option.Value); + Device.SetProperty(PropertyId, option.Value); } public override void SetDefault() @@ -653,8 +590,8 @@ namespace CameraExplorer protected override void PopulateOptions() { - IReadOnlyList supportedValues = PhotoCaptureDevice.GetSupportedPropertyValues(Device.SensorLocation, Guid); - object value = Device.GetProperty(Guid); + IReadOnlyList supportedValues = PhotoCaptureDevice.GetSupportedPropertyValues(Device.SensorLocation, PropertyId); + object value = Device.GetProperty(PropertyId); foreach (dynamic i in supportedValues) { @@ -673,7 +610,7 @@ namespace CameraExplorer protected override void SetOption(ArrayParameterOption option) { - Device.SetProperty(Guid, option.Value); + Device.SetProperty(PropertyId, option.Value); } public override void SetDefault() diff --git a/CameraExplorer/CameraExplorer.csproj b/CameraExplorer/CameraExplorer.csproj index 1faef62..9f1dd7d 100644 --- a/CameraExplorer/CameraExplorer.csproj +++ b/CameraExplorer/CameraExplorer.csproj @@ -116,7 +116,6 @@ True AppResources.resx - SettingsPage.xaml diff --git a/CameraExplorer/DataContext.cs b/CameraExplorer/DataContext.cs index 3412c63..1c382c7 100644 --- a/CameraExplorer/DataContext.cs +++ b/CameraExplorer/DataContext.cs @@ -1,13 +1,7 @@ using System; -using System.Collections.Generic; +using System.Collections.ObjectModel; using System.ComponentModel; using System.IO; -using System.IO.IsolatedStorage; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Controls; -using System.Windows.Media.Imaging; using Windows.Phone.Media.Capture; namespace CameraExplorer @@ -16,31 +10,45 @@ namespace CameraExplorer { public event PropertyChangedEventHandler PropertyChanged; - static DataContext _singleton; - public static CameraExplorer.DataContext Singleton + private static DataContext _singleton; + private PhotoCaptureDevice _device = null; + private ObservableCollection _parameters = new ObservableCollection(); + + public static DataContext Singleton { get { if (_singleton == null) - _singleton = new CameraExplorer.DataContext(); + { + _singleton = new DataContext(); + } return _singleton; } } - Settings _settings = null; - public Settings Settings + public ObservableCollection Parameters { get { - if (_settings == null) - _settings = new Settings(); + return _parameters; + } - return _settings; + private set + { + if (_parameters != value) + { + _parameters = value; + + + if (PropertyChanged != null) + { + PropertyChanged(this, new PropertyChangedEventArgs("Parameters")); + } + } } } - PhotoCaptureDevice _device = null; public PhotoCaptureDevice Device { get @@ -53,8 +61,46 @@ namespace CameraExplorer if (_device != value) { _device = value; + + if (_device != null) + { + ObservableCollection newParameters = new ObservableCollection(); - Settings.CreateParameters(); + Action addParameter = (Parameter parameter) => + { + if (parameter.Supported && parameter.Modifiable) + { + try + { + parameter.Refresh(); + parameter.SetDefault(); + + newParameters.Add(parameter); + } + catch (Exception) + { + System.Diagnostics.Debug.WriteLine("Setting default to " + parameter.Name.ToLower() + " failed"); + } + } + else + { + System.Diagnostics.Debug.WriteLine("Parameter " + parameter.Name.ToLower() + " is not supported or not modifiable"); + } + }; + + addParameter(new SceneModeParameter(_device)); + addParameter(new WhiteBalancePresetParameter(_device)); + addParameter(new FlashModeParameter(_device)); + addParameter(new FlashPowerParameter(_device)); + addParameter(new IsoParameter(_device)); + addParameter(new ExposureCompensationParameter(_device)); + addParameter(new ExposureTimeParameter(_device)); + addParameter(new AutoFocusRangeParameter(_device)); + addParameter(new FocusIlluminationModeParameter(_device)); + addParameter(new CaptureResolutionParameter(_device)); + + Parameters = newParameters; + } if (PropertyChanged != null) { @@ -65,30 +111,5 @@ namespace CameraExplorer } public MemoryStream ImageStream { get; set; } - - public async Task InitializeCamera(CameraSensorLocation sensorLocation) - { - Windows.Foundation.Size initialResolution = new Windows.Foundation.Size(640, 480); - Windows.Foundation.Size previewResolution = new Windows.Foundation.Size(640, 480); - Windows.Foundation.Size captureResolution = new Windows.Foundation.Size(640, 480); - - PhotoCaptureDevice d = await PhotoCaptureDevice.OpenAsync(sensorLocation, initialResolution); - - await d.SetPreviewResolutionAsync(previewResolution); - await d.SetCaptureResolutionAsync(captureResolution); - - d.SetProperty(KnownCameraGeneralProperties.EncodeWithOrientation, d.SensorLocation == CameraSensorLocation.Back ? d.SensorRotationInDegrees : - d.SensorRotationInDegrees); - - Device = d; - } - - public void UnitializeCamera() - { - if (Device != null) - { - Device.Dispose(); - Device = null; - } - } } } \ No newline at end of file diff --git a/CameraExplorer/MainPage.xaml b/CameraExplorer/MainPage.xaml index 1c92467..ecef371 100644 --- a/CameraExplorer/MainPage.xaml +++ b/CameraExplorer/MainPage.xaml @@ -51,7 +51,7 @@ - + sensorLocations = PhotoCaptureDevice.AvailableSensorLocations; if (currentSensorLocation == sensorLocations[1]) { - await _dataContext.InitializeCamera(sensorLocations[0]); + await InitializeCamera(sensorLocations[0]); } else { - await _dataContext.InitializeCamera(sensorLocations[1]); + await InitializeCamera(sensorLocations[1]); } videoBrush.RelativeTransform = new CompositeTransform() { CenterX = 0.5, CenterY = 0.5, - Rotation = _dataContext.Device.SensorLocation == CameraSensorLocation.Back ? _dataContext.Device.SensorRotationInDegrees : _dataContext.Device.SensorRotationInDegrees + 180 + Rotation = _dataContext.Device.SensorLocation == CameraSensorLocation.Back ? + _dataContext.Device.SensorRotationInDegrees : _dataContext.Device.SensorRotationInDegrees + 180 }; videoBrush.SetSource(_dataContext.Device); @@ -184,6 +176,22 @@ namespace CameraExplorer SystemTray.SetProgressIndicator(this, _progressIndicator); } + private async Task InitializeCamera(CameraSensorLocation sensorLocation) + { + Windows.Foundation.Size initialResolution = new Windows.Foundation.Size(640, 480); + Windows.Foundation.Size previewResolution = new Windows.Foundation.Size(640, 480); + Windows.Foundation.Size captureResolution = new Windows.Foundation.Size(640, 480); + + PhotoCaptureDevice d = await PhotoCaptureDevice.OpenAsync(sensorLocation, initialResolution); + + await d.SetPreviewResolutionAsync(previewResolution); + await d.SetCaptureResolutionAsync(captureResolution); + + d.SetProperty(KnownCameraGeneralProperties.EncodeWithOrientation, d.SensorLocation == CameraSensorLocation.Back ? d.SensorRotationInDegrees : -d.SensorRotationInDegrees); + + _dataContext.Device = d; + } + private async Task AutoFocus() { if (!_capturing && PhotoCaptureDevice.IsFocusSupported(_dataContext.Device.SensorLocation)) diff --git a/CameraExplorer/Parameter.cs b/CameraExplorer/Parameter.cs index 581cd67..76c6201 100644 --- a/CameraExplorer/Parameter.cs +++ b/CameraExplorer/Parameter.cs @@ -1,14 +1,4 @@ -using Microsoft.Devices; -using Microsoft.Phone.Shell; -using System; -using System.Collections; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using System.Windows.Media.Imaging; +using System.ComponentModel; using Windows.Phone.Media.Capture; namespace CameraExplorer diff --git a/CameraExplorer/PreviewPage.xaml.cs b/CameraExplorer/PreviewPage.xaml.cs index 57c99d7..dcb48d9 100644 --- a/CameraExplorer/PreviewPage.xaml.cs +++ b/CameraExplorer/PreviewPage.xaml.cs @@ -42,6 +42,7 @@ namespace CameraExplorer _dataContext.ImageStream.Position = 0; MediaLibrary library = new MediaLibrary(); + library.SavePictureToCameraRoll("CameraExplorer_" + DateTime.Now.ToString() + ".jpg", _dataContext.ImageStream); } catch (Exception) diff --git a/CameraExplorer/Properties/WMAppManifest.xml b/CameraExplorer/Properties/WMAppManifest.xml index 3d020b2..4bf0030 100644 --- a/CameraExplorer/Properties/WMAppManifest.xml +++ b/CameraExplorer/Properties/WMAppManifest.xml @@ -1,7 +1,7 @@  - + Assets\ApplicationIcon.png diff --git a/CameraExplorer/RangeParameter.cs b/CameraExplorer/RangeParameter.cs index c342a3f..ea77044 100644 --- a/CameraExplorer/RangeParameter.cs +++ b/CameraExplorer/RangeParameter.cs @@ -15,29 +15,24 @@ namespace CameraExplorer { public abstract class RangeParameter : Parameter { - private Guid _guid; + private Guid _propertyId; private T _value; private T _minimum; private T _maximum; - private bool _constructing; - protected RangeParameter(PhotoCaptureDevice device, Guid guid, string name) + protected RangeParameter(PhotoCaptureDevice device, Guid propertyId, string name) : base(device, name) { - _guid = guid; - - _constructing = true; + _propertyId = propertyId; Refresh(); - - _constructing = false; } public override void Refresh() { try { - CameraCapturePropertyRange range = PhotoCaptureDevice.GetSupportedPropertyRange(Device.SensorLocation, _guid); + CameraCapturePropertyRange range = PhotoCaptureDevice.GetSupportedPropertyRange(Device.SensorLocation, _propertyId); if (range == null) { @@ -47,7 +42,7 @@ namespace CameraExplorer { Minimum = (T)range.Min; Maximum = (T)range.Max; - Value = (T)Device.GetProperty(_guid); + _value = (T)Device.GetProperty(_propertyId); Supported = true; } } @@ -112,20 +107,20 @@ namespace CameraExplorer set { - try + if (!_value.Equals(value)) { - if (!_constructing) + try { - Device.SetProperty(_guid, (T)value); + _value = value; + + Device.SetProperty(_propertyId, (T)value); + + NotifyPropertyChanged("Value"); + } + catch (Exception) + { + System.Diagnostics.Debug.WriteLine("Setting " + Name.ToLower() + " failed"); } - - _value = value; - - NotifyPropertyChanged("Value"); - } - catch (Exception) - { - System.Diagnostics.Debug.WriteLine("Setting " + Name.ToLower() + " failed"); } } } diff --git a/CameraExplorer/Settings.cs b/CameraExplorer/Settings.cs deleted file mode 100644 index 4949344..0000000 --- a/CameraExplorer/Settings.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System; -using System.Collections.ObjectModel; -using System.ComponentModel; -using Windows.Phone.Media.Capture; - -namespace CameraExplorer -{ - class Settings : INotifyPropertyChanged - { - public event PropertyChangedEventHandler PropertyChanged; - - private CameraExplorer.DataContext _dataContext = CameraExplorer.DataContext.Singleton; - private ObservableCollection _parameters = new ObservableCollection(); - - public ObservableCollection Parameters - { - get - { - return _parameters; - } - - private set - { - if (_parameters != value) - { - _parameters = value; - - PropertyChanged(this, new PropertyChangedEventArgs("Parameters")); - } - } - } - - public void CreateParameters() - { - if (_dataContext.Device != null) - { - ObservableCollection newParameters = new ObservableCollection(); - - Action addParameter = (Parameter parameter) => - { - if (parameter.Supported && parameter.Modifiable) - { - try - { - parameter.Refresh(); - parameter.SetDefault(); - - newParameters.Add(parameter); - } - catch (Exception) - { - System.Diagnostics.Debug.WriteLine("Setting default to " + parameter.Name.ToLower() + " failed"); - } - } - else - { - System.Diagnostics.Debug.WriteLine("Parameter " + parameter.Name.ToLower() + " is not supported or not modifiable"); - } - }; - - addParameter(new SceneModeParameter(_dataContext.Device)); - addParameter(new WhiteBalancePresetParameter(_dataContext.Device)); - addParameter(new FlashModeParameter(_dataContext.Device)); // todo throws exception when setting this - addParameter(new FlashPowerParameter(_dataContext.Device)); - addParameter(new IsoParameter(_dataContext.Device)); - addParameter(new ExposureCompensationParameter(_dataContext.Device)); // todo does not work, does not capture after setting this - addParameter(new ManualWhiteBalanceParameter(_dataContext.Device)); // todo dependency with wb preset - addParameter(new ExposureTimeParameter(_dataContext.Device)); - addParameter(new AutoFocusRangeParameter(_dataContext.Device)); - addParameter(new FocusIlluminationModeParameter(_dataContext.Device)); - addParameter(new CaptureResolutionParameter(_dataContext.Device)); - - Parameters = newParameters; - } - } - - public void Refresh() - { - foreach (Parameter p in _parameters) - { - p.Refresh(); - } - } - } -} \ No newline at end of file diff --git a/CameraExplorer/SettingsPage.xaml b/CameraExplorer/SettingsPage.xaml index 1e359fe..c7e01b9 100644 --- a/CameraExplorer/SettingsPage.xaml +++ b/CameraExplorer/SettingsPage.xaml @@ -26,7 +26,7 @@ - + diff --git a/CameraExplorer/SettingsPage.xaml.cs b/CameraExplorer/SettingsPage.xaml.cs index 6d8ba4e..1c77fcc 100644 --- a/CameraExplorer/SettingsPage.xaml.cs +++ b/CameraExplorer/SettingsPage.xaml.cs @@ -41,7 +41,7 @@ namespace CameraExplorer { SetScreenButtonsEnabled(false); - foreach (Parameter i in _dataContext.Settings.Parameters) + foreach (Parameter i in _dataContext.Parameters) { i.SetDefault(); } diff --git a/CameraExplorer/SettingsTemplateSelector.cs b/CameraExplorer/SettingsTemplateSelector.cs index 0b64060..7136b21 100644 --- a/CameraExplorer/SettingsTemplateSelector.cs +++ b/CameraExplorer/SettingsTemplateSelector.cs @@ -37,4 +37,4 @@ namespace CameraExplorer } } } -} +} \ No newline at end of file diff --git a/Documentation/CameraExplorer.mdzip b/Documentation/CameraExplorer.mdzip index 4f386db..96e42fa 100644 Binary files a/Documentation/CameraExplorer.mdzip and b/Documentation/CameraExplorer.mdzip differ diff --git a/Documentation/architecture.png b/Documentation/architecture.png index 04f4340..d3939ce 100644 Binary files a/Documentation/architecture.png and b/Documentation/architecture.png differ