如何在平方SurfaceView(如Instagram)中将相机预览大小设置为平方纵横比

ces*_*rds 46 camera android surfaceview

我正在尝试开发自己的相机活动,但我有一个问题,我无法解决...

我想要的是与instagram相框非常相似的东西,这就是我得到的:

我的形象

什么时候我应该得到这样的东西:

Instagram图片

和...

我的第二张照片

当我应该得到这样的东西:

Instagram 2

我想我只是在使用SurfaceView和Camera预览

Camera.Parameters parameters = camera.getParameters();
camera.setDisplayOrientation(90);
Run Code Online (Sandbox Code Playgroud)

和自定义SurfaceView:

public class SquaredSurfaceView extends SurfaceView {

private int width;
private int height;

public SquaredSurfaceView(Context context) {
    super(context);
}

public SquaredSurfaceView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public SquaredSurfaceView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    width = MeasureSpec.getSize(widthMeasureSpec);
    height = width;
    setMeasuredDimension(width, width);
}

public int getViewWidth() {
    return width;
}

public int getViewHeight() {
    return height;
}
Run Code Online (Sandbox Code Playgroud)

}

我做错了什么?:-(

小智 19

如前所述,您需要找到正确的预览大小(宽高比为1:1的大小),并且您可能必须使用FrameLayout作为SurfacePreview.看起来你有宽高比问题可能你有正确的预览尺寸,但你把它放在一个不正确的布局.

另一个解决方案可能是(就像Instagram一样)让您的相机全尺寸,然后隐藏布局的某些区域,使其看起来像一个正方形.然后通过软件你必须剪切图像,使其成为一个真正的正方形.

希望这对你有所帮助

  • @xramos我该怎么做instagram做的那件事?你有参考或指针吗?请帮助我:)谢谢。 (2认同)

小智 6

对我有用的解决方案是第二个答案,但因为我们需要旋转相机90º是必要的,用HEIGTH切换WIDTH,这样的......

   camera.setDisplayOrientation(90);
   Camera.Parameters params= camera.getParameters();
   surfaceView.getLayoutParams().width=params.getPreviewSize().height;
   surfaceView.getLayoutParams().height=params.getPreviewSize().width;
Run Code Online (Sandbox Code Playgroud)

希望此解决方案有所帮 :d