Camera Capture Task in windows phone 8

社会主义新天地 提交于 2020-01-17 03:55:09

问题


I am working on windows phone application in which i need to store a captured image from the camera in isolated storage without saving it in the camera roll. I am able to store the captured image in the isolated storage but a copy of the captured image in also stored in the camera roll. Is there any way i can keep the image within the isolated storage rather than the camera roll.

Thanks


回答1:


If you want to save to ONLY isolated storage, you cannot use the CameraCaptureTask. In WP8, it will transparently save a copy of the image to the camera roll, regardless of what you do.

That said, there is a solution. You'll need to use the camera APIs to basically create and use your own CameraCaptureTask. I'm not going to go into huge depth, but this should get you started.

First thing you need to do is follow this tutorial to create the view and basic application. They use the cam_CaptureImageAvailable method to store the image to the camera roll. You'll want to modify that to store it in isolated storage like so:

using (e.ImageStream)
{
   using(IsolatedStorageFile storageFile = IsolatedStorageFile.GetuserStoreForApplication())
   {
      if( !sotrageFile.DirectoryExists(<imageDirectory>)
      {
         storageFile.CreateDirectory(<imageDirectory>);
      }

      using( IsolatedStorageFileStream targetStream = storageFile.OpenFile( <filename+path>, FileMode.Create, FileAccess.Write))
      {
         byte[] readBuffer = new byte[4096];
         int bytesRead;
         while( (bytesRead = e.ImageStream.Read( readBuffer, 0, readBuffer.Length)) > 0)
         {
            targetStream.Write(readBuffer, 0, bytesRead);
         }
      }
   }
}

From this point, you have a functional camera application that stores only to isolated storage. You may want to spice it up with color effects or something, but it's not necessary.




回答2:


The accepted answer above is not correct for Windows Phone 8 Silverlight 8.1

I used the Completed event to customize the code I want to be executed after the photo is taken and accepted.

    public MainPage()
    {

        InitializeComponent();

        cameraTask = new CameraCaptureTask();
        cameraTask.Completed += new EventHandler<PhotoResult>(cameraTask_Completed);
        cameraTask.Show();

    }

    void cameraTask_Completed(object sender, PhotoResult e)
    {
        if (e.TaskResult != TaskResult.OK) 
            return;
            // Save picture as JPEG to isolated storage.
            using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream targetStream = isStore.OpenFile(fileName, FileMode.Create, FileAccess.Write))
                {
                    // Initialize the buffer for 4KB disk pages.
                    byte[] readBuffer = new byte[4096];
                    int bytesRead = -1;

                    // Copy the image to isolated storage. 
                    while ((bytesRead = e.ImageStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
                    {
                        targetStream.Write(readBuffer, 0, bytesRead);
                    }
                }
            }

    }

Resources http://msdn.microsoft.com/library/windowsphone/develop/microsoft.phone.tasks.cameracapturetask http://code.msdn.microsoft.com/wpapps/CSWP8ImageFromIsolatedStora-8dcf8411



来源:https://stackoverflow.com/questions/18118749/camera-capture-task-in-windows-phone-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!