简体   繁体   English

相机需要时间来完成Windows Phone中的任务

[英]Camera is taking time to finish the task in Windows phone

Actually i load the VideoBrush using the Camera to take the Snap shot of the object using the PhotoCamera option in Windows Phone 7.1. 实际上,我使用Camera加载VideoBrush,以使用Windows Phone 7.1中的PhotoCamera选项为对象拍摄快照。

Once snap shot is take i set the e.ImageSteam from the CaptureImageAvailable event to the ImageBrush source. 拍摄快照后,我将从CaptureImageAvailable事件将e.ImageSteam设置为ImageBrush源。

Once i taken the snapshot, it takes time to call the CaptureImageAvailable or CaptureCompleted events in the WindowsPhone . 拍摄快照后,将需要一些时间来调用WindowsPhone中的CaptureImageAvailable或CaptureCompleted事件。 Why this case ? 为什么会这样呢?

Do i need to write something new to resolve this problem? 我需要写一些新的东西来解决这个问题吗?

For Take Photo and Choose photo , simply you can follow this, 对于“拍照并选择照片” ,只需遵循以下步骤,

MainPage.xaml: MainPage.xaml:

<Button Content="Take Photo" Height="72" HorizontalAlignment="Left" Margin="0,10,0,0" Click="btntakephoto_Click" Name="btntakephoto" VerticalAlignment="Top" Width="456" />
            <Image Height="431" HorizontalAlignment="Left" Margin="10,92,0,0" Name="imgphoto" Stretch="Fill" VerticalAlignment="Top" Width="440" />
            <Button Content="Choose Photo" Height="72" HorizontalAlignment="Left" Margin="0,529,0,0" Click="btnchoosephoto_Click" Name="btnchoosephoto" VerticalAlignment="Top" Width="450" />

MainPage.cs MainPage.cs

namespace TakePicture
{
    public partial class MainPage : PhoneApplicationPage
    {
        CameraCaptureTask camera = new CameraCaptureTask();
        PhotoChooserTask choosephoto = new PhotoChooserTask();

        public MainPage()
        {
            InitializeComponent();
            camera.Completed += new EventHandler<PhotoResult>(task_Completed);
            choosephoto.Completed += new EventHandler<PhotoResult>(task_Completed);
        }

        private void btntakephoto_Click(object sender, RoutedEventArgs e)
        {
            camera.Show();
        }

        private void btnchoosephoto_Click(object sender, RoutedEventArgs e)
        {
            choosephoto.Show();
        }

        private string SaveImageToLocalStorage(WriteableBitmap image, string imageFileName)
        {

            if (image == null)
            {
                throw new ArgumentNullException("imageBytes");
            }
            try
            {
                var isoFile = IsolatedStorageFile.GetUserStoreForApplication();

                if (!isoFile.DirectoryExists("MyPhotos"))
                {
                    isoFile.CreateDirectory("MyPhotos");
                }

                string filePath = System.IO.Path.Combine("/" + "MyPhotos" + "/", imageFileName);
                using (var stream = isoFile.CreateFile(filePath))
                {
                     image.SaveJpeg(stream, image.PixelWidth, image.PixelHeight, 0, 80);
                }

                return new Uri(filePath, UriKind.Relative).ToString();
            }
            catch (Exception)
            {
                //TODO: log or do something else
                throw;
            }
        }

        void task_Completed(object sender, PhotoResult e)
        {
            if (e.ChosenPhoto != null)
            {
                if (e.TaskResult == TaskResult.OK)
                {
                    try
                    {
                       string imagePathOrContent = string.Empty;
                       WriteableBitmap image = PictureDecoder.DecodeJpeg(e.ChosenPhoto);
                       imgphoto.Source = image;
                       imagePathOrContent = this.SaveImageToLocalStorage(image, System.IO.Path.GetFileName(e.OriginalFileName));
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM