C# 摄像头在Windows phone中完成任务需要时间

C# 摄像头在Windows phone中完成任务需要时间,c#,xaml,windows-phone-7.1,C#,Xaml,Windows Phone 7.1,实际上,我使用相机加载了视频画笔,使用WindowsPhone7.1中的PhotoCamera选项拍摄对象的快照 拍摄快照后,我将CaptureImageAvailable事件中的e.ImageSteam设置为ImageBrush源 拍摄快照后,在WindowsPhone中调用CaptureImageAvailable或CaptureCompleted事件需要时间。为什么会这样 我需要写一些新的东西来解决这个问题吗?对于拍照并选择Photo,您只需按照以下步骤操作即可 MainPage.xaml

实际上,我使用相机加载了视频画笔,使用WindowsPhone7.1中的PhotoCamera选项拍摄对象的快照

拍摄快照后,我将CaptureImageAvailable事件中的e.ImageSteam设置为ImageBrush源

拍摄快照后,在WindowsPhone中调用CaptureImageAvailable或CaptureCompleted事件需要时间。为什么会这样


我需要写一些新的东西来解决这个问题吗?

对于拍照并选择Photo,您只需按照以下步骤操作即可

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

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)
                    {
                    }
                }
            }
        }
    }
}
名称空间拍摄
{
公共部分类主页:PhoneApplicationPage
{
CameraCaptureTask camera=新CameraCaptureTask();
PhotoChooserTask choosephoto=新建PhotoChooserTask();
公共主页()
{
初始化组件();
camera.Completed+=新事件处理程序(任务_已完成);
选择Photo.Completed+=新事件处理程序(任务\u已完成);
}
私有void btntakephoto_单击(对象发送者,路由目标e)
{
摄像头显示();
}
私有无效btnchoosephoto_单击(对象发送者,路由目标e)
{
选择Show();
}
私有字符串SaveImageToLocalStorage(可写位图图像,字符串imageFileName)
{
if(image==null)
{
抛出新ArgumentNullException(“imageBytes”);
}
尝试
{
var isoFile=IsolatedStorageFile.GetUserStoreForApplication();
如果(!isoFile.directory存在(“MyPhotos”))
{
创建目录(“MyPhotos”);
}
字符串filePath=System.IO.Path.Combine(“/”+“MyPhotos”+“/”,imageFileName);
使用(var stream=isoFile.CreateFile(filePath))
{
SaveJpeg(流,image.PixelWidth,image.PixelHeight,0,80);
}
返回新的Uri(filePath,UriKind.Relative).ToString();
}
捕获(例外)
{
//TODO:记录或执行其他操作
投掷;
}
}
无效任务_已完成(对象发送方,照片结果e)
{
如果(e.ChosenPhoto!=null)
{
if(e.TaskResult==TaskResult.OK)
{
尝试
{
string imagePathOrContent=string.Empty;
WriteableBitmap image=PictureDecoder.DecodeJpeg(e.ChosenPhoto);
imgphoto.Source=图像;
imagePathOrContent=this.SaveImageToLocalStorage(image,System.IO.Path.GetFileName(e.OriginalFileName));
}
捕获(例外)
{
}
}
}
}
}
}

您可以发布您尝试过的代码吗?聪明地回答您会很有帮助。您可以查看下面的msdn文档。。。对于示例,我不想使用WindowsPhone中现有的API。。。我想让它成为我自己的相机应用与接受和重拍按钮一样的Windows Phone相机。。。这就是为什么我使用了光电摄像机选项。。