Example usage for android.hardware Camera takePicture

List of usage examples for android.hardware Camera takePicture

Introduction

In this page you can find the example usage for android.hardware Camera takePicture.

Prototype

public final void takePicture(ShutterCallback shutter, PictureCallback raw, PictureCallback jpeg) 

Source Link

Document

Equivalent to
takePicture(Shutter, raw, null, jpeg)
.

Usage

From source file:com.googlecode.android_scripting.facade.CameraFacade.java

private void takePicture(final File file, final BooleanResult takePictureResult, final Camera camera)
        throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    camera.takePicture(null, null, new PictureCallback() {
        @Override// w  w  w  .j a va2 s.c  om
        public void onPictureTaken(byte[] data, Camera camera) {
            if (!FileUtils.makeDirectories(file.getParentFile(), 0755)) {
                takePictureResult.mmResult = false;
                return;
            }
            try {
                FileOutputStream output = new FileOutputStream(file);
                output.write(data);
                output.close();
                takePictureResult.mmResult = true;
            } catch (FileNotFoundException e) {
                Log.e("Failed to save picture.", e);
                takePictureResult.mmResult = false;
                return;
            } catch (IOException e) {
                Log.e("Failed to save picture.", e);
                takePictureResult.mmResult = false;
                return;
            } finally {
                latch.countDown();
            }
        }
    });
    latch.await();
}

From source file:openscience.crowdsource.video.experiments.MainActivity.java

/**
 * @return absolute path to image/*from  w w w  .  jav a2s . com*/
 */
private void captureImageFromCameraPreviewAndPredict(final boolean isPredictionRequired) {
    synchronized (camera) {
        camera.takePicture(null, null, new Camera.PictureCallback() {
            @Override
            public void onPictureTaken(byte[] data, Camera camera) {
                try {
                    createDirIfNotExist(externalSDCardOpenscienceTmpPath);
                    String takenPictureFilPath = String.format(
                            externalSDCardOpenscienceTmpPath + File.separator + "%d.jpg",
                            System.currentTimeMillis());
                    FileOutputStream fos = new FileOutputStream(takenPictureFilPath);
                    fos.write(data);
                    fos.close();
                    stopCameraPreview();

                    rotateImageAccoridingToOrientation(takenPictureFilPath);

                    AppConfigService.updateActualImagePath(takenPictureFilPath);
                    if (isPredictionRequired) {
                        predictImage(takenPictureFilPath);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    AppLogger.logMessage("Error on image capture " + e.getLocalizedMessage());

                } catch (OutOfMemoryError e) {
                    e.printStackTrace();
                    AppLogger.logMessage("Error on image capture " + e.getLocalizedMessage());
                }
            }
        });
    }
}