在自定义大小View上显示Camera预览,不变形
2019-08-23 本文已影响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);
}
}