如何使用android camera2 API缩放相机

时间:2018-09-04 03:27:43

标签: android zoom android-camera2

我正在为我的应用程序使用Camera2 Api。我有一个放大功能。我可以使用捏手势来放大和缩小。但是如何使用搜索栏实现相同的功能。

1 个答案:

答案 0 :(得分:1)

要缩放的代码:

int mProgress;
{
    minZoom = getMinZoom();
    maxZoom = getMaxZoom() - 1;
    final int zoomStep = 1;

    seekBarCardZoom.setMax(Math.round(maxZoom - minZoom));
    seekBarCardZoom.setOnSeekBarChangeListener(
            new SeekBar.OnSeekBarChangeListener()
            {
                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    setCurrentZoom(Math.round(minZoom + (mProgress * zoomStep)));//not tested
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {}

                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    setCurrentZoom(Math.round(minZoom + (progress * zoomStep)));
                    if(fromUser) mProgress = progress;
                }
            }
    );
}

并在我的CameraManager中使用了以下代码,该代码扩展了BaseCameraManager:

@Override
public float getCurrentZoom() {
    return zoomLevel;
}

@Override
public void setCurrentZoom(float zoomLevel) {
    Rect zoomRect = getZoomRect(zoomLevel);
    if(zoomRect != null) {
        try {
            //you can try to add the synchronized object here 
            previewRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoomRect);
            captureSession.setRepeatingRequest(previewRequestBuilder.build(), captureCallback, backgroundHandler);
        } catch (Exception e) {
            Log.e(TAG, "Error updating preview: ", e);
        }
        this.zoomLevel = (int) zoomLevel;
    }
}

private Rect getZoomRect(float zoomLevel) {
    try {
        CameraCharacteristics characteristics = manager.getCameraCharacteristics(this.currentCameraId);
        float maxZoom = (characteristics.get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM)) * 10;
        Rect activeRect = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
        if((zoomLevel <= maxZoom) && (zoomLevel > 1)) {
            int minW = (int) (activeRect.width() / maxZoom);
            int minH = (int) (activeRect.height() / maxZoom);
            int difW = activeRect.width() - minW;
            int difH = activeRect.height() - minH;
            int cropW = difW / 100 * (int) zoomLevel;
            int cropH = difH / 100 * (int) zoomLevel;
            cropW -= cropW & 3;
            cropH -= cropH & 3;
            return new Rect(cropW, cropH, activeRect.width() - cropW, activeRect.height() - cropH);
        } else if(zoomLevel == 0){
            return new Rect(0, 0, activeRect.width(), activeRect.height());
        }
        return null;
    } catch (Exception e) {
        Log.e(TAG, "Error during camera init");
        return null;
    }
}

@Override
public float getMaxZoom() {
    try {
        return (manager.getCameraCharacteristics(this.currentCameraId).get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM)) * 10;
    } catch (Exception e) {
        Log.e(TAG, "Error during camera init");
        return -1;
    }
}