public class CameraActivity extends Activity implements SurfaceHolder.Callback { private static final String TAG = CameraActivity.class.getSimpleName(); private static final SparseIntArray ORIENTATIONS = new SparseIntArray(); static { ORIENTATIONS.append(Surface.ROTATION_0, 90); ORIENTATIONS.append(Surface.ROTATION_90, 0); ORIENTATIONS.append(Surface.ROTATION_180, 270); ORIENTATIONS.append(Surface.ROTATION_270, 180); } private SurfaceView surfaceView; private SurfaceHolder surfaceHolder; private CameraManager cameraManager; private CameraDevice cameraDevice; private CameraCaptureSession cameraCaptureSession; private String cameraId; private ImageReader imageReader; private int imageId; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.camera_surface_view); surfaceView = (SurfaceView) findViewById(R.id.cameraSurfaceView); Button captureButton = (Button) findViewById(R.id.camera_view_capture_button); captureButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Start taking a new picture createCaptureSession(); } }); Button okButton = (Button) findViewById(R.id.camera_view_ok_button); okButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Go back to the activity that started the camera an return id of the taken image Intent intent = new Intent(CameraActivity.this,ProductEditorActivity.class); intent.putExtra(Constants.CAMERA_PICTURE_ID_EXTRA,imageId); startActivity(intent); finish(); } }); Button retryButton = (Button) findViewById(R.id.camera_view_retry_button); retryButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Start taking a new picture createCaptureSession(); } }); surfaceHolder = surfaceView.getHolder(); cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); cameraId = null; Size[] sizes = null; try{ Search available cameras String[] ids = cameraManager.getCameraIdList(); for(String id ids){ CameraCharacteristics cc = cameraManager.getCameraCharacteristics(id); Log.d(TAG,id + + cc.toString() ); if(cc.get(CameraCharacteristics.LENS_FACING) ==CameraCharacteristics.LENS_FACING_BACK){ cameraId = id; StreamConfigurationMap configs = cc.get( CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); sizes = configs.getOutputSizes(SurfaceHolder.class); break; } } } catch (CameraAccessException e) { Log.e(TAG,getCameraIdList() or getCameraCharacteristics(), e); } if((cameraId == null) (sizes == null)){ Log.d(TAG,no usable camera); finish(); } else{ final int width = sizes[0].getWidth(); final int height = sizes[0].getHeight(); surfaceHolder.setFixedSize(width,height); ImageReader erzeugen imageReader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 2); imageReader.setOnImageAvailableListener( new ImageReader.OnImageAvailableListener() { @Override public void onImageAvailable(ImageReader reader) { Log.d(TAG, setOnImageAvailableListener()); Image image = imageReader.acquireLatestImage(); final Image.Plane[] planes = image.getPlanes(); ByteBuffer buffer = planes[0].getBuffer(); saveJPG(buffer); image.close(); } }, null); } } @Override protected void onPause() { super.onPause(); if(cameraDevice != null){ if(cameraCaptureSession != null){ cameraCaptureSession.close(); cameraCaptureSession = null; } cameraDevice.close(); cameraDevice = null; } surfaceHolder.removeCallback(this); Log.d(TAG, onPause()); } @Override protected void onResume() { super.onResume(); surfaceHolder.addCallback(this); Log.d(TAG,onResume()); } @Override public void surfaceCreated(SurfaceHolder holder) { Log.d(TAG, surfaceCreated()); openCamera(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { Log.d(TAG, surfaceChanged()); } @Override public void surfaceDestroyed(SurfaceHolder holder) { Log.d(TAG, surfaceDestroyed()); } private void openCamera(){ try{ cameraManager.openCamera(cameraId, new CameraDevice.StateCallback() { @Override public void onOpened(CameraDevice camera) { Log.d(TAG, onOpened()); CameraActivity.this.cameraDevice = camera; createPreviewCaptureSession(); } @Override public void onDisconnected(CameraDevice camera) { Log.d(TAG,onDisconnected()); } @Override public void onError(CameraDevice camera, int error) { Log.d(TAG,onError()); } },null); }catch (CameraAccessException e){ Log.e(TAG,openCamera(),e); } } private void createPreviewCaptureSession(){ ListSurface outputs = new ArrayList(); outputs.add(surfaceHolder.getSurface()); try{ final CaptureRequest.Builder builder = cameraDevice. createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); Camera Configs builder.addTarget(surfaceHolder.getSurface()); builder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_AUTO); builder.set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_START); builder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH); cameraDevice.createCaptureSession(outputs, new CameraCaptureSession.StateCallback() { @Override public void onConfigured(CameraCaptureSession session) { try { session.setRepeatingRequest(builder.build(), null, null); CameraActivity.this.cameraCaptureSession = session; } catch (CameraAccessException e) { Log.e(TAG, capture(), e); } } @Override public void onConfigureFailed(CameraCaptureSession session) { Log.e(TAG, onConfigureFailed()); } }, null); } catch (CameraAccessException e) { Log.e(TAG,createPreviewCaptureSession(), e); } } private void createCaptureSession() { ListSurface outputs = new ArrayList(); Surface surface = imageReader.getSurface(); outputs.add(surface); try { final CaptureRequest.Builder builder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE); builder.addTarget(surface); Camera Configs builder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_AUTO); builder.set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_START); builder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH); int rotation = getWindowManager().getDefaultDisplay().getRotation(); builder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(rotation)); cameraDevice.createCaptureSession(outputs, new CameraCaptureSession.StateCallback() { @Override public void onConfigured(CameraCaptureSession session) { try { session.capture(builder.build(), new CameraCaptureSession.CaptureCallback() { @Override public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) { createPreviewCaptureSession(); } @Override public void onCaptureFailed(CameraCaptureSession session, CaptureRequest request, CaptureFailure failure) { createPreviewCaptureSession(); } }, null); CameraActivity.this.cameraCaptureSession = session; } catch (CameraAccessException e) { Log.e(TAG, capture(), e); } } @Override public void onConfigureFailed(CameraCaptureSession session) { Log.e(TAG, onConfigureFailed()); } }, null); } catch (CameraAccessException e) { Log.e(TAG, createPreviewCaptureSession(), e); } }