void ConfigureTransform(int viewWidth, int viewHeight)
        {
            if (textureView == null || previewSize == null || Context == null)
            {
                return;
            }

            SurfaceOrientation rotation = (Context as Activity).WindowManager.DefaultDisplay.Rotation;

            Matrix matrix = new Matrix();

            if (rotation == SurfaceOrientation.Rotation90 || rotation == SurfaceOrientation.Rotation270)
            {
                RectF viewRect   = new RectF(0, 0, viewWidth, viewHeight);
                RectF bufferRect = new RectF(0, 0, previewSize.Width, previewSize.Height);

                bufferRect.Offset(viewRect.CenterX() - bufferRect.CenterX(), viewRect.CenterY() - bufferRect.CenterY());

                float scaleH = (float)viewHeight / previewSize.Width;
                float scaleW = scaleH * 4f / 3f * 4f / 3f;

                matrix.SetRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.Fill);
                matrix.PostRotate(90 * ((int)rotation - 2), viewRect.CenterX(), viewRect.CenterY());
                matrix.PostScale(scaleW, scaleH, viewRect.CenterX(), viewRect.CenterY());
            }

            textureView.SetTransform(matrix);
        }
Exemple #2
0
        /// <summary>
        ///     Configures the necessary transformation to autoFitTextureView.
        ///     This method should be called after the camera preciew size is determined in openCamera, and also the size of
        ///     autoFitTextureView is fixed
        /// </summary>
        /// <param name="viewWidth">The width of autoFitTextureView</param>
        /// <param name="viewHeight">VThe height of autoFitTextureView</param>
        private void ConfigureTransform(int viewWidth, int viewHeight)
        {
            Activity activity = this.Activity;

            if (this.autoFitTextureView == null || this.previewSize == null || activity == null)
            {
                this.tracer.Debug("ConfigureTransform: Could not perform transformation.");
                return;
            }

            SurfaceOrientation rotation = activity.WindowManager.DefaultDisplay.Rotation;
            Matrix             matrix   = new Matrix();
            RectF viewRect   = new RectF(0, 0, viewWidth, viewHeight);
            RectF bufferRect = new RectF(0, 0, this.previewSize.Width, this.previewSize.Height);
            float centerX    = viewRect.CenterX();
            float centerY    = viewRect.CenterY();

            if (rotation == SurfaceOrientation.Rotation90 || rotation == SurfaceOrientation.Rotation270)
            {
                bufferRect.Offset(centerX - bufferRect.CenterX(), centerY - bufferRect.CenterY());
                matrix.SetRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.Fill);
                float verticalScale   = (float)viewHeight / this.previewSize.Height;
                float horizontalScale = (float)viewWidth / this.previewSize.Width;
                float scale           = Math.Max(verticalScale, horizontalScale);
                matrix.PostScale(scale, scale, centerX, centerY);
                matrix.PostRotate(90 * ((int)rotation - 2), centerX, centerY);
            }
            this.autoFitTextureView.SetTransform(matrix);
        }
Exemple #3
0
        public static void SetCameraDisplayOrientation(Activity activity, int cameraId, Android.Hardware.Camera camera)
        {
            Android.Hardware.Camera.CameraInfo info = new Android.Hardware.Camera.CameraInfo();
            Android.Hardware.Camera.GetCameraInfo(cameraId, info);
            //IWindowManager windowManager = activity.GetSystemService(activity.WindowManager).JavaCast<IWindowManager>();
            SurfaceOrientation rotation = activity.WindowManager.DefaultDisplay.Rotation;
            int 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;
            }

            int result;

            if (info.Facing == Android.Hardware.Camera.CameraInfo.CameraFacingFront)
            {
                result = (info.Orientation + degrees) % 360;
                result = (360 - result) % 360;      // compensate the mirror
            }
            else
            {      // back-facing
                result = (info.Orientation - degrees + 360) % 360;
            }
            camera.SetDisplayOrientation(result);
        }
Exemple #4
0
        void GetScreenRects(Rect screenRect1, Rect screenRect2, SurfaceOrientation rotation)
        {
            Rect hinge      = GetHinge(rotation);
            Rect windowRect = GetWindowRect();

            GetScreenRects(windowRect, hinge, screenRect1, screenRect2);
        }
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            // Open an instance of the first camera and retrieve its info.
            camera = GetCameraInstance(CAMERA_ID);
            Camera.CameraInfo cameraInfo = null;

            if (camera != null)
            {
                // Get camera info only if the camera is available
                cameraInfo = new Camera.CameraInfo();
                Camera.GetCameraInfo(CAMERA_ID, cameraInfo);
            }

            if (camera == null || cameraInfo == null)
            {
                Toast.MakeText(Activity, "Camera is not available.", ToastLength.Short).Show();
                return(inflater.Inflate(Resource.Layout.fragment_camera_unavailable, null));
            }

            View root = inflater.Inflate(Resource.Layout.fragment_camera, null);

            // Get the rotation of the screen to adjust the preview image accordingly.
            SurfaceOrientation displayRotation = Activity.WindowManager.DefaultDisplay.Rotation;

            // Create the Preview view and set it as the content of this Activity.
            cameraPreview = new CameraPreview(Activity, camera, cameraInfo, displayRotation);
            var preview = root.FindViewById <FrameLayout> (Resource.Id.camera_preview);

            preview.AddView(cameraPreview);

            return(root);
        }
Exemple #6
0
        public int GetImageRotation()
        {
            //     var wm = Android.App.Application.Context.GetSystemService(Android.Content.Context.WindowService)
            //.JavaCast<IWindowManager>();
            //     var d = wm.DefaultDisplay;
            IWindowManager windowManager = Android.App.Application.Context.GetSystemService(Context.WindowService).JavaCast <IWindowManager>();

            SurfaceOrientation rotation = windowManager.DefaultDisplay.Rotation;

            //bool isLandscape = orientation == SurfaceOrientation.Rotation90 ||
            //    orientation == SurfaceOrientation.Rotation270;
            //return isLandscape ? DeviceOrientation.Landscape : DeviceOrientation.Portrait;


            if (rotation == SurfaceOrientation.Rotation90)
            {
                return(0);
            }
            if (rotation == SurfaceOrientation.Rotation180)
            {
                return(180);
            }
            if (rotation == SurfaceOrientation.Rotation270)
            {
                return(180);
            }
            if (rotation == SurfaceOrientation.Rotation0)
            {
                return(90);
            }
            return(0);
        }
Exemple #7
0
        /// <summary>
        /// Configures the necessary transformation to mTextureView.
        /// This method should be called after the camera preciew size is determined in openCamera, and also the size of mTextureView is fixed
        /// </summary>
        /// <param name="viewWidth">The width of mTextureView</param>
        /// <param name="viewHeight">VThe height of mTextureView</param>
        public void ConfigureTransform(int viewWidth, int viewHeight)
        {
            Activity activity = Activity;

            if (textureView == null || mPreviewSize == null || activity == null)
            {
                return;
            }

            SurfaceOrientation rotation = activity.WindowManager.DefaultDisplay.Rotation;
            Matrix             matrix   = new Matrix();
            RectF viewRect   = new RectF(0, 0, viewWidth, viewHeight);
            RectF bufferRect = new RectF(0, 0, mPreviewSize.Width, mPreviewSize.Height);
            float centerX    = viewRect.CenterX();
            float centerY    = viewRect.CenterY();

            if (rotation == SurfaceOrientation.Rotation90 || rotation == SurfaceOrientation.Rotation270)
            {
                bufferRect.Offset(centerX - bufferRect.CenterX(), centerY - bufferRect.CenterY());
                matrix.SetRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.Fill);
                float scale = System.Math.Max((float)viewHeight / mPreviewSize.Height, (float)viewWidth / mPreviewSize.Width);
                matrix.PostScale(scale, scale, centerX, centerY);
                matrix.PostRotate(90 * ((int)rotation - 2), centerX, centerY);
            }
            textureView.SetTransform(matrix);
        }
Exemple #8
0
		public static Bitmap RotateToCorrentOrientation(this Bitmap bitmap, SurfaceOrientation currentOrientation)
		{
			//Calculate rotation
			float degrees = 0;
			switch (currentOrientation)
			{
				case SurfaceOrientation.Rotation180:
					degrees = -180;
					break;
				case SurfaceOrientation.Rotation270:
					degrees = 90;
					break;
				case SurfaceOrientation.Rotation90:
					degrees = -90;
					break;
			}

			//Rotate if needed
			if (degrees != 0)
			{
				using (Matrix mtx = new Matrix())
				{
					mtx.PreRotate(degrees);
					bitmap = Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, mtx, false);
				}
			}
			return bitmap;
		}
Exemple #9
0
        private bool IsCameraRoationSameAsDevice(SurfaceOrientation displayRotation, int sensorOrientation)
        {
            switch (displayRotation)
            {
            case SurfaceOrientation.Rotation0:
            case SurfaceOrientation.Rotation180:
                if (sensorOrientation == 90 || sensorOrientation == 270)
                {
                    return(true);
                }
                break;

            case SurfaceOrientation.Rotation90:
            case SurfaceOrientation.Rotation270:
                if (sensorOrientation == 0 || sensorOrientation == 180)
                {
                    return(true);
                }
                break;

            default:
                Log.Error(TAG, "Display rotation is invalid: " + displayRotation);
                return(false);
            }
            return(false);
        }
        private int GetSurfaceOrientation()
        {
            SurfaceOrientation rotation = _windowManager.DefaultDisplay.Rotation;
            int 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;
            }

            return(degrees);
        }
Exemple #11
0
        public static Bitmap RotateToCorrentOrientation(this Bitmap bitmap, SurfaceOrientation currentOrientation)
        {
            //Calculate rotation
            float degrees = 0;

            switch (currentOrientation)
            {
            case SurfaceOrientation.Rotation180:
                degrees = -180;
                break;

            case SurfaceOrientation.Rotation270:
                degrees = 90;
                break;

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

            //Rotate if needed
            if (degrees != 0)
            {
                using (var mtx = new Matrix())
                {
                    mtx.PreRotate(degrees);
                    bitmap = Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, mtx, false);
                }
            }
            return(bitmap);
        }
        public void TakePhoto()
        {
            var characteristics = cameraManager.GetCameraCharacteristics(CameraDevice.Id);
            var jpegSizes       = ((StreamConfigurationMap)characteristics.Get(CameraCharacteristics.ScalerStreamConfigurationMap)).GetOutputSizes((int)ImageFormatType.Jpeg);
            var width           = jpegSizes[0].Width;
            var height          = jpegSizes[0].Height;

            var reader         = ImageReader.NewInstance(width, height, ImageFormatType.Jpeg, 1);
            var outputSurfaces = new List <Surface> {
                reader.Surface
            };

            CaptureRequest.Builder captureBuilder = CameraDevice.CreateCaptureRequest(CameraTemplate.StillCapture);
            captureBuilder.AddTarget(reader.Surface);
            captureBuilder.Set(CaptureRequest.ControlMode, (int)ControlMode.Auto);

            // Orientation
            var windowManager           = Context.GetSystemService(Context.WindowService).JavaCast <IWindowManager>();
            SurfaceOrientation rotation = windowManager.DefaultDisplay.Rotation;

            captureBuilder.Set(CaptureRequest.JpegOrientation, orientation);

            var readerListener = new ImageAvailableListener();

            readerListener.Photo += (sender, e) =>
            {
                (Element as Controls.CameraPage).SetPhotoResult(e, width, height);
            };

            HandlerThread thread = new HandlerThread("CameraPicture");

            thread.Start();
            Handler backgroundHandler = new Handler(thread.Looper);

            reader.SetOnImageAvailableListener(readerListener, backgroundHandler);

            var captureListener = new CameraCaptureListener();

            captureListener.PhotoComplete += (sender, e) =>
            {
                StartPreview();
            };

            CameraDevice.CreateCaptureSession(outputSurfaces, new CameraCaptureStateListener()
            {
                OnConfiguredAction = (CameraCaptureSession session) =>
                {
                    try
                    {
                        previewSession = session;
                        session.Capture(captureBuilder.Build(), captureListener, backgroundHandler);
                    }
                    catch (CameraAccessException ex)
                    {
                        Log.WriteLine(LogPriority.Info, "Capture Session error: ", ex.ToString());
                    }
                }
            }, backgroundHandler);
        }
 public override void OnOrientationChanged(int orientation)
 {
     if (_renderer.windowManager.DefaultDisplay.Rotation != _lastOrientation)
     {
         _lastOrientation = _renderer.windowManager.DefaultDisplay.Rotation;
         _renderer.SetupPreviewMatrix();
     }
 }
Exemple #14
0
        public DeviceOrientations GetOrientation()
        {
            IWindowManager     windowManager = Android.App.Application.Context.GetSystemService(Context.WindowService).JavaCast <IWindowManager>();
            SurfaceOrientation rotation      = windowManager.DefaultDisplay.Rotation;
            bool isLandscape = rotation == SurfaceOrientation.Rotation90 || rotation == SurfaceOrientation.Rotation270;

            return(isLandscape ? DeviceOrientations.Landscape : DeviceOrientations.Portrait);
        }
Exemple #15
0
        Rect GetHinge(SurfaceOrientation rotation)
        {
            // Hinge's coordinates of its 4 edges in different mode
            // Double Landscape Rect(0, 1350 - 1800, 1434)
            // Double Portrait  Rect(1350, 0 - 1434, 1800)
            var boundings = DisplayMask.GetBoundingRectsForRotation(rotation);

            return(boundings.FirstOrDefault());
        }
Exemple #16
0
        private void SetCameraDisplayOrientation()
        {
            if (_camera == null)
            {
                return;
            }

            // определ¤ем насколько повернут экран от нормального положени¤
            SurfaceOrientation rotation = _activity.WindowManager.DefaultDisplay.Rotation;
            int 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;
            }

            int result = 0;

            // получаем инфо по камере cameraId
            Android.Hardware.Camera.CameraInfo info = new Android.Hardware.Camera.CameraInfo();
            Android.Hardware.Camera.GetCameraInfo(0, info);

            // задн¤¤ камера
            if (info.Facing == Android.Hardware.CameraFacing.Back)
            {
                result = ((360 - degrees) + info.Orientation);
            }
            else if (info.Facing == Android.Hardware.CameraFacing.Front)
            {
                // передн¤¤ камера
                result  = ((360 - degrees) - info.Orientation);
                result += 360;
            }
            result = result % 360;
            try
            {
                _camera.SetDisplayOrientation(result);
            }
            catch (Java.Lang.Exception e)
            {
                e.PrintStackTrace();
            }
        }
 public CameraPreview(Context context, Camera camera, Camera.CameraInfo cameraInfo,
                      SurfaceOrientation displayOrientation) :
     base(context)
 {
     this.mCamera        = camera;
     this.mSurfaceHolder = this.Holder;
     this.mSurfaceHolder.AddCallback(this);
     this.mSurfaceHolder.SetType(SurfaceType.PushBuffers);
 }
 private int ToDegrees(SurfaceOrientation rotation)
 {
     return(rotation switch
     {
         SurfaceOrientation.Rotation0 => 0,
         SurfaceOrientation.Rotation90 => 90,
         SurfaceOrientation.Rotation180 => 180,
         SurfaceOrientation.Rotation270 => 270,
         _ => throw new RuntimeException("Unknown rotation " + rotation),
     });
Exemple #19
0
        private void RefreshDisplayMetricsCache()
        {
            using var displayMetrics = new DisplayMetrics();
            using var windowManager  = CreateWindowManager();
            if (windowManager.DefaultDisplay is { } defaultDisplay)
            {
                defaultDisplay.GetRealMetrics(displayMetrics);

                _cachedDisplayMetrics = new DisplayMetricsCache(displayMetrics);
                _cachedRotation       = windowManager.DefaultDisplay.Rotation;
            }
Exemple #20
0
        private void RefreshDisplayMetricsCache()
        {
            using var displayMetrics = new DisplayMetrics();
            using var windowManager  = CreateWindowManager();
            if (windowManager.DefaultDisplay is { } defaultDisplay)
            {
#pragma warning disable CS0618 // GetRealMetrics is obsolete in API 31
                defaultDisplay.GetRealMetrics(displayMetrics);
#pragma warning restore CS0618 // GetRealMetrics is obsolete in API 31

                _cachedDisplayMetrics = new DisplayMetricsCache(displayMetrics);
                _cachedRotation       = windowManager.DefaultDisplay.Rotation;
            }
        private void gameForm_Resize(object sender, EventArgs e)
        {
            var windowManager = StrideGameForm.Context.GetSystemService(Context.WindowService).JavaCast<IWindowManager>();
            if (windowManager != null)
            {
                var newOrientation = windowManager.DefaultDisplay.Rotation;

                if (currentOrientation != newOrientation)
                {
                    currentOrientation = newOrientation;
                    OnOrientationChanged(this, EventArgs.Empty);
                }
            }
        }
        void UseDualMode(SurfaceOrientation rotation)
        {
            switch (rotation)
            {
            case SurfaceOrientation.Rotation90:
            case SurfaceOrientation.Rotation270:
                // Setting layout for double landscape
                UseSingleMode();
                break;

            default:
                ShowFragment(dualPortrait);
                break;
            }
        }
        /// <summary>
        /// 获取设备的朝向
        /// </summary>
        /// <returns></returns>
        private static Orientation GetDeviceNaturalOrientation()
        {
            var orientation             = Game.Activity.Resources.Configuration.Orientation;
            SurfaceOrientation rotation = Game.Activity.WindowManager.DefaultDisplay.Rotation;

            if (((rotation == SurfaceOrientation.Rotation0 || rotation == SurfaceOrientation.Rotation180) && orientation == Orientation.Landscape) ||
                ((rotation == SurfaceOrientation.Rotation90 || rotation == SurfaceOrientation.Rotation270) && orientation == Orientation.Portrait))
            {
                return(Orientation.Landscape);
            }
            else
            {
                return(Orientation.Portrait);
            }
        }
Exemple #24
0
 void UseDualMode(SurfaceOrientation rotation)
 {
     if (rotation == SurfaceOrientation.Rotation90 || rotation == SurfaceOrientation.Rotation270)
     {
         dualLandscape.setCurrentPosition(currentPosition);
         ShowFragment(dualLandscape);
         HideFragment(dualPortrait);
         HideFragment(singlePortrait);
     }
     else
     {
         dualPortrait.SetCurrentPosition(currentPosition);
         ShowFragment(dualPortrait);
         HideFragment(singlePortrait);
         HideFragment(dualLandscape);
     }
 }
Exemple #25
0
        public static Bitmap GetScaledAndRotatedBitmap(this Bitmap bitmap, SurfaceOrientation currentOrientation,
                                                       float maxPixelDimension)
        {
            var newBitmapSize = getScaledSize(bitmap.Width, bitmap.Height, maxPixelDimension);

            if (newBitmapSize != SizeF.Empty)
            {
                var scaled  = bitmap.ScaleBitmap(newBitmapSize);
                var rotated = scaled.RotateToCorrentOrientation(currentOrientation);
                if (rotated != scaled)
                {
                    scaled.Dispose();
                }
                return(rotated);
            }
            return(bitmap.RotateToCorrentOrientation(currentOrientation));
        }
		public CameraPreview (Context context, Camera camera, Camera.CameraInfo cameraInfo, 
			SurfaceOrientation displayOrientation) :
			base (context)
		{
			// Do not initialize if no camera has been set
			if (camera == null || cameraInfo == null)
				return;
			
			this.camera = camera;
			this.cameraInfo = cameraInfo;
			this.displayOrientation = displayOrientation;

			// Install a SurfaceHolder.Callback so we get notified when the
			// underlying surface is created and destroyed.
			holder = Holder;
			holder.AddCallback (this);
		}
Exemple #27
0
        public CameraPreview(Context context, Android.Hardware.Camera camera, int cameraId, SurfaceOrientation displayRotation) : base(context)
        {
            _context  = context;
            _camera   = camera;
            _cameraId = cameraId;
            _supportedPreviewSizes = _camera.GetParameters().SupportedPreviewSizes;
            _supportedPicSizes     = _camera.GetParameters().SupportedPictureSizes;
            _holder = Holder;
            _holder.AddCallback(this);

            // deprecated setting, but required on Android versions prior to 3.0
            //  if (Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.Cupcake)
            {
                _holder.SetType(SurfaceType.PushBuffers);
            }

            this.displayRotation = displayRotation;
        }
        public CameraPreview(Context context, Camera camera, Camera.CameraInfo cameraInfo,
                             SurfaceOrientation displayOrientation) :
            base(context)
        {
            // Do not initialize if no camera has been set
            if (camera == null || cameraInfo == null)
            {
                return;
            }

            this.camera             = camera;
            this.cameraInfo         = cameraInfo;
            this.displayOrientation = displayOrientation;

            // Install a SurfaceHolder.Callback so we get notified when the
            // underlying surface is created and destroyed.
            holder = Holder;
            holder.AddCallback(this);
        }
Exemple #29
0
        void UseDualMode(SurfaceOrientation rotation)
        {
            switch (rotation)
            {
            case SurfaceOrientation.Rotation90:
            case SurfaceOrientation.Rotation270:
                // Setting layout for double landscape
                SetContentView(dual);
                ShowTwoPages = false;
                break;

            default:
                // Setting layout for double portrait
                SetContentView(single);
                ShowTwoPages = true;
                break;
            }
            SetupViewPager();
        }
Exemple #30
0
        private void SetUpMediaRecorder()
        {
            if (null == Activity)
            {
                return;
            }
            mediaRecorder.SetAudioSource(AudioSource.Mic);
            mediaRecorder.SetVideoSource(VideoSource.Surface);
            mediaRecorder.SetOutputFormat(OutputFormat.Mpeg4);
            mediaRecorder.SetOutputFile(thumbnailfilename);
            //CamcorderProfile profile;
            //if (CamcorderProfile.HasProfile(CamcorderQuality.Q1080p))
            //    profile = CamcorderProfile.Get(CURRENTCAMERA, CamcorderQuality.Q1080p);
            //else
            //    profile = CamcorderProfile.Get(CURRENTCAMERA, CamcorderQuality.High);
            //mediaRecorder.SetProfile(profile);
            mediaRecorder.SetVideoEncodingBitRate(10000000);
            mediaRecorder.SetVideoFrameRate(30);
            mediaRecorder.SetVideoSize(videoSize.Width, videoSize.Height);
            mediaRecorder.SetVideoEncoder(VideoEncoder.H264);
            mediaRecorder.SetAudioEncoder(AudioEncoder.Aac);


            //int rotation = (int)Activity.WindowManager.DefaultDisplay.Rotation;
            //int orientation = ORIENTATIONS.Get(rotation);

            SurfaceOrientation    rotation        = Activity.WindowManager.DefaultDisplay.Rotation;
            CameraCharacteristics characteristics = manager.GetCameraCharacteristics(cameraDevice.Id);
            var mSensorOrientation = (int)characteristics.Get(CameraCharacteristics.SensorOrientation);
            int finalOrientation   = (ORIENTATIONS.Get((int)rotation) + mSensorOrientation + 270) % 360;

            ////captureBuilder.Set(CaptureRequest.JpegOrientation, new Java.Lang.Integer(finalOrientation));

            if (CURRENTCAMERA == Video.CAMERA_POSITION.FRONT)
            {
                mediaRecorder.SetOrientationHint(0);
            }
            else
            {
                mediaRecorder.SetOrientationHint(finalOrientation);
            }
            mediaRecorder.Prepare();
        }
Exemple #31
0
        private void RefreshDisplayMetricsCache()
        {
            using var displayMetrics = new DisplayMetrics();
            using var windowManager  = CreateWindowManager();
            if (windowManager.DefaultDisplay is { } defaultDisplay)
            {
                if (Android.OS.Build.VERSION.SdkInt <= Android.OS.BuildVersionCodes.R)
                {
#pragma warning disable CS0618 // GetRealMetrics is obsolete in API 31
                    defaultDisplay.GetRealMetrics(displayMetrics);
#pragma warning restore CS0618 // GetRealMetrics is obsolete in API 31
                    _cachedDisplayMetrics = new DisplayMetricsCache(displayMetrics);
                }
                else
                {
                    _cachedDisplayMetrics = new DisplayMetricsCache(windowManager.CurrentWindowMetrics, Android.Content.Res.Resources.System?.Configuration);
                }
                _cachedRotation = windowManager.DefaultDisplay.Rotation;
            }
Exemple #32
0
        private Orientation GetDeviceNaturalOrientation(Activity activity)
        {
            var windowManager = activity.WindowManager;

            Configuration config = activity.Resources.Configuration;

            SurfaceOrientation rotation = windowManager.DefaultDisplay.Rotation;

            if (((rotation == SurfaceOrientation.Rotation0 || rotation == SurfaceOrientation.Rotation180) &&
                 config.Orientation == Orientation.Landscape) ||
                ((rotation == SurfaceOrientation.Rotation90 || rotation == SurfaceOrientation.Rotation270) &&
                 config.Orientation == Orientation.Portrait))
            {
                return(Orientation.Landscape);
            }
            else
            {
                return(Orientation.Portrait);
            }
        }
		/**
     	* Calculate the correct orientation for a {@link Camera} preview that is displayed on screen.
     	*
     	* Implementation is based on the sample code provided in
     	* {@link Camera#setDisplayOrientation(int)}.
     	*/
		public static int CalculatePreviewOrientation (Camera.CameraInfo info, SurfaceOrientation rotation)
		{
			int 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;
			}

			int result;
			if (info.Facing == CameraFacing.Front) {
				result = (info.Orientation + degrees) % 360;
				result = (360 - result) % 360;  // compensate the mirror
			} else {  // back-facing
				result = (info.Orientation - degrees + 360) % 360;
			}

			return result;
		}
Exemple #34
0
		public static Bitmap GetScaledAndRotatedBitmap(this Bitmap bitmap, SurfaceOrientation currentOrientation, float maxPixelDimension)
		{
			var newBitmapSize = getScaledSize(bitmap.Width, bitmap.Height, maxPixelDimension);
			if (newBitmapSize != SizeF.Empty)
			{
				var scaled = bitmap.ScaleBitmap(newBitmapSize);
				var rotated = scaled.RotateToCorrentOrientation(currentOrientation);
				if (rotated != scaled)
					scaled.Dispose();
				return rotated;
			}
			else
			{
				return bitmap.RotateToCorrentOrientation(currentOrientation);
			}
		}
        static ExEnInterfaceOrientation ConvertOrientation(int width, int height, SurfaceOrientation orientation)
        {
            if(width > height) // Current orientation is landscape
            {
                switch(orientation)
                {
                case SurfaceOrientation.Rotation0: // natural landscape device
                case SurfaceOrientation.Rotation90:
                    return ExEnInterfaceOrientation.LandscapeRight;
                case SurfaceOrientation.Rotation180: // natural landscape device
                case SurfaceOrientation.Rotation270:
                    return ExEnInterfaceOrientation.LandscapeLeft;
                }
            }
            else // Current orientation is portrait (this is probably a bug on square screens - but who uses those?)
            {
                switch(orientation)
                {
                case SurfaceOrientation.Rotation0:
                case SurfaceOrientation.Rotation270: // natural landscape device
                    return ExEnInterfaceOrientation.Portrait;
                case SurfaceOrientation.Rotation180:
                case SurfaceOrientation.Rotation90: // natural landscape device
                    return ExEnInterfaceOrientation.PortraitUpsideDown;
                }
            }

            // This line should never be reached
            System.Diagnostics.Debug.Assert(false);
            return ExEnInterfaceOrientation.Portrait;
        }