Win8 Metro calls the camera to take a photo and save the photo in the appropriate location

Source: Internet
Author: User

Just done this kind of development, so first caught dead, of course, the source code posted on is verified, has been implemented successfully, I hope to give you some reference:

Here's the Metro UI code:

<page    x:class= "camera.mainpage"    xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:local= "Using:camera"    xmlns:d= "http:/ /schemas.microsoft.com/expression/blend/2008 "    xmlns:mc=" http://schemas.openxmlformats.org/ markup-compatibility/2006 "    mc:ignorable=" D ">    <grid background=" {ThemeResource Applicationpagebackgroundthemebrush} ">        <button x:name=" Btncamera "content=" Turn on the camera "horizontalalignment=" Left "margin=" 48,259,0,0 "verticalalignment=" Top "click=" Btncamera_click "height=" "/> <image        x:Name=" Img1 "horizontalalignment=" left "height=" 609 "margin=" 240,78,0,0 "verticalalignment=" Top "width=" 718 "stretch=" Fill " />        <button x:name= "Btnsave" content= "Save Picture" horizontalalignment= "left" margin= "48,369,0,0" Verticalalignment= "Top" height= "click=" Btnsave_click "/>    </Grid></Page>

The displayed interface is actually very easy, but this is not important, the function is basic, the following paste the basic development code:

Using system;using system.collections.generic;using system.io;using system.linq;using System.runtime.interopservices.windowsruntime;using windows.foundation;using Windows.Foundation.Collections; Using windows.ui.xaml;using windows.ui.xaml.controls;using windows.ui.xaml.controls.primitives;using Windows.ui.xaml.data;using windows.ui.xaml.input;using windows.ui.xaml.media;using Windows.UI.Xaml.Navigation; Using windows.media.capture;using windows.storage;using windows.storage.pickers;using Windows.ui.xaml.media.imaging;using windows.storage.streams;//the Blank Page item template is documented at/HTTP go.microsoft.com/fwlink/? linkid=234238/* * * Li Tianpeng * Function: Call the camera on the PC to implement the camera function, and save in the corresponding directory * */namespace camera{//<summary>//an empt    Y page that can is used on it own or navigated to within a Frame.        </summary> public sealed partial class Mainpage:page {private StorageFile file = null; Public MainPage () {this. InitializeComPonent ();  Private async void Btncamera_click (object sender, RoutedEventArgs e) {Cameracaptureui dialog =            New Cameracaptureui (); Dialog.            Photosettings.croppedaspectratio = new Size (16, 9); File = Await dialog.            Capturefileasync (Cameracaptureuimode.photo);                if (file = null) {BitmapImage BitmapImage = new BitmapImage (); using (Irandomaccessstream FileStream = await file.                OpenAsync (Fileaccessmode.read)) {Bitmapimage.setsource (fileStream); } img1.            Source = BitmapImage; }} private Async void Btnsave_click (object sender, RoutedEventArgs e) {if (img1.            Source = = null) return;                else {Filesavepicker picker = new Filesavepicker ();                Picker.commitbuttontext = "Save"; Picker.          Suggestedfilename = "Hello";      Picker.                Filetypechoices.add ("Picture", New string[]{". jpg", ". jpeg", ". bmp", ". png"}); Picker.                Suggestedstartlocation = pickerlocationid.pictureslibrary; StorageFile FilePath = await picker.                Picksavefileasync (); if (FilePath! = null) {//Opens a photo taken by the camera and returns the stream as a stream to read the file var Streamrando m = await file.                    OpenAsync (Fileaccessmode.read);                    Reads the shot in stream form to buffer IBuffer = Randomaccessstreamtobuffer (streamrandom);                Writes buffer contents to the corresponding directory await Fileio.writebufferasync (filePath, buffer); }}}//write the picture to the buffer private IBuffer randomaccessstreamtobuffer (Irandomaccessstream randomstr EAM) {Stream stream = Windowsruntimestreamextensions.asstreamforread (randomstream.            Getinputstreamat (0));            MemoryStream MemoryStream = new MemoryStream ();          if (stream! = null)  {byte[] bytes = Convertstreamtobyte (stream); Converts the flow to a byte array if (bytes! = null) {var binaryWriter = new BinaryWriter (mem                    Orystream);                BinaryWriter.Write (bytes); }} IBuffer buffer = Windowsruntimebufferextensions.getwindowsruntimebuffer (MemoryStream, 0, (int) Me            Morystream.length);        return buffer; }//Change the flow to binary public static byte[] Convertstreamtobyte (Stream input) {byte[] buffer = new            BYTE[16 * 1024];                using (MemoryStream ms = new MemoryStream ()) {int read; while (read = input. Read (buffer, 0, buffer. Length)) > 0) {Ms.                Write (buffer, 0, read); } return Ms.            ToArray (); }        }    }}

However, this is not enough, assuming the need to call the camera, but also need some permissions, should be in the Package.appxmanifest to change the permissions,


The changes are as follows: Just hook up the webcam.



Source code Download:

http://download.csdn.net/detail/litianpeng1991/7548065

Win8 Metro calls the camera to take a photo and save the photo in the appropriate location

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.