private static void TurnOffFlashAndSetContinuousAutoMode(AVCaptureDevice device) { device.LockForConfiguration(out var error); if (error != null) { return; } if (device.IsFlashModeSupported(AVCaptureFlashMode.Off)) { device.FlashMode = AVCaptureFlashMode.Off; } if (device.IsFocusModeSupported(AVCaptureFocusMode.ContinuousAutoFocus)) { device.FocusMode = AVCaptureFocusMode.ContinuousAutoFocus; } if (device.IsExposureModeSupported(AVCaptureExposureMode.ContinuousAutoExposure)) { device.ExposureMode = AVCaptureExposureMode.ContinuousAutoExposure; } if (device.IsWhiteBalanceModeSupported(AVCaptureWhiteBalanceMode.ContinuousAutoWhiteBalance)) { device.WhiteBalanceMode = AVCaptureWhiteBalanceMode.ContinuousAutoWhiteBalance; } device.UnlockForConfiguration(); }
static void SetFlashModeForDevice(AVCaptureFlashMode flashMode, AVCaptureDevice device) { if (device.HasFlash && device.IsFlashModeSupported(flashMode)) { NSError error; if (device.LockForConfiguration(out error)) { device.FlashMode = flashMode; device.UnlockForConfiguration(); } else { Console.WriteLine("Could not lock device for configuration: {0}", error); } } }
/// <summary> /// Sets the device's flash settings /// </summary> /// <returns><see cref="T:ChilliSource.Mobile.Core.OperationResult"/> instance indicating the outcome of the operation</returns> /// <param name="device">Device.</param> /// <param name="flashMode">Flash mode.</param> public static OperationResult SetFlashMode(this AVCaptureDevice device, AVCaptureFlashMode flashMode) { if (device.HasFlash && device.IsFlashModeSupported(flashMode)) { NSError error; if (device.LockForConfiguration(out error)) { device.FlashMode = flashMode; device.UnlockForConfiguration(); return(OperationResult.AsSuccess()); } else { return(OperationResult.AsFailure(string.Format("Could not lock device for configuration: {0}", error))); } } else { return(OperationResult.AsSuccess()); } }
static void SetFlashModeForDevice (AVCaptureFlashMode flashMode, AVCaptureDevice device) { if (device.HasFlash && device.IsFlashModeSupported (flashMode)) { NSError error; if (device.LockForConfiguration (out error)) { device.FlashMode = flashMode; device.UnlockForConfiguration (); } else { Console.WriteLine ("Could not lock device for configuration: {0}", error); } } }
private async void SnapStillImage() { // if ((m_videoDevice != null) && (m_stillImageOutput != null)) { if (m_videoDevice.HasFlash && m_videoDevice.IsFlashModeSupported(AVCaptureFlashMode.Auto)) { NSError error; if (m_videoDevice.LockForConfiguration(out error)) { m_videoDevice.FlashMode = AVCaptureFlashMode.Auto; m_videoDevice.UnlockForConfiguration(); } } AVCaptureConnection connection = m_stillImageOutput.ConnectionFromMediaType(AVMediaType.Video); var imageDataSampleBuffer = await m_stillImageOutput.CaptureStillImageTaskAsync(connection); //获得当前帧的压缩图像 var imageData = AVCaptureStillImageOutput.JpegStillToNSData(imageDataSampleBuffer); //得到当前帧压缩图像的图像数据... //RequestAuthorization(handler), handler是用户与权限对话框交互后,执行的动作。 PHPhotoLibrary.RequestAuthorization(status => { if (status == PHAuthorizationStatus.Authorized) { // 若用户授权了 // To preserve the metadata, we create an asset from the JPEG NSData representation. // Note that creating an asset from a UIImage discards the metadata. // In iOS 9, we can use AddResource method on PHAssetCreationRequest class. // In iOS 8, we save the image to a temporary file and use +[PHAssetChangeRequest creationRequestForAssetFromImageAtFileURL:]. if (UIDevice.CurrentDevice.CheckSystemVersion(9, 0)) { //PHPhotoLibrary.SharedPhotoLibrary 返回的是一个(共享)图片库对象 //PerformChanges (changeHandler, completionHandler) changeHandler 以及 completionHandler 是一个lambda PHPhotoLibrary.SharedPhotoLibrary.PerformChanges(() => { var request = PHAssetCreationRequest.CreationRequestForAsset(); request.AddResource(PHAssetResourceType.Photo, imageData, null); //保存当前照片 }, (success, err) => { if (!success) { Console.WriteLine("Error occurred while saving image to photo library: {0}", err); } }); } else { //用户没有授权 string outputFileName = NSProcessInfo.ProcessInfo.GloballyUniqueString; string tmpDir = Path.GetTempPath(); string outputFilePath = Path.Combine(tmpDir, outputFileName); string outputFilePath2 = Path.ChangeExtension(outputFilePath, "jpg"); NSUrl temporaryFileUrl = new NSUrl(outputFilePath2, false); PHPhotoLibrary.SharedPhotoLibrary.PerformChanges(() => { NSError error = null; if (imageData.Save(temporaryFileUrl, NSDataWritingOptions.Atomic, out error)) { PHAssetChangeRequest.FromImage(temporaryFileUrl); } else { Console.WriteLine("Error occured while writing image data to a temporary file: {0}", error); } }, (success, error) => { if (!success) { Console.WriteLine("Error occurred while saving image to photo library: {0}", error); } // Delete the temporary file. NSError deleteError; NSFileManager.DefaultManager.Remove(temporaryFileUrl, out deleteError); }); } } }); } }