private void ConfigureTransform(int viewWidth, int viewHeight) { if (_viewSurface == null || _previewSize == null || _context == null) { return; } var windowManager = _context.GetSystemService(Context.WindowService).JavaCast <IWindowManager>(); var rotation = windowManager.DefaultDisplay.Rotation; var matrix = new Matrix(); var viewRect = new RectF(0, 0, viewWidth, viewHeight); var bufferRect = new RectF(0, 0, _previewSize.Width, _previewSize.Height); var centerX = viewRect.CenterX(); var 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); matrix.PostRotate(90 * ((int)rotation - 2), centerX, centerY); } _cameraTexture.SetTransform(matrix); }
// * // * Sets the TextureView transform to preserve the aspect ratio of the video. // private void AdjustAspectRatio(int videoWidth, int videoHeight) { int viewWidth = mTextureView.Width; int viewHeight = mTextureView.Height; double aspectRatio = (double)videoHeight / videoWidth; int newWidth, newHeight; if (viewHeight > (int)(viewWidth * aspectRatio)) { // limited by narrow width; restrict height newWidth = viewWidth; newHeight = (int)(viewWidth * aspectRatio); } else { // limited by short height; restrict width newWidth = (int)(viewHeight / aspectRatio); newHeight = viewHeight; } int xoff = (viewWidth - newWidth) / 2; int yoff = (viewHeight - newHeight) / 2; Log.Verbose(TAG, "video=" + videoWidth + "x" + videoHeight + " view=" + viewWidth + "x" + viewHeight + " newView=" + newWidth + "x" + newHeight + " off=" + xoff + "," + yoff); Matrix txform = new Matrix(); mTextureView.GetTransform(txform); txform.SetScale((float)newWidth / viewWidth, (float)newHeight / viewHeight); //txform.postRotate(10); // just for fun txform.PostTranslate(xoff, yoff); mTextureView.SetTransform(txform); }
/// <summary> /// Configures the necessary Matrix transformation to TextureView. /// This method should be called after the camera preview size is determined in /// SetUpCameraOutputs and also the size of TextureView is fixed. /// </summary> /// <param name="viewWidth"></param> /// <param name="viewHeight"></param> public void ConfigureTransform(int viewWidth, int viewHeight) { var activity = Activity; if (null == TextureView || null == mPreviewSize || null == activity) { return; } var rotation = (int)activity.WindowManager.DefaultDisplay.Rotation; Matrix matrix = new Matrix(); RectF viewRect = new RectF(0, 0, viewWidth, viewHeight); RectF bufferRect = new RectF(0, 0, mPreviewSize.Height, mPreviewSize.Width); float centerX = viewRect.CenterX(); float centerY = viewRect.CenterY(); if ((int)SurfaceOrientation.Rotation90 == rotation || (int)SurfaceOrientation.Rotation270 == rotation) { 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 * (rotation - 2), centerX, centerY); } else if ((int)SurfaceOrientation.Rotation180 == rotation) { matrix.PostRotate(180, centerX, centerY); } TextureView.SetTransform(matrix); }
private void configureTransform(int viewWidth, int viewHeight) { if (null == textureView || null == previewSize || null == context) { return; } int rotation = orientation; RectF viewRect = new RectF(0, 0, viewWidth, viewHeight); RectF bufferRect = new RectF(0, 0, previewSize.Height, previewSize.Width); float centerX = viewRect.CenterX(); float centerY = viewRect.CenterY(); //if ( Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) //{ // bufferRect.Offset(centerX - bufferRect.CenterX(), centerY - bufferRect.CenterY()); // matrix.SetRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.Fill); // float scale = Java.Lang.Math.Max( // (float)viewHeight / previewSize.Height, // (float)viewWidth / previewSize.Width); // matrix.PostScale(scale, scale, centerX, centerY); // matrix.PostRotate(90 * (rotation - 2), centerX, centerY); //} //else if (Surface.ROTATION_180 == rotation) //{ // matrix.PostRotate(180, centerX, centerY); //} textureView.SetTransform(matrix); }
public void OnSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { var viewSurface = surface; var windowManager = Context.GetSystemService(Context.WindowService).JavaCast <IWindowManager>(); var rotation = windowManager.DefaultDisplay.Rotation; var matrix = new Matrix(); var viewRect = new RectF(0, 0, width, height); var bufferRect = new RectF(0, 0, previewSize.Width, previewSize.Height); var centerX = viewRect.CenterX(); var 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); matrix.PostRotate(90 * ((int)rotation - 2), centerX, centerY); } liveTextureView.SetTransform(matrix); //camera = Android.Hardware.Camera.Open(); //var parameters = camera.GetParameters(); //var aspect = ((decimal)height) / ((decimal)width); //// Find the preview aspect ratio that is closest to the surface aspect //var previewSize = parameters.SupportedPreviewSizes // .OrderBy(s => Math.Abs(s.Width / (decimal)s.Height - aspect)) // .First(); //System.Diagnostics.Debug.WriteLine($"Preview sizes: {parameters.SupportedPreviewSizes.Count}"); //parameters.SetPreviewSize(previewSize.Width, previewSize.Height); //camera.SetParameters(parameters); //camera.SetPreviewTexture(surface); //StartCamera(); }