【问题标题】:Android full screen camera - while keeping the camera selected ratioAndroid全屏摄像头——同时保持摄像头选择比例
【发布时间】:2015-06-03 20:01:18
【问题描述】:

我们正在尝试构建类似于Instagram 相机屏幕的东西。即允许用户拍摄square 照片。在执行此操作时,U.i 必须能够让用户在fullScreen 模式下看到相机。我们要强制用户以portrait 模式拍照

获取相机可能的比例

我们正在计算来自camerabest 比率

   private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
    final double ASPECT_TOLERANCE = 0.1;
    double targetRatio = (double) h / w;

    if (sizes == null) {
        return null;
    }

    Camera.Size optimalSize = null;
    double minDiff = Double.MAX_VALUE;

    int targetHeight = h;

    for (Camera.Size size : sizes) {
        double ratio = (double) size.width / size.height;
        if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) {
            continue;
        }
        if (Math.abs(size.height - targetHeight) < minDiff) {
            optimalSize = size;
            minDiff = Math.abs(size.height - targetHeight);
        }
    }

    if (optimalSize == null) {
        minDiff = Double.MAX_VALUE;
        for (Camera.Size size : sizes) {
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }
    }
    return optimalSize;
}

使其全屏显示。

public FrameLayout setCameraLayout(int width, int height) {
     float newProportion = (float) width / (float) height;
     // Get the width of the screen
     int screenWidth =     this.customCameraActivity.getWindowManager().getDefaultDisplay()
            .getWidth();
     int screenHeight =    this.customCameraActivity.getWindowManager().getDefaultDisplay()
            .getHeight();
      float screenProportion = (float) screenWidth / (float) screenHeight;
      // Get the SurfaceView layout parameters
      ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams)preview.getLayoutParams();


     if (newProportion > screenProportion) {
        lp.width = screenWidth;
        lp.height = (int) ((float) screenWidth / newProportion);
     } else {
        lp.width = (int) (newProportion * (float) screenHeight);
        lp.height = screenHeight;
    }

    //calculate the amount that takes to make it full screen (in the `height` parameter)
    float propHeight = screenHeight / lp.height;

    //make it full screen(
    lp.width = (int)(lp.width * propHeight);
    lp.height = (int)(lp.height * propHeight);

    frameLayout.setLayoutParams(lp);
    return frameLayout;

}

setCameraLayout 调用者

OnCreate 来自 Activity,然后是 surfaceChanged

 @Override
 public void surfaceChanged(SurfaceHolder holder, int format, int width,
                           int height) {
    if (getHolder().getSurface() == null) {
        return;
    }
    try {
        cameraManager.getCamera().stopPreview();
    } catch (Exception e) {
        // tried to stop a non-existent preview
    }

    try {
        Camera.Parameters cameraSettings = cameraManager.getCamera().getParameters();

        cameraSettings.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
        this.cameraView.setCameraLayout(mPreviewSize.width, mPreviewSize.height);
        cameraManager.getCamera().setParameters(cameraSettings);
        cameraManager.getCamera().setPreviewDisplay(holder);

        cameraManager.getCamera().startPreview();
    } catch (Exception e) {
        Log.d(TAG, "Error starting camera preview: " + e.getMessage());
    }
}

目标

手机上的全屏相机预览。

问题

现在我们得到了没有失真的预览,它是 GOOD! 并且它具有与 phone 相同的 height 以及也是 GOOD! . 但是!preview 的宽度大于phone width(当然)所以事实证明相机的中心不在手机的中心。我们考虑过的可能解决方案:

  1. move 布局左到负位置,使预览中心位于屏幕中心。
  2. crop 布局并仅绘制new preview 的中心,phone screens 应该可见

任何想法如何克服这个问题?

非常感谢! :)

【问题讨论】:

  • 嗨,你有机会分享你的整个代码吗?我遇到了同样的问题:/谢谢
  • 嘿,你可以看看这篇博文。 shakedos.com/2015/Aug/26/… 代码不是最新的,但应该可以解决您的问题
  • 谢谢。那是你的吗?
  • Its Shaked 的博客。他为我们做了一些关于这个问题的工作。

标签: java android camera android-camera android-framelayout


【解决方案1】:

我通过设置 SurfaceView 的重力成功地使预览居中:

FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(wid, hei);
lp.gravity = Gravity.CENTER;
mSv.setLayoutParams(lp);

代码sn-p取自here,有耐心破译的欢迎使用。我想我正在解决类似的问题(预览旋转、扭曲、适合/填充)

祝你好运。

【讨论】:

  • 嘿@seanpj。谢谢你的回答。一旦我在我的情况下验证这项工作,我就会接受你的回答。
  • 那篇 GitHub 文章试图解决我遇到的特定问题,因此它可能无法 100% 适合您的情况,但这个想法是相关的,请在此处查看“readme.md”:github.com/seanpjanson/AndyCam 如果您可以借鉴那里的一些想法。
  • 谢谢,顺便说一句。我希望在这周我们能够验证你的答案
  • 我们已经检查过了。有用!问题是如何将屏幕坐标转换为相机坐标直到打开。也许您对此也有一些想法?
  • 当我使用它时(很久以前)我没有使用坐标,基本上只有两个矩形:预览(CamVw.surfaceChanged 中的 wid/hei 值)和 wid/hei 出现的矩形从 Camera.Size prvwSz() 方法返回。假设您始终将一个中心放在另一个中心,我要解决的唯一问题是我的预览是否适合(侧面有黑色条纹)或填充屏幕(即平移和扫描),基本上全屏已填充并且部分预览“溢出”。
【解决方案2】:

如果我从代码 sn-ps 中理解,每次用户更改方向或旋转相机时,您都会重新实例化相机。有什么理由这样做吗?

为什么不将预览限制为仅纵向并具有可旋转的叠加视图?我很确定其他人(Instagram、Snapchat 等)就是这样做的。如果我能找到它,我将用这个例子进行编辑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-28
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    相关资源
    最近更新 更多