static Task ToggleTorchAsync(bool switchOn) { return(Task.Run(() => { lock (locker) { if (Platform.HasApiLevel(BuildVersionCodes.M) && !AlwaysUseCameraApi) { var cameraManager = Platform.CameraManager; foreach (var id in cameraManager.GetCameraIdList()) { var hasFlash = cameraManager.GetCameraCharacteristics(id).Get(CameraCharacteristics.FlashInfoAvailable); if (Java.Lang.Boolean.True.Equals(hasFlash)) { cameraManager.SetTorchMode(id, switchOn); break; } } } else { if (camera == null) { if (surface == null) { surface = new SurfaceTexture(0); } #pragma warning disable CS0618 // Camera types are deprecated in Android 10+ camera = Camera.Open(); // Nexus 5 and some devices require a preview texture camera.SetPreviewTexture(surface); } var param = camera.GetParameters(); // Deprecated in an earlier android version param.FlashMode = switchOn ? Camera.Parameters.FlashModeTorch : Camera.Parameters.FlashModeOff; camera.SetParameters(param); if (switchOn) { camera.StartPreview(); } else { camera.StopPreview(); camera.Release(); camera.Dispose(); #pragma warning restore CS0618 // Type or member is obsolete camera = null; surface.Dispose(); surface = null; } } } })); }
public void TurnOff() { if (camera == null) { camera = Camera.Open(); } if (camera == null) { return; } var p = camera.GetParameters(); var supportedFlashModes = p.SupportedFlashModes; if (supportedFlashModes == null) { supportedFlashModes = new List <string>(); } var flashMode = string.Empty; if (supportedFlashModes.Contains(Android.Hardware.Camera.Parameters.FlashModeTorch)) { flashMode = Android.Hardware.Camera.Parameters.FlashModeOff; } if (!string.IsNullOrEmpty(flashMode)) { p.FlashMode = flashMode; camera.SetParameters(p); } }
public void OnSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { _camera = Camera.Open(FindBackFacingCamera()); //_textureView.LayoutParameters = new FrameLayout.LayoutParams(width, WallpaperDesiredMinimumHeight); var parameters = _camera.GetParameters(); try { var aspect = ((decimal)height) / ((decimal)width); var previewSize = parameters.SupportedPreviewSizes .OrderBy(s => Math.Abs(s.Width / (decimal)s.Height - aspect)) .First(); parameters.SetPreviewSize(previewSize.Width, previewSize.Height); parameters.FocusMode = Camera.Parameters.FocusModeContinuousPicture; _camera.SetParameters(parameters); _camera.SetPreviewTexture(surface); StartCamera(); } catch (Java.IO.IOException ex) { // todo handle error here System.Diagnostics.Debug.WriteLine(ex); } }
public async Task GetPhoto() { _camera = Camera.Open(); var parameters = _camera.GetParameters(); var sizes = parameters.SupportedPictureSizes; int index = 0; for (int i = 0; i < sizes.Count; i++) { if (sizes[i].Width > 1200 && sizes[i].Width < 1300) { index = i; } } parameters.SetPictureSize(sizes[index].Width, sizes[index].Height); parameters.SetRotation(90); parameters.SceneMode = Camera.Parameters.SceneModeAuto; parameters.WhiteBalance = Camera.Parameters.WhiteBalanceAuto; parameters.FocusMode = Camera.Parameters.FocusModeContinuousPicture; parameters.PictureFormat = ImageFormatType.Jpeg; parameters.JpegQuality = 100; _camera.SetParameters(parameters); _camera.SetPreviewCallback(this); _camera.Lock(); SurfaceTexture st = new SurfaceTexture(100); _camera.SetPreviewTexture(st); _camera.StartPreview(); await TakePicture(); }
public override void OnResume() { base.OnResume(); // Open the default i.e. the first rear facing camera. mCamera = Camera.Open(mDefaultCameraId); mCameraCurrentlyLocked = mDefaultCameraId; mPreview.SetCamera(mCamera); }
void UrhoAppStarted() { urhoApp = Urho.Application.Current as UrhoApp; var camera = Camera.Open(); camera.SetPreviewCallback(this); var parameters = camera.GetParameters(); parameters.SetPreviewSize(320, 240); //TODO: check if this size is supported camera.SetParameters(parameters); camera.StartPreview(); //camera.StartFaceDetection(); urhoApp.CaptureVideo(OnFrameRequested); }
public void OnSurfaceTextureAvailable(SurfaceTexture surface, int w, int h) { _camera = Camera.Open(); _textureView.LayoutParameters = new RelativeLayout.LayoutParams(w, h); try { _camera.SetPreviewTexture(surface); _camera.StartPreview(); } catch (Java.IO.IOException ex) { Console.WriteLine(ex.Message); } }
public void OnSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { _camera = Camera.Open(); _textureView.LayoutParameters = new FrameLayout.LayoutParams(width, height); try { _camera.SetPreviewTexture(surface); _camera.SetDisplayOrientation(90); _camera.StartPreview(); } catch (Java.IO.IOException ex) { Console.WriteLine(ex.Message); } }
public void SurfaceCreated(ISurfaceHolder holder) { // The Surface has been created, acquire the camera and tell it where // to draw. camera = Camera.Open(); try { camera.SetPreviewDisplay(holder); camera.SetPreviewCallback(this); } catch (Exception) { camera.Release(); camera = null; // TODO: add more exception handling logic here } }
protected override void OnResume() { NowOriantation = DetectScreenOrientation(); m_Camera = AndroidCamera.Open(); m_Camera.SetAutoFocusMoveCallback(this); // 写真のサイズを画面サイズの4倍(縦横2倍)に設定 var param = m_Camera.GetParameters(); param.SetPictureSize(DisplayHeight * 2, DisplayWidth * 2); m_Camera.SetParameters(param); m_LocationManager.RequestLocationUpdates(LocationManager.GpsProvider, 1, 0, this); base.OnResume(); }
public void OnSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { _camera = Camera.Open(); var parameters = _camera.GetParameters(); var aspect = height / (decimal)width; // Find the preview aspect ratio that is closest to the surface aspect var previewSize = parameters.SupportedPreviewSizes .OrderBy(s => Math.Abs(s.Width / (decimal)s.Height - aspect)) .First(); System.Diagnostics.Debug.WriteLine($"Preview sizes: { parameters.SupportedPreviewSizes.Count }"); parameters.SetPreviewSize(previewSize.Width, previewSize.Height); _camera.SetParameters(parameters); _camera.SetPreviewTexture(surface); StartCamera(); }
public void TurnOn() { if (camera == null) { camera = Camera.Open(); } if (camera == null) { return; } var p = camera.GetParameters(); var supportedFlashModes = p.SupportedFlashModes; if (supportedFlashModes == null) { supportedFlashModes = new List <string>(); } var flashMode = string.Empty; if (supportedFlashModes.Contains(Android.Hardware.Camera.Parameters.FlashModeTorch)) { flashMode = Android.Hardware.Camera.Parameters.FlashModeTorch; } if (!string.IsNullOrEmpty(flashMode)) { p.FlashMode = flashMode; camera.SetParameters(p); } camera.StartPreview(); try { camera.SetPreviewTexture(new SurfaceTexture(0)); } catch { } }
public void TurnOff() { if (camera == null) { camera = Camera.Open(); } if (camera == null) { Console.WriteLine("Camera failed to initialize"); return; } var p = camera.GetParameters(); var supportedFlashModes = p.SupportedFlashModes; if (supportedFlashModes == null) { supportedFlashModes = new List <string>(); } var flashMode = string.Empty; if (supportedFlashModes.Contains(Android.Hardware.Camera.Parameters.FlashModeTorch)) { flashMode = Android.Hardware.Camera.Parameters.FlashModeOff; } if (!string.IsNullOrEmpty(flashMode)) { p.FlashMode = flashMode; camera.SetParameters(p); } camera.StopPreview(); //added on 1/3/2016 by aditmer to try to fix the ongoing issue on Android 6.x and the battery drain issue camera.Release(); camera = null; }
private void prepareCamera(int encWidth, int encHeight) { if (_camera != null) { throw new RuntimeException("camera already initialized"); } Camera.CameraInfo info = new Camera.CameraInfo(); // Try to find a front-facing camera (e.g. for videoconferencing). int numCameras = Camera.NumberOfCameras; for (int i = 0; i < numCameras; i++) { Camera.GetCameraInfo(i, info); if (info.Facing == Camera.CameraInfo.CameraFacingFront) { _camera = Camera.Open(i); break; } } if (_camera == null) { Log.Debug(TAG, "No front-facing camera found; opening default"); _camera = Camera.Open(); // opens first back-facing camera } if (_camera == null) { throw new RuntimeException("Unable to open camera"); } Camera.Parameters parms = _camera.GetParameters(); choosePreviewSize(parms, encWidth, encHeight); // leave the frame rate set to default _camera.SetParameters(parms); Camera.Size size = parms.PreviewSize; Log.Debug(TAG, "Camera preview size is " + size.Width + "x" + size.Height); }
public override bool OnOptionsItemSelected(IMenuItem item) { // Handle item selection switch (item.ItemId) { case Resource.Id.switch_cam: // Release this camera -> mCameraCurrentlyLocked if (mCamera != null) { mCamera.StopPreview(); mPreview.SetCamera(null); mCamera.Release(); mCamera = null; } // Acquire the next camera and request Preview to reconfigure // parameters. mCamera = Camera.Open((mCameraCurrentlyLocked + 1) % mNumberOfCameras); mCameraCurrentlyLocked = (mCameraCurrentlyLocked + 1) % mNumberOfCameras; mPreview.SwitchCamera(mCamera); // Start the preview mCamera.StartPreview(); return(true); case Android.Resource.Id.Home: Intent intent = new Intent(this.Activity, typeof(MainActivity)); intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop); StartActivity(intent); goto default; default: return(base.OnOptionsItemSelected(item)); } }
protected override void OnElementChanged(ElementChangedEventArgs <PocketPro.CameraPreview> e) { base.OnElementChanged(e); if (Control == null) { cameraPreview = new CameraPreview(Context); cameraPreview.OnPictureReturn = e.NewElement.OnPictureReturn; cameraPreview.OnVideoStarted = e.NewElement.OnVideoStarted; cameraPreview.OnVideoFinished = e.NewElement.OnVideoFinished; SetNativeControl(cameraPreview); } if (e.OldElement != null) { // Unsubscribe cameraPreview.Click -= OnCameraPreviewClicked; } if (e.NewElement != null) { Camera camera = null; e.NewElement.TakePhotoEvent += (sender, args) => { if (cameraPreview.Preview != null) { cameraPreview.OnPictureReturn = ((PocketPro.CameraPreview)(sender)).OnPictureReturn; cameraPreview.OnVideoStarted = ((PocketPro.CameraPreview)(sender)).OnVideoStarted; cameraPreview.OnVideoFinished = ((PocketPro.CameraPreview)(sender)).OnVideoFinished; //cameraPreview.Preview.AutoFocus(new AutofocusCallBack(cameraPreview) //{ // isTakePhoto = true //}); cameraPreview.camera.EnableShutterSound(false); cameraPreview.camera.TakePicture(new ShutterCallback(), null, new JpegCallback(this.cameraPreview)); //} //else camera.AutoFocus(new AutofocusCallBack(cameraPreview)); // TODO: Verify No Memory Leak On Failure } else { } }; e.NewElement.StartRecordingEvent += (sender, args) => { cameraPreview.Preview.StopPreview(); cameraPreview.Preview.Release(); var cameraOption = cameraOptions.Equals(CameraOptions.Front) ? CameraOptions.Front : CameraOptions.Rear; var dcimPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Environment.DirectoryMovies).Path; var sdf = new SimpleDateFormat("yyyyMMdd_HHmmss"); var currentDateandTime = sdf.Format(new Date()); var filename = $"PocketPro - {currentDateandTime}.mp4"; var dcimFilePath = System.IO.Path.Combine(dcimPath, filename); videoFilePath = dcimFilePath; camera = Camera.Open((int)cameraOption); cameraOptions = cameraOption; cameraPreview.Preview = camera; camera.Unlock(); try { mRecorder = new MediaRecorder(); mRecorder.SetCamera(camera); mRecorder.SetVideoSource(VideoSource.Camera); mRecorder.SetAudioSource(AudioSource.Mic); mRecorder.SetOutputFormat(OutputFormat.Mpeg4); mRecorder.SetVideoEncoder(VideoEncoder.H264); mRecorder.SetAudioEncoder(AudioEncoder.Aac); mRecorder.SetOutputFile(dcimFilePath); mRecorder.SetOrientationHint(0); mRecorder.SetVideoFrameRate(30); // mRecorder.SetVideoEncodingBitRate(512 * 1000); mRecorder.SetVideoSize(1920, 1080); mRecorder.SetPreviewDisplay(cameraPreview.holder.Surface); mRecorder.Prepare(); mRecorder.Start(); } catch (Exception exception) { Console.WriteLine(exception.Message); } }; e.NewElement.StopRecordingEvent += (sender, args) => { mRecorder.Stop(); mRecorder.Reset(); mRecorder.Release(); //camera = Camera.Open((int)cameraOption); //cameraOptions = cameraOption; //camera.Unlock(); var dcimPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Environment.DirectoryMovies).Path; var fileBase = System.IO.Path.GetFileNameWithoutExtension(videoFilePath); var dcimFilePath = System.IO.Path.Combine(dcimPath, $"{fileBase}.png"); var brightnessService = DependencyService.Get <IRateApplication>(); var thumb = brightnessService.GenerateThumbImage(videoFilePath, 1000); System.IO.File.WriteAllBytes(dcimFilePath, thumb); }; e.NewElement.ToggleCamera += (sender, args) => { cameraPreview.Preview.StopPreview(); cameraPreview.Preview.Release(); var cameraOption = cameraOptions.Equals(CameraOptions.Front) ? CameraOptions.Front : CameraOptions.Rear; camera = Camera.Open((int)cameraOption); cameraOptions = cameraOption; cameraPreview.Preview = camera; cameraPreview.Preview.StartPreview(); cameraPreview = new CameraPreview(Context); cameraPreview.OnPictureReturn = e.OldElement.OnPictureReturn; cameraPreview.OnVideoStarted = e.OldElement.OnVideoStarted; cameraPreview.OnVideoFinished = e.OldElement.OnVideoFinished; SetNativeControl(cameraPreview); Control.Preview = camera; Control.Preview.StartPreview(); }; e.NewElement.AutoFocusEvent += (sender, args) => { cameraPreview.Preview.AutoFocus(new AutofocusCallBack(cameraPreview)); }; e.NewElement.EnableTorchEvent += (sender, args) => { var parameters = cameraPreview.Preview.GetParameters(); parameters.FlashMode = Camera.Parameters.FlashModeTorch; //cameraPreview.Preview.StopPreview(); cameraPreview.Preview.SetParameters(parameters); }; e.NewElement.DisableTorchEvent += (sender, args) => { var parameters = cameraPreview.Preview.GetParameters(); parameters.FlashMode = Camera.Parameters.FlashModeOff; //cameraPreview.Preview.StartPreview(); cameraPreview.Preview.SetParameters(parameters); }; // Subscribe cameraPreview.Click += OnCameraPreviewClicked; try { if (camera == null) { Control.Preview = Camera.Open((int)e.NewElement.Camera); } } catch (Exception cameraException) { Console.WriteLine(cameraException.Message); } } }
Task ToggleTorchAsync(bool switchOn) { return(Task.Run(() => { lock (locker) { if (OperatingSystem.IsAndroidVersionAtLeast((int)BuildVersionCodes.M) && !AlwaysUseCameraApi) { foreach (var id in CameraManager.GetCameraIdList()) { var hasFlash = CameraManager.GetCameraCharacteristics(id).Get(CameraCharacteristics.FlashInfoAvailable); if (Java.Lang.Boolean.True.Equals(hasFlash)) { try { CameraManager.SetTorchMode(id, switchOn); break; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"Unable to SetTorchMode on {id}: {ex.Message}"); } } } } else { if (camera == null) { if (surface == null) { surface = new SurfaceTexture(0); } #pragma warning disable CS0618 // Camera types are deprecated in Android 10+ camera = Camera.Open(); // Nexus 5 and some devices require a preview texture camera.SetPreviewTexture(surface); } var param = camera.GetParameters(); // Deprecated in an earlier android version param.FlashMode = switchOn ? Camera.Parameters.FlashModeTorch : Camera.Parameters.FlashModeOff; camera.SetParameters(param); if (switchOn) { camera.StartPreview(); } else { camera.StopPreview(); camera.Release(); camera.Dispose(); #pragma warning restore CS0618 // Type or member is obsolete camera = null; surface.Dispose(); surface = null; } } } })); }