Launching Webcam in Metro style apps - c#

I am building a win8 app which requires to launch the webcam for taking photos only.
I have seen the sample codes given in MSDN for Camera captures but I only want is onclick of CAPTURE Button the webcam should launched, take a pic and save it.
While in the sample codes they made the user to select option from the list box and on selectionchanged, the required function has been called. My problem is that I don't required any Listbox. Also they have used a class called SuspensionManager which I didn't understand. I am really confused.
Can somebody show me a way out?

Try this:
using Windows.Media.Capture;
var ui = new CameraCaptureUI();
ui.PhotoSettings.CroppedAspectRatio = new Size(4, 3);
var file = await ui.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (file != null)
{
var bitmap = new BitmapImage();
bitmap.SetSource(await file.OpenAsync(FileAccessMode.Read));
Photo.Source = bitmap;
}
Took from here

Related

Image on windows phone app is not updating when URI remains same but image data is change

I am building a windows phone sample app in in C# in which I have to display an image from SD card on the UI.
For this, I have made a small function as :
Private void UpdateImage()
{
BitmapImage bitmapd = new BitmapImage(new Uri(D:\\Pictures\\img1.bmp));
FingerImage.Source = bitmapd;
}
This image D:\\Pictures\\img1.bmp file is being replaced by my application with another image dynamically. So the URI remains same and only image data is being changed.
I need to update this new image on the UI, for this, I am calling above function. But the above function does not update the image on UI every time, except for the first time. After that, the image remains same on the UI.
I got the solution for my problem. I needed to ignore existing image cache. Below is the working code:
Private void UpdateImage()
{
BitmapImage bitmapd = new BitmapImage(new Uri(D:\\Pictures\\img1.bmp));
bitmapd.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
FingerImage.Source = bitmapd;
}

How to use ZXing Barcode Scanner in a Xamarin Forms app?

I'm trying to figure out how use to use ZXing Barcode Scanner nuget package in a simple Xamarin Forms application.
I'm trying to have a large area of my form show what my camera is seeing .. and if it scan's something.. then stop scanning and goto the next Page.
All the examples basically show this:
buttonScan.Click += (sender, e) => {
var scanner = new ZXing.Mobile.MobileBarcodeScanner();
var result = await scanner.Scan();
if (result != null)
Console.WriteLine("Scanned Barcode: " + result.Text);
};
but I don't understand what the actual view/control/widget which gets displayed onto the actual page, is? Like a big square with the camera contents getting displayed etc.
What is the trick to drop a control onto the page .. which I can then wire up to scan whatever the camera is 'seeing' .. and hopefully it will 'read' the barcode or qrcode?

Access camera preview-stream on Windows Phone 8.1

I'm trying to create a basic camera application (for my first application targeted towards WP). And I want to, of course, preview the camera data to the screen before taking a shot.
But the only samples I see online from MSDN etc are too old (many objects have been removed, ie the libraries are being updated frequently making the articles obsolete)
I'm having a problem just getting started with the preview-stream. It would be greatly appreciated if someone with knowledge could give me some help in this matter.
Thank you.
I suggest using the CaptureElement control
On your XAML add this:
<CaptureElement x:Name="Capture"
Stretch="UniformToFill"
FlowDirection="LeftToRight" />
I don't know if you want to use front or back webcam so I'll show code for both.
In your codeBehind (or your ViewModel if your using MVVM) add 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 newCapture = new MediaCapture();
await newCapture.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
Capture.Source = newCapture;
// Start the preview
await newCapture.StartPreviewAsync();
This will show the stream of the chosen webcam in your CaptureElement control, and works on both Windows Phone 8.1 and Windows 8.1

Can't play mp3 with MediaElement on WP7 emulator

I have an app where I have some prerecorded text to speech (As there is no default support for text to speech) then I want to play them like this
var mediaElement = new MediaElement();
mediaElement.Source = new Uri("sound.mp3", UriKind.Relative);
mediaElement.Position = new TimeSpan(0);
mediaElement.Play();
But nothing happens, do I HAVE to create a "real" control in my UI? I just want to play this sound when an event happens, I get no errors or nothing, nomatter if the mp3 is in the default folder or not.
The reason it's not playing is because you haven't added it to the Visual Tree. When you create a MediaElement programmatically, it needs to be added somewhere in the tree. You'll have to create a 'real control' in your UI, but it doesn't have to be seen.
var mediaElement = new MediaElement();
mediaElement.Source = new Uri("sound.mp3", UriKind.Relative);
mediaElement.Position = new TimeSpan(0);
LayoutRoot.Children.Add(mediaElement); //Add to visual tree
mediaElement.Play();
This depends on what happens in your code after mediaElement is declared. Currently, as soon as the method you declare it in ends, mediaElement will fall out of scope and become eligible for garbage collection.
You need to either:
Parent mediaElement to something, perhaps your UI
Make mediaElement a static field on the class
All that said, is playing through MediaElement supported in the Emulator?

Playing mp3 in WPF

MediaElement doesnt work for me in my WPF application.
mediaElement1.LoadedBehavior = MediaState.Manual;
mediaElement1.Source = new Uri(#"C:\Music\MySong.mp3", UriKind.RelativeOrAbsolute);
mediaElement1.Play();
When I do this in my Window1.xaml.cs file. Nothing happens. Atleast I cant hear anything. I have tried all kind of different things, but no sound.
In winforms:
axWindowsMediaPlayer1.URL = #"C:\Music\MySong.mp3";
axWindowsMediaPlayer1.Ctlcontrols.play();
Works without any problems. Any simple solution or things to try?
Ok I solved it. WPF only support MediaElement if you have Windows Media Player 10 or above. I was running WMP9.
Though I'm also new in wpf,One thing you should notice about media element is that providing source in the XAML tag is not worth working.
you need to provide the source with the urikind like this
media.Source = new Uri(#"E:\Pehli_Baar_Mohabbat.mp3",UriKind.RelativeOrAbsolute);
put this line in window constructor
and set loadedbehavious=manual and then check.
mediaElement1.LoadedBehavior = MediaState.Manual;
---- edit to-----
mediaElement1.LoadedBehavior = System.Windows.Controls.MediaState.Manual;

Categories

Resources