private void startIfReady()
 {
     if (this.mStartRequested && this.mSurfaceAvailable)
     {
         this.mLensEngine.Run(this.mSurfaceView.Holder);
         if (this.mOverlay != null)
         {
             Huawei.Hms.Common.Size.Size size = this.mLensEngine.DisplayDimension;
             int min = Math.Min(640, 480);
             int max = Math.Max(640, 480);
             if (this.isPortraitMode())
             {
                 // Swap width and height sizes when in portrait, since it will be rotated by 90 degrees.
                 this.mOverlay.SetCameraInfo(min, max, this.mLensEngine.LensType);
             }
             else
             {
                 this.mOverlay.SetCameraInfo(max, min, this.mLensEngine.LensType);
             }
             this.mOverlay.Clear();
         }
         this.mStartRequested = false;
     }
 }
        protected override void OnLayout(bool changed, int l, int t, int r, int b)
        {
            int previewWidth  = 480;
            int previewHeight = 360;

            if (this.mLensEngine != null)
            {
                Huawei.Hms.Common.Size.Size size = this.mLensEngine.DisplayDimension;
                if (size != null)
                {
                    previewWidth  = 640;
                    previewHeight = 480;
                }
            }

            // Swap width and height sizes when in portrait, since it will be rotated 90 degrees
            if (this.isPortraitMode())
            {
                int tmp = previewWidth;
                previewWidth  = previewHeight;
                previewHeight = tmp;
            }

            int viewWidth  = r - l;
            int viewHeight = b - t;

            int   childWidth;
            int   childHeight;
            int   childXOffset = 0;
            int   childYOffset = 0;
            float widthRatio   = (float)viewWidth / (float)previewWidth;
            float heightRatio  = (float)viewHeight / (float)previewHeight;

            // To fill the view with the camera preview, while also preserving the correct aspect ratio,
            // it is usually necessary to slightly oversize the child and to crop off portions along one
            // of the dimensions. We scale up based on the dimension requiring the most correction, and
            // compute a crop offset for the other dimension.
            if (widthRatio > heightRatio)
            {
                childWidth   = viewWidth;
                childHeight  = (int)((float)previewHeight * widthRatio);
                childYOffset = (childHeight - viewHeight) / 2;
            }
            else
            {
                childWidth   = (int)((float)previewWidth * heightRatio);
                childHeight  = viewHeight;
                childXOffset = (childWidth - viewWidth) / 2;
            }

            for (int i = 0; i < this.ChildCount; ++i)
            {
                // One dimension will be cropped. We shift child over or up by this offset and adjust
                // the size to maintain the proper aspect ratio.
                this.GetChildAt(i).Layout(-1 * childXOffset, -1 * childYOffset, childWidth - childXOffset,
                                          childHeight - childYOffset);
            }

            try
            {
                this.startIfReady();
            }
            catch (Exception e)
            {
                Log.Info(LensEnginePreview.Tag, "Could not start camera source.", e);
            }
        }