protected sealed override void StartPreview()
        {
            _camera = Camera.Open((int)CurrentCamera);
            _camera.SetDisplayOrientation(90);

            var parameters = _camera.GetParameters();
            if (parameters.SupportedFocusModes.Contains(Camera.Parameters.FocusModeContinuousPicture))
            {
                parameters.FocusMode = Camera.Parameters.FocusModeContinuousPicture;
            }

            var optimalSize = GetOptimalPreviewSize(_width, _height);
            if (optimalSize != null)
            {
                parameters.SetPreviewSize(optimalSize.Width, optimalSize.Height);
            }

            _camera.SetParameters(parameters);

            try
            {
                _camera.SetPreviewTexture(_surface);
                _camera.StartPreview();
            }
            catch (Java.IO.IOException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Esempio n. 2
0
        public CameraPreview(Context context, Android.Hardware.Camera camera) : base(context)
        {
            _camera = camera;
            _camera.SetDisplayOrientation(90);

            //Surface holder callback is set so theat SurfaceChanged, Created, destroy...
            //Could be called from here.
            Holder.AddCallback(this);
            // deprecated but required on Android versions less than 3.0
            Holder.SetType(SurfaceType.PushBuffers);
        }
Esempio n. 3
0
        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();
        }
Esempio n. 4
0
        public void SetCameraDisplayOrientation(Activity context)
        {
            var degrees = getCameraDisplayOrientation(context);

            Android.Util.Log.Debug("ZXING", "Changing Camera Orientation to: " + degrees);
            cameraDegrees = degrees;

            try { camera.SetDisplayOrientation(degrees); }
            catch (Exception ex) {
                Android.Util.Log.Error("ZXING", ex.ToString());
            }
        }
Esempio n. 5
0
        public void SetCameraDisplayOrientation(Activity context)
        {
            var degrees = getCameraDisplayOrientation(context);

            Android.Util.Log.Debug(MobileBarcodeScanner.TAG, "Changing Camera Orientation to: " + degrees);

            try {
                camera.SetDisplayOrientation(degrees);
            } catch (Exception ex) {
                Android.Util.Log.Error(MobileBarcodeScanner.TAG, ex.ToString());
            }
        }
        public void SetOrientation()
        {
            Android.Hardware.Camera camera = Methods.GetCamera(cameraSource);
            switch (windowManager.DefaultDisplay.Rotation)
            {
            case SurfaceOrientation.Rotation0:
                camera?.SetDisplayOrientation(90);
                break;

            case SurfaceOrientation.Rotation90:
                camera?.SetDisplayOrientation(0);
                break;

            case SurfaceOrientation.Rotation180:
                camera?.SetDisplayOrientation(270);
                break;

            case SurfaceOrientation.Rotation270:
                camera?.SetDisplayOrientation(180);
                break;
            }
        }
Esempio n. 7
0
        private void StartCamera()
        {
            holder = view.Holder;
            holder.AddCallback(this);

            camera = Android.Hardware.Camera.Open(1);
            Android.Hardware.Camera.Parameters p = camera.GetParameters();
            p.SetPreviewSize(320, 240);

            camera.SetDisplayOrientation(90);
            camera.SetParameters(p);
            camera.SetPreviewCallback(this);
        }
Esempio n. 8
0
        private void start()
        {
            holder = view.Holder;
            holder.AddCallback(this);

            camera = Android.Hardware.Camera.Open(1);
            Camera.Parameters p = camera.GetParameters();
            p.SetPreviewSize(320, 240);
            //p.SetPictureSize(320, 240);

            camera.SetDisplayOrientation(90);
            camera.SetParameters(p);
            //camera.SetPreviewDisplay(holder);
        }
Esempio n. 9
0
        private void SetCameraDisplayOrientation()
        {
            var degrees = GetCameraDisplayOrientation();

            LastCameraDisplayOrientationDegree = degrees;

            System.Diagnostics.Debug.WriteLine("Changing Camera Orientation to: " + degrees);

            try
            {
                camera.SetDisplayOrientation(degrees);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
        }
Esempio n. 10
0
        public void OnSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
        {
            mCamera     = Open();
            mCameraInfo = GetCameraInfo();


            //https://www.captechconsulting.com/blogs/android-camera-orientation-made-simple
            mCamera.SetDisplayOrientation(getCorrectCameraOrientation(mCameraInfo, mCamera));

            try {
                mCamera.SetPreviewTexture(surface);
                mCamera.SetPreviewCallback(this);
                mCamera.StartPreview();
            } catch (IOException ioe) {
                // Something bad happened
            }
        }
Esempio n. 11
0
 public void SurfaceCreated(ISurfaceHolder holder)
 {
     try
     {
         camera.SetPreviewDisplay(holder);
         camera.SetDisplayOrientation(90);
         camera.SetPreviewCallback(new MyPreviewCallback()
         {
             v = output
         });
         camera.StartPreview();
     }
     catch (Java.IO.IOException e)
     {
         throw e;
     }
 }
Esempio n. 12
0
        private void SetCameraDisplayOrientation()
        {
            var degrees = GetCameraDisplayOrientation();

            LastCameraDisplayOrientationDegree = degrees;

            Android.Util.Log.Debug(MobileBarcodeScanner.TAG, "Changing Camera Orientation to: " + degrees);

            try
            {
                Camera.SetDisplayOrientation(degrees);
            }
            catch (Exception ex)
            {
                Android.Util.Log.Error(MobileBarcodeScanner.TAG, ex.ToString());
            }
        }
Esempio n. 13
0
        public void determineDisplayOrientation()
        {
            var cameraInfo = new Android.Hardware.Camera.CameraInfo();

            Android.Hardware.Camera.GetCameraInfo(0, cameraInfo);

            var rotation = _activity.WindowManager.DefaultDisplay.Rotation;
            var degrees  = 0;

            switch (rotation)
            {
            case SurfaceOrientation.Rotation0:
                degrees = 0;
                break;

            case SurfaceOrientation.Rotation180:
                degrees = 180;
                break;

            case SurfaceOrientation.Rotation270:
                degrees = 270;
                break;

            case SurfaceOrientation.Rotation90:
                degrees = 90;
                break;
            }

            int displayOrientation;

            if (cameraInfo.Facing == CameraFacing.Front)
            {
                displayOrientation = (cameraInfo.Orientation + degrees) % 360;
                displayOrientation = (360 - displayOrientation) % 360;
            }
            else
            {
                displayOrientation = (cameraInfo.Orientation - degrees + 360) % 360;
            }

            _displayOrientation = displayOrientation;
            _layoutOrientation  = degrees;

            _camera.SetDisplayOrientation(displayOrientation);
        }
        public void SurfaceChanged(ISurfaceHolder holder, global::Android.Graphics.Format format, int w, int h)
        {
            if (camera == null)
            {
                return;
            }

            var parameters = camera.GetParameters();

            parameters.PreviewFormat = ImageFormatType.Nv21;

            camera.SetParameters(parameters);
            camera.SetDisplayOrientation(90);
            camera.StartPreview();

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

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

            Android.Hardware.Camera.Parameters parameters = camera.GetParameters();

            width  = parameters.PreviewSize.Width;
            height = parameters.PreviewSize.Height;

            camera.SetParameters(parameters);
            camera.SetDisplayOrientation(90);
            camera.StartPreview();

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

            AutoFocus();
        }
Esempio n. 16
0
        public void OnSurfaceTextureAvailable(
            Android.Graphics.SurfaceTexture surface, int w, int h)
        {
            _camera = Android.Hardware.Camera.Open(1);


            _textureView.LayoutParameters =
                new FrameLayout.LayoutParams(w, h);

            try
            {
                _camera.SetDisplayOrientation(270);
                _camera.SetPreviewTexture(surface);
                _camera.StartPreview();
            }
            catch (Java.IO.IOException ex)
            {
                System.Console.WriteLine(ex.Message);
            }
        }
Esempio n. 17
0
        //初始化相机
        private void initCamera()
        {
            mCamera.StopPreview();
            if (null != mCamera)
            {
                Android.Hardware.Camera.Parameters myParam = mCamera.GetParameters();

                //设置大小和方向等参数
                myParam.SetPreviewSize(1280, 720);
                myParam.SetPictureSize(640, 480);
                myParam.SetRotation(90);
                myParam.Set("iso", 100);
                myParam.Set("jpeg-quality", 100);
                myParam.Zoom = 14;
                mCamera.SetDisplayOrientation(90);
                mCamera.SetParameters(myParam);
                mCamera.StartPreview();
                //mCamera.SetPreviewCallback (this);
            }
        }
Esempio n. 18
0
        void SetCameraDisplayOrientation()
        {
            int currentDegrees;
            int correctedDegrees;

            currentDegrees = GetCurrentCameraDisplayDegrees();
            CurrentCameraDisplayRotationDegree = currentDegrees;

            correctedDegrees = GetCorrectedCameraDisplayDegrees(currentDegrees);
            Android.Util.Log.Debug(MobileBarcodeScanner.TAG, "Changing Camera Orientation to: " + correctedDegrees);

            try
            {
                Camera.SetDisplayOrientation(correctedDegrees);
            }
            catch (Exception ex)
            {
                Android.Util.Log.Error(MobileBarcodeScanner.TAG, ex.ToString());
            }
        }
Esempio n. 19
0
 private void StartCamera()
 {
     System.Console.WriteLine("wertk");
     GetBestSupportedCameraSize();
     try
     {
         var p = _parameters;
         p.PictureFormat = ImageFormatType.Jpeg;
         p.JpegQuality   = 100;
         p.Set("orientation", "portrait");
         p.SetRotation(90);
         p.SetPictureSize(_bestWidth, _bestHeight);
         _camera.SetParameters(p);
         _camera.SetPreviewDisplay(_surfaceHolder);
         _camera.SetDisplayOrientation(90);
         _camera.StartPreview();
     }
     catch (IOException e)
     {
     }
 }
Esempio n. 20
0
 public void start()
 {
     if (originalStart)
     {
         originalStart = false;
         Thread t = new Thread(new ThreadStart(reStarter));
         t.Start();
     }
     try
     {
         cam = Android.Hardware.Camera.Open();
         cam.GetParameters().PreviewFormat = ImageFormatType.Rgb565;
         var param = cam.GetParameters();
         param.SetPreviewSize(320, 240);
         cam.SetParameters(param);
         var x = cam.GetParameters().SupportedPreviewSizes;
         cam.SetDisplayOrientation(90);
         cam.SetPreviewCallback(this);
         cam.SetPreviewTexture(image.SurfaceTexture);
         cam.StartPreview();
     }
     catch (Exception e) { }
 }
Esempio n. 21
0
        private bool StartPreview(Camera camera)
        {
            if (camera == null) { return false; }

            StopPreview(camera);

            // Mark preview as started if surface holder not yet created. We will really start it later
            _previewStarted = true;
            if (_surfaceHolder == null) { return false; }

            camera.SetDisplayOrientation(_displayOrientation);
            camera.SetPreviewDisplay(_surfaceHolder);

            try { camera.StartPreview(); }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("StartPreview {0}", ex);
                return false;
            }

            return true;
        }
Esempio n. 22
0
        public void SetCameraDisplayOrientation(Activity context)
        {
            var degrees = 0;

            var display = context.WindowManager.DefaultDisplay;

            var rotation = display.Rotation;

            var displayMetrics = new Android.Util.DisplayMetrics();

            display.GetMetrics(displayMetrics);

            int width  = displayMetrics.WidthPixels;
            int height = displayMetrics.HeightPixels;

            if ((rotation == SurfaceOrientation.Rotation0 || rotation == SurfaceOrientation.Rotation180) && height > width ||
                (rotation == SurfaceOrientation.Rotation90 || rotation == SurfaceOrientation.Rotation270) && width > height)
            {
                switch (rotation)
                {
                case SurfaceOrientation.Rotation0:
                    degrees = 90;
                    break;

                case SurfaceOrientation.Rotation90:
                    degrees = 0;
                    break;

                case SurfaceOrientation.Rotation180:
                    degrees = 270;
                    break;

                case SurfaceOrientation.Rotation270:
                    degrees = 180;
                    break;
                }
            }
            //Natural orientation is landscape or square
            else
            {
                switch (rotation)
                {
                case SurfaceOrientation.Rotation0:
                    degrees = 0;
                    break;

                case SurfaceOrientation.Rotation90:
                    degrees = 270;
                    break;

                case SurfaceOrientation.Rotation180:
                    degrees = 180;
                    break;

                case SurfaceOrientation.Rotation270:
                    degrees = 90;
                    break;
                }
            }

            Android.Util.Log.Debug("ZXING", "Changing Camera Orientation to: " + degrees);

            try { camera.SetDisplayOrientation(degrees); }
            catch (Exception ex) {
                Android.Util.Log.Error("ZXING", ex.ToString());
            }
        }
Esempio n. 23
0
        /// <summary>
        /// Create the camera and try to set auto focus with flash
        /// </summary>
        /// <param name="holder"></param>
		public void CreateCamera(ISurfaceHolder holder) {
			try
			{
				_camera = Camera.Open();
				Camera.Parameters p = _camera.GetParameters();
				p.PictureFormat = Android.Graphics.ImageFormatType.Jpeg;
				p.JpegQuality = 100;
				hasAutoFocus = (p.SupportedFocusModes.Contains(Camera.Parameters.FocusModeAuto));
				if (hasAutoFocus)
				{
					p.FocusMode = Camera.Parameters.FocusModeAuto;
					focusReady = true;
				}
				else if (p.SupportedFocusModes.Contains(Camera.Parameters.FocusModeContinuousPicture))
				{
					p.FocusMode = Camera.Parameters.FocusModeContinuousPicture;
				}
				if (p.SupportedFlashModes.Contains(Camera.Parameters.FlashModeOn))
				{
					p.FlashMode = Camera.Parameters.FlashModeOn;
				}
				_camera.SetParameters(p);
				_camera.SetDisplayOrientation(90);
				_camera.SetPreviewCallback(this);
				_camera.Lock();
				_camera.SetPreviewDisplay(holder);
				_camera.StartPreview();
				if (hasAutoFocus)
				{
					Toast.MakeText(this, "Press screen to focus", ToastLength.Long).Show();
				}
			}
			catch (Exception e)
			{
			}
		}
Esempio n. 24
0
		//PREPARAMOS NUESTRO MEDIA RECORD Y CAMARA
		private bool PrepararVideoRecorder()
		{
			//OBTENEMOS LA INSTANCIA DE NUESTRA CAMARA YA PREPARADA
			_camera = ObtenerInstanciaCamara();
			//INICIALIZAMOS NUESTRA VARIABLE MEDIARECORDER
			_mediaRecorder = new MediaRecorder();

			//LE INDICAMOS A NUESTRA CAMARA QUE CAMBIE DE ROTACION ESTO ES DEVIDO QUE POR DEFUALT NOS MUESTRA EN POSICION HORIZONTAL,
			//Y COMO DEFINIMOS QUE LA POSICION DE NUESTRA APP SEA Portrait, REALIZAMOS EL CAMBIO.
			_camera.SetDisplayOrientation (90);
			//ABRIMOS NUESTRA CAMARA PARA SER USADA
			_camera.Unlock();


			//DE IGUAL FORMA QUE NUESTRA CAMARA CAMBIAMOS LA POSICION DE NUESTRA MEDIARECORDER
			_mediaRecorder.SetOrientationHint (90);
			//ASIGNAMOS LA CAMARA A NUESTRA MEDIARECORDER
			_mediaRecorder.SetCamera(_camera);

			//ASIGNAMOS LOS FORMATOS DE VIDEO Y AUDIO
			_mediaRecorder.SetAudioSource(AudioSource.Camcorder);
			_mediaRecorder.SetVideoSource(VideoSource.Camera);

			//RECUPERAMOS EL PERFIL QUE TIENE NUESTRA CAMARA PARA PODER ASIGNARSELA A NUESTRA MEDIARECORDER
			var camcorderProfile = ((int)Build.VERSION.SdkInt) >= 9 ? CamcorderProfile.Get(0, CamcorderQuality.High) : CamcorderProfile.Get(CamcorderQuality.High);

			//ASIGNAMOS EL PERFIL A NUESTRO MEDIARECORDER
			_mediaRecorder.SetProfile(camcorderProfile);

			//LE ASIGNAMOS EL PATH DONDE SE ENCUESTRA NUESTRO ARCHIVO DE VIDEO PARA PODER CREARLO
			_mediaRecorder.SetOutputFile(PathArchivoVideo ());


			//ASIGNAMOS EL SURFACE A NUESTRO MEDIARECORDER QUE UTILIZARA PARA VISUALIZAR LO QUE ESTAMOS GRABANDO
			_mediaRecorder.SetPreviewDisplay(_videoView.Holder.Surface);
			try
			{
				//CONFIRMAMOS LOS CAMBIOS HECHOS EN NUESTRO MEDIA RECORDER PARA PODER INICIAR A GRABAR
				_mediaRecorder.Prepare();
				return true;
			}
			catch
			{
				//SI OCURRE ALGUN PROBLEMA LIBRAMOS LOS RECURSOS ASIGNADOS A NUESTRO MEDIARECORDER
				LiberarMediaRecorder();
				return false;
			}
		}
Esempio n. 25
0
 private void StartCamera()
 {
     camera.SetDisplayOrientation(90);
     camera.StartPreview();
 }
        public void SurfaceChanged(ISurfaceHolder holder, Format format, int wx, int hx)
        {
            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 = scanningOptions.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(MobileBarcodeScanner.TAG, "Selected Resolution: " + resolution.Width + "x" + resolution.Height);
                parameters.SetPreviewSize(resolution.Width, resolution.Height);
            }

            camera.SetParameters(parameters);

            //Test to set cameraorientation to always 0 degree to prevent problem with upsidedown-picture
            //SetCameraDisplayOrientation (this.activity);
            camera.SetDisplayOrientation(0);

            camera.SetPreviewDisplay(this.Holder);
            camera.StartPreview();

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

            AutoFocus();
        }
Esempio n. 27
0
        public void SurfaceChanged(ISurfaceHolder holder, [GeneratedEnum] Format format, int width, int height)
        {
            // TODO Auto-generated method stub

            if (previewing)
            {
                camera.StopPreview();
                previewing = false;
            }
            try
            {
                float w = 0;
                float h = 0;

                if (this.Resources.Configuration.Orientation != Android.Content.Res.Orientation.Landscape)
                {
                    camera.SetDisplayOrientation(90);
                    Android.Hardware.Camera.Size cameraSize = camera.GetParameters().PictureSize;
                    int   wr    = relativeLayout.Width;
                    int   hr    = relativeLayout.Height;
                    float ratio = relativeLayout.Width * 1f / cameraSize.Height;
                    w = cameraSize.Width * ratio;
                    h = cameraSize.Height * ratio;
                    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams((int)h, (int)w);
                    cameraSurfaceView.LayoutParameters = (lp);
                    frameImageView.LayoutParameters    = lp;
                }
                else
                {
                    camera.SetDisplayOrientation(0);
                    Android.Hardware.Camera.Size cameraSize = camera.GetParameters().PictureSize;
                    float ratio = relativeLayout.Height * 1f / cameraSize.Height;
                    w = cameraSize.Width * ratio;
                    h = cameraSize.Height * ratio;
                    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams((int)w, (int)h);
                    cameraSurfaceView.LayoutParameters = (lp);
                    frameImageView.LayoutParameters    = lp;
                }

                camera.SetPreviewDisplay(cameraSurfaceHolder);
                camera.StartPreview();
                previewing = true;
                //float imgHeight = frameImageView.Height;
                //float imgeWidth = frameImageView.Width;

                float imgeWidth = frameImageView.Drawable.IntrinsicWidth;
                float imgHeight = frameImageView.Drawable.IntrinsicHeight;

                DisplayMetrics displayMetrics = new DisplayMetrics();
                WindowManager.DefaultDisplay.GetMetrics(displayMetrics);
                int widthS = displayMetrics.WidthPixels;

                float hR = h / imgHeight;
                float wR = w / widthS;
                //framMatrix.SetScale(2*wR,2*hR);
                frameImageView.SetScaleType(ImageView.ScaleType.FitXy);
            }
            catch (System.Exception e)
            {
                // TODO Auto-generated catch block
                System.Console.WriteLine("SurfaceChanged:" + e.ToString());
            }
        }
        private void SetCameraDisplayOrientation(int cameraId)
        {
            var rotation = Activity.WindowManager.DefaultDisplay.Rotation;
            var degrees  = 0;

            switch (rotation)
            {
            case SurfaceOrientation.Rotation0:
                degrees = 0;
                break;

            case SurfaceOrientation.Rotation90:
                degrees = 90;
                break;

            case SurfaceOrientation.Rotation180:
                degrees = 180;
                break;

            case SurfaceOrientation.Rotation270:
                degrees = 270;
                break;
            }

            var result = 0;
            var info   = new Camera.CameraInfo();

            Camera.GetCameraInfo(cameraId, info);
            if (info.Facing == CameraFacing.Back)
            {
                result = 360 - degrees + info.Orientation;
            }
            else if (info.Facing == CameraFacing.Front)
            {
                result  = 360 - degrees - info.Orientation;
                result += 360;
            }
            result = result % 360;
            _camera.SetDisplayOrientation(result);

            var parameters = _camera.GetParameters();

            parameters.PictureFormat = ImageFormat.Jpeg;
            parameters.JpegQuality   = 90;

            if (parameters.SupportedFocusModes != null && parameters.SupportedFocusModes.Contains(Camera.Parameters.FocusModeContinuousVideo))
            {
                parameters.FocusMode = Camera.Parameters.FocusModeContinuousVideo;
            }
            if (parameters.SupportedFlashModes != null && parameters.SupportedFlashModes.Contains(Camera.Parameters.FlashModeOff))
            {
                parameters.FlashMode = Camera.Parameters.FlashModeOff;
            }
            if (parameters.SupportedSceneModes != null && parameters.SupportedSceneModes.Contains(Camera.Parameters.SceneModeAuto))
            {
                parameters.SceneMode = Camera.Parameters.SceneModeAuto;
            }
            if (parameters.SupportedWhiteBalance != null && parameters.SupportedWhiteBalance.Contains(Camera.Parameters.WhiteBalanceAuto))
            {
                parameters.WhiteBalance = Camera.Parameters.WhiteBalanceAuto;
            }
            _camera.SetParameters(parameters);
        }
 public void SurfaceCreated(ISurfaceHolder holder)
 {
     camera = Android.Hardware.Camera.Open();
     camera.SetDisplayOrientation(90);
 }
Esempio n. 30
0
        private void configureCameraAndStartPreview(Android.Hardware.Camera camera)
        {
            // Setting camera parameters when preview is running can cause crashes on some android devices
            stopPreview();

            // Configure camera orientation. This is needed for both correct preview orientation
            // and recognition
            orientation = getCameraOrientation();
            camera.SetDisplayOrientation(orientation);

            // Configure camera parameters
            Android.Hardware.Camera.Parameters parameters = camera.GetParameters();

            // Select preview size. The preferred size for Text Capture scenario is 1080x720. In some scenarios you might
            // consider using higher resolution (small text, complex background) or lower resolution (better performance, less noise)
            cameraPreviewSize = null;
            foreach (Android.Hardware.Camera.Size size in parameters.SupportedPreviewSizes)
            {
                if (size.Height <= 720 || size.Width <= 720)
                {
                    if (cameraPreviewSize == null)
                    {
                        cameraPreviewSize = size;
                    }
                    else
                    {
                        int resultArea = cameraPreviewSize.Width * cameraPreviewSize.Height;
                        int newArea    = size.Width * size.Height;
                        if (newArea > resultArea)
                        {
                            cameraPreviewSize = size;
                        }
                    }
                }
            }
            parameters.SetPreviewSize(cameraPreviewSize.Width, cameraPreviewSize.Height);

            // Zoom
            parameters.Zoom = cameraZoom;
            // Buffer format. The only currently supported format is NV21
            parameters.PreviewFormat = Android.Graphics.ImageFormatType.Nv21;
            // Default focus mode
            parameters.FocusMode = Android.Hardware.Camera.Parameters.FocusModeAuto;

            // Done
            camera.SetParameters(parameters);

            // The camera will fill the buffers with image data and notify us through the callback.
            // The buffers will be sent to camera on requests from recognition service (see implementation
            // of ITextCaptureService.Callback.onRequestLatestFrame above)
            camera.SetPreviewCallbackWithBuffer(cameraPreviewCallback);

            // Clear the previous recognition results if any
            clearRecognitionResults();

            // Width and height of the preview according to the current screen rotation
            int width  = 0;
            int height = 0;

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

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

            // Configure the view scale and area of interest (camera sees it as rotated 90 degrees, so
            // there's some confusion with what is width and what is height)
            surfaceViewWithOverlay.setScaleX(surfaceViewWithOverlay.Width, width);
            surfaceViewWithOverlay.setScaleY(surfaceViewWithOverlay.Height, height);
            // Area of interest
            int marginWidth  = (areaOfInterestMargin_PercentOfWidth * width) / 100;
            int marginHeight = (areaOfInterestMargin_PercentOfHeight * height) / 100;

            surfaceViewWithOverlay.setAreaOfInterest(
                new Rect(marginWidth, marginHeight, width - marginWidth,
                         height - marginHeight));

            // Start preview
            camera.StartPreview();

            setCameraFocusMode(Android.Hardware.Camera.Parameters.FocusModeAuto);
            autoFocus(finishCameraInitialisationAutoFocusCallback);

            inPreview = true;
        }