Example usage for android.graphics SurfaceTexture SurfaceTexture

List of usage examples for android.graphics SurfaceTexture SurfaceTexture

Introduction

In this page you can find the example usage for android.graphics SurfaceTexture SurfaceTexture.

Prototype

public SurfaceTexture(boolean singleBufferMode) 

Source Link

Document

Construct a new SurfaceTexture to stream images to a given OpenGL texture.

Usage

From source file:com.mienaikoe.deltamonitor.CameraWatcherService.java

private static SurfaceTexture getTexture() {
    int[] textures = new int[1];
    GLES20.glGenTextures(1, textures, 0);
    GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, textures[0]);
    return new SurfaceTexture(textures[0]);
}

From source file:com.macleod2486.androidswissknife.components.Flashlight.java

private void turnOnLight() {
    Log.i("Flashlight", "Toggling on light");

    try {//from  w  ww. j  av  a2 s .  co m
        cam = Camera.open();
        p = cam.getParameters();
        p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        cam.setParameters(p);
        SurfaceTexture mPreviewTexture = new SurfaceTexture(0);
        cam.setPreviewTexture(mPreviewTexture);
        cam.startPreview();
        torchOn = true;
    } catch (Exception ex) {
        torchOn = false;
        Log.e("Flashlight", ex.toString());
    }
}

From source file:com.wlanjie.streaming.camera.CameraView.java

@SuppressWarnings("WrongConstant")
public CameraView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    // Internal setup

    final View view = View.inflate(context, R.layout.texture_view, this);
    mGLSurfaceView = (GLSurfaceView) view.findViewById(R.id.gl_surface_view);
    mGLSurfaceView.setEGLContextClientVersion(2);
    mGLSurfaceView.setRenderer(this);
    mGLSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

    mTextureId = OpenGLUtils.getExternalOESTextureID();
    mSurfaceTexture = new SurfaceTexture(mTextureId);

    mCallbacks = new CallbackBridge();
    if (Build.VERSION.SDK_INT < 21) {
        mImpl = new Camera1(mCallbacks);
    } else if (Build.VERSION.SDK_INT < 23) {
        mImpl = new Camera2(mCallbacks, context);
    } else {//from   w ww . j  a  v a  2  s .  c  o m
        mImpl = new Camera2Api23(mCallbacks, context);
    }
    mImpl.setPreviewSurface(mSurfaceTexture);
    // Attributes
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CameraView, defStyleAttr,
            R.style.Widget_CameraView);
    mAdjustViewBounds = a.getBoolean(R.styleable.CameraView_android_adjustViewBounds, false);
    setFacing(a.getInt(R.styleable.CameraView_facing, FACING_BACK));
    String aspectRatio = a.getString(R.styleable.CameraView_aspectRatio);
    if (aspectRatio != null) {
        setAspectRatio(AspectRatio.parse(aspectRatio));
    } else {
        setAspectRatio(Constants.DEFAULT_ASPECT_RATIO);
    }
    setFlash(a.getInt(R.styleable.CameraView_flash, Constants.FLASH_AUTO));
    a.recycle();
    // Display orientation detector
    mDisplayOrientationDetector = new DisplayOrientationDetector(context) {
        @Override
        public void onDisplayOrientationChanged(int displayOrientation) {
            mImpl.setDisplayOrientation(displayOrientation);
        }
    };
}

From source file:havidarou.com.appgestosfoto.MainActivity.java

@Override
public void onResume() {
    super.onResume();
    try {/*from w  w w.  ja  va2 s  .  c om*/
        camera.setPreviewTexture(new SurfaceTexture(10));
    } catch (IOException e) {
    }

}

From source file:com.sveder.cardboardpassthrough.MainActivity.java

public void startCamera(int texture) {
    surface = new SurfaceTexture(texture);
    surface.setOnFrameAvailableListener(this);

    camera = Camera.open();/*from ww  w.  j  av a2  s  .c o m*/

    try {
        camera.setPreviewTexture(surface);
        camera.startPreview();
    } catch (IOException ioe) {
        Log.w("MainActivity", "CAM LAUNCH FAILED");
    }
}

From source file:com.ezartech.ezar.flashlight.Flashlight.java

private void updateLight(int newLightState, final CallbackContext callbackContext) {
    Camera releaseableCamera = null;/* w w  w .java 2  s. c o m*/

    try {
        lightState = newLightState;

        if (localPreviewCamera == null) {
            localPreviewCamera = Camera.open(cameraId);
        }
        Parameters parameters;
        parameters = localPreviewCamera.getParameters();
        parameters.setFlashMode(
                newLightState == LIGHT_ON ? Parameters.FLASH_MODE_TORCH : Parameters.FLASH_MODE_OFF);
        localPreviewCamera.setParameters(parameters);

        if (localPreviewCamera != null) {
            if (newLightState == LIGHT_ON) {
                try {
                    localPreviewCamera.startPreview();
                    if (hiddenSurfaceTexture == null) {
                        hiddenSurfaceTexture = new SurfaceTexture(0);
                    }
                    localPreviewCamera.setPreviewTexture(hiddenSurfaceTexture);
                } catch (Exception ex) {
                    callbackContext.error("Unable to start light.");
                }
            } else {
                localPreviewCamera.stopPreview();
                releaseableCamera = localPreviewCamera;
                localPreviewCamera = null;
            }
        }

    } finally {
        if (releaseableCamera != null) {
            releaseableCamera.release();
        }
    }

    if (callbackContext != null) {
        callbackContext.success();
    }
}

From source file:com.Yamate.Camera.Camera.java

/**
 * Creates a new {@link CameraCaptureSession} for camera preview.
 *///from w  ww  .  j a  v  a  2s  . c om
private void createCameraPreviewSession() {
    try {
        mSurfaceTexture = new SurfaceTexture(mTextureID);
        mSurfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
        Surface surface = new Surface(mSurfaceTexture);
        List<Surface> surfaces = new ArrayList<>();
        //assert texture != null;

        // We configure the size of default buffer to be the size of camera preview we want.
        //mSurfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());

        // This is the output Surface we need to start preview.
        //Surface surface = new Surface(mSurfaceTexture);

        // We set up a CaptureRequest.Builder with the output Surface.
        mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
        mPreviewRequestBuilder.addTarget(surface);
        surfaces.add(surface);
        surfaces.add(mImageReader.getSurface());
        surfaces.add(mMediaRecorder.getSurface());
        ///mMediaRecorder.setInputSurface(surface);
        mPreviewRequestBuilder.addTarget(mMediaRecorder.getSurface());
        // Here, we create a CameraCaptureSession for camera preview.
        mCameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {

            @Override
            public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
                // The camera is already closed
                if (null == mCameraDevice) {
                    return;
                }

                // When the session is ready, we start displaying the preview.
                mCaptureSession = cameraCaptureSession;
                try {
                    // Auto focus should be continuous for camera preview.
                    mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                            CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
                    // Flash is automatically enabled when necessary.
                    setAutoFlash(mPreviewRequestBuilder);

                    // Finally, we start displaying the camera preview.
                    mPreviewRequest = mPreviewRequestBuilder.build();
                    mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, mBackgroundHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
                showToast("Failed");
            }
        }, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }

    mInitialized = true;
}

From source file:com.example.aschere.cdhprototype2.Camera2RawFragment.java

/**
 * Creates a new {@link CameraCaptureSession} for camera preview.
 * <p/>//from w  w w. j  a va2 s  .com
 * Call this only with {@link #mCameraStateLock} held.
 */
private void createCameraPreviewSessionLocked() {
    try {
        Log.i(TAG, "create camera preview called");
        SurfaceTexture texture = new SurfaceTexture(2);

        // This is the output Surface we need to start preview.
        Surface surface = new Surface(texture);

        // We set up a CaptureRequest.Builder with the output Surface.
        if (mCameraDevice == null) {
            setUpCameraOutputs();
            openCamera();
        }
        if (mCameraDevice != null) {
            mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
            mPreviewRequestBuilder.addTarget(surface);

            // Here, we create a CameraCaptureSession for camera preview.
            mCameraDevice.createCaptureSession(Arrays.asList(surface, mJpegImageReader.get().getSurface(),
                    mRawImageReader.get().getSurface()), new CameraCaptureSession.StateCallback() {
                        @Override
                        public void onConfigured(CameraCaptureSession cameraCaptureSession) {
                            synchronized (mCameraStateLock) {
                                // The camera is already closed
                                if (null == mCameraDevice) {
                                    return;
                                }

                                try {
                                    setup3AControlsLocked(mPreviewRequestBuilder);
                                    // Finally, we start displaying the camera preview.
                                    cameraCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(),
                                            mPreCaptureCallback, mBackgroundHandler);
                                    mState = STATE_PREVIEW;
                                } catch (CameraAccessException | IllegalStateException e) {
                                    e.printStackTrace();
                                    return;
                                }
                                // When the session is ready, we start displaying the preview.
                                mCaptureSession = cameraCaptureSession;
                            }
                        }

                        @Override
                        public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
                            showToast("Failed to configure camera.");
                        }
                    }, mBackgroundHandler);
        }
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

From source file:com.aimfire.demo.CamcorderActivity.java

@Override
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
    if (BuildConfig.DEBUG)
        Log.d(TAG, "onSurfaceCreated");

    /*//from   ww w. j  av  a 2 s. co  m
     * Set up the texture blitter that will be used for on-screen display.  This
     * is *not* applied to the recording, because that uses a separate shader.
     */
    mFullScreen = new FullFrameRect(new Texture2dProgram(Texture2dProgram.ProgramType.TEXTURE_EXT));

    mTextureId = mFullScreen.createTextureObject();

    /*
     * create a SurfaceTexture, with an external texture, in this EGL context.  
     * we don't have a Looper in this thread -- GLSurfaceView doesn't create one 
     * -- so the frame available messages will arrive on the main thread.
     */
    mSurfaceTexture = new SurfaceTexture(mTextureId);

    /*
     * Tell the UI thread to enable the camera preview.
     */
    mCameraHandler.sendMessage(mCameraHandler
            .obtainMessage(CamcorderActivity.CameraHandler.MSG_SET_SURFACE_TEXTURE, mSurfaceTexture));
}