locked
How could i make camera full screen? RRS feed

  • Question

  • User136407 posted

    ` class Preview : ViewGroup, ISurfaceHolderCallback { private const string TAG = "Preview";

        SurfaceView mSurfaceView;
        ISurfaceHolder mHolder;
        Camera.Size mPreviewSize;
        IList<Camera.Size> mSupportedPreviewSizes;
        Camera mCamera;
    
        internal Preview (Context context) 
            : base (context)
        {
            mSurfaceView = new SurfaceView (context);
            AddView (mSurfaceView);
    
            // Install a SurfaceHolder.Callback so we get notified when the
            // underlying surface is created and destroyed.
            mHolder = mSurfaceView.Holder;
            mHolder.AddCallback (this);
            mHolder.SetType (SurfaceType.PushBuffers);
        }
    
        public void SetCamera (Camera camera)
        {
            mCamera = camera;
            if (mCamera != null) {
                mSupportedPreviewSizes = mCamera.GetParameters ()
                    .SupportedPreviewSizes;
                RequestLayout ();
            }
        }
    
        public void SwitchCamera (Camera camera)
        {
            SetCamera (camera);
            try {
                camera.SetPreviewDisplay (mHolder);
            } catch (IOException exception) {
                Log.Error (TAG, "IOException caused by setPreviewDisplay()", exception);
            }
            Camera.Parameters parameters = camera.GetParameters ();
            parameters.SetPreviewSize (mPreviewSize.Width, mPreviewSize.Height);
            RequestLayout();
    
            camera.SetParameters (parameters);
        }
    
        protected override void OnMeasure (int widthMeasureSpec, int heightMeasureSpec) 
        {
            // We purposely disregard child measurements because act as a
            // wrapper to a SurfaceView that centers the camera preview instead
            // of stretching it.
            int width = ResolveSize (SuggestedMinimumWidth, widthMeasureSpec);
            int height = ResolveSize (SuggestedMinimumHeight, heightMeasureSpec);
            SetMeasuredDimension (width, height);
    
            if (mSupportedPreviewSizes != null) {
                mPreviewSize = GetOptimalPreviewSize (mSupportedPreviewSizes, width, height);
            }
        }
    
        protected override void OnLayout (bool changed, int l, int t, int r, int b) 
        {
            if (changed && ChildCount > 0) {
                View child = GetChildAt (0);
    
                int width = r - l;
                int height = b - t;
    
                int previewWidth = width;
                int previewHeight = height;
                if (mPreviewSize != null) {
                    previewWidth = mPreviewSize.Width;
                    previewHeight = mPreviewSize.Height;
                }
    
                // Center the child SurfaceView within the parent.
                if (width * previewHeight > height * previewWidth) {
                    int scaledChildWidth = previewWidth * height / previewHeight;
                    child.Layout ((width - scaledChildWidth) / 2, 0,
                        (width + scaledChildWidth) / 2, height);
                } else {
                    int scaledChildHeight = previewHeight * width / previewWidth;
                    child.Layout (0, (height - scaledChildHeight) / 2, width,
                        (height + scaledChildHeight) / 2);
                }
            }
        }
    
        public void SurfaceCreated(ISurfaceHolder holder)
        {
            // The Surface has been created, acquire the camera and tell it where
            // to draw.
            try
            {
                if (mCamera != null)
                {
                    mCamera.SetPreviewDisplay(holder);
                }
            }
            catch (IOException exception)
            {
                Log.Error(TAG, "IOException caused by setPreviewDisplay()", exception);
            }
    
        }
    
        public void SurfaceDestroyed (ISurfaceHolder holder) {
            // Surface will be destroyed when we return, so stop the preview.
            if (mCamera != null) {
                mCamera.StopPreview ();
            }
        }
    
        private Camera.Size GetOptimalPreviewSize (IList<Camera.Size> sizes, int w, int h) 
        {
            double ASPECT_TOLERANCE = 0.1;
            double targetRatio = (double) w / h;
            if (sizes == null)
                return null;
    
            Camera.Size optimalSize = null;
            double minDiff = double.MaxValue;
    
            int targetHeight = h;
    
            // Try to find an size match aspect ratio and size
            foreach (Camera.Size size in sizes) {
                double ratio = (double) size.Width / size.Height;
                if (Math.Abs (ratio - targetRatio) > ASPECT_TOLERANCE)
                    continue;
                if (Math.Abs (size.Height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.Abs (size.Height - targetHeight);
                }
            }
    
            // Cannot find the one match the aspect ratio, ignore the requirement
            if (optimalSize == null) {
                minDiff = double.MaxValue;
                foreach (Camera.Size size in sizes) {
                    if (Math.Abs (size.Height - targetHeight) < minDiff) {
                        optimalSize = size;
                        minDiff = Math.Abs (size.Height - targetHeight);
                    }
                }
            }
            return optimalSize;
        }
    
        public void SurfaceChanged (ISurfaceHolder holder, Android.Graphics.Format format, int w, int h)
        {
            // Now that the size is known, set up the camera parameters and begin
            // the preview.
            Camera.Parameters parameters = mCamera.GetParameters ();
            parameters.SetPreviewSize (mPreviewSize.Width, mPreviewSize.Height);
            RequestLayout();
    
            mCamera.SetParameters (parameters);
            mCamera.StartPreview ();
        }
    }
    

    `

    Friday, October 9, 2015 8:40 PM

Answers

  • User1175 posted

    Hey @zirkzingtive

    try to comment these lines and see:

    //if (mPreviewSize != null) 
    //{
        //previewWidth = mPreviewSize.Width;
        //previewHeight = mPreviewSize.Height;
    //}
    

    Cheers!! RIYAZ

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Saturday, October 10, 2015 6:53 AM