title: Switching from Android Camera API to Camera2 API
body: <p>I'm using a tablet running Android 5.1. I have implemented a service using the <code>Camera</code> API for recording the front cam in the background. The code is shown below. Now I would like to change it to <code>Camera2</code> API. In addition I would like to manually set the apperture and the exposure time (which is only possible with <code>Camera2</code> API but not <code>Camera</code> API I think). How can I adapt my code?</p>
  
  <pre><code>import android.app.Notification;
  import android.app.PendingIntent;
  import android.app.Service;
  import android.content.Context;
  import android.content.Intent;
  import android.graphics.PixelFormat;
  import android.hardware.Camera;
  import android.media.CamcorderProfile;
  import android.media.MediaRecorder;
  import android.os.IBinder;
  import android.support.v7.app.NotificationCompat;
  import android.view.Gravity;
  import android.view.SurfaceHolder;
  import android.view.SurfaceView;
  import android.view.WindowManager;
  import inf.ethz.ch.affectivestudy.MainActivity;
  
  public class RecorderService extends Service implements SurfaceHolder.Callback {
      private WindowManager windowManager;
      private SurfaceView surfaceView;
      private Camera camera = null;
      private MediaRecorder mediaRecorder = null;
  
      @Override
      public void onCreate() {
          windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
  
          surfaceView = new SurfaceView(this);
          WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
                  1, 1,
                  WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                  WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                  PixelFormat.TRANSLUCENT
          );
          layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
          windowManager.addView(surfaceView, layoutParams);
          surfaceView.getHolder().addCallback(this);
      }
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          Intent notificationIntent = new Intent(this, MainActivity.class);
  
          PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                  notificationIntent, 0);
  
          Notification notification = new NotificationCompat.Builder(this)
                  //.setSmallIcon(R.mipmap.app_icon)
                  .setContentTitle("Background Video Recorder")
                  .setContentText("")
                  .setContentIntent(pendingIntent).build();
  
          startForeground(MainActivity.NOTIFICATION_ID_RECORDER_SERVICE, notification);
  
          return Service.START_NOT_STICKY;
      }
  
      @Override
      public void surfaceCreated(SurfaceHolder surfaceHolder) {
          camera = Camera.open(1);
          mediaRecorder = new MediaRecorder();
          camera.unlock();
  
          mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
          mediaRecorder.setCamera(camera);
          mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
          mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
          mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_720P));
  
          mediaRecorder.setOutputFile("/storage/emulated/0/Study/Camera/test.mp4");
  
          try { mediaRecorder.prepare(); } catch (Exception ignored) {}
          mediaRecorder.start();
      }
  
      @Override
      public void onDestroy() {
          mediaRecorder.stop();
          mediaRecorder.reset();
          mediaRecorder.release();
  
          camera.lock();
          camera.release();
  
          windowManager.removeView(surfaceView);
      }
  
      @Override
      public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) {}
  
      @Override
      public void surfaceDestroyed(SurfaceHolder surfaceHolder) {}
  
      @Override
      public IBinder onBind(Intent intent) { return null; }
  }
  </code></pre>