Yesterday, I was very sloppy to tell you about the topic of dealing with the physical camera flip, today, or this topic, and content is not bad, just for completeness, and by the way also provides a version of the runtime API, actually implemented with the SL framework version, after all, the two frameworks have a lot of APIs are shared.
First, open the manifest file, on the Application tab, turn the "supported rotation" to the right side of the selection, the other not selected, only the landscape .
Then switch to the "Features" tab and hook up the webcam and the picture library because we need to use them.
Similarly, when using the Mediacapture class, be aware that it is freed when the application hangs, and initializes it when the application starts or continues to run.
Add the following code to the App class:
/// <summary> ///Video Capture Object/// </summary> PublicMediaCapture Thecapture {Get;Private Set; } /// <summary> ///Initializing the camera/// </summary> Private AsyncTask initializecapture () {thecapture=NewMediaCapture (); //find the rear-facing camera varDevicecollection =awaitDeviceinformation.findallasync (deviceclass.videocapture); Deviceinformation Backcamera= Devicecollection.firstordefault (d = D.enclosurelocation.panel = =Windows.Devices.Enumeration.Panel.Back); if(Backcamera! =NULL) {mediacaptureinitializationsettings setting=Newmediacaptureinitializationsettings (); Setting. Audiodeviceid=""; Setting. Videodeviceid=backcamera.id; awaitThecapture.initializeasync (setting); } Else { awaitThecapture.initializeasync (); } } /// <summary> ///clean up your webcam related resources/// </summary> Private voidcleanupcapture () {if(Thecapture! =NULL) {thecapture.dispose (); Thecapture=NULL; } }
The Initializecapture method is used to initialize the capture component, and the Cleanupcapture method is used to clean it. The Initializecapture method uses a task to indicate that it can wait asynchronously, because it is called later in launch, and is called before navigating to the home page, and the application will enter the home page before the mediacapture is initialized without waiting asynchronously. An exception occurs when you open a preview in the home page, so by waiting asynchronously you can ensure that the initialization of the Mediacapture object is completed before entering the home page.
In the Onlaunched method, add the following code to initialize the capture component.
protected Async Override voidOnLaunched (Launchactivatedeventargs e) {//Hide the status barWindows.UI.ViewManagement.StatusBar StatusBar =Windows.UI.ViewManagement.StatusBar.GetForCurrentView (); awaitStatusBar. Hideasync ();#ifDEBUGif(System.Diagnostics.Debugger.IsAttached) { This. Debugsettings.enableframeratecounter =true; }#endif await This. Initializecapture (); ...
Use the StatusBar class is to hide the system bar, the System icon bar is the top of the mobile phone icon bar, display signal, time and other information places.
When the application hangs, the Mediacapture object is disposed to handle the suspending event.
Private Async void Onsuspending (object sender, Suspendingeventargs e) { var deferral = E. Suspendingoperation.getdeferral (); // TODO: Save application state and stop any background activity // Stop shooting preview await Thecapture.stoppreviewasync (); This . Cleanupcapture (); Deferral.complete (); }
When the application resumes from a suspended state (such as switching to another app, or back to the start screen), the resuming event is raised, the event is processed, and the Mediacapture object is reinitialized.
Async voidOnresuming (ObjectSenderObjecte) {await This. Initializecapture (); Frame Root= Window.Current.Content asFrame; if(Root! =NULL) {MainPage page= root. Content asMainPage; if(Page! =NULL) awaitpage. Setcapturesourceasync (); } }
The Setcapturesourceasync method is a method that is defined in the MainPage page class to obtain a reference to the Mediacapture object and to begin shooting previews. The method is defined as follows:
Public Async System.Threading.Tasks.Task Setcapturesourceasync () { as App). thecapture; = capture; await capture. Startpreviewasync (); }
In the Silverlight framework, the preview screen of the camera is displayed through VideoBrush, while in the runtime API, the Captureelement class is ported from the RT application, which has a source property, Lets you set the associated Mediacapture instance so that you can see the preview of the camera in the captureelement visual element, and then call the Startpreviewasync method to start the preview.
The method for taking and saving photos is similar to the previous example, but the runtime API can use the classes in Windows.storage for file processing.
Get a reference to the picture Library folder by using the following code:
Storagefolder picdir = knownfolders.pictureslibrary;
The other process, like the previous example, snaps the photo to the stream, and then decodes/encodes the direction of the picture. There is, however, a detail to be mentioned here:
The following is an example: Http://files.cnblogs.com/tcjiaan/CameraRTTestApp.zip
"WP 8.1 development" solves the camera rollover problem (Runtimeapp article)