如何在平方 SurfaceView(如 Instagram)中将相机预览大小设置为平方纵横比 [英] How can I set camera preview size to squared aspect ratio in a squared SurfaceView (like Instagram)

查看:23
本文介绍了如何在平方 SurfaceView(如 Instagram)中将相机预览大小设置为平方纵横比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to develop my own camera activity, but I have a problem that I'm not unable to solve...

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

What I want, is something very similiar to instagram photo frame, and this is what I get:

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

When I should get something like this:

还有……

什么时候我应该得到类似的东西:

when I should get something like:

我认为我可以很好地管理 SurfaceView 和相机预览,仅使用

I think I'm maanaging the SurfaceView and Camera preview well, only using

Camera.Parameters parameters = camera.getParameters();
camera.setDisplayOrientation(90);

和自定义 SurfaceView:

and Custom 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;
}

}

我做错了什么??:-(

What I'm doing wrong?? :-(

推荐答案

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

As said before you need to find the correct preview size (the one with aspect ratio 1:1) and probably you have to use FrameLayout for the SurfacePreview. It seems that you have and aspect ratio problem maybe you have the right preview size but you are placing it in an incorrect layout.

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

Another solution might be (just like Instagram does) to make your camera at full size and then hide some areas of the layout just to make it look like a square. Then by software you would have to cut the image to make it a real square.

希望对你有帮助

这篇关于如何在平方 SurfaceView(如 Instagram)中将相机预览大小设置为平方纵横比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆