Windows Phone Development-camera feature development

Source: Internet
Author: User

Original: Windows Phone development-camera feature development

camera function is different from the phone PC a big function, in the mobile phone application, if the reasonable use of the camera function, it may be a lot of color for their own applications. There are two ways to use the camera features of Windows Phone , one is to use the Photocamera class to build your own camera UI, Another option is to implement this function through the Cameracapturetask selector.

The difference between them is:

    • Photocamera class allows the app to control photo properties, such as ISO, exposure compensation, and manual focus position, and the app can have more control over the photo, and of course it's a lot of trouble. You need to implement operations such as Flash, focus, resolution, shutter button, and more.
    • Cameracapturetask the photo will call the system's camera function, return a return value of the photo data, and, once photographed, enter the phone album.

a . Cameracapturetask The selector.

    1. First you need to reference
using Microsoft.Phone.Tasks;

    1. Declares the task object, which needs to be declared before the page's constructor function
Cameracapturetask Cameracapturetask;

Instantiate the Cameracapturetask object in the constructor and register the callback method.

Newnew eventhandler<photoresult> (cameracapturetask_completed);

Add the following code in the desired location in the application, such as in a keystroke click event

Cameracapturetask.show ();

Add the code for the completed event handler on the page. This code runs after the user finishes the task. The result is a Photoresult object that exposes the stream that contains the image data.

 void  cameracapturetask_completed (object   sender, Photoresult e) { if  (E. Taskresult == Taskresult.ok) {MessageBox.Show (e.chosenphoto.length.tostring ()); 
    // code to display the photo on the page in an        Image control named MyImage.   // system.windows.media.imaging.bitmapimage        BMP = new System.Windows.Media.Imaging.BitmapImage ();  // bmp.        SetSource (E.chosenphoto);  // myimage.source = BMP;   

This part is relatively simple, not much to say, to a Demo Bar: Http://pan.baidu.com/s/1pJ0Polt

two . Photocamera

Photocamera is in windows Phone OS 7.1 To get started, you need to add permission to the app to access the camera before you use it,

Add Id_cap_isv_camera in Wmappmanifest.xml

    1. Create UI

when creating a viewfinder, you will typically use the VideoBrush , if you need to support switching between the screen, you need to join Relativetransform , the following code is a typical camera UI

<!--Camera Viewfinder -        <Canvasx:name= "Videocanvas">            <Canvas.background>                <VideoBrushx:name= "Backgroundvideobrush" >                    <Videobrush.relativetransform>                        <Compositetransformx:name= "Videobrushtransform"CenterY= "0.5"CenterX= "0.5"/>                    </Videobrush.relativetransform>                </VideoBrush>            </Canvas.background>        </Canvas>

Of course, you should also consider the other elements on the page, such as clicking the viewfinder focus, shutter, flash button, etc., which can be customized with personal washing.

    1. Implement viewfinder and related camera events.
      1. first realize the viewfinder, first determine whether the phone has the relevant hardware equipment ( rear camera ( master ) or front camera )
if((photocamera.iscameratypesupported (cameratype.primary) = =true) ||(photocamera.iscameratypesupported (cameratype.frontfacing)==true))            {                //Initialize the camera, when available.                if(photocamera.iscameratypesupported (cameratype.frontfacing)) {//Use front-facing Camera if available.Cam=NewMicrosoft.Devices.PhotoCamera (cameratype.frontfacing); }                Else                {                    //Otherwise, use standard camera on the back of device.Cam=NewMicrosoft.Devices.PhotoCamera (cameratype.primary); }    //Set The VideoBrush source to the camera.Viewfinderbrush.setsource (CAM); }            Else            {                //The camera is not a supported on the device.                 This. Dispatcher.begininvoke (Delegate()                {                    //Write message.Txtdebug.text="A Camera is not available on the this device.";                 }); //Disable UI.shutterbutton.isenabled=false; Flashbutton.isenabled=false; Afbutton.isenabled=false; Resbutton.isenabled=false; }

    1. Various action events are also required at load time
//Event is fired when the Photocamera object has been initialized.Cam. Initialized+=NewEventhandler<microsoft.devices.cameraoperationcompletedeventargs>(cam_initialized); //Event was fired when the capture sequence was complete.Cam. Capturecompleted+=NewEventhandler<cameraoperationcompletedeventargs>(cam_capturecompleted); //Event was fired when the capture sequence was complete and a image is available.Cam. Captureimageavailable+=NewEventhandler<microsoft.devices.contentreadyeventargs>(cam_captureimageavailable); //Event was fired when the capture sequence was complete and a thumbnail image was available.Cam. Capturethumbnailavailable+=NewEventhandler<contentreadyeventargs>(cam_capturethumbnailavailable); //The event was fired when Auto-focus was complete.Cam. Autofocuscompleted+=NewEventhandler<cameraoperationcompletedeventargs>(cam_autofocuscompleted); //The event is fired when the viewfinder are tapped (for focus).Viewfindercanvas.tap+=NewEventhandler<gestureeventargs>(focus_tapped); //The event is fired when the shutter button receives a half press.camerabuttons.shutterkeyhalfpressed+=onbuttonhalfpress; //The event is fired when the shutter button receives a full press.camerabuttons.shutterkeypressed+=onbuttonfullpress; //The event is fired when the shutter button is released.camerabuttons.shutterkeyreleased+ = Onbuttonrelease;

    1. There are so many events loaded above that you need to release it when you leave this page:
protected Override voidOnnavigatingfrom (System.Windows.Navigation.NavigatingCancelEventArgs e) {if(Cam! =NULL)            {                //Dispose camera to minimize power consumption and to expedite shutdown.Cam.                 Dispose (); //Release memory, ensure garbage collection.Cam. Initialized-=cam_initialized; Cam. Capturecompleted-=cam_capturecompleted; Cam. Captureimageavailable-=cam_captureimageavailable; Cam. Capturethumbnailavailable-=cam_capturethumbnailavailable; Cam. Autofocuscompleted-=cam_autofocuscompleted; Camerabuttons.shutterkeyhalfpressed-=onbuttonhalfpress; Camerabuttons.shutterkeypressed-=onbuttonfullpress; Camerabuttons.shutterkeyreleased-=onbuttonrelease; }        }

above these events, look at the name estimate also understand what is doing, here explain their order of execution, capturethumbnailavailable - > captureimageavailable - > capturecompleted .

    1. after the photo has been taken, it needs to be saved and saved to the camera, using Savepicturetocameraroll method, which can be saved to a separate storage space for easy reading later ( If you only save in the album, you must use the photo selector to let the user select the photos the next time you read ) :
 Public voidCam_capturethumbnailavailable (Objectsender, Contentreadyeventargs e) {            stringFileName = Savedcounter +"_th.jpg"; Try            {                //Write message to UI thread.Deployment.Current.Dispatcher.BeginInvoke (Delegate() {Txtdebug.text="captured image available, saving thumbnail.";                 }); //Save thumbnail 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]; intBytesread =-1; //Copy the thumbnail to isolated storage.                         while(Bytesread = E.imagestream.read (Readbuffer,0, readbuffer.length)) >0) {targetstream.write (Readbuffer,0, Bytesread); }                    }                }                 //Write message to UI thread.Deployment.Current.Dispatcher.BeginInvoke (Delegate() {Txtdebug.text="Thumbnail have been saved to isolated storage.";            }); }            finally            {                //Close Image StreamE.imagestream.close (); }        }

There are two ways to save a photo: SavePicture and the Savepicturetocameraroll , the previous method is saved to Photo center "saved Photos", and the latter method is saved to "camera".

    1. For Flash, focus, resolution and shutter have a corresponding method, from the above code can also see the shutter half-press, full press, release and other events, here no longer repeat, you can see the relevant events from the source code.

This example of Demo is provided by Microsoft, more detailed, the source code is as follows: Http://pan.baidu.com/s/1c0rIqSK

Reference article: Windows Phone's camera and photos

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.