Example usage for android.hardware Camera setPreviewTexture

List of usage examples for android.hardware Camera setPreviewTexture

Introduction

In this page you can find the example usage for android.hardware Camera setPreviewTexture.

Prototype

public native final void setPreviewTexture(SurfaceTexture surfaceTexture) throws IOException;

Source Link

Document

Sets the SurfaceTexture to be used for live preview.

Usage

From source file:com.ezartech.ezar.videooverlay.ezAR.java

private void initCamera(Camera camera) {
    Camera.Parameters cameraParameters = camera.getParameters();

    defaultFocusMode = cameraParameters.getFocusMode();

    continousFocusSupported = cameraParameters.getSupportedFocusModes()
            .contains(Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
    focusAreaSupported = (cameraParameters.getMaxNumFocusAreas() > 0
            && Util.isSupported(Parameters.FOCUS_MODE_AUTO, cameraParameters.getSupportedFocusModes()));
    meteringAreaSupported = (cameraParameters.getMaxNumMeteringAreas() > 0);
    aeLockSupported = cameraParameters.isAutoExposureLockSupported();
    awbLockSupported = cameraParameters.isAutoWhiteBalanceLockSupported();

    if (continousFocusSupported) {
        cameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
    } else if (Util.isSupported(Parameters.FOCUS_MODE_AUTO, cameraParameters.getSupportedFocusModes())) {
        cameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
    }//from w  ww .  jav  a  2 s  . co  m

    //      camera.enableShutterSound(true);  //requires api 17
    int camWidth = isPortraitOrientation() ? cameraView.getHeight() : cameraView.getWidth();
    int camHt = isPortraitOrientation() ? cameraView.getWidth() : cameraView.getHeight();
    previewSizePair = selectSizePair(cameraParameters.getPreferredPreviewSizeForVideo(),
            cameraParameters.getSupportedPreviewSizes(), cameraParameters.getSupportedPictureSizes(), camWidth,
            camHt);

    Log.d(TAG, "preview size: " + previewSizePair.previewSize.width + ":" + previewSizePair.previewSize.height);

    cameraParameters.setPreviewSize(previewSizePair.previewSize.width, previewSizePair.previewSize.height);

    //commenting out; not used now
    //Camera.Size picSize = previewSizePair.pictureSize != null ? previewSizePair.pictureSize : previewSizePair.previewSize;
    //cameraParameters.setPictureSize(picSize.width,picSize.height);
    //Log.d(TAG, "picture size: " + picSize.width + ":" + picSize.height);

    camera.setParameters(cameraParameters);

    try {
        if (cameraView.getSurfaceTexture() != null) {
            camera.setPreviewTexture(cameraView.getSurfaceTexture());
        }
    } catch (IOException e) {
        Log.e(TAG, "Unable to attach preview to camera!", e);
    }
}