private void TurnOnContinuousFocus(Camera.Parameters providedParameters = null) { var parameters = providedParameters ?? _camera.GetParameters(); if (parameters.SupportedFocusModes.Contains(Camera.Parameters.FocusModeContinuousPicture)) { parameters.FocusMode = Camera.Parameters.FocusModeContinuousPicture; } if (parameters.SupportedWhiteBalance.Contains(Camera.Parameters.WhiteBalanceAuto)) { parameters.WhiteBalance = Camera.Parameters.WhiteBalanceAuto; } if (parameters.SupportedSceneModes != null && parameters.SupportedSceneModes.Contains(Camera.Parameters.SceneModeAuto)) { parameters.SceneMode = Camera.Parameters.SceneModeAuto; } if (parameters.IsAutoWhiteBalanceLockSupported) { parameters.AutoWhiteBalanceLock = false; } if (parameters.IsAutoExposureLockSupported) { parameters.AutoExposureLock = false; } if (providedParameters == null) { _camera.SetParameters(parameters); } }
public void OpenCamera() { if (MyCamera != null) { MyCamera.StopPreview(); MyCamera.Release(); MyCamera = null; } if (_cameraId >= 0) { Camera.GetCameraInfo(_cameraId, _cameraInfo); MyCamera = Camera.Open(_cameraId); Camera.Parameters param = MyCamera.GetParameters(); param.SetRotation(0); MyCamera.SetParameters(param); try { if (_surfaceTexture != null) { MyCamera.SetPreviewTexture(_surfaceTexture); MyCamera.StartPreview(); } } catch (Exception) { } } UpdateRotation(); }
/// <summary> ///Scanning Improvement, VK, Apacheta Corp 11/14/2018. ///This method sets the best expsure setting for the device. /// </summary> private void SetBestExposure(Camera.Parameters parameters, bool lowLight) { int minExposure = parameters.MinExposureCompensation; int maxExposure = parameters.MaxExposureCompensation; float step = parameters.ExposureCompensationStep; if ((minExposure != 0 || maxExposure != 0) && step > 0.0f) { // Set low when light is on float targetCompensation = MAX_EXPOSURE_COMPENSATION; int compensationSteps = (int)(targetCompensation / step); float actualCompensation = step * compensationSteps; // Clamp value: compensationSteps = lowLight ? Math.Max(Math.Min(compensationSteps, maxExposure), minExposure) : (int)MIN_EXPOSURE_COMPENSATION; if (parameters.ExposureCompensation == compensationSteps) { Log.Debug(MobileBarcodeScanner.TAG, "Exposure compensation already set to " + compensationSteps + " / " + actualCompensation); } else { Log.Debug(MobileBarcodeScanner.TAG, "Setting exposure compensation to " + compensationSteps + " / " + actualCompensation); parameters.ExposureCompensation = compensationSteps; } } else { Log.Debug(MobileBarcodeScanner.TAG, "Camera does not support exposure compensation"); } }
/** * Attempts to find a preview size that matches the provided width and height (which * specify the dimensions of the encoded video). If it fails to find a match it just * uses the default preview size. * <p> * TODO: should do a best-fit match. */ private void choosePreviewSize(Camera.Parameters parms, int width, int height) { // We should make sure that the requested MPEG size is less than the preferred // size, and has the same aspect ratio. Camera.Size ppsfv = parms.PreferredPreviewSizeForVideo; if (AppSettings.Logging.SendToConsole && ppsfv != null) { Log.Debug(TAG, "Camera preferred preview size for video is " + ppsfv.Width + "x" + ppsfv.Height); } foreach (Camera.Size size in parms.SupportedPreviewSizes) { if (size.Width == width && size.Height == height) { parms.SetPreviewSize(width, height); return; } } Log.Warn(TAG, "Unable to set preview size to " + width + "x" + height); if (ppsfv != null) { parms.SetPreviewSize(ppsfv.Width, ppsfv.Height); } }
private int?IndexOfClosestZoom(Camera.Parameters parameters, double targetZoomRatio) { var ratios = parameters.ZoomRatios.ToList(); Log.Debug(MobileBarcodeScanner.TAG, "Zoom ratios: " + ratios); int maxZoom = parameters.MaxZoom; if (ratios == null || ratios.Count == 0 || ratios.Count != maxZoom + 1) { Log.Debug(MobileBarcodeScanner.TAG, "Invalid zoom ratios!"); return(null); } double target100 = 100.0 * targetZoomRatio; double smallestDiff = Double.PositiveInfinity; int closestIndex = 0; for (int i = 0; i < ratios.Count; i++) { double diff = Math.Abs(ratios[i]?.LongValue() ?? 0 - target100); if (diff < smallestDiff) { smallestDiff = diff; closestIndex = i; } } Log.Debug(MobileBarcodeScanner.TAG, "Chose zoom ratio of " + ((ratios[closestIndex]?.LongValue() ?? 0) / 100.0)); return(closestIndex); }
/// <summary> /// Selects the fps range which is nearest to the desired target fps by calculating the diff between the /// target fps and the min and max range individually and then selecting the range with the lowest difference. /// If no matching fps range can be found, the currently active range will be returned. /// </summary> private FpsRange determineFpsRange(AndroidCamera.Parameters parameters, uint targetFps) { var ranges = parameters.SupportedPreviewFpsRange; if (ranges.Count == 1) { return(new FpsRange(ranges.First())); } Func <int[], long> fpsSorter = range => { var min = targetFps - range[(int)Preview.FpsMinIndex]; var max = targetFps - range[(int)Preview.FpsMaxIndex]; return(Math.Abs(min) + Math.Abs(max)); }; var currentRange = new [] { 0, 0 }; parameters.GetPreviewFpsRange(currentRange); var targetRange = ranges .OrderBy(fpsSorter) .DefaultIfEmpty(currentRange) .First(); return(new FpsRange(targetRange)); }
private void UpdateCameraAspect() { try { Camera.Parameters camParams = camera.GetParameters(); Camera.CameraInfo info = new Camera.CameraInfo(); Camera.GetCameraInfo((int)Android.Hardware.CameraFacing.Back, info); Camera.Size size = GetOptimalPreviewSize(camParams.SupportedPreviewSizes, width, height); camParams.SetPreviewSize(size.Width, size.Height); int rotation = (info.Orientation + 360) % 360; camParams.SetRotation(rotation); if (camParams.SupportedFocusModes.Contains(Camera.Parameters.FocusModeContinuousPicture)) { camParams.FocusMode = Camera.Parameters.FocusModeContinuousPicture; } camera.SetParameters(camParams); } catch (Exception e) { Console.WriteLine(e.Message); } }
public Size FindBestPreviewSize(Camera.Parameters p, Size screenRes) { var max = p.SupportedPreviewSizes.Count; var s = p.SupportedPreviewSizes [max - 1]; return(new Size(s.Width, s.Height)); }
public static void Zoom(int Z) { Z = Math.Clamp(Z, 0, MaxZoom); Camera.Parameters Parms = Cam.GetParameters(); Parms.Zoom = Z; Cam.SetParameters(Parms); }
private void FlashLight() { if (camera == null || mParams == null) { return; } if (!isFlashLight) { player.Start(); //mParams = camera.GetParameters(); //mParams.FlashMode = Parameters.FlashModeTorch; //camera.SetParameters(mParams); //camera.StartPreview(); //isFlashLight = true; //btnFlash.SetImageResource(Resource.Drawable.power_on); camera.Release(); camera = null; camera = Android.Hardware.Camera.Open(); Android.Hardware.Camera.Parameters mParams = camera.GetParameters(); mParams.FlashMode = (Android.Hardware.Camera.Parameters.FlashModeTorch); camera.SetParameters(mParams); var mPreviewTexture = new SurfaceTexture(0); camera.SetPreviewTexture(mPreviewTexture); camera.StartPreview(); btnFlash.SetImageResource(Resource.Drawable.power_on); isFlashLight = true; } else { //camera.Release(); //camera.StopPreview(); //camera = null; //player.Start(); //mParams = camera.GetParameters(); //mParams.FlashMode = Parameters.FlashModeOff; //camera.SetParameters(mParams); //camera.StartPreview(); //isFlashLight = false; //btnFlash.SetImageResource(Resource.Drawable.power_off); camera.Release(); camera = null; camera = Android.Hardware.Camera.Open(); Android.Hardware.Camera.Parameters mParams = camera.GetParameters(); mParams.FlashMode = (Android.Hardware.Camera.Parameters.FlashModeOff); camera.SetParameters(mParams); var mPreviewTexture = new SurfaceTexture(0); camera.SetPreviewTexture(mPreviewTexture); camera.StartPreview(); btnFlash.SetImageResource(Resource.Drawable.power_off); isFlashLight = false; } }
public void SurfaceChanged(ISurfaceHolder holder, [GeneratedEnum] Format format, int width, int height) { if (_holder.Surface == null) { return; } try { _camera.StopPreview(); } catch (Exception ex) { string message = ex.Message; } if (_previewSize != null) { try { Android.Hardware.Camera.Parameters parameters = _camera.GetParameters(); parameters.SetPreviewSize(_previewSize.Width, _previewSize.Height); parameters.SetPictureSize(_picSize.Width, _picSize.Height); _camera.SetParameters(parameters); _camera.SetPreviewDisplay(_holder); } catch (Exception ex) { throw; } } Android.App.Activity activity = _context as Android.App.Activity; if (activity != null) { int orientation = CameraHelper.GetCameraDisplayOrientation(activity, _cameraId, displayRotation); _camera.SetDisplayOrientation(orientation); } try { Parameters parameters = _camera.GetParameters(); foreach (var previewSize in _camera.GetParameters().SupportedPreviewSizes) { // if the size is suitable for you, use it and exit the loop. parameters.SetPreviewSize(previewSize.Width, previewSize.Height); break; } _camera.SetParameters(parameters); _camera.SetPreviewDisplay(_holder); _camera.StartPreview(); } catch (Exception ex) { Log.Debug(Tag.ToString(), "Error starting camera preview: " + ex.Message); } }
/// <summary> /// Determines if the camera supports the scene mode "Barcode". /// </summary> private string determineSceneMode(AndroidCamera.Parameters parameters) { if (parameters.SupportedSceneModes.Contains(AndroidCamera.Parameters.SceneModeBarcode)) { return(AndroidCamera.Parameters.SceneModeBarcode); } return(AndroidCamera.Parameters.SceneModeAuto); }
/// <summary> /// Determines if the camera supports the white balance mode "auto". /// </summary> private string determineWhiteBalance(AndroidCamera.Parameters parameters) { if (parameters.SupportedWhiteBalance.Contains(AndroidCamera.Parameters.WhiteBalanceAuto)) { return(AndroidCamera.Parameters.WhiteBalanceAuto); } return(""); }
public void SurfaceChanged(ISurfaceHolder holder, Android.Graphics.Format format, int w, int h) { // Now that the size is known, set up the camera parameters and begin // the preview. Camera.Parameters parameters = mCamera.GetParameters(); parameters.SetPreviewSize(mPreviewSize.Width, mPreviewSize.Height); RequestLayout(); mCamera.SetParameters(parameters); mCamera.StartPreview(); }
/// <summary> /// Determines if the camera supports one of the requested focus modes in FIFO order. /// <see cref="Configuration.FocusModes" /> /// </summary> private string determineFocusMode(AndroidCamera.Parameters parameters) { foreach (var mode in config.FocusModes) { if (parameters.SupportedFocusModes.Contains(mode)) { return(mode); } } return(""); }
private void AddGps(Camera.Parameters parameters) { if (isGpsEnable && _currentLocation != null) { parameters.RemoveGpsData(); parameters.SetGpsLatitude(_currentLocation.Latitude); parameters.SetGpsLongitude(_currentLocation.Longitude); parameters.SetGpsAltitude(_currentLocation.Altitude); parameters.SetGpsTimestamp(_currentLocation.Time); parameters.SetGpsProcessingMethod(_currentLocation.Provider); } }
/// <summary> ///Scanning Improvement, VK Apacheta Corp 11/14/2018. ///This method sets the meter setting for the device. center rectangle /// </summary> private void SetMetering(Camera.Parameters parameters) { if (parameters?.MaxNumMeteringAreas > 0) { List <Camera.Area> middleArea = BuildMiddleArea(AREA_PER_1000); Log.Debug(MobileBarcodeScanner.TAG, "Setting metering areas: " + middleArea.Select(f => f.Rect.FlattenToString()).Aggregate((first, next) => first + "; " + next)); parameters.MeteringAreas = middleArea; } else { Log.Debug(MobileBarcodeScanner.TAG, "Device does not support metering areas"); } }
public void SwitchCamera(Camera camera) { SetCamera(camera); try { camera.SetPreviewDisplay(mHolder); } catch (IOException exception) { Log.Error(TAG, "IOException caused by setPreviewDisplay()", exception); } Camera.Parameters parameters = camera.GetParameters(); parameters.SetPreviewSize(mPreviewSize.Width, mPreviewSize.Height); RequestLayout(); camera.SetParameters(parameters); }
public void SetFlash(bool flash) { Android.Hardware.Camera.Parameters parameters = camera.GetParameters(); if (flash) { parameters.FlashMode = Parameters.FlashModeTorch; } else { parameters.FlashMode = Parameters.FlashModeOff; } camera.SetParameters(parameters); }
/// <summary> ///Scanning Improvement, VK Apacheta Corp 11/14/2018. ///This method sets the scene to barcode for the device. If the device supports scenes. /// </summary> private void SetBarcodeSceneMode(Camera.Parameters parameters) { if (parameters.SceneMode == Camera.Parameters.SceneModeBarcode) { Log.Debug(MobileBarcodeScanner.TAG, "Barcode scene mode already set"); return; } var supportedSceneModes = parameters.SupportedSceneModes; if (supportedSceneModes?.Contains(Camera.Parameters.SceneModeBarcode) == true) { Log.Debug(MobileBarcodeScanner.TAG, $"Previous SceneMode={parameters.SceneMode}"); parameters.SceneMode = Camera.Parameters.SceneModeBarcode; Log.Debug(MobileBarcodeScanner.TAG, "Barcode scene mode is set"); } }
public BarcodeScannerRenderer(Context context) : base(context) { cameraInfo = new Camera.CameraInfo(); Camera.GetCameraInfo(0, cameraInfo); cam = Camera.Open(0); parameters = cam.GetParameters(); Camera.Size size = parameters.PictureSize; cheight = size.Height; cwidth = size.Width; maxZoom = parameters.MaxZoom; cam.SetPreviewCallback(this); }
public void SurfaceChanged(ISurfaceHolder holder, [GeneratedEnum] Format format, int width, int height) { Camera.Parameters parameters = Parent.camera.GetParameters(); Camera.Size size = Parent.GetBestPreviewSize(width, height, parameters); if (size != null) { parameters.SetPreviewSize(size.Width, size.Height); parameters.SetPictureSize(size.Width, size.Height); parameters.SetRotation(90); parameters.FocusMode = Camera.Parameters.FocusModeContinuousPicture; Parent.camera.SetParameters(parameters); Parent.camera.StartPreview(); Parent.inPreview = true; } }
/// <summary> /// Filters picture sizes based on the given preview size by considering the aspect ratio and the resolution. /// The first size in the remaining list is then selected. /// </summary> private AndroidCamera.Size determinePictureResolution(AndroidCamera.Parameters parameters, AndroidCamera.Size preview) { /** * The picture aspect ratio has to be as close as possible to the preview ratio. */ Func <AndroidCamera.Size, bool> ratioFilter = picture => { var rawr = ((double)picture.Width / (double)picture.Height) - ((double)preview.Width / (double)preview.Height); return(Math.Abs(rawr) <= 0.1); }; return(parameters.SupportedPictureSizes .Where(ratioFilter) // the picture resolution should not exceed the preview resolution .Where(picture => picture.Width * picture.Height <= preview.Width * preview.Height) .OrderByDescending(picture => picture.Width * picture.Height) .DefaultIfEmpty(parameters.PictureSize) .First()); }
private void SvOnTouch(object sender, View.TouchEventArgs touchEventArgs) { Camera.Parameters cameraParams = _camera.GetParameters(); var action = touchEventArgs.Event.Action; if (touchEventArgs.Event.PointerCount > 1) { if (action == MotionEventActions.PointerDown) { _dist = GetFingerSpacing(touchEventArgs.Event); } else if (action == MotionEventActions.Move && cameraParams.IsZoomSupported) { _camera.CancelAutoFocus(); HandleZoom(touchEventArgs.Event, cameraParams); } } touchEventArgs.Handled = true; }
/// <summary> ///Scanning Improvement, VK Apacheta Corp 11/14/2018. ///This method sets the Video stabilization setting for the device. ///This method is not used in the code for now. /// </summary> private void SetVideoStabilization(Camera.Parameters parameters) { if (parameters.IsVideoStabilizationSupported) { if (parameters.VideoStabilization) { Log.Debug(MobileBarcodeScanner.TAG, "Video stabilization already enabled"); } else { Log.Debug(MobileBarcodeScanner.TAG, "Enabling video stabilization..."); parameters.VideoStabilization = true; } } else { Log.Debug(MobileBarcodeScanner.TAG, "This device does not support video stabilization"); } }
/// <summary> /// Determines if the camera supports flash modes that allow us to switch it on or off. /// </summary> private string determineFlashMode(AndroidCamera.Parameters p, bool state) { if (state && p.SupportedFlashModes.Contains(AndroidCamera.Parameters.FlashModeTorch)) { return(AndroidCamera.Parameters.FlashModeTorch); } if (state && p.SupportedFlashModes.Contains(AndroidCamera.Parameters.FlashModeOn)) { return(AndroidCamera.Parameters.FlashModeOn); } if (!state && p.SupportedFlashModes.Contains(AndroidCamera.Parameters.FlashModeOff)) { return(AndroidCamera.Parameters.FlashModeOff); } return(""); }
public void OnSurfaceTextureAvailable(Android.Graphics.SurfaceTexture surface, int w, int h) { _camera = Camera.Open(); try { _camera.SetPreviewTexture(surface); _camera.SetDisplayOrientation(90); _camera.StartPreview(); } catch (Java.IO.IOException ex) { Console.WriteLine(ex.Message); } Camera.Parameters tmp = _camera.GetParameters(); tmp.FocusMode = Camera.Parameters.FocusModeContinuousPicture; _camera.SetParameters(tmp); DrawRectangle(); }
public void SurfaceChanged(ISurfaceHolder holder, Format format, int w, int h) { // Now that the size is known, set up the camera parameters and begin // the preview. Camera.Parameters parameters = camera.GetParameters(); IList <Camera.Size> sizes = parameters.SupportedPreviewSizes; Camera.Size optimalSize = GetOptimalPreviewSize(sizes, w, h); parameters.SetPreviewSize(optimalSize.Width, optimalSize.Height); camera.SetDisplayOrientation(90); camera.SetParameters(parameters); int dataBufferSize = (int)(optimalSize.Width * optimalSize.Height * (ImageFormat.GetBitsPerPixel(camera.GetParameters().PreviewFormat) / 8.0)); _reader = new QRCodeReader(); camera.StartPreview(); }
public static void Start(SurfaceTexture Surface, int Width, int Height) { if (Cam == null) { Cam = Camera.Open(); } Camera.Parameters Parms = Cam.GetParameters(); MaxZoom = Parms.MaxZoom; if (Parms.SupportedFocusModes.Contains(Camera.Parameters.FocusModeContinuousPicture)) { Parms.FocusMode = Camera.Parameters.FocusModeContinuousPicture; Cam.SetParameters(Parms); } Cam.SetPreviewTexture(Surface); Cam.SetDisplayOrientation(90); StartPreview(); SetVertical(); }
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); }