// Open the camera Camera camera = Camera.open(); // Create a surface for the preview SurfaceView surfaceView = new SurfaceView(context); SurfaceHolder holder = surfaceView.getHolder(); // Start the preview camera.setPreviewDisplay(holder); camera.startPreview(); // Set up the PictureCallback to handle the image data camera.takePicture(null, null, new PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { // Save the image to a file File pictureFile = new File(Environment.getExternalStorageDirectory(), "picture.jpg"); try { FileOutputStream fos = new FileOutputStream(pictureFile); fos.write(data); fos.close(); } catch (IOException e) { e.printStackTrace(); } } });
// Open the camera Camera camera = Camera.open(); // Create a surface for the preview SurfaceView surfaceView = new SurfaceView(context); SurfaceHolder holder = surfaceView.getHolder(); // Start the preview camera.setPreviewDisplay(holder); camera.startPreview(); // Set up the ShutterCallback to handle the timing between images camera.setShutterCallback(new ShutterCallback() { @Override public void onShutter() { // Pause for 2 seconds between captures try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } }); // Set up the PictureCallback to handle the image data camera.takePicture(null, null, new PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { // Save the image to a file File pictureFile = new File(Environment.getExternalStorageDirectory(), "picture.jpg"); try { FileOutputStream fos = new FileOutputStream(pictureFile); fos.write(data); fos.close(); // Take another picture camera.takePicture(null, null, this); } catch (IOException e) { e.printStackTrace(); } } });In both examples, the android.hardware.Camera package library is used.