2012年6月28日 星期四

Google Street View Application (kinect) -- 2

Ref:
a. kinect SDK for windows v1.5 Green Screen -WPF
b. Google street view api

1. Green Screen WPF application
2. Add .NET Reference
    System.Drawing
    System.Windows.Forms
3. Add 3 methods (2 event methods) as follows
3-1 SaveWebPage2Image was used to convert google street view as a bitmap
3-2 Mouse_Up was used to replace the background image as a pre-defined street view
3-3 Mouse_Wheel was used to change the heading angle of the previous street view



  public System.Drawing.Bitmap SaveWebPage2Image(string url)
        {
            // Load the webpage into a WebBrowser control
            System.Windows.Forms.WebBrowser wb = new System.Windows.Forms.WebBrowser();
            wb.ScrollBarsEnabled = false;
            wb.ScriptErrorsSuppressed = true;
            wb.Navigate(url);

            while (wb.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete) { System.Windows.Forms.Application.DoEvents(); }

            // Take Screenshot of the web pages full width
            wb.Width = wb.Document.Body.ScrollRectangle.Width;

            // Take Screenshot of the web pages full height
            wb.Height = wb.Document.Body.ScrollRectangle.Height;

            // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(wb.Width, wb.Height);
            wb.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, wb.Width, wb.Height));
            wb.Dispose();

            return bitmap;
        }

        int angle1 = 120;
        private void Window_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {

           
            this.Title = "Headingangle : " + angle1.ToString();
            System.Drawing.Bitmap bmp = SaveWebPage2Image(@"http://maps.googleapis.com/maps/api/streetview?size=600x300&location=25.080067,121.397699&heading=" + angle1.ToString() + @"&fov=90&pitch=-10&sensor=false");
            IntPtr hBitmap = bmp.GetHbitmap();
            System.Windows.Media.ImageSource WpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

            Backdrop.Source = WpfBitmap;
        }

        private void Window_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
        {
            angle1 += 20;
            this.Title = "Headingangle : " + angle1.ToString();
            System.Drawing.Bitmap bmp = SaveWebPage2Image(@"http://maps.googleapis.com/maps/api/streetview?size=600x300&location=25.080067,121.397699&heading=" + angle1.ToString() + @"&fov=90&pitch=-10&sensor=false");
            IntPtr hBitmap = bmp.GetHbitmap();
            System.Windows.Media.ImageSource WpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

            Backdrop.Source = WpfBitmap;
        }
4. 下載

2012年6月27日 星期三

Google Street View WPF Application -- 1

將Google 街景圖以WPF之Image控制項展示。
1. 加入WIndowForm元件。 

// Add ref System.Drawing
//         System.Windows.Forms
2. 加入html2bitmap方法

        public System.Drawing.Bitmap SaveWebPage2Image(string url)
        {
            // Load the webpage into a WebBrowser control
            System.Windows.Forms.WebBrowser wb = new System.Windows.Forms.WebBrowser();
            wb.ScrollBarsEnabled = false;
            wb.ScriptErrorsSuppressed = true;
            wb.Navigate(url);

            while (wb.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete) { System.Windows.Forms.Application.DoEvents(); }

            // Take Screenshot of the web pages full width
            wb.Width = wb.Document.Body.ScrollRectangle.Width;

            // Take Screenshot of the web pages full height
            wb.Height = wb.Document.Body.ScrollRectangle.Height;

            // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(wb.Width, wb.Height);
            wb.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, wb.Width, wb.Height));
            wb.Dispose();

            return bitmap;
        }
3. 初始設定
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //convert System.Drawing.Image to WPF image
            System.Drawing.Bitmap bmp = SaveWebPage2Image(@"http://maps.googleapis.com/maps/api/streetview?size=600x300&location=25.080067,121.397699&heading=300&fov=90&pitch=-10&sensor=false");
            IntPtr hBitmap = bmp.GetHbitmap();
            System.Windows.Media.ImageSource WpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
            im.Source = WpfBitmap;
        }
4. MouseUp Event 改辦視角
        int angle1 = 120;
        private void Window_MouseUp(object sender, MouseButtonEventArgs e)
        {
            angle1 += 20;
            this.Title = "Headingangle : " + angle1.ToString();
            System.Drawing.Bitmap bmp = SaveWebPage2Image(@"http://maps.googleapis.com/maps/api/streetview?size=600x300&location=25.080067,121.397699&heading=" + angle1.ToString() + @"&fov=90&pitch=-10&sensor=false");
            IntPtr hBitmap = bmp.GetHbitmap();
            System.Windows.Media.ImageSource WpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
            im.Source = WpfBitmap;
        }
    }
}
5. 下載

Convert url to bitmap and then WPF(Image)

Ref: Wait to add

1. There are some examples to save webpage into bitmap.

public System.Drawing.Bitmap SaveWebPage2Image(string url)
        {
            // Load the webpage into a WebBrowser control
            System.Windows.Forms.WebBrowser wb = new System.Windows.Forms.WebBrowser();
            wb.ScrollBarsEnabled = false;
            wb.ScriptErrorsSuppressed = true;
            wb.Navigate(url);

            while (wb.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete) { System.Windows.Forms.Application.DoEvents(); }

            // Take Screenshot of the web pages full width
            wb.Width = wb.Document.Body.ScrollRectangle.Width;

            // Take Screenshot of the web pages full height
            wb.Height = wb.Document.Body.ScrollRectangle.Height;

            // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(wb.Width, wb.Height);
            wb.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, wb.Width, wb.Height));
            wb.Dispose();

            return bitmap;
        }
2. xaml file
 <Grid>
        <Image Name="im" />
    </Grid>
3. Add System.Drawing and System.Windows.Forms
4. Window_Loaded
   private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            
            //convert System.Drawing.Image to WPF image
            System.Drawing.Bitmap bmp = SaveWebPage2Image(@"http://www.google.com.tw");
            IntPtr hBitmap = bmp.GetHbitmap();
            System.Windows.Media.ImageSource WpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

            im.Source = WpfBitmap;
        }
5. 

2012年6月26日 星期二

Speech SDK 11 -- 2

中文語音辨識 (ASUS A43S with Windows 7 x64)
1) Re-Install Speech SDK
1-1 Repair speeh platform runtime
      http://www.microsoft.com/en-us/download/details.aspx?id=27225
1-2 Repair Speech SDK
      http://www.microsoft.com/en-us/download/details.aspx?id=27226
1-3 Install speech runtime Language (zh-TW)
     http://www.microsoft.com/en-us/download/details.aspx?id=27224#instructions
2) Add Ref :C:\Program Files\Microsoft SDKs\Speech\v11.0\Assembly\Microsoft.Speech.dll

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Speech.Recognition;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a new SpeechRecognitionEngine instance.
            SpeechRecognitionEngine sre = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("zh-TW"));

            // Create a simple grammar that recognizes "red", "green", or "blue".
            Choices colors = new Choices();
            colors.Add("向上");
            colors.Add("向下");
            colors.Add("左");
            colors.Add("右");
            colors.Add("前");
            colors.Add("後");

            GrammarBuilder gb = new GrammarBuilder();
            gb.Append(colors);

            // Create the actual Grammar instance, and then load it into the speech recognizer.
            Grammar g = new Grammar(gb);
            sre.LoadGrammar(g);

            // Register a handler for the SpeechRecognized event.
            sre.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);
            sre.SetInputToDefaultAudioDevice();
            sre.RecognizeAsync(RecognizeMode.Multiple);
            while (true)
            { }

        }

        static void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            Console.WriteLine(e.Result.Text);
        }
    }
}