Android相机 - 录制视频时预览会放大

时间:2016-09-02 16:03:36

标签: android video android-camera surfaceview video-recording

我一直试图弄清楚这一点,但出于某种原因,当我开始使用相机录制视频时,预览会放大。我从以下示例中获得以下代码:

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    Camera.Parameters myParameters = mCamera.getParameters();
    List<Camera.Size> sizes = myParameters.getSupportedPreviewSizes();
    Camera.Size myBestSize = getBestPreviewSize(sizes, width, height);

    if (myBestSize != null) {
        myParameters.setPreviewSize(myBestSize.width, myBestSize.height);
        myParameters.setVideoStabilization(false);
        mCamera.setParameters(myParameters);
        mCamera.startPreview();
        mCamera.unlock();
    }
}

private Camera.Size getBestPreviewSize(List<Camera.Size> sizes, int width, int height) {
    final double ASPECT_TOLERANCE = 0.05;
    double targetRatio = (double) width / height;

    if (sizes == null) return null;

    Camera.Size optimalSize = null;

    double minDiff = Double.MAX_VALUE;

    int targetHeight = height;

    // Find size
    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;
}

但是在录制视频时预览仍会放大。我尝试手动将预览大小设置为1280x720,但是,它并不适用于我测试的所有设备,有些显示黑色SurfaceView。

录制视频时有没有办法阻止预览放大? StackOverflow的解决方案似乎都不起作用。

编辑:我尝试使用自己的onMesaure方法创建了一个自定义SurfaceView,但现在我启动MediaRecorder时出现-19错误。 (无视赏金中的-16)

2 个答案:

答案 0 :(得分:0)

我对你所说的缩放类型感到困惑,可能是软件缩放,也可能是自动对焦,所以我会提出不同的潜在提示,以便找到最终解决方案:

  1. 实现mCamera.setZoomChangeListener,例如:

    mCamera.setZoomChangeListener(new Camera.OnZoomChangeListener() {
        @Override public void onZoomChange(int i, boolean b, Camera camera) {
            Log.e("StackOverflow","Zoom just changed !");
            // Inspect things from here
        }
    });
    
  2. 禁用平滑缩放:

    mCamera.stopSmoothZoom(); // My device doesn't support it, but maybe your device does
    
  3. 检查所有人的Camera.Parameters:

    Log.e("StackOverflow", "All Camera parameters :");
    for(String s : mCamera.getParameters().flatten().split(";"))
        Log.e("StackOverflow", s + "\n");
    
  4. 然后切换任何可能与缩放相关的内容,例如:

    Camera.Parameters parameters = mCamera.getParameters();
    parameters.setZoom(100); // My device support some values in the range 100 | 400
    parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY);
    parameters.setVideoStabilization(false);
    
    // Don't forget to add the new parameters to the camera :
    mCamera.setParameters(parameters);
    
  5. 此致

答案 1 :(得分:0)

你可能有setVideoStabilization(true)。在高分辨率,例如4K,如果你启用它,它将创建这个跳跃缩放。