Thursday, January 24, 2013

Windows phone PhotoCapture application

we can make a photo capturing , saving it to file system and showing on the screen. for this purpose please make a project with the name PhotoCapture and copy the given layout to MainPage.xaml code to it


<phone:PhoneApplicationPage
    x:Class="PhotoCapture.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="696"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Image Height="334" HorizontalAlignment="Left" Margin="12,6,0,0" Name="imgPhoto" Stretch="Fill" VerticalAlignment="Top" Width="438" />
            <TextBlock Height="30" Margin="12,484,6,0" Name="txtStatus" Text="Status" VerticalAlignment="Top" />
            <Button Content="TwitPic" Height="72" HorizontalAlignment="Left" Margin="296,406,0,0" Name="btnUpload" VerticalAlignment="Top" Width="160" Click="btnUpload_Click" />
        </Grid>
    </Grid>

    <phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True">
            <shell:ApplicationBar.Buttons>
                <shell:ApplicationBarIconButton x:Name="btnCamera" Text="Take Photo" IconUri="images/appbar.feature.camera.rest.png" Click="btnCamera_Click"/>
                <shell:ApplicationBarIconButton Text="Open Photo" IconUri="images/appbar.folder.rest.png" Click="btnOpenPhoto_Click"/>
                <shell:ApplicationBarIconButton Text="Save Photo" IconUri="images/appbar.save.rest.png" Click="btnSave_Click"/>
            </shell:ApplicationBar.Buttons>
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>

    <!--Sample code showing usage of ApplicationBar-->
    <!--<phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
            <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
            <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
            <shell:ApplicationBar.MenuItems>
                <shell:ApplicationBarMenuItem Text="MenuItem 1"/>
                <shell:ApplicationBarMenuItem Text="MenuItem 2"/>
            </shell:ApplicationBar.MenuItems>
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>-->

</phone:PhoneApplicationPage>

the MainPage.xaml.cs code is as follows


using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Xml.Linq;
using Microsoft.Phone;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using Microsoft.Xna.Framework.Media;

namespace PhotoCapture
{
    public partial class MainPage : PhoneApplicationPage
    {
        private CameraCaptureTask cameraCaptureTask;
        WriteableBitmap original;
        byte[] imageBits;

        WriteableBitmap resized;
        private PhotoChooserTask photoChooserTask;

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            cameraCaptureTask = new CameraCaptureTask();
            cameraCaptureTask.Completed += PhotoChooserTaskCompleted;

            photoChooserTask = new PhotoChooserTask();
            photoChooserTask.Completed += new EventHandler<PhotoResult>(PhotoChooserTaskCompleted);

        }

        private void btnCamera_Click(object sender, EventArgs e)
        {
            cameraCaptureTask.Show();
        }

        private void btnOpenPhoto_Click(object sender, EventArgs e)
        {
            photoChooserTask.Show();
        }

        private void PhotoChooserTaskCompleted(object sender, PhotoResult e)
        {
            if (e.ChosenPhoto != null)
            {
                imageBits = new byte[(int)e.ChosenPhoto.Length];
                e.ChosenPhoto.Read(imageBits, 0, imageBits.Length);
                e.ChosenPhoto.Seek(0, System.IO.SeekOrigin.Begin);

                var bitmapImage = PictureDecoder.DecodeJpeg(e.ChosenPhoto);
                this.imgPhoto.Source = bitmapImage;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                var library = new MediaLibrary();
                library.SavePicture("PhotoCapture Photo", imageBits);

                txtStatus.Text = "Successfully saved photo.";
            }
            catch (Exception ex)
            {
                txtStatus.Text = "Failed to save photo. Exception: " + ex.Message;
            }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            try
            {              
                IDictionary<string, string> queryStrings = this.NavigationContext.QueryString;
                if (queryStrings.ContainsKey("token"))
                {
                    MediaLibrary library = new MediaLibrary();
                    Picture picture = library.GetPictureFromToken(queryStrings["token"]);

                    BitmapImage bitmap = new BitmapImage();
                    bitmap.SetSource(picture.GetImage());
                    WriteableBitmap picLibraryImage = new WriteableBitmap(bitmap);
                    imgPhoto.Source = picLibraryImage;
                }

                if (queryStrings.ContainsKey("FileId"))
                {
                    MediaLibrary library = new MediaLibrary();
                    Picture picture = library.GetPictureFromToken(queryStrings["FileId"]);


                    BitmapImage bitmap = new BitmapImage();
                    bitmap.SetSource(picture.GetImage());
                    WriteableBitmap picLibraryImage = new WriteableBitmap(bitmap);
                    imgPhoto.Source = picLibraryImage;
                }
            }
            catch (Exception ex)
            {
                Dispatcher.BeginInvoke(() => txtStatus.Text = ex.Message);
            }
        }

        public void UploadPhoto()
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://twitpic.com/api/upload");
            request.ContentType = "application/x-www-form-urlencoded";
            request.Method = "POST";
            request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
        }


        private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
        {
            try
            {

                HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
                string encoding = "iso-8859-1";
                // End the operation
                Stream postStream = request.EndGetRequestStream(asynchronousResult);
                string boundary = Guid.NewGuid().ToString();
                request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);

                string header = string.Format("--{0}", boundary);
                string footer = string.Format("--{0}--", boundary);

                StringBuilder contents = new StringBuilder();
                contents.AppendLine(header);

                string fileHeader = String.Format("Content-Disposition: file; name=\"{0}\"; filename=\"{1}\"; ", "media", "testpic.jpg");
                string fileData = Encoding.GetEncoding(encoding).GetString(imageBits, 0, imageBits.Length);

                contents.AppendLine(fileHeader);
                contents.AppendLine(String.Format("Content-Type: {0};", "image/jpeg"));
                contents.AppendLine();
                contents.AppendLine(fileData);
                contents.AppendLine(header);
                contents.AppendLine(String.Format("Content-Disposition: form-data; name=\"{0}\"", "username"));
                contents.AppendLine();
                contents.AppendLine("BeginningWP7");

                contents.AppendLine(header);
                contents.AppendLine(String.Format("Content-Disposition: form-data; name=\"{0}\"", "password"));
                contents.AppendLine();
                contents.AppendLine("windowsphone7");

                contents.AppendLine(footer);

                // Convert the string into a byte array.
                byte[] byteArray = Encoding.GetEncoding(encoding).GetBytes(contents.ToString());

                // Write to the request stream.
                postStream.Write(byteArray, 0, contents.ToString().Length);
                postStream.Close();

                // Start the asynchronous operation to get the response
                request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
            }
            catch (Exception ex)
            {
                Dispatcher.BeginInvoke(() => txtStatus.Text = ex.Message);              
            }
        }

        private void GetResponseCallback(IAsyncResult asynchronousResult)
        {
            try
            {

                HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
                // End the operation
                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
                Stream streamResponse = response.GetResponseStream();
                StreamReader streamRead = new StreamReader(streamResponse);
                string responseString = streamRead.ReadToEnd();

                XDocument doc = XDocument.Parse(responseString);
                XElement rsp = doc.Element("rsp");
                string status = rsp.Attribute(XName.Get("status")) != null ? rsp.Attribute(XName.Get("status")).Value : rsp.Attribute(XName.Get("stat")).Value;

                // Close the stream object
                streamResponse.Close();
                streamRead.Close();

                // Release the HttpWebResponse
                response.Close();

                Dispatcher.BeginInvoke(() => txtStatus.Text = "Successfully uploaded photo to TwitPic");              

            }
            catch (Exception ex)
            {
                Dispatcher.BeginInvoke(() => txtStatus.Text = ex.Message);
            }
        }

        private void btnUpload_Click(object sender, RoutedEventArgs e)
        {
            UploadPhoto();
        }

    }
}



No comments:

Post a Comment