如何获得Android Camera Preview 1:1的比例

时间:2014-02-03 07:24:49

标签: android android-layout camera

我正在尝试实现Android相机预览,并希望以1:1的比例显示它(就像Instagram相机一样)。
我知道如何处理预览帧大小为1:1,但预览中显示的摄像机输出被挤压。看起来实时预览仍然是4:3比例,但被挤压到1:1帧。有没有办法以1:1的比例进行实时预览?

我没有在这里粘贴任何代码,因为我尝试了很多方法,但都失败了,我无法发布...

谢谢

3 个答案:

答案 0 :(得分:6)

正如您所注意到的,相机预览帧是4:3。通常,您还可以 setPreviewSize()使宽高比为~9:4(例如800x480)。我没有遇到过在 getSupportedPreviewSizes()中有正方形的Android设备。

显示方形预览的选项如下:在两侧覆盖预览SurfaceView,边缘不透明(有效裁剪外观)。或者,您可以在通过OpenGL显示 TextureSurface 的同时裁剪图像。或者你可以 setPreviewCallbackWithBuffer(),将yuv帧裁剪成你想要的大小,并显示它(最好是通过OpenGL)。

答案 1 :(得分:0)

试试这个

private void setCameraPreview_Frame()
{
    RelativeLayout rel_Camera_Preview = (RelativeLayout)findViewById(R.id.rel_Camera_Preview);
    int width = rel_Camera_Preview.getWidth();
    int height = rel_Camera_Preview.getHeight();

    if(width<height)
        height = width;
    else
        width = height;

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, height);
    layoutParams.setMargins(10, 10, 10, 10);
    layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
    camera_preview.setLayoutParams(layoutParams);
}

camera_preview是包含Camera Preview的FrameLayout。

修改

是的,就像这样称呼这个方法

@Override
public void onWindowFocusChanged(boolean hasFocus) 
{
    super.onWindowFocusChanged(hasFocus);
    if(hasFocus)
    {
        setCameraPreview_Frame();
    }
}

答案 2 :(得分:-1)

我知道这是一个古老的问题,但在这里我是如何做到的。

在您的自定义SurfaceView课程中添加此

    @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width
}

这将使预览变为正方形而不是拍摄的图像。这就是诀窍

在捕获button时,您必须先autoFocus preview,然后立即stopPreivew,在这种情况下,您将停止'freeze' preview您可以将surfaceView设置为FrameLayoutheight设置为wrap_content,然后您可以将该framelayout的缓存视图保存为bitmap然后保存bitmap

相关问题