/// <summary> /// ////////////////////////////////////////////////////// /// </summary> /// <param name="isFirst"></param> /// <returns></returns> public static async Task <StorageFile> LowLagCapturePhoto(bool isFirst) { var file = await AppPathHelper.GetTempPhotoFile(isFirst == true? -10 : 0); if (file == null) { return(null); } try { var rotation = GetBitmapRotation(); var directSave = true; if (rotation == BitmapRotation.None) { if (IsHavePhotoStream == true && IsSupportHWZoom == false) { directSave = false; } else { directSave = true; } } else { directSave = false; } var photoData = await LowLagPhoto.CaptureAsync(); var photoFrame = photoData.Frame; if (photoFrame.Size < 1) { return(null); } using (var stream = photoFrame.CloneStream()) { //if (directSave == true) { using (var saveStream = await file.OpenStreamForWriteAsync()) { await RandomAccessStream.CopyAndCloseAsync(stream, saveStream.AsOutputStream()); } } // else { // await RotationZoomImage(stream, file, rotation, (int)AppHelper.CameraZoomNumber); } } await Task.Delay(200); } catch (Exception ex) { } return(file); }
/// <summary> /// 初始化摄像头并启动预览 /// </summary> public static async void InitCamera(bool isNewInit = true, bool isMinZoom = false) { try { AppPathHelper.Init(); ConfigHelper.Init(); if (isNewInit == true && IsLoading == true) { TriggerInitCameraOver(ErrorMessageType.Camera_Start_IsLoading); if (CameraLoadedEvent != null) { CameraLoadedEvent(); } return; } IsLoading = true; if (isNewInit == true) { FailedIndex = 1; IsZoomEffectAdded = false; } //使用UI线程进行初始化,因为需要一些事件中进行调用或重试,这些事件可能在子线程中。 await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { IsCanSetCamera = false; IsCanUseCamera = false; }); try { await StopPreview(); await LoadDevice(); if (CurrentCamDevice == null) { TriggerInitCameraOver(ErrorMessageType.Camera_Start_NoDevice); if (CameraLoadedEvent != null) { CameraLoadedEvent(); } return; } var useRes = IsCanUseCurrentDevice(); if (useRes != ErrorMessageType.None) { TriggerInitCameraOver(useRes); if (CameraLoadedEvent != null) { CameraLoadedEvent(); } return; } var res = await InitCamera(); if (!res) { TriggerInitCameraOver(ErrorMessageType.Camera_Start_UserDenied); if (CameraLoadedEvent != null) { CameraLoadedEvent(); } return; } if (isMinZoom) { AppHelper.CameraZoomNumber = MinZoomSetting; } SetZoom(AppHelper.CameraZoomNumber, true); GetPreviewSize(); GetPhotoSize(); if (CameraPhotoSizeList != null) { IsHavePhotoStream = CameraPhotoSizeList.Count > 0; } else { IsHavePhotoStream = false; } //获取录像分辨率,在摄像头初始化的时候判断是否支持两种录像的分辨率。 var recordSize = MainCamera.VideoDeviceController .GetAvailableMediaStreamProperties(MediaStreamType.VideoRecord) .Where(li => li is VideoEncodingProperties) .Select(li => li as VideoEncodingProperties); var cameraSize = CameraHelper.GetSizeList(); var oneRecordSize = RecordSizeList[0]; var twoRecordSize = RecordSizeList[1]; IsCanRecord = recordSize.Count(li => li.Width == oneRecordSize.Width && li.Height == oneRecordSize.Height) > 0 && cameraSize.Count(li => (li.Width == oneRecordSize.Width && li.Height == oneRecordSize.Height) || (li.Width == twoRecordSize.Width && li.Height == twoRecordSize.Height)) > 0; //设置分辨率 await SetSize(); MainCamera.Failed += async(ds, de) => { try { await StopPreview(); } catch (Exception ex) { } switch (de.Code) { case 0xC00D3EA3: case 0xC00D3704: TriggerInitCameraOver(ErrorMessageType.Camera_Start_AppLocked); break; case 0xC00D3E82: if (FailedIndex <= 2) { FailedIndex++; await Task.Delay(500); InitCamera(false); } else { TriggerInitCameraOver(ErrorMessageType.Camera_Start_AppLocked); } break; case 0x80070326: TriggerInitCameraOver(ErrorMessageType.Camera_Start_NoAccessRight); break; } }; if (OnNeedSetControls != null) { await OnNeedSetControls(MainCamera); } await MainCamera.StartPreviewAsync(); //await PrepareLowLagPhoto(); //调用结束事件 TriggerInitCameraOver(ErrorMessageType.None); } catch (Exception ex) { } } catch (Exception ex) { } finally { IsLoading = false; if (CameraLoadedEvent != null) { CameraLoadedEvent(); } } }
/// <summary> /// 高级多张拍摄 /// </summary> public static async void ContinuousCapture() { try { await CloseLowLagPhoto(); //设置拍摄速度 var maxCount = 5U; var tempCount = MainCamera.VideoDeviceController.LowLagPhotoSequence.MaxPastPhotos; if (maxCount > tempCount) { maxCount = tempCount; } MainCamera.VideoDeviceController.LowLagPhotoSequence.PastPhotoLimit = maxCount; var tempPerCount = MainCamera.VideoDeviceController.LowLagPhotoSequence.MaxPhotosPerSecond; if (maxCount < tempPerCount) { tempPerCount = maxCount; } MainCamera.VideoDeviceController.LowLagPhotoSequence.PhotosPerSecondLimit = tempPerCount; //开始拍摄 var rotation = GetBitmapRotation(); var directSave = true; //判断处理状态 if (rotation == BitmapRotation.None) { if (IsHavePhotoStream == true && IsSupportHWZoom == false) { directSave = false; } else { directSave = true; } } else { directSave = false; } var captureObj = await MainCamera.PrepareLowLagPhotoSequenceCaptureAsync(ImageEncodingProperties.CreateJpeg()); var nowCount = 0; var frameList = new List <CapturedFrame>(); captureObj.PhotoCaptured += async(ds, de) => { nowCount++; if (maxCount < nowCount) { return; } else if (maxCount > nowCount) { frameList.Add(de.Frame); } else if (maxCount == nowCount) { //当到达最后的一张的时候,进入处理阶段 frameList.Add(de.Frame); //停止拍摄 try { await captureObj.StopAsync(); } catch (Exception ex) { } try { await captureObj.FinishAsync(); } catch (Exception ex) { } try { var fileList = new List <StorageFile>(); //保存所有图片 var listCount = frameList.Count; for (int i = 0; i < listCount; i++) { var frame = frameList[i]; if (frame == null) { continue; } using (var stream = frame.CloneStream()) { if (stream == null) { continue; } if (stream.Size < 1) { TriggerCaptureOver(null, CaptureOverResType.Error); break; } var file = await AppPathHelper.GetTempPhotoFile(i + 1); fileList.Add(file); //if (directSave == true) { using (var saveStream = await file.OpenStreamForWriteAsync()) { await RandomAccessStream.CopyAndCloseAsync(stream, saveStream.AsOutputStream()); } } //else //{ // await RotationZoomImage(stream, file, rotation,(int)AppHelper.CameraZoomNumber); //} } } //await PrepareLowLagPhoto(); TriggerCaptureOver(fileList, CaptureOverResType.BurstCapture_Over); } catch (Exception ex) { TriggerCaptureOver(null, CaptureOverResType.Error); } } }; await captureObj.StartAsync(); } catch (Exception ex) { LogHelper.AddString(ex.Message); TriggerCaptureOver(null, CaptureOverResType.Error); } }
public static async Task <StorageFile> FirstCapture(bool isFirst) { //await Task.Delay(2000);//TODO//TEST//延迟拍摄,用于测试 var file = await AppPathHelper.GetTempPhotoFile(isFirst == true? -10 : 0); if (file == null) { return(null); } try { var rotation = GetBitmapRotation(); var directSave = true; if (rotation == BitmapRotation.None) { if (IsHavePhotoStream == true && IsSupportHWZoom == false) { directSave = false; } else { directSave = true; } } else { directSave = false; } await MainCamera.CapturePhotoToStorageFileAsync(ImageEncodingProperties.CreateJpeg(), file); await Task.Delay(200); //using (var photoFrame = new InMemoryRandomAccessStream()) //{ // await MainCamera.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(),photoFrame); // //var photoData = await LowLagPhoto.CaptureAsync(); // //var photoFrame = photoData.Frame; // if (photoFrame.Size < 1) { return null; } // using (var stream = photoFrame.CloneStream()) // { // if (directSave == true) // { // using (var saveStream = await file.OpenStreamForWriteAsync()) // { // await RandomAccessStream.CopyAndCloseAsync(stream, saveStream.AsOutputStream()); // } // } // else // { // await RotationZoomImage(stream, file, rotation, (int)AppHelper.CameraZoomNumber); // } // } //} } catch (Exception ex) { } return(file); }