How to display Camera Stream with Xamarin.Forms. Is it possible? - c#

So I have created an interface and implemented this interface currently in my Windows Phone 8 application to start a camera stream. Like so:
public void StartCamera()
{
var cam = new Microsoft.Devices.PhotoCamera(Microsoft.Devices.CameraType.Primary);
System.Windows.Controls.Canvas canvas = new System.Windows.Controls.Canvas();
var videobrush = new System.Windows.Media.VideoBrush();
canvas.Background = videobrush;
videobrush.SetSource(cam);
}
so now the canvas object is holding a camera stream. But how Do I then pass this back to Xamarin forms and display it.
In Xamarin Forms I have:
IOperations camera = DependencyService.Get<IOperations>();
if (camera != null)
{
camera.StartCamera();
}
and this works apart from obviously I have no control to display the camera stream. How would I display this stream in Xamarin forms?

There is now a new Custom Renderer example project available here:
http://developer.xamarin.com/samples/xamarin-forms/CustomRenderers/ContentPage/
With a walkthrough of this sample available here:
https://developer.xamarin.com/guides/cross-platform/xamarin-forms/custom-renderer/contentpage/
I know this is a link only answer but due to the complexity of the question it would be too time consuming to repost the links contents
There is also now a control by the Xamarin Community toolkit called CameraView
Xamarin Community Toolkit Camera View

Related

How to add a gif animation in a wpf page of class library project? [duplicate]

This question already has answers here:
Animating Gif in WPF
(3 answers)
Closed 4 years ago.
I am new to creating revit addins and using class library projects. Currently, I am creating an addin using a class library project and the first screen is a login screen(using wpf for this) and I want to add a gif animation to that wpf page. I have searched online and found some solutions (wpfanimatedgif nuget package, mediaelement) but the gif won't play inside revit. So I tried these solutions in a WPF project instead of class library project. And they worked. Can someone please help me? Is there another nuget package available?
Thanks
EDIT:-
Inside
C# code
, it does shows the thumbnail of the GIF when I use the media element.
But inside
Revit
, even the thumbnail for the GIF is not visible
In the Xaml:
<MediaElement Name="My_GIF" LoadedBehavior="Play" UnloadedBehavior="Manual" MediaEnded="MediaElement_MediaEnded" Source="Images\planBIMGIF.gif"/>
In the code behind:-
private void MediaElement_MediaEnded(object sender, RoutedEventArgs e)
{
My_GIF.Position = new TimeSpan(0, 0, 20);
My_GIF.Play();
//MessageBox.Show("Playing GIF");
}
I just use an image in WPF instead of a media element because with a media element my animations keep stopping before finishing the animation. So now I just feed it to the image frame by frame. You also have more control over it like this.
XAML:
<Image x:Name="image"/>
Code-behind (in a function that gets called after InitializeComponent):
Uri myUri = new Uri("Images\planBIMGIF.gif", UriKind.RelativeOrAbsolute);
GifBitmapDecoder decoder = new GifBitmapDecoder(myUri,
BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bitmapSource;
int frameCount = decoder.Frames.Count;
Thread th = new Thread(() =>
{
for (int i = 0; i < frameCount - 1; i++)
{
this.Dispatcher.Invoke(new Action(delegate ()
{
bitmapSource = decoder.Frames[i];
image.Source = bitmapSource;
}));
System.Threading.Thread.Sleep(50);
}
});
th.Start();
This plays the gif once and stops at the frame before the last one (my gifs had a blank frame at the end). If you want it to repeat, you can put the 'for' loop in a while loop or some other loop of your preference.
You can adjust the speed by setting the Thread.Sleep(50) time to something else.
Also, be sure to set your image as "resource" in its properties, and you might have to reference it like this in the Uri: "pack://application:,,,/<ProjectName>;component/Images/planBIMGIF.gif" with <ProjectName> being the name of your project in VS.

Display Current Video in Windows Phone 8 using AudioVideoCaptureDevice?

I've managed to setup code for a Windows Phone 8 Application that initializes and can start/stop recording video using an AudioVideoCaptureDevice. (saves it to an IRandomAccessStream)
//Initialize Camera Recording
Windows.Foundation.Size resolution = new Windows.Foundation.Size(640, 480);
captureDevice = await AudioVideoCaptureDevice.OpenAsync(CameraSensorLocation.Back, resolution);
captureDevice.VideoEncodingFormat = CameraCaptureVideoFormat.H264;
captureDevice.AudioEncodingFormat = CameraCaptureAudioFormat.Aac;
captureDevice.RecordingFailed += captureDevice_RecordingFailed;
However, I cannot figure out how to hook this recording up to a VideoBrush to display the recording to the user. I want the user to be able to see the video they are recording as it is happening...
I know there is a tutorial that shows how to do this using the old APIs for Windows Phone 7 (CaptureSource, VideoDevice, etc.) but I specifically need to use the AudioVideoCaptureDevice to record. Anyone know how to display this video on screen?
Well, I was able to solve my problem.
Apparently there is a library in Microsoft.Devices that contains extensions for the VideoBrush class.
Therefore, in order to set the videobrush source to an AudioVideoCaptureDevice, you must first have:
using Microsoft.Devices;
at the top of your class in which your using the videobrush.
Hope this is able to help someone else.
You should be able to simply use VideoBrush.SetSource(captureDevice).

Animations in Windows 8 [duplicate]

Is there a control to show an animated gif in a Windows Store (Metro) app in Windows 8? I am using C# and XAML with databinding.
The Image control doesn't support animated GIFs. You will need to extract the frames and animate them on your own timer.
You should take a look at this link which might help you regarding your question:
http://advertboy.wordpress.com/2012/05/08/animated-gifs-in-xamlc/
You cannot display an animated gif in a grid. However, you can display an animated gift in the webview controller.
Just for a note: you can use the BitmapDecoder class to read the GIF frames, create a storyboard an animated them.
I've got an example of an Windows 8 user control on my blog: http://www.henrikbrinch.dk/Blog/2013/02/21/Windows-8---GIF-animations--the-RIGHT-way
I managed to display gif's in my windows 8 metro app simply by converting the gifs to jpegs and then running an infinite loop.
The code is as follows :
public async void UpdateImage()
{
await Task.Delay(300);
var frame = this.Frame.CurrentSourcePageType.Name;
if (frame != ("CURRENTFRAME")) return;
if (count <= 4)
{
var img = (BitmapImage) Resources["bitmap" + count];
imgTap.Source = img;
UpdateLayout();
count++;
UpdateImage();
}
else
{
count = 1;
UpdateImage();
}
}
So basically I'm saving the converted jpegs in my page.resources and then naming them as bitmap1,bitmap2 .. etc .
I m checking whether the current frame is the frame which has to display the gif or else the gif code would run in the background for the entire time which is a waste of memory and CPU.
Just call this method on any of the Loaded method and it should run fine.
This is probably overkill, but you could try using a WebView to display the GIF. The WebView control introduces its own set of headaches, however, so unless you're willing to deal with said headaches, I would recommend avoiding it unless you absolutely have to use it.
If I am right then Silverlight doesn't support GIF and Metro Apps are based on Silverlight platform. Hence dont contain support for GIF images natively. You can however use 3rd party controls like http://www.componentone.com/SuperProducts/ImageSilverlight/.

Display an Animated GIF in a Metro App

Is there a control to show an animated gif in a Windows Store (Metro) app in Windows 8? I am using C# and XAML with databinding.
The Image control doesn't support animated GIFs. You will need to extract the frames and animate them on your own timer.
You should take a look at this link which might help you regarding your question:
http://advertboy.wordpress.com/2012/05/08/animated-gifs-in-xamlc/
You cannot display an animated gif in a grid. However, you can display an animated gift in the webview controller.
Just for a note: you can use the BitmapDecoder class to read the GIF frames, create a storyboard an animated them.
I've got an example of an Windows 8 user control on my blog: http://www.henrikbrinch.dk/Blog/2013/02/21/Windows-8---GIF-animations--the-RIGHT-way
I managed to display gif's in my windows 8 metro app simply by converting the gifs to jpegs and then running an infinite loop.
The code is as follows :
public async void UpdateImage()
{
await Task.Delay(300);
var frame = this.Frame.CurrentSourcePageType.Name;
if (frame != ("CURRENTFRAME")) return;
if (count <= 4)
{
var img = (BitmapImage) Resources["bitmap" + count];
imgTap.Source = img;
UpdateLayout();
count++;
UpdateImage();
}
else
{
count = 1;
UpdateImage();
}
}
So basically I'm saving the converted jpegs in my page.resources and then naming them as bitmap1,bitmap2 .. etc .
I m checking whether the current frame is the frame which has to display the gif or else the gif code would run in the background for the entire time which is a waste of memory and CPU.
Just call this method on any of the Loaded method and it should run fine.
This is probably overkill, but you could try using a WebView to display the GIF. The WebView control introduces its own set of headaches, however, so unless you're willing to deal with said headaches, I would recommend avoiding it unless you absolutely have to use it.
If I am right then Silverlight doesn't support GIF and Metro Apps are based on Silverlight platform. Hence dont contain support for GIF images natively. You can however use 3rd party controls like http://www.componentone.com/SuperProducts/ImageSilverlight/.

XNA with Windows Forms - Opening new window

Okay, so here we go. I'm attempting to make an application, using XNA as the base because of its renderer. One of the things that is necessary in this project is to open a new window (as a dialog), in which is embedded a separate XNA render panel. I'm using this as an interactive preview panel, so I absolutely need XNA to render in there. However, it seems XNA is not very well equipped to do this. I have tried various things myself, but to no avail (either producing errors and not rendering correctly, or rendering in the wrong aspect ratio, etc.). Normally, I would post code here, but since I have had such little luck, there isn't much to post.
My application currently consists of an XNA application embedded within a Form, and I have a button to open the preview panel, which in theory should pop up as a Form Dialog, containing the XNA renderer, to allow me to draw the preview. I have been trying this for several hours, and got nowhere, so I'm asking for a bit of help here.
Thanks, anyway.
EDIT: Okay, I've made a little progress, but I have 2 problems. Firstly, any textures drawn with a sprite batch appear the right dimensions, but filled with solid black. Also, when I open the dialog, and then close it, and close the application, I get an AccessViolationException. I strongly suspect the two errors are linked in some way.
Here is my code initialising the preview dialog. (a is a custom class that essentially consists of a LinkedList of Texture2D objects).
animPrev = new AnimationPreview(a);
animPrev.Show();
My AnimationPreview class is an extension of the Form class, and contains a PreviewControl object, which is an extension of the GraphicsDeviceControl found in the XNA Winforms sample. Note that my main form extends the XNA Game class, for various reasons.
The PreviewControl object is set up like this:
protected override void Initialize()
{
sb = new SpriteBatch(GraphicsDevice);
Application.Idle += delegate { Invalidate(); };
}
And the Draw method contains:
protected override void Draw()
{
GraphicsDevice.Clear(Microsoft.Xna.Framework.Graphics.Color.Violet);
if (frame != null)
{
sb.Begin();
sb.Draw(Image, Vector2.Zero, Color.White);
sb.End();
}
}
This clears the background of the form violet, as expected, and draws a black box of the same size as Image. This is not expected. Hopefully someone can help me out here.
NOTE: An acceptable alternative would be to convert XNA Texture2D objects to System.Drawing.Image objects. However, I am using XNA 3.1, so I can't just save the texture to a stream and reload it.
Actually, after having tried this, it's a bit dodgy, and very slow, so I'd rather not do it this way.
Did you take a look at the following official tutorials / samples?
XNA WinForms Series 1: Graphics Device
XNA WinForms Series 2: Content Loading
They should explain everything in my opinion. You even find downloadable source for the samples.

Categories

Resources