wp8.1 photo (get focus, use rear camera)

Source: Internet
Author: User

wp8.1 didn't start the camera directly with the launcher, like WP8, and he wanted to turn on the camera to render a stream from a capture device (such as a camera or webcam) with captureelement. Talk today about how to turn on the camera, get focus, and take pictures. Nonsense not much to say, the following directly on the code. Of course the premise is must remember in appxmanifest file capabilities option Select Webcam, otherwise will error

First, the XAML code:

<grid name= "LayoutRoot" >            <captureelement  x:name= "Capturepreview" stretch= "UniformToFill"/>            <image name= "Profilepic" ></Image> </Grid>          <stackpanel grid.row= "1" verticalalignment = "Bottom" >            <slider x:name= "Focusvalueslider" maximum= "" "minimum=" 0 "grid.row=" 0 "margin=" 12,0,15,0 " Header= "Focus Adjustment:" valuechanged= "focusvalueslider_valuechanged" value= "Smallchang e=  " 1 "largechange=" > </Slider>            <stackpanel orientation= "Horizontal" >                <button content= "Start Camera" click= " Photographbutton_click "></Button>                <button margin=" 50,0,0,0 "content=" photo "click=" capturephoto_ Click "></Button>                        </StackPanel> </StackPanel>

Then the boot camera event:

    MediaCapture Capturemanager = null; Async private void Photographbutton_click (object sender, RoutedEventArgs e) {if (Capturemanager = = NULL                ) {capturepreview.visibility = visibility.visible;                profilepic.visibility = visibility.collapsed;                Capturemanager = new MediaCapture ();                Select Rear camera var cameraid = await getcameraid (Windows.Devices.Enumeration.Panel.Back); Await Capturemanager.initializeasync (new Mediacaptureinitializationsettings {STREAMINGC                    Apturemode = streamingcapturemode.video, Photocapturesource = Photocapturesource.photo, Audiodeviceid = string.                 Empty, Videodeviceid = cameraid.id});                Camera rotation 90 degrees capturemanager.setpreviewrotation (videorotation.clockwise90degrees);      Capturepreview.source = Capturemanager;          await Capturemanager.startpreviewasync (); }        }

Get back Camera Method:

     private static Async task<deviceinformation> Getcameraid (Windows.Devices.Enumeration.Panel desired)       {           deviceinformation DeviceID = (await Deviceinformation.findallasync (deviceclass.videocapture))               . FirstOrDefault (x = x.enclosurelocation = null && X.enclosurelocation.panel = = desired);           if (DeviceID! = null) return DeviceID;           else throw new Exception (string. Format ("Camera of type {0} doesn ' t exist.", desired));       }

Here's how to take a photo:

Async private void Capturephoto_click (object sender, RoutedEventArgs e) {if (Capturemanager! =               NULL) {capturepreview.visibility = visibility.collapsed;               profilepic.visibility = visibility.visible; Declare string for filename string capturefilename = string.               Empty;               Picture formats Imageencodingproperties format = Imageencodingproperties.createjpeg ();                   Create a local storage folder StorageFile file = Await ApplicationData.Current.LocalFolder.CreateFileAsync (                             "Photo.jpg", creationcollisionoption.replaceexisting);               Await Capturemanager.capturephototostoragefileasync (format, file); BitmapImage bmpimage = new BitmapImage (The new Uri (file.                            Path)); Profilepic.source = Bmpimage;
Release the camera resource Capturemanager.dispose (); Capturemanager = null; } }

The general process for taking pictures has already been completed, and now it's time to focus and get focus:

private void Focusvalueslider_valuechanged (object sender, Rangebasevaluechangedeventargs e) {try               {UINT focus = Convert.touint32 (E.newvalue);           SetFocus (focus); } catch {}}
Set camera focus Method Private async void SetFocus (uint? focusvalue = null) {try { if (!focusvalue.hasvalue) {focusvalue = 500; } if (captureManager.VideoDeviceController.FocusControl.Supported) { captureManager.VideoDeviceController.FlashControl.AssistantLightEnabled = false; CaptureManager.VideoDeviceController.FocusControl.Configure (New Focussettings () {Mode = Focusmode.manu Al, Value = focusvalue, Disabledriverfallback = true}); await CaptureManager.VideoDeviceController.FocusControl.FocusAsync (); }} catch {}}

This completes the focus acquisition.

 

  

wp8.1 photo (get focus, use rear camera)

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.