import android.content.Context;
import android.graphics.ImageFormat;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CaptureRequest;
import android.media.ImageReader;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Surface;
import android.view.TextureView;
import android.widget.ImageView;

import java.util.Arrays;

public class MainActivity extends AppCompatActivity {



private CameraDevice mCameraDevice;
private TextureView mTextureView;
private CameraCaptureSession mPreviewCaptureSession;
private CaptureRequest.Builder mCaptureRequestBuilder;



private ImageReader mImageReader = ImageReader.newInstance(1920,1080, ImageFormat.JPEG, 1);

private TextureView.SurfaceTextureListener mSurfaceTextureListener = new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {connectCamera();}
@Override public void onSurfaceTextureSizeChanged(SurfaceTexture surface,int width,int height){}
@Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surface){return false;}
@Override public void onSurfaceTextureUpdated(SurfaceTexture surface){}
};


private CameraDevice.StateCallback mCameraDeviceStateCallback = new CameraDevice.StateCallback(){
@Override
public void onOpened(CameraDevice camera) {mCameraDevice = camera; startPreview();}

@Override
public void onDisconnected(CameraDevice camera) { camera.close(); mCameraDevice = null;}

@Override
public void onError(CameraDevice camera, int error) {camera.close(); mCameraDevice = null;}
};


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


mTextureView = (TextureView)findViewById(R.id.textureView);
mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
}


private void connectCamera() {
CameraManager cameraManager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);

try { ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA);
cameraManager.openCamera("0", mCameraDeviceStateCallback, null);}
catch (CameraAccessException e) { e.printStackTrace(); }}



CameraCaptureSession.StateCallback run = new CameraCaptureSession.StateCallback(){

@Override
public void onConfigured(CameraCaptureSession session) {

mPreviewCaptureSession = session;

try {mPreviewCaptureSession.setRepeatingRequest(mCaptureRequestBuilder.build(),null, null);}
catch (CameraAccessException e) {e.printStackTrace();}}
@Override
public void onConfigureFailed(CameraCaptureSession session) {}

};

private void startPreview() {
SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
surfaceTexture.setDefaultBufferSize(1920,1080);
Surface previewSurface = new Surface(surfaceTexture);

try {
mCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
mCaptureRequestBuilder.addTarget(previewSurface);
mCameraDevice.createCaptureSession(Arrays.asList(previewSurface,mImageReader.getSurface()),run, null);}

catch (CameraAccessException e) {e.printStackTrace();}}

}