/// <summary> /// 获得预览分辨率 /// </summary> private static void GetPreviewSize() { var previewSizeList = MainCamera.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview); var sizeList = new List <VideoEncodingProperties>(); var listCount = previewSizeList.Count; foreach (var item in previewSizeList) { var sizeItem = item as VideoEncodingProperties; if (sizeItem == null) { continue; } if (sizeItem.Width < AppDefaultHelper.CAMERA_VGA_WIDTH || sizeItem.Height < AppDefaultHelper.CAMERA_VGA_HEIGHT) { continue; } var oldItem = sizeList.FirstOrDefault(li => (li.Width == sizeItem.Width && li.Height == sizeItem.Height)); if (oldItem != null) { uint oldRate = oldItem.FrameRate.Numerator / oldItem.FrameRate.Denominator; uint rate = sizeItem.FrameRate.Numerator / sizeItem.FrameRate.Denominator; if (oldRate < rate) { sizeList.Remove(oldItem); } else { continue; } } sizeList.Add(sizeItem); } if (CameraPreviewSizeList == null) { CameraPreviewSizeList = new List <CameraSizeInfo>(); } else { CameraPreviewSizeList.Clear(); } foreach (var item in sizeList.OrderBy(li => li.Width)) { CameraPreviewSizeList.Add(new CameraSizeInfo(item, item.Width, item.Height)); } }
/// <summary> /// 停止预览 /// </summary> public static async Task StopPreview() { try { if (CameraPreviewSizeList != null) { CameraPreviewSizeList.Clear(); } CameraPreviewSizeList = null; if (CameraPhotoSizeList != null) { CameraPhotoSizeList.Clear(); } await CloseLowLagPhoto(); CameraPhotoSizeList = null; IsSupportROI = false; IsSupportContinuousCapture = false; IsSupportHWZoom = false; if (MainCamera != null) { try { await MainCamera.StopPreviewAsync(); } catch (Exception ex) { } try { MainCamera.Dispose(); } catch (Exception ex) { } } MainCamera = null; CurrentCamDevice = null; FailedIndex = 1; IsCanUseCamera = false; } catch (Exception ex) { } }
/// <summary> /// 设置分辨率 /// </summary> private static async Task SetSize() { try { var width = ConfigHelper.Info.CameraPhotoSizeWidth; var height = ConfigHelper.Info.CameraPhotoSizeHeight; if (IsHavePhotoStream == true) { //获得分辨率 CameraSizeInfo photoSize = null; foreach (var item in CameraPhotoSizeList) { if (item.Width == width && item.Height == height) { photoSize = item; break; } } if (photoSize == null) { var maxTag = CameraPhotoSizeList.Max(li => li.SizeTag); photoSize = CameraPhotoSizeList.FirstOrDefault(li => li.SizeTag == maxTag); } var photoRato = Convert.ToDouble(photoSize.Width) / photoSize.Height; var tempPreviewList = CameraPreviewSizeList.Where(li => Convert.ToDouble(li.Width) / li.Height == photoRato); CameraSizeInfo previewSize = null; if (tempPreviewList.Count() > 0) { var maxTag = tempPreviewList.Max(li => li.SizeTag); previewSize = tempPreviewList.FirstOrDefault(li => li.SizeTag == maxTag); } else { var maxTag = CameraPreviewSizeList.Max(li => li.SizeTag); previewSize = CameraPreviewSizeList.FirstOrDefault(li => li.SizeTag == maxTag); } if (previewSize == null) { return; } //设置 await MainCamera.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, previewSize.Data); await MainCamera.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, photoSize.Data); } else { //获得分辨率 CameraSizeInfo nowSize = null; foreach (var item in CameraPreviewSizeList) { if (item.Width == width && item.Height == height) { nowSize = item; break; } } if (nowSize == null) { var maxTag = CameraPreviewSizeList.Max(li => li.SizeTag); nowSize = CameraPreviewSizeList.FirstOrDefault(li => li.SizeTag == maxTag); } //设置 await MainCamera.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, nowSize.Data); } //获得设置分辨率 var settingVideoRes = (VideoEncodingProperties)MainCamera.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview); CurrentPreviewW = settingVideoRes.Width; CurrentPreviewH = settingVideoRes.Height; //获得设置分辨率 if (IsPhotoFromImagePropStream == true) { var settingPhotoRes = (ImageEncodingProperties)MainCamera.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.Photo); CurrentPhotoW = settingPhotoRes.Width; CurrentPhotoH = settingPhotoRes.Height; } else { var settingPhotoRes = (VideoEncodingProperties)MainCamera.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.Photo); CurrentPhotoW = settingPhotoRes.Width; CurrentPhotoH = settingPhotoRes.Height; } //保存设置 if (ConfigHelper.Info.CameraPhotoSizeWidth != CurrentPhotoW || ConfigHelper.Info.CameraPhotoSizeHeight != CurrentPhotoH) { ConfigHelper.Info.CameraPhotoSizeWidth = CurrentPhotoW; ConfigHelper.Info.CameraPhotoSizeHeight = CurrentPhotoH; ConfigHelper.Save(); } } catch (Exception ex) { } }