Example usage for android.hardware Camera startPreview

List of usage examples for android.hardware Camera startPreview

Introduction

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

Prototype

public native final void startPreview();

Source Link

Document

Starts capturing and drawing preview frames to the screen.

Usage

From source file:Main.java

public static void open(Context context) {
    Camera cam = Camera.open();
    Camera.Parameters p = cam.getParameters();
    p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    cam.setParameters(p);/*from  w w w  . j av a 2 s  .c  om*/
    cam.startPreview();

}

From source file:Main.java

public static Camera openFlightLight() {
    try {// ww w  .  java  2s  .co  m
        Camera camera = Camera.open();
        if (null != camera) {
            Parameters parameters = camera.getParameters();
            List<String> list = parameters.getSupportedFlashModes();
            for (String string : list) {
                if (Parameters.FLASH_MODE_TORCH.equals(string)) {
                    parameters.setFlashMode(string);
                    camera.setParameters(parameters);
                    camera.startPreview();
                    return camera;
                }
            }
        }
    } catch (Exception e) {
    }
    return null;
}

From source file:com.octopepper.mediapickerinstagram.commons.cameraview.api14.Camera1.java

public void takePictureInternal() {
    mCamera.takePicture(null, null, null, new Camera.PictureCallback() {
        @Override// w  ww  .  ja  va2 s.  c  o  m
        public void onPictureTaken(byte[] data, Camera camera) {
            mCallback.onPictureTaken(data);
            camera.startPreview();
        }
    });
}

From source file:io.mariachi.allianzvision.camera.api14.Camera1.java

public void takePictureInternal() {
    mCamera.takePicture(null, null, null, new Camera.PictureCallback() {
        @Override//from   w  w  w .  j  av  a  2  s .  com
        public void onPictureTaken(byte[] data, Camera camera) {
            mCallback.onPictureTaken(data);
            camera.cancelAutoFocus();
            camera.startPreview();
        }
    });
}

From source file:com.personal.xiri.only.common.camera.Camera1.java

void takePictureInternal() {
    if (!isPictureCaptureInProgress.getAndSet(true)) {
        mCamera.takePicture(null, null, null, new Camera.PictureCallback() {
            @Override/*from   w ww.  j a  va 2s .com*/
            public void onPictureTaken(byte[] data, Camera camera) {
                isPictureCaptureInProgress.set(false);
                mCallback.onPictureTaken(data);
                camera.cancelAutoFocus();
                camera.startPreview();
            }
        });
    }
}

From source file:com.google.android.cameraview.Camera1.java

void takePictureInternal() {
    if (!isPictureCaptureInProgress.getAndSet(true)) {
        mCamera.takePicture(null, null, null, new Camera.PictureCallback() {
            @Override//from www. j  a va2  s  .c o m
            public void onPictureTaken(byte[] data, Camera camera) {
                isPictureCaptureInProgress.set(false);
                mCallback.onPictureTaken(data);
                if (!isCameraOpened())
                    return;
                camera.cancelAutoFocus();
                camera.startPreview();
            }
        });
    }
}

From source file:com.example.android.animationsdemo.CameraActivity.java

private void startFocus(int x, int y) {
    if (mCamera != null) {
        mCamera.cancelAutoFocus();//from ww  w  .j  a v  a2 s.  co m
        Rect focusRect = calculateTapArea(x, y);

        Camera.Parameters parameters = mCamera.getParameters();
        if (parameters.getFocusMode() != Camera.Parameters.FOCUS_MODE_AUTO) {
            parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
        }
        if (parameters.getMaxNumFocusAreas() > 0) {
            List<Camera.Area> mylist = new ArrayList<Camera.Area>();
            mylist.add(new Camera.Area(focusRect, 1000));
            parameters.setFocusAreas(mylist);
        }

        try {
            mCamera.cancelAutoFocus();
            mCamera.setParameters(parameters);
            mCamera.startPreview();
            mCamera.autoFocus(new Camera.AutoFocusCallback() {
                @Override
                public void onAutoFocus(boolean success, Camera camera) {
                    if (camera.getParameters()
                            .getFocusMode() != Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) {
                        Camera.Parameters parameters = camera.getParameters();
                        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
                        if (parameters.getMaxNumFocusAreas() > 0) {
                            parameters.setFocusAreas(null);
                        }
                        camera.setParameters(parameters);
                        camera.startPreview();
                    }
                }
            });
        } catch (Exception e) {
            DKLog.e(TAG, Trace.getCurrentMethod() + e.toString());
        }
    }
}

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

@Rpc(description = "Take a picture and save it to the specified path.", returns = "A map of Booleans autoFocus and takePicture where True "
        + "indicates success. cameraId also included.")
public Bundle cameraCapturePicture(@RpcParameter(name = "targetPath") final String targetPath,
        @RpcParameter(name = "useAutoFocus") @RpcDefault("true") Boolean useAutoFocus,
        @RpcParameter(name = "cameraId", description = "Id of camera to use. SDK 9") @RpcDefault("0") Integer cameraId)
        throws Exception {
    final BooleanResult autoFocusResult = new BooleanResult();
    final BooleanResult takePictureResult = new BooleanResult();
    Camera camera = openCamera(cameraId);
    if (camera == null) {
        String msg = String.format("can't initialize camera id %d, try to use another id", cameraId);
        Log.e(msg);//from w  w w  . j av a2s. c om

        Bundle result = new Bundle();
        result.putInt("cameraId", cameraId);
        result.putBoolean("autoFocus", false);
        result.putBoolean("takePicture", false);
        result.putString("reason", msg + ", see logcat for details");
        return result;
    }
    Parameters prm = camera.getParameters();
    camera.setParameters(prm);

    try {
        Method method = camera.getClass().getMethod("setDisplayOrientation", int.class);
        method.invoke(camera, 90);
    } catch (Exception e) {
        Log.e(e);
    }

    try {
        FutureActivityTask<SurfaceHolder> previewTask = setPreviewDisplay(camera);
        camera.startPreview();
        if (useAutoFocus) {
            autoFocus(autoFocusResult, camera);
        }
        takePicture(new File(targetPath), takePictureResult, camera);
        previewTask.finish();
    } catch (Exception e) {
        Log.e(e);
    } finally {
        camera.release();
    }

    Bundle result = new Bundle();
    result.putBoolean("autoFocus", autoFocusResult.mmResult);
    result.putBoolean("takePicture", takePictureResult.mmResult);
    result.putInt("cameraId", cameraId);
    return result;
}

From source file:com.android.launcher3.Utilities.java

public static void turnOnFlashLight(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        CameraManager camManager = (CameraManager) context.getApplicationContext()
                .getSystemService(Context.CAMERA_SERVICE);
        String cameraId = null;//from   w ww  .  j a  v  a2  s. co  m
        try {
            cameraId = camManager.getCameraIdList()[0];
            camManager.setTorchMode(cameraId, true);
            isFlashLightOn = true;
        } catch (CameraAccessException e) {
            isFlashLightOn = false;
            e.printStackTrace();
        }
    } else {
        try {
            if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
                Camera cam = Camera.open();
                Camera.Parameters p = cam.getParameters();
                p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                cam.setParameters(p);
                cam.startPreview();
                isFlashLightOn = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            isFlashLightOn = false;
        }
    }
}

From source file:jp.co.cyberagent.android.gpuimage.sample.activity.ActivityCamera.java

License:asdf

private void takePicture() {
    // TODO get a size that is about the size of the screen
    Camera.Parameters params = mCameraLoader.mCameraInstance.getParameters();
    params.setPictureSize(1280, 960);/*  w ww  . j  a v a2 s  . c om*/
    params.setRotation(90);
    mCameraLoader.mCameraInstance.setParameters(params);
    for (Camera.Size size2 : mCameraLoader.mCameraInstance.getParameters().getSupportedPictureSizes()) {
        Log.i("ASDF", "Supported: " + size2.width + "x" + size2.height);
    }
    mCameraLoader.mCameraInstance.takePicture(null, null, new Camera.PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, final Camera camera) {

            final File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
            if (pictureFile == null) {
                Log.d("ASDF", "Error creating media file, check storage permissions");
                return;
            }

            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();
            } catch (FileNotFoundException e) {
                Log.d("ASDF", "File not found: " + e.getMessage());
            } catch (IOException e) {
                Log.d("ASDF", "Error accessing file: " + e.getMessage());
            }

            data = null;
            Bitmap bitmap = BitmapFactory.decodeFile(pictureFile.getAbsolutePath());
            // mGPUImage.setImage(bitmap);
            final GLSurfaceView view = (GLSurfaceView) findViewById(R.id.surfaceView);
            view.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
            mGPUImage.saveToPictures(bitmap, "GPUImage", System.currentTimeMillis() + ".jpg",
                    new OnPictureSavedListener() {

                        @Override
                        public void onPictureSaved(final Uri uri) {
                            pictureFile.delete();
                            camera.startPreview();
                            view.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
                        }
                    });
        }
    });
}