internal CameraResolution GetResolution(List <CameraResolution> availableResolutions)
        {
            CameraResolution r = null;

            var dg = CameraResolutionSelector;

            if (dg != null)
            {
                r = dg(availableResolutions);
            }

            return(r);
        }
Exemple #2
0
        public void SurfaceChanged(ISurfaceHolder holder, global::Android.Graphics.Format format, int w, int h)
        {
            if (camera == null)
            {
                return;
            }

            var perf = PerformanceCounter.Start();

            var parameters = camera.GetParameters();

            parameters.PreviewFormat = ImageFormatType.Nv21;


            var availableResolutions = new List <CameraResolution>();

            foreach (var sps in parameters.SupportedPreviewSizes)
            {
                availableResolutions.Add(new CameraResolution
                {
                    Width  = sps.Width,
                    Height = sps.Height
                });
            }

            // Try and get a desired resolution from the options selector
            var resolution = options.GetResolution(availableResolutions);

            // If the user did not specify a resolution, let's try and find a suitable one
            if (resolution == null)
            {
                // Loop through all supported sizes
                foreach (var sps in parameters.SupportedPreviewSizes)
                {
                    // Find one that's >= 640x360 but <= 1000x1000
                    // This will likely pick the *smallest* size in that range, which should be fine
                    if (sps.Width >= 640 && sps.Width <= 1000 && sps.Height >= 360 && sps.Height <= 1000)
                    {
                        resolution = new CameraResolution
                        {
                            Width  = sps.Width,
                            Height = sps.Height
                        };
                        break;
                    }
                }
            }

            // Google Glass requires this fix to display the camera output correctly
            if (Build.Model.Contains("Glass"))
            {
                resolution = new CameraResolution
                {
                    Width  = 640,
                    Height = 360
                };
                // Glass requires 30fps
                parameters.SetPreviewFpsRange(30000, 30000);
            }

            // Hopefully a resolution was selected at some point
            if (resolution != null)
            {
                Android.Util.Log.Debug("ZXing.Net.Mobile", "Selected Resolution: " + resolution.Width + "x" + resolution.Height);
                parameters.SetPreviewSize(resolution.Width, resolution.Height);
            }

            camera.SetParameters(parameters);

            SetCameraDisplayOrientation(this.activity);

            camera.StartPreview();

            //cameraResolution = new Size(parameters.PreviewSize.Width, parameters.PreviewSize.Height);

            PerformanceCounter.Stop(perf, "SurfaceChanged took {0}ms");

            AutoFocus();
        }