Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In onCreate() of my activity I add the following onClick listener to a button overlayed on my Preview SurfaceView (there are numerous example on the web for previewing):</p> <pre><code>ImageButton useOtherCamera = (ImageButton) findViewById(R.id.useOtherCamera); //if phone has only one camera, hide "switch camera" button if(Camera.getNumberOfCameras() == 1){ useOtherCamera.setVisibility(View.INVISIBLE); } else { useOtherCamera.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (inPreview) { camera.stopPreview(); } //NB: if you don't release the current camera before switching, you app will crash camera.release(); //swap the id of the camera to be used if(currentCameraId == Camera.CameraInfo.CAMERA_FACING_BACK){ currentCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT; } else { currentCameraId = Camera.CameraInfo.CAMERA_FACING_BACK; } camera = Camera.open(currentCameraId); //Code snippet for this method from somewhere on android developers, i forget where setCameraDisplayOrientation(CameraActivity.this, currentCameraId, camera); try { //this step is critical or preview on new camera will no know where to render to camera.setPreviewDisplay(previewHolder); } catch (IOException e) { e.printStackTrace(); } camera.startPreview(); } </code></pre> <p>On my test device the back camera has an ID of 0 and the front has an id of 1. I suggest using Camera.CameraInfo static variables for your camera id's rather than hard-coding values. I am sure that will only cause issues on other devices.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload