简体   繁体   中英

How to zoom camera using android camera2 API

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM