美文网首页代码片段分享
在自定义大小View上显示Camera预览,不变形

在自定义大小View上显示Camera预览,不变形

作者: 花艺荣 | 来源:发表于2019-08-23 18:51 被阅读0次

    需求:
    在项目里某一界面大约2 / 3面积用于显示camera预览, 预览Size不确定,要求预览不变形。
    如图:

    显示预览控件用的textureview
    核心代码:

     private void fixPreviewFrame()
           {
               if (camera != null)
               {
                   if (TextureWidth == 0 || TextureHeight == 0)
                   {
                       return;
                   }
                   Matrix matrix = textureView.Matrix;
                   global::Android.Hardware.Camera.Size preSize = camera.GetParameters().PreviewSize;
                   int previewWid = shouldRotate() ? preSize.Height : preSize.Width;
                   int previewHei = shouldRotate() ? preSize.Width : preSize.Height;
                   float scaleWid = (float)previewWid / TextureWidth;
                   float scaleHei = (float)previewHei / TextureHeight;
    
                 /*  if(scaleWid<1 || scaleHei < 1)
                   {*/
                       if(scaleWid < scaleHei)
                       {   
                           float secondScale = 1f / scaleWid;
                           scaleWid = 1;
                           scaleHei *= secondScale;
                       }
                       else
                       {
                           float secondScale = 1f / scaleHei;
                           scaleHei = 1;
                           scaleWid *= secondScale;
                       }
    /*
                   }
                   else
                   {
                       if (scaleWid < scaleHei)
                       {
                           float secondScale = 1f / scaleWid;
                           scaleWid = 1;
                           scaleHei *= secondScale;
                       }
                       else
                       {
                           float secondScale = 1f / scaleHei;
                           scaleHei = 1;
                           scaleWid *= secondScale;
                       }
                   }*/
    
                
    
                   matrix.SetScale(scaleWid, scaleHei, TextureWidth / 2, TextureHeight / 2);
                   textureView.SetTransform(matrix);
               }
           }
    
    

    相关文章

      网友评论

        本文标题:在自定义大小View上显示Camera预览,不变形

        本文链接:https://www.haomeiwen.com/subject/ohatectx.html