使用camera2时获取当前的缩放值

时间:2019-03-28 08:33:56

标签: android android-camera android-camera2

使用Legacy Camera API(series.dataLabels.formatter),我可以使用下一行获取当前的缩放级别

  series: [{
    name: 'NSV',
    color: 'rgba(165,170,217,1)',
    data: [38891, 35098, 21296, 19345, 19092],
    pointPadding: 0.3,
    pointPlacement: -0.2
  }, {
    name: 'Margin',
    color: 'rgba(126,86,134,.9)',
    data: [8594.911, 6422.934, 5302.704, 580.35, 3417.468],
    pointPadding: 0.4,
    pointPlacement: -0.2,
    dataLabels: {
     formatter: function() {
      var chart = this.series.chart,
        percentage = (this.y / chart.series[0].points[this.point.index].y) * 100;

        return percentage.toFixed(1) + '%';
     }
    }
  }]

如何使用Camera2 API(android.hardware.Camera)?

Camera.Parameters p = mCamera.getParameters();
int zoom = p.getZoomRatios().get(p.getZoom());

1 个答案:

答案 0 :(得分:0)

我发现,了解当前缩放的最简单方法实际上是在更改它时始终对其进行跟踪。

下一个缩放控制器可用于计算新缩放和获取当前缩放,而实际上这只是传递给.set()方法的缓存值:

public final class Zoom
{
    private static final float DEFAULT_ZOOM_FACTOR = 1.0f;

    @NonNull
    private final Rect mCropRegion = new Rect();

    public final float maxZoom;

    private float mCurrentZoomFactor = Zoom.DEFAULT_ZOOM_FACTOR;

    @Nullable
    private final Rect mSensorSize;

    public final boolean hasSupport;

    public Zoom(@NonNull final CameraCharacteristics characteristics)
    {
        this.mSensorSize = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);

        if (this.mSensorSize == null)
        {
            this.maxZoom = Zoom.DEFAULT_ZOOM_FACTOR;
            this.hasSupport = false;
            return;
        }

        final Float value = characteristics.get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM);

        this.maxZoom = ((value == null) || (value < Zoom.DEFAULT_ZOOM_FACTOR))
                ? Zoom.DEFAULT_ZOOM_FACTOR
                : value;

        this.hasSupport = (Float.compare(this.maxZoom, Zoom.DEFAULT_ZOOM_FACTOR) > 0);
    }

    public void set(@NonNull final CaptureRequest.Builder builder, final float newZoom)
    {
        if (this.hasSupport == false)
        {
            return;
        }

        this.mCurrentZoomFactor = MathUtils.clamp(newZoom, Zoom.DEFAULT_ZOOM_FACTOR, this.maxZoom);

        final int centerX = this.mSensorSize.width() / 2;
        final int centerY = this.mSensorSize.height() / 2;
        final int deltaX  = (int)((0.5f * this.mSensorSize.width()) / this.mCurrentZoomFactor);
        final int deltaY  = (int)((0.5f * this.mSensorSize.height()) / this.mCurrentZoomFactor);

        this.mCropRegion.set(centerX - deltaX,
                centerY - deltaY,
                centerX + deltaX,
                centerY + deltaY);

        builder.set(CaptureRequest.SCALER_CROP_REGION, this.mCropRegion);
    }

    public float get()
    {
        return this.mCurrentZoomFactor;
    }
}

在调用zoom.set(...)之后,使用下一个代码段来提交新的缩放因子并使新的缩放因子生效到活动的摄像机会话中。无论是在活动预览阶段还是在拍摄照片时,都需要完成捕获阶段:

yourCameraSession.setRepeatingRequest(builder.build(), yourPreviewOrCaptureSessionCallback, yourCameraThreadHandler);