Cannot find Front, Back Camera in windows 8.1 store app - c#

I have developed windows 8.1 store app, it need to be capture photo by using back camera and post.
MediaCaptureInitializationSettings _captureSettings = new var devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
foreach (var device in devices)
{
if (device.EnclosureLocation != null && device.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back)
{
deviceId = device.Id;
break;
}
}
if (!string.IsNullOrEmpty(deviceId))
{
_captureSettings.AudioDeviceId = "";
_captureSettings.VideoDeviceId = deviceId;
_captureSettings.PhotoCaptureSource = Windows.Media.Capture.PhotoCaptureSource.Photo;
_captureSettings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.Video;
}
captureManager = new MediaCapture();
await captureManager.InitializeAsync(_captureSettings);
await captureManager.ClearEffectsAsync(MediaStreamType.Photo);
capturePreview1.Source = captureManager;
await captureManager.StartPreviewAsync();
</code>
Here i am getting two devices but that devices EnclosureLocation is null, so i can't find which one is front and back camera.
so have decided to get second device from list
<code>
deviceId = devices[1].Id;
</code>
but it throws an error like "The current capture source does not have an independent photo stream."
in the line of initializing MediaCapture
<code>
await captureManager.InitializeAsync(_captureSettings);
</code>
i have tried in windows surface pro 2 and acer devices.
Please advise. Thanks in advance.

Try to organise your code better, you got two equals signs on the same line and your code is not well formated so it's hard to read.
To use Camera in Windows 8.1 stop app I use this code :
// First need to find all webcams
DeviceInformationCollection webcamList = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture)
// Then I do a query to find the front webcam
DeviceInformation frontWebcam = (from webcam in webcamList
where webcam.EnclosureLocation != null
&& webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front
select webcam).FirstOrDefault();
// Same for the back webcam
DeviceInformation backWebcam = (from webcam in webcamList
where webcam.EnclosureLocation != null
&& webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back
select webcam).FirstOrDefault();
// Then you need to initialize your MediaCapture
var captureManager = new MediaCapture();
await captureManager.InitializeAsync(new MediaCaptureInitializationSettings
{
// Choose the webcam you want (backWebcam or frontWebcam)
VideoDeviceId = backWebcam.Id,
AudioDeviceId = "",
StreamingCaptureMode = StreamingCaptureMode.Video,
PhotoCaptureSource = PhotoCaptureSource.VideoPreview
});
// Set the source of the CaptureElement to your MediaCapture
capturePreview1.Source = captureManager;
// Start the preview
await captureManager.StartPreviewAsync();
This way it's easier to read. The code is not very different, MediaCaptureInitializationSettings is not the same.
This code works for me on Surface 2 RT and Nokia 635 so it should work for you.
Edit:
Seems it works on devices with Windows RT but on full windows 8.1 devices it's always null.
Msdn says that:
If no enclosure location information is available, the property will
be null
so what you can do is first try to see if you find a backwebcam and if it's null take the last one;
DeviceInformation backWebcam = (from webcam in webcamList
where webcam.EnclosureLocation != null
&& webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back
select webcam).FirstOrDefault();
if (backWebcam == null)
{
backWebcam = webcamList.Last();
}
But you are not sure the last one in the collection is the back one, so you should add a button to let the user switch camera
If you change camera,
await captureManager.StopPreviewAsync();
await captureManager.InitializeAsync(new MediaCaptureInitializationSettings
{
// Choose an other webcam
VideoDeviceId = //id of the new webcam,
AudioDeviceId = "",
StreamingCaptureMode = StreamingCaptureMode.Video,
PhotoCaptureSource = PhotoCaptureSource.VideoPreview
});
await captureManager.StartPreviewAsync();
this way you can be sure the user can choose the right camera even is you programmaticaly cannot tell which one is which

Related

Playing in-built webcam feed in a UWP app stopped working after?

I'm trying to play the built-in webcam feed in a MediaElement within a UWP app. It works fine for a few users but there is no feed played for most and I'm lost on what could be the issue.
Some observations when the webcam feed doesn't play:
The code executes without any exceptions
The dialog that requests user permission to access the camera is shown
The LED indicating the webcam is in use turns on soon as it is executed, but there is no feed.
Skype and Camera apps work fine.
The app was working as expected until a week back. A few things that changed in the mean time that could have had an impact are
Installed Kaspersky
A bunch of windows updates
Uninstalled VS2017 professional edition & VS2019 Community edition and installed VS2019 Professional Edition
Some additional information that might be needed to narrow down the reason.
Webcam is enabled in the Package manifest of the app
App Target version: 18362
App Min version: 18362
Windows OS Version : 18362
Any help on this would be highly appreciated. Thanks much in advance!
Here is the piece of code used to play the webcam feed where VideoStreamer is a MediaElement.
private async Task PlayLiveVideo()
{
var allGroups = await MediaFrameSourceGroup.FindAllAsync();
var eligibleGroups = allGroups.Select(g => new
{
Group = g,
// For each source kind, find the source which offers that kind of media frame,
// or null if there is no such source.
SourceInfos = new MediaFrameSourceInfo[]
{
g.SourceInfos.FirstOrDefault(info => info.DeviceInformation?.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front
&& info.SourceKind == MediaFrameSourceKind.Color),
g.SourceInfos.FirstOrDefault(info => info.DeviceInformation?.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back
&& info.SourceKind == MediaFrameSourceKind.Color)
}
}).Where(g => g.SourceInfos.Any(info => info != null)).ToList();
if (eligibleGroups.Count == 0)
{
System.Diagnostics.Debug.WriteLine("No source group with front and back-facing camera found.");
return;
}
var selectedGroupIndex = 0; // Select the first eligible group
MediaFrameSourceGroup selectedGroup = eligibleGroups[selectedGroupIndex].Group;
MediaFrameSourceInfo frontSourceInfo = selectedGroup.SourceInfos[0];
MediaCapture mediaCapture = new MediaCapture();
MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings()
{
SourceGroup = selectedGroup,
SharingMode = MediaCaptureSharingMode.ExclusiveControl,
MemoryPreference = MediaCaptureMemoryPreference.Cpu,
StreamingCaptureMode = StreamingCaptureMode.Video,
};
try
{
await mediaCapture.InitializeAsync(settings);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("MediaCapture initialization failed: " + ex.Message);
return;
}
var frameMediaSource1 = MediaSource.CreateFromMediaFrameSource(mediaCapture.FrameSources[frontSourceInfo.Id]);
VideoStreamer.SetPlaybackSource(frameMediaSource1);
VideoStreamer.Play();
}
As mentioned by Faywang-MSFT here , it worked after marking the application as trusted in Kaspersky.

DeviceConnectionChangeTrigger - Value does not fall within the expected range

I'm developing an UWP app for Desktop and Mobile. I'd like using DeviceConnectionChangeTrigger to identify when my BT device is in range, but when i try using it, I cannot register background task cause it keeps return System.ArgumentException "Value does not fall within the expected range." when executing "Register" method of BackgroundTaskBuilder.
code is very simple
var current = BackgroundExecutionManager.GetAccessStatus();
if (current == BackgroundAccessStatus.Unspecified || current == BackgroundAccessStatus.Denied)
{
var result = await BackgroundExecutionManager.RequestAccessAsync();
if (result == BackgroundAccessStatus.Denied || result == BackgroundAccessStatus.Unspecified)
throw new Exception("You cannot register Access");
}
//I tested it with BT and BTLE device but no luck
//var devices = await DeviceInformation.FindAllAsync(BluetoothLEDevice.GetDeviceSelector());
var devices = await DeviceInformation.FindAllAsync(BluetoothDevice.GetDeviceSelector());
var device = devices.FirstOrDefault();
if (device == null) throw new Exception("Device not found");
var trigger = await DeviceConnectionChangeTrigger.FromIdAsync(device.Id);
//trigger.MaintainConnection = true;
var builder = new BackgroundTaskBuilder
{
Name = "BluetoothConnectionWatcher",
TaskEntryPoint = typeof(BluetoothConnectionWatcherTask).FullName
};
builder.SetTrigger(trigger);
//error on next line!
var r = builder.Register();
My project has Bluetooth capabilities and one background task registered with Bluetooth property flagged.
All Bluetooth functionalities work well and I can communicate with devices without problem.
I tried several BT and BTLE device and for all of them seems I have the same problem.
I tried on Desktop and Mobile version of Windows 10 (both last fast insider build). Same problem.
The device.Id returned by code is similar to "Bluetooth#Bluetooth00:1a:7d:da:71:0a-fc:58:fa:4c:17:0a" for every BT device i tried (of course 2nd MAC address change based on device...)
Any advice?
Thanks in advance
As far as I know, with BLE you should use the Triggers designed for it. Thus if it is characteristics change you would want to get knowledge about, then you should use GattCharacteristicNotificationTrigger, and example on using it can be found from my blog.
Then if it just BLE device availability, and you can detect yours via scanrecord data, then do use the BluetoothLEAdvertisementWatcherTrigger instead. My Friend Juhana has example fro this in his blog.

turn on flashlight on Windows 10

My issue is quite simple.
I want to turn the flash On (and keep it On) on a Windows 10 universal app project but nothing I try works.
This is the code
MediaCapture MyMediaCapture = new MediaCapture();
var allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
DeviceInformation cameraDevice =
allVideoDevices.FirstOrDefault(x => x.EnclosureLocation != null &&
x.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back);
cameraDevice = cameraDevice ?? allVideoDevices.FirstOrDefault();
if (cameraDevice == null)
{
Debug.WriteLine("No camera device found!");
}
else
{
await MyMediaCapture.InitializeAsync(new MediaCaptureInitializationSettings
{
VideoDeviceId = cameraDevice.Id
});
var MyVideoDeviceController = MyMediaCapture.VideoDeviceController;
var MyTorch = MyVideoDeviceController.TorchControl;
if (MyTorch.Supported)
{
var captureElement = new CaptureElement();
captureElement.Source = MyMediaCapture;
await MyMediaCapture.StartPreviewAsync();
FileStream tmp = new FileStream(System.IO.Path.GetTempFileName() + Guid.NewGuid().ToString() + ".mp4", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, 10000, FileOptions.RandomAccess | FileOptions.DeleteOnClose);
var videoFile = await KnownFolders.VideosLibrary.CreateFileAsync(tmp.Name, CreationCollisionOption.GenerateUniqueName);
var encodingProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Wvga);
await MyMediaCapture.StartRecordToStorageFileAsync(encodingProfile, videoFile);
MyTorch.PowerPercent = 100;
MyTorch.Enabled = true;
}
}
Edit: add code
It looks like you're trying to use an old method of accessing the flashlight which we no longer have to use in Windows 10 UWP development. Take a look at the new Lamp feature in Windows.Devices.Lights in this sample on GitHub.
It's a great starting point for using the flash independent of access the camera APIs.
You're on the right path. Depending on the device (because of driver-specific implementations), you'll have to start the preview or maybe even start a video recording session for the light to turn on.
Because of that, and to guarantee compatibility with most devices, I'd recommend you actually do both.

Windows (Phone) 8.1 Camera Use

I am creating a Windows Universal application. I want to the user to be able to upload a picture and the user should have the option of taking one on the spot and sending that. I have this working using the MediaCapture api. However I can only seem to use one camera, so for example if my phone has a front and a back camera only the front camera is used. How would I be able to switch the camera that is in use?
I had read something somewhere about using something like this:
private static async Task<DeviceInformation> GetCameraID(Windows.Devices.Enumeration.Panel desired)
{
DeviceInformation deviceID = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture))
.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == desired);
return deviceID;
}
However this always returns null for me, since the deviceID is always null.
Alternatively is there the option of giving control to an application that takes the picture and returns the taken picture to my application? I have found the following, but it doesn't work for Windows Universal apps:
http://msdn.microsoft.com/en-us/library/windows/apps/hh394006(v=vs.105).aspx
Here is how I would do it:
First the initialization part
// First need to find all webcams
DeviceInformationCollection webcamList = await DeviceInformation.FindAllAsync(DeviceClass.All)
// Then I do a query to find the front webcam
DeviceInformation frontWebcam = (from webcam in webcamList
where webcam.EnclosureLocation != null
&& webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front
select webcam).FirstOrDefault();
// Same for the back webcam
DeviceInformation backWebcam = (from webcam in webcamList
where webcam.EnclosureLocation != null
&& webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back
select webcam).FirstOrDefault();
// Then you need to initialize your MediaCapture
newCapture = new MediaCapture();
await newCapture.InitializeAsync(new MediaCaptureInitializationSettings
{
// Choose the webcam you want
VideoDeviceId = backWebcam.Id,
AudioDeviceId = "",
StreamingCaptureMode = StreamingCaptureMode.Video,
PhotoCaptureSource = PhotoCaptureSource.VideoPreview
});
// Set the source of the CaptureElement to your MediaCapture
// (In my XAML I called the CaptureElement *Capture*)
Capture.Source = newCapture;
// Start the preview
await newCapture.StartPreviewAsync();
Secondly take the picture
//Set the path of the picture you are going to take
StorageFolder folder = ApplicationData.Current.LocalFolder;
var picPath = "\\Pictures\\newPic.jpg";
StorageFile captureFile = await folder.CreateFileAsync(picPath, CreationCollisionOption.GenerateUniqueName);
ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg();
//Capture your picture into the given storage file
await newCapture.CapturePhotoToStorageFileAsync(imageProperties, captureFile);
That should solve your problem.

Toggle flashlight in Windows Phone 8.1

Can anyone say how to toggle flashlight in Windows Phone 8.1 using C#? It seems like there are lots of API changes in Windows Phone 8.1 and most of the API's in WP 8.0 are not supported. Answers are highly appreciated.
I'm able to use TorchControl on my Lumia 820 like this - first you have to specify which camera you will use - the default is front (I think that's why you may find some problems) and we want the back one - the one with flash light. Sample code:
// edit - I forgot to show GetCameraID:
private static async Task<DeviceInformation> GetCameraID(Windows.Devices.Enumeration.Panel desiredCamera)
{
DeviceInformation deviceID = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture))
.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == desiredCamera);
if (deviceID != null) return deviceID;
else throw new Exception(string.Format("Camera of type {0} doesn't exist.", desiredCamera));
}
// init camera
async private void InitCameraBtn_Click(object sender, RoutedEventArgs e)
{
var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Back);
captureManager = new MediaCapture();
await captureManager.InitializeAsync(new MediaCaptureInitializationSettings
{
StreamingCaptureMode = StreamingCaptureMode.Video,
PhotoCaptureSource = PhotoCaptureSource.VideoPreview,
AudioDeviceId = string.Empty,
VideoDeviceId = cameraID.Id
});
}
// then to turn on/off camera
var torch = captureManager.VideoDeviceController.TorchControl;
if (torch.Supported) torch.Enabled = true;
// turn off
if (torch.Supported) torch.Enabled = false;
Note that it's a good idea to call captureManager.Dispose() after you finish with it.
Note also that on some phones to turn on torch/flashlight you will need to start preview first.
Windows Phone 8.1 is the first version with a dedicated API for controlling the camera light. This API stems from Windows 8.1 but is usable in Windows Phone 8.1 projects and in Windows Phone Silverlight 8.1 projects.
var mediaDev = new MediaCapture();
await mediaDev.InitializeAsync();
var videoDev = mediaDev.VideoDeviceController;
var tc = videoDev.TorchControl;
if (tc.Supported)
{
if (tc.PowerSupported)
tc.PowerPercent = 100;
tc.Enabled = true;
}
Note:
Note: TorchControl.Supported returns false on most phones in WP8.1 developer preview. It is expected to be fixed by a firmware update by the time WP 8.1 is released. Tested Phones at the time of writing: Lumia 620, 822, 1020: not working, Lumia 1520: working.
In Nokia Lumia 1520, you use FlashControl to toggle the flash light instead of TorchControl.
//to switch OFF flash light
mediacapture.VideoDeviceController.FlashControl.Enabled = false;
//to switch ON flash light
mediacapture.VideoDeviceController.FlashControl.Enabled = true;
Doesn't work on my Lumia 1520. You need to start video recording to get flashlight working:
var videoEncodingProperties = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Vga);
var videoStorageFile = await KnownFolders.VideosLibrary.CreateFileAsync("tempVideo.mp4", CreationCollisionOption.GenerateUniqueName);
await captureManager.StartRecordToStorageFileAsync(videoEncodingProperties, videoStorageFile);
In my Lumia 1520. I need to start video recording and start preview to get flashlight working:
await captureManager.StartPreviewAsync();

Categories

Resources