Windows Phone camera and Image Retrieval (3)

Source: Internet
Author: User


To use a camera, you must reference the following namespace:


Using Microsoft. Phone. tasks;


In this case, we also use the bitmapimage class, so we need to reference the namespace


Using System. Windows. Media. imaging;


As follows:



 



The mainpage. XAML file is initialized with two elements.


<Textblock X: Name = " Txtname " TEXT = " Start the camera " Grid. Row = " 1 " > </Textblock>
<Image X: Name = " IMG " Grid. Row = " 1 " Margin = " 12, 10, 12, 0 " > </Image>


 CodeHide a file


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
// quote
using System.Windows.Media.Imaging;
using Microsoft.Phone.Tasks;

namespace CameraShoot
{
    public partial class MainPage: PhoneApplicationPage
    {
        // Camera capture task example
        CameraCaptureTask cameraCT = new CameraCaptureTask ();
        // Constructor
        public MainPage ()
        {
            InitializeComponent ();
            // Called after the mobile phone camera function is completed
            cameraCT.Completed + = new EventHandler <PhotoResult> (cameraCT_Completed);
        }
        // Rewrite touch screen events
        protected override void OnManipulationStarted (ManipulationStartedEventArgs e)
        {
            // Knowledge points①
            if (e.OriginalSource == txtName)
            {
                // Call the camera
                cameraCT.Show ();
            }
            // Knowledge points②
            // The touch event is completed
            e.Complete ();
            // Knowledge points③
            // Not passing to the parent element
            e.Handled = true;
            // Knowledge points④
            base.OnManipulationStarted (e);
        }
        //carry out
        void cameraCT_Completed (object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                BitmapImage bmp = new BitmapImage ();
                // Knowledge point ⑤
                // Get the file stream is different from Source
                bmp.SetSource (e.ChosenPhoto);
                // Set the picture source
                img.Source = bmp;
                txtName.Text = e.OriginalFileName;
            }
        }
    }
} 


 


Cameracapturetask derived from chooserbase


Among them, cameracapturetask has two practical methods, that is, what we use here



Show; call the camera function



Completed; can be called after camera shooting is complete



 



Knowledge Point ①: E in this event refers to the parameters passed through this event, so we can get some information from it, attribute originalsource indicates the elements that generate the event (called elements rather than controls in Windows Phone );



Knowledge Point ②: The completed method is called here. After this method is added, the system will not process the corresponding routing event. Here it refers to the rewritten onmanipulationstarted event;



Knowledge Point ③: E. Handled = true; this attribute indicates that the routing event has been processed and does not need to be passed to the upper layer of the visualization tree;



Knowledge Point 4: The base class method of this method is called. Although the base class method is rewritten here, the basic operations that are inevitably completed in the base class are not completely overwritten. In this way, some errors may occur.



Knowledge Point ⑤: Use setsource to set the source to stream. WP supports PNG and JPEG formats.



All these operations are performed on the simulator. If you test on a real machine, disable Zune before the test.



Http://www.cnblogs.com/fwind/archive/2011/11/28/2265890.html



Summary: Use the camera to directly call the show method of the cameracapturetask class, so that the camera is turned on until the image is generated and then the completed method is called, obtain the stream of the image in photoresult in the completed method and use it as the source of the bitmap, and set the source of the image. The file name of the current image is the originalfilename attribute of photoresult, the obtained file name is the complete path of the image.








Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.