5

Sto creando un'applicazione Windows universale. Voglio che l'utente sia in grado di caricare un'immagine e l'utente dovrebbe avere la possibilità di prenderne uno sul posto e inviarlo. Ho questo lavoro utilizzando l'API MediaCapture. Tuttavia, posso solo utilizzare una videocamera, quindi ad esempio se il mio telefono ha una fotocamera anteriore e una posteriore, viene utilizzata solo la fotocamera frontale. Come potrei essere in grado di cambiare la videocamera che è in uso?Windows (telefono) 8.1 Fotocamera Utilizzo

Avevo letto qualcosa da qualche parte su come utilizzare qualcosa di simile:

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); 

    return deviceID; 
} 

Tuttavia questo restituisce sempre null per me, dal momento che il deviceID è sempre nullo.

In alternativa esiste l'opzione di dare il controllo a un'applicazione che prende l'immagine e restituisce l'immagine scattata alla mia applicazione? Ho trovato quanto segue, ma non funziona per le applicazioni di Windows universali: http://msdn.microsoft.com/en-us/library/windows/apps/hh394006(v=vs.105).aspx

+0

Si può provare a eseguire in modalità di debug la riga: 'var devices = (attendi DeviceInformation.FindAllAsync (DeviceClass.All)). ToList();', quindi controlla quali dispositivi vengono restituiti? Riesci a trovare le telecamere lì? – Romasz

risposta

4

Ecco come lo farei:

prima l'inizializzazione parte

// First need to find all webcams 
DeviceInformationCollection webcamList = await DeviceInformation.FindAllAsync(DeviceClass.All) 

// Then I do a query to find the front webcam 
DeviceInformation frontWebcam = (from webcam in webcamList 
where webcam.EnclosureLocation != null 
&& webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front 
select webcam).FirstOrDefault(); 

// Same for the back webcam 
DeviceInformation backWebcam = (from webcam in webcamList 
where webcam.EnclosureLocation != null 
&& webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back 
select webcam).FirstOrDefault(); 

// Then you need to initialize your MediaCapture 
newCapture = new MediaCapture(); 
await newCapture.InitializeAsync(new MediaCaptureInitializationSettings 
     { 
      // Choose the webcam you want 
      VideoDeviceId = backWebcam.Id, 
      AudioDeviceId = "", 
      StreamingCaptureMode = StreamingCaptureMode.Video, 
      PhotoCaptureSource = PhotoCaptureSource.VideoPreview 
     }); 

// Set the source of the CaptureElement to your MediaCapture 
// (In my XAML I called the CaptureElement *Capture*) 
Capture.Source = newCapture; 

// Start the preview 
await newCapture.StartPreviewAsync(); 

In secondo luogo prendere la foto

//Set the path of the picture you are going to take 
StorageFolder folder = ApplicationData.Current.LocalFolder; 
var picPath = "\\Pictures\\newPic.jpg"; 

StorageFile captureFile = await folder.CreateFileAsync(picPath, CreationCollisionOption.GenerateUniqueName); 

ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg(); 

//Capture your picture into the given storage file 
await newCapture.CapturePhotoToStorageFileAsync(imageProperties, captureFile); 

Questo dovrebbe risolvere il problema.