void AutoFocus(int x, int y, bool useCoordinates)
        {
            if (camera != null)
            {
                var cameraParams = camera.GetParameters();

                Android.Util.Log.Debug(MobileBarcodeScanner.TAG, "AutoFocus Requested");

                // Cancel any previous requests
                camera.CancelAutoFocus();

                try {
                    // If we want to use coordinates
                    // Also only if our camera supports Auto focus mode
                    // Since FocusAreas only really work with FocusModeAuto set
                    if (useCoordinates &&
                        cameraParams.SupportedFocusModes.Contains(Camera.Parameters.FocusModeAuto))
                    {
                        // Let's give the touched area a 20 x 20 minimum size rect to focus on
                        // So we'll offset -10 from the center of the touch and then
                        // make a rect of 20 to give an area to focus on based on the center of the touch
                        x = x - 10;
                        y = y - 10;

                        // Ensure we don't go over the -1000 to 1000 limit of focus area
                        if (x >= 1000)
                        {
                            x = 980;
                        }
                        if (x < -1000)
                        {
                            x = -1000;
                        }
                        if (y >= 1000)
                        {
                            y = 980;
                        }
                        if (y < -1000)
                        {
                            y = -1000;
                        }

                        // Explicitly set FocusModeAuto since Focus areas only work with this setting
                        cameraParams.FocusMode = Camera.Parameters.FocusModeAuto;
                        // Add our focus area
                        cameraParams.FocusAreas = new List <Camera.Area> {
                            new Camera.Area(new Rect(x, y, x + 20, y + 20), 1000)
                        };
                        camera.SetParameters(cameraParams);
                    }

                    // Finally autofocus (weather we used focus areas or not)
                    camera.AutoFocus(this);
                } catch (Exception ex) {
                    Android.Util.Log.Debug(MobileBarcodeScanner.TAG, "AutoFocus Failed: {0}", ex);
                }
            }
        }
示例#2
0
        public override bool OnTouchEvent(MotionEvent e)
        {
            var newParameters = _camera.GetParameters();
            var action        = e.Action;

            if (e.PointerCount > 1)
            {
                if (action == MotionEventActions.PointerDown)
                {
                    _mDist = GetFingerSpacing(e);
                }
                else if (action == MotionEventActions.Move && newParameters.IsZoomSupported)
                {
                    _camera.CancelAutoFocus();
                    HandleZoom(e, newParameters);
                }
            }

            return(true);
        }
        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;
        }
示例#4
0
        public void OnPictureTaken(byte[] data, Android.Hardware.Camera camera)
        {
            try
            {
                camera.CancelAutoFocus();

                var baseBitmap = BitmapFactory.DecodeByteArray(data, 0, data.Length);
                var rotation   = (_displayOrientation + _orientationListener.getRememberedOrientation() + _layoutOrientation) % 360;
                if (rotation != 0)
                {
                    var oldBitmap = baseBitmap;
                    var matrix    = new Matrix();
                    matrix.PostRotate(rotation);
                    baseBitmap = Bitmap.CreateBitmap(baseBitmap, 0, 0, baseBitmap.Width, baseBitmap.Height, matrix, false);

                    oldBitmap.Recycle();
                    oldBitmap.Dispose();
                    oldBitmap = null;
                }

                var bitmap = baseBitmap.Copy(Bitmap.Config.Argb8888, true);
                baseBitmap.Recycle();
                baseBitmap.Dispose();
                baseBitmap = null;

                // get filepath wo das gespeichert ist
                ReformatAndSearchKennzeichen(bitmap, DateTime.Now.ToString());
            }
            catch (Exception ex)
            {
                AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.Instance);
                AlertDialog         alert  = dialog.Create();
                alert.SetTitle("Fehler");
                alert.SetMessage($"Fehler aufgetreten:\n{ex.Message}");
                alert.SetButton("OK", (c, ev) =>
                {
                });

                alert.Show();
                Element.Navigation.PopModalAsync();
            }
        }
示例#5
0
        // Sets camera focus mode and focus area
        public void setCameraFocusMode(string mode)
        {
            // Camera sees it as rotated 90 degrees, so there's some confusion with what is width and what is height)
            int  width             = 0;
            int  height            = 0;
            int  halfCoordinates   = 1000;
            int  lengthCoordinates = 2000;
            Rect area = new Rect();

            surfaceViewWithOverlay.GetDrawingRect(area);
            switch (orientation)
            {
            case 0:
            case 180:
                height = cameraPreviewSize.Height;
                width  = cameraPreviewSize.Width;
                break;

            case 90:
            case 270:
                width  = cameraPreviewSize.Height;
                height = cameraPreviewSize.Width;
                break;
            }

            camera.CancelAutoFocus();
            Android.Hardware.Camera.Parameters parameters = camera.GetParameters();
            // Set focus and metering area equal to the area of interest. This action is essential because by defaults camera
            // focuses on the center of the frame, while the area of interest in this sample application is at the top
            List <Android.Hardware.Camera.Area> focusAreas = new List <Android.Hardware.Camera.Area>();
            Rect areasRect;

            switch (orientation)
            {
            case 0:
                areasRect = new Rect(
                    -halfCoordinates + area.Left * lengthCoordinates / width,
                    -halfCoordinates + area.Top * lengthCoordinates / height,
                    -halfCoordinates + lengthCoordinates * area.Right / width,
                    -halfCoordinates + lengthCoordinates * area.Bottom / height
                    );
                break;

            case 180:
                areasRect = new Rect(
                    halfCoordinates - area.Right * lengthCoordinates / width,
                    halfCoordinates - area.Bottom * lengthCoordinates / height,
                    halfCoordinates - lengthCoordinates * area.Left / width,
                    halfCoordinates - lengthCoordinates * area.Top / height
                    );
                break;

            case 90:
                areasRect = new Rect(
                    -halfCoordinates + area.Top * lengthCoordinates / height,
                    halfCoordinates - area.Right * lengthCoordinates / width,
                    -halfCoordinates + lengthCoordinates * area.Bottom / height,
                    halfCoordinates - lengthCoordinates * area.Left / width
                    );
                break;

            case 270:
                areasRect = new Rect(
                    halfCoordinates - area.Bottom * lengthCoordinates / height,
                    -halfCoordinates + area.Left * lengthCoordinates / width,
                    halfCoordinates - lengthCoordinates * area.Top / height,
                    -halfCoordinates + lengthCoordinates * area.Right / width
                    );
                break;

            default:
                throw new IllegalArgumentException();
            }

            focusAreas.Add(new Android.Hardware.Camera.Area(areasRect, 800));
            if (parameters.MaxNumFocusAreas >= focusAreas.Count)
            {
                parameters.FocusAreas = focusAreas;
            }
            if (parameters.MaxNumMeteringAreas >= focusAreas.Count)
            {
                parameters.MeteringAreas = focusAreas;
            }

            parameters.FocusMode = mode;

            // Commit the camera parameters
            camera.SetParameters(parameters);
        }
示例#6
0
        /// <summary>
        /// Configures the specified camera.
        /// </summary>
        public AndroidCamera Configure(AndroidCamera camera)
        {
            var parameters = camera.GetParameters();

            camera.CancelAutoFocus();

            parameters.PreviewFormat = ImageFormatType.Nv21;

            var focusMode = determineFocusMode(parameters);

            invokeIfAvailable(focusMode, val => {
                this.Debug("Focus Mode [{0}]", val);
                parameters.FocusMode = val;
            });

            var preview = determinePreviewResolution(parameters);

            this.Debug("Preview resolution [Width: {0}] x [Height {1}]", preview.Width, preview.Height);
            parameters.SetPreviewSize(preview.Width, preview.Height);

            // Set picture size with an equal ratio as the preview image.
            // According to google you will get a distorted image on some hardware, if you would only set the preview size.
            var picture = determinePictureResolution(parameters, preview);

            this.Debug("Picture resolution [Width: {0}] x [Height {1}]", picture.Width, picture.Height);
            parameters.SetPictureSize(picture.Width, picture.Height);


            if (config.TargetFps > 0)
            {
                var range = determineFpsRange(parameters, config.TargetFps);
                this.Debug("FPS Range [Min: {0}] - [Max {1}]", range.Min / 1000, range.Max / 1000);
                parameters.SetPreviewFpsRange((int)range.Min, (int)range.Max);
            }

            if (config.CompatibilityMode)
            {
                this.Debug("Compatibility mode enabled. Skipping advanced configuration to ensure highest compatibility.");
            }

            if (!config.CompatibilityMode)
            {
                if (config.SceneMode)
                {
                    var sceneMode = determineSceneMode(parameters);

                    invokeIfAvailable(sceneMode, val => {
                        this.Debug("Scene Mode [{0}]", val);
                        parameters.SceneMode = val;
                    });
                }

                if (config.MeteringAreas && parameters.MaxNumMeteringAreas > 0)
                {
                    this.Debug("Metering area [{0}]", (parameters.MaxNumMeteringAreas > 0).ToString());
                    parameters.MeteringAreas = createAreas();
                }

                if (config.FocusAreas && parameters.MaxNumFocusAreas > 0)
                {
                    this.Debug("Focusing area [{0}]", (parameters.MaxNumFocusAreas > 0).ToString());
                    parameters.FocusAreas = createAreas();
                }

                if (config.VideoStabilization && parameters.IsVideoStabilizationSupported)
                {
                    this.Debug("Video stabilization [{0}]", parameters.IsVideoStabilizationSupported.ToString());
                    parameters.VideoStabilization = true;
                }

                if (config.WhiteBalance)
                {
                    var whiteBalance = determineWhiteBalance(parameters);

                    invokeIfAvailable(whiteBalance, val => {
                        this.Debug("White balance [{0}]", val);
                        parameters.WhiteBalance = val;
                    });
                }
            }

            camera.SetParameters(parameters);

            if (config.Zoom > 0)
            {
                Zoom(camera, config.Zoom);
            }

            return(camera);
        }