How to zoom camera using android camera2 API

夙愿已清 提交于 2020-07-08 01:55:29

问题


I m using Camera2 Api for my app. I have a zoom in out function. I am able to zoom in and zoom out using pinch gesture. But how do I implement the same using a seekbar.


回答1:


The code to zoom:

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;
                }
            }
    );
}

And used this code in my CameraManager which extends 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;
    }
}


来源:https://stackoverflow.com/questions/52158395/how-to-zoom-camera-using-android-camera2-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!