This project is not to use the system comes with the Cameracaptureui, is the call of their own written camera, the interface does not do well, so, do not put, but can achieve the photo function:
The following is the using program namespace:
Using windows.media.capture;using windows.media.mediaproperties;using windows.ui.xaml.media.imaging;using Windows.storage;using windows.storage.pickers;using windows.storage.streams;using Windows.Devices.Enumeration;
You can look at the invocation of these methods in the WIN8 reference,
Here are the global variables:
<span style= "White-space:pre" ></span>private StorageFile file = null; Private StorageFile photo = null; Private StorageFile video = null; Private MediaCapture Mediaphoto = null; Private MediaCapture mediavideo = null; private bool camerastarted = false;
First we do not call the system comes with the camera method Cameracaptureui, we can use another method, the following is the call camera trigger method:
Private async void Btncamera_click (object sender, RoutedEventArgs e) { try { Deviceinformationcollection devices = await deviceinformation.findallasync (deviceclass.videocapture); if (Devices. Count > 0) { if (Mediaphoto = = null) { Mediaphoto = new MediaCapture (); await Mediaphoto.initializeasync (); Capture1. Source = Mediaphoto; await Mediaphoto.startpreviewasync (); camerastarted = True;}} } catch (Exception msg) { mediaphoto = null; } }
The camera is called and the ability to take pictures is also required:
<span style= "White-space:pre" ></span>private async void Btnphoto_click (object sender, RoutedEventArgs e) { if (Mediaphoto! = null) { imageencodingproperties Imgformat = Imageencodingproperties.createjpeg ( ); Photo = await ApplicationData.Current.LocalFolder.CreateFileAsync ("Hello.jpg", Creationcollisionoption.generateuniquename); Await Mediaphoto.capturephototostoragefileasync (Imgformat, photo); } }
Above is the trigger method for taking pictures.
Our program also requires the implementation of a good photo to be saved, this also requires a trigger method:
<span style= "White-space:pre" ></span>private async void Photosave_click (object sender, RoutedEventArgs e) {if (photo! = null) {Filesavepicker photopicker = new Filesavepicker (); Photopicker.commitbuttontext = "Save Picture"; Photopicker.suggestedfilename = "Hello"; PHOTOPICKER.FILETYPECHOICES.ADD ("Picture", New string[]{". jpg", ". jpeg", ". png", ". bmp"}); Photopicker.suggestedstartlocation = pickerlocationid.pictureslibrary; StorageFile Photofile = await photopicker.picksavefileasync (); if (photofile! = null) {//Opens a photo taken by the camera and returns the stream as a stream to read the file var Streamra Ndom = await photo. OpenAsync (Fileaccessmode.read); Reads the shot in stream form to buffer IBuffer = Randomaccessstreamtobuffer (streamrandom); Writes the buffer contents to the appropriate folder await Fileio.writebufferasynC (photofile, buffer); } } }
Here are the two functions that you wrote:
Writes a picture to the buffer private IBuffer randomaccessstreamtobuffer (Irandomaccessstream randomstream) { Stream stream = Windowsruntimestreamextensions.asstreamforread (randomstream. Getinputstreamat (0)); MemoryStream MemoryStream = new MemoryStream (); IBuffer buffer = null; if (stream! = null) {byte[] bytes = Convertstreamtobyte (stream); The stream is converted to a byte array if (bytes! = null) {var binaryWriter = new BinaryWriter (m Emorystream); BinaryWriter.Write (bytes); }} try {buffer = Windowsruntimebufferextensions.getwindowsruntimebuffer ( MemoryStream, 0, (int) memorystream.length); } catch (Exception msg) {} return buffer; }//Change the flow to binary public static byte[] Convertstreamtobyte (Stream input){byte[] buffer = new byte[1024 * 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 (); } }
Can run, thank you for correcting me.