android - Camera2 with a SurfaceView -
i'm trying new camera2 working simple surfaceview , i'm having problems live preview. on devices image stretched out of proportions while looking fine on others.
i've setup surfaceview programatically adjust fit size of preview stream size.
on nexus 5 looks fine 1 samsung devices way off. samsung devices have black border on right part of preview.
is not possible work surfaceview or time switch textureview ?
yes, possible. note surfaceview
, associated surface
2 different things, , each can/must assigned size.
the surface
actual memory buffer hold output of camera, , setting size dictates size of actual image each frame. each format available camera, there small set of possible (exact) sizes can make buffer.
the surfaceview
displaying of image when available, , can size in layout. stretch underlying associated image data fit whatever layout size is, note display size different data's size- android resize image data display automatically. causing stretching.
for example, can make surfaceview
-based autofit view similar camera2basic's autofittextureview follows (this use):
import android.content.context; import android.util.attributeset; import android.view.surfaceview; public class autofitsurfaceview extends surfaceview { private int mratiowidth = 0; private int mratioheight = 0; public autofitsurfaceview(context context) { this(context, null); } public autofitsurfaceview(context context, attributeset attrs) { this(context, attrs, 0); } public autofitsurfaceview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); } /** * sets aspect ratio view. size of view measured based on ratio * calculated parameters. note actual sizes of parameters don't matter, * is, calling setaspectratio(2, 3) , setaspectratio(4, 6) make same result. * * @param width relative horizontal size * @param height relative vertical size */ public void setaspectratio(int width, int height) { if (width < 0 || height < 0) { throw new illegalargumentexception("size cannot negative."); } mratiowidth = width; mratioheight = height; requestlayout(); } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { super.onmeasure(widthmeasurespec, heightmeasurespec); int width = measurespec.getsize(widthmeasurespec); int height = measurespec.getsize(heightmeasurespec); if (0 == mratiowidth || 0 == mratioheight) { setmeasureddimension(width, height); } else { if (width < height * mratiowidth / mratioheight) { setmeasureddimension(width, width * mratioheight / mratiowidth); } else { setmeasureddimension(height * mratiowidth / mratioheight, height); } } } }
Comments
Post a Comment