5

Tôi đang tạo một ứng dụng Windows Universal. Tôi muốn người dùng có thể tải lên hình ảnh và người dùng sẽ có tùy chọn lấy một hình ảnh tại chỗ và gửi đi. Tôi có làm việc này bằng cách sử dụng api MediaCapture. Tuy nhiên, tôi chỉ có thể sử dụng một camera, ví dụ như nếu điện thoại của tôi có camera trước và camera sau thì chỉ sử dụng camera trước. Làm thế nào tôi có thể chuyển đổi máy ảnh đang được sử dụng?Windows (Điện thoại) 8.1 Sử dụng máy ảnh

Tôi đã đọc một cái gì đó ở đâu đó về việc sử dụng một cái gì đó như thế này:

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

Tuy nhiên này luôn trả về null cho tôi, kể từ khi deviceID luôn là null.

Cách khác là có tùy chọn cấp quyền kiểm soát cho ứng dụng chụp ảnh và trả lại ảnh đã chụp cho ứng dụng của tôi? Tôi đã tìm thấy những điều sau đây, nhưng nó không hoạt động cho các ứng dụng Windows phổ: http://msdn.microsoft.com/en-us/library/windows/apps/hh394006(v=vs.105).aspx

+0

Bạn có thể thử chạy trong chế độ gỡ lỗi dòng: 'var devices = (await DeviceInformation.FindAllAsync (DeviceClass.All)). ToList();', sau đó kiểm tra thiết bị nào được trả về? Bạn có thể tìm thấy camera ở đó không? – Romasz

Trả lời

4

Dưới đây là làm thế nào tôi sẽ làm điều đó:

Đầu tiên là khởi tạo phần

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

Thứ hai chụp ảnh

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

Điều đó sẽ giải quyết vấn đề của bạn.

Các vấn đề liên quan