How to use Ink recognize in WPF? - wpf

I am using .Net 4.0 WPF. I want to use InkCanvas to collect user input, then recognize what character is inputted.
After googling, I found a solution in http://msdn.microsoft.com/zh-cn/library/ms754080.aspx
Unfortunately, it is for .net 3.5. Is there a solution for .net 4.0? Thanks.
===================
Download a TablePC SDK from http://www.microsoft.com/en-us/download/details.aspx?id=20989
then got the Micorsoft.Ink.dll. Below is the sample code...
using Microsoft.Ink;
using System.IO;
private void btnGetInput_Click(object sender, RoutedEventArgs e)
{
if (inkInput.Strokes.Count > 0)
{
System.Windows.Ink.StrokeCollection strokeList = inkInput.Strokes;
MemoryStream ms = new MemoryStream();
inkInput.Strokes.Save(ms);
InkCollector myInkCollector = new InkCollector();
Ink ink = new Ink();
ink.Load(ms.ToArray());
using (RecognizerContext myRecoContext = new RecognizerContext())
{
RecognitionStatus status;
RecognitionResult recoResult;
myRecoContext.Strokes = ink.Strokes;
recoResult = myRecoContext.Recognize(out status);
tbUserName.Text += recoResult.TopString;
}
inkInput.Strokes.Clear();
}
}

Related

Windows Phone - MediaElement not working anymore after navigation

I have a problem with the MediaElement in my windows phone (8) application. It's working fine until I navigate to some other page and come back on the page containing the MediaElement.
I have the following code to play a stream coming from Bing Translator APIs:
private void TranslationService_SpeakComplete(object sender, SpeakCompleteEventArgs e)
{
var stream = e.Stream;
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
try
{
MediaElement.Stop();
MediaElement.Source = null;
string filename = "FlipNLearnItAudio";
using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
{
bool fileExists = isf.FileExists(filename);
if (fileExists)
{
isf.DeleteFile(filename);
}
var isfs = isf.CreateFile(filename);
using (isfs)
{
Helpers.SaveFile(stream, isfs);
isfs.Position = 0;
MediaElement.AutoPlay = true;
MediaElement.SetSource(isfs);
}
}
}
catch (Exception ex)
{
//TODO: log exception
}
});
}
The biggest problem is that there's no exception.... the "MediaElement.SetSource(isfs);" is called normally without any exception!
Do you have any idea of what can happen?
Thanks for any help!
Bastien
Ok, I find a solution to my problem. I put the MediaElement in the App.xaml resources and now it's working fine...
But it doesn't explain why it was not working with the MediaElement in the MainPage.xaml.
If somebody has the answer, I would be very interested to hear it ;-).

WP7 Silverlight/XNA split

I want to make an app that has Silverlight menus but the game part of the app is XNA. I am trying to get the Silverlight/XNA split working using the code from this example game and the method of doing XNA rendering in Silverlight here. Combining these 2 tutorials I have source code that looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
namespace FYP
{
public partial class GamePage : PhoneApplicationPage
{
GameTimer timer;
SpriteBatch spriteBatch;
Texture2D ballTexture;
IList<Ball> balls = new List<Ball>();
bool touching = false;
public GamePage(ContentManager contentManager)
{
InitializeComponent();
//base.Initialize();
// Create a timer for this page
timer = new GameTimer();
timer.UpdateInterval = TimeSpan.FromTicks(333333);
//timer.Update += OnUpdate;
//timer.Draw += OnDraw;
// TODO: use this.Content to load your game content here
ballTexture = contentManager.Load<Texture2D>("Ball");
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
// Set the sharing mode of the graphics device to turn on XNA rendering
SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);
timer.Start();
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
// Set the sharing mode of the graphics device to turn off XNA rendering
SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);
// Stop the timer
timer.Stop();
}
private void OnUpdate(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
//this.Exit();
// TODO: Add your update logic here
//base.Update(gameTime);
HandleTouches();
UpdateBalls();
}
private void OnDraw(GameTime gameTime)
{
SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.White);
// TODO: Add your drawing code here
foreach (Ball ball in balls)
{
ball.Draw(spriteBatch);
}
//base.Draw(gameTime);
}
private void HandleTouches()
{
TouchCollection touches = TouchPanel.GetState();
if (!touching && touches.Count > 0)
{
touching = true;
Random random = new Random(DateTime.Now.Millisecond);
Color ballColor = new Color(random.Next(255), random.Next(255), random.Next(255));
Vector2 velocity = new Vector2((random.NextDouble() > .5 ? -1 : 1) * random.Next(9), (random.NextDouble() > .5 ? -1 : 1) * random.Next(9)) + Vector2.UnitX + Vector2.UnitY;
//Vector2 center = new Vector2((float)SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Width / 2, (float)SharedGraphicsDeviceManager.Current.GraphicsDevice.Height / 2);
Vector2 center = new Vector2((float)SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Width / 2, 200);
float radius = 25f * (float)random.NextDouble() + 5f;
balls.Add(new Ball(this, ballColor, ballTexture, center, velocity, radius));
}
else if (touches.Count == 0)
{
touching = false;
}
}
private void UpdateBalls()
{
foreach (Ball ball in balls)
{
ball.Update();
}
}
}
}
I do not understand how base works, I've had to comment out base.initialize, update and draw however base.OnNavigatedFrom works.
Also, should I be able to get my code to work in theory? I find it very complicated and although reading about XNA/Silverlight to be possible I can't find any source code where people have successfully combined XNA and Silverlight into the same app.
these videos will help you out to understand XNA and SilverLight/XNA combined platform
http://channel9.msdn.com/Series/Mango-Jump-Start/Mango-Jump-Start-11a-XNA-for-Windows-Phone--Part-1
http://channel9.msdn.com/Series/Mango-Jump-Start/Mango-Jump-Start-11b-XNA-for-Windows-Phone--Part-2
Theoretically XNA and SilverLight XNA Combined platform are pretty much the same, just a difference of bits and pieces, You can even ask XNA to render some SilverLight Control, which will make it easier to handle some button event in your game.
Hope this helps

How to convert type of ImageColorFrame in Kinect for Windows SDK v1 to type of Image<> in Emgu Cv?

I am currently working on face detection WPF Application using Emgu CV and Kinect for Windows SDK v1. I ever made same application before with Emgu CV, but using a web cam to capture the image frame. Using a web cam, it's not difficult to get the camera frame and convert it to type of Image<> in Emgu CV so that I could process it with face detection algorithm. But now, when I use color RGB camera form Kinect hardware, i can't do that. Neither implicit nor explicit conversion from type of ImageColorFrame (in Kinect for Windows SDK) to type of Image<> (in Emgu CV) are not working. How i could do that?
FYI, instead of doing a conversion, I also try to assign bytes property of type Image<> like this code below. But it's not working too. Thanks in advance.
using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
{
if (colorFrame == null)
{
return;
}
byte[] pixels = new byte[colorFrame.PixelDataLength];
colorFrame.CopyPixelDataTo(pixels);
nextFrame = new Image<Bgr, byte>(448, 336);
nextFrame.Bytes = pixels;
using (nextFrame.Bitmap = colorFrame.ToBitmap())
{
grayImage = new Image<Gray, byte>(448, 336);
grayImage = nextFrame.Convert<Gray, Byte>();
HaarCascade faceHaarCascade = new HaarCascade("" + Environment.CurrentDirectory + "\\haarcascade_frontalface_alt_tree.xml");
MCvAvgComp[][] facesDetected = grayImage.DetectHaarCascade(
faceHaarCascade,
1.1,
10,
Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
new System.Drawing.Size(20, 20));
foreach (MCvAvgComp face in facesDetected[0])
{
// draw rectangle in the facial image
nextFrame.Draw(face.rect, new Bgr(System.Drawing.Color.Blue), 2);
}
imageVideoRGB.Source = ToBitmapSource(nextFrame);
}
}
To convert Microsoft.Kinect.ImageColorFrame to Emgu.CV.Image, you should try this :
var bitmapSource = colorImageFrame.ToBitmapSource(); // using Coding4Fun.Kinect.Wpf extension
//Get a bitmap
var currentImage = KinectHelpers.GetBitmapFromBitmapSource(ColorBitmap);
// Then get the Image
var image = new Image<Bgr, Byte>(currentImage)
With KinectHelpers a static class :
public static class KinectHelpers
{
public static Bitmap GetBitmapFromBitmapSource(BitmapSource bSource)
{
Bitmap bmp;
using (MemoryStream ms = new MemoryStream())
{
BitmapEncoder enc = new BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bSource));
enc.Save(ms);
bmp = new Bitmap(ms);
}
return bmp;
}
}

Silverlight WebClient class returning empty string

I am trying to make a basic 'Silverlight Class Library' in Silverlight 4 to return basic Facebook Information using Facebook's Graph API, but I am only getting empty strings being returned.
I am using the following code:
string _Response = "";
public string GetFacebookMe(string access_token)
{
WebClient facebookClient = new WebClient();
facebookClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(facebookClientDownloadStringCompleted);
facebookClient.DownloadStringAsync(new Uri("https://graph.facebook.com/me" + "?access_token=" + access_token));
string ret = _Response;
return ret;
}
private void facebookClientDownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
_Response = e.Result;
}
else
{
_Response = e.Error.Message;
}
}
I tried while debugging to init _Response to the value "Default", and the string "Default" was consequently being returned. I have been messing with this for a while and I'm not sure where I'm going wrong.
Thanks in advance!
It means a lot of effort to use directly WebClient. When using Silverlight and .Net Framework 4.0 you may use Facebook C# SDK at Codeplex
Usage of the SDK is excellently covered in this blog by Prabir Shrestha

Windows Phone 7 : Silverlight and playing Music

I have built a simple game in WP7 and I am trying to add background music to it using MediaPlayer. The problem is it just bombs with
{"An unexpected error has occurred."} System.Exception {System.InvalidOperationException}
Code
try
{
MediaPlayer.Stop();
// Timer to run the XNA internals (MediaPlayer is from XNA)
DispatcherTimer dt = new DispatcherTimer();
dt.Interval = TimeSpan.FromMilliseconds(33);
dt.Tick += delegate { try { FrameworkDispatcher.Update(); } catch { } };
dt.Start();
Uri pathToFile = new Uri("Audio/music.m4a", UriKind.Relative);
Song playingSong = Song.FromUri("Music", pathToFile);
MediaPlayer.Play(playingSong);
}
catch (Exception e)
{
musicFailed = true;
Console.WriteLine("Exception: {0}", e.ToString());
MessageBox.Show("Warning, music failed to play however you can still continue to play your game.");
}
}
I tried a few tweaks, converting file to mp3, different paths etc. The file is marked for copy always and content type I also tried removing the Dispatcher as dont know what that is for.
If you can convert your audio files to WAV format, you can try using the XNA SoundEffect and SoundEffectInstance classes:
SoundEffect se = SoundEffect.FromStream(isolatedStorageFileStream);
SoundEffectInstance sei = se.CreateInstance();
sei.Play();
For this to work, you will need to reference the XNA library (Microsoft.XNA.Framework) and initialize the framework in this way:
App.xaml:
<Application>
<Application.ApplicationLifetimeObjects>
<local:XNAFrameworkDispatcherService />
...
</Application.ApplicationLifetimeObjects>
</Application>
And create this class somewhere in the app namespace ("local" in the previous xaml references this namespace):
public class XNAFrameworkDispatcherService : IApplicationService
{
private DispatcherTimer frameworkDispatcherTimer;
public XNAFrameworkDispatcherService()
{
this.frameworkDispatcherTimer = new DispatcherTimer();
this.frameworkDispatcherTimer.Interval = TimeSpan.FromTicks(333333);
this.frameworkDispatcherTimer.Tick += frameworkDispatcherTimer_Tick;
FrameworkDispatcher.Update();
}
void frameworkDispatcherTimer_Tick(object sender, EventArgs e) { FrameworkDispatcher.Update(); }
void IApplicationService.StartService(ApplicationServiceContext context) { this.frameworkDispatcherTimer.Start(); }
void IApplicationService.StopService() { this.frameworkDispatcherTimer.Stop(); }
}
Your problem is that your timer maybe not fired the first tick and FrameworkDispatcher.Update() not ran. Then Play throws a System.InvalidOperationException.
Better dispatch XNA Events global in your Application.
Complete Guide: XNAAsyncDispatcher

Resources