Example usage for android.content Context CAMERA_SERVICE

List of usage examples for android.content Context CAMERA_SERVICE

Introduction

In this page you can find the example usage for android.content Context CAMERA_SERVICE.

Prototype

String CAMERA_SERVICE

To view the source code for android.content Context CAMERA_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.hardware.camera2.CameraManager for interacting with camera devices.

Usage

From source file:com.ape.camera2raw.Camera2RawFragment.java

/**
 * Opens the camera specified by {@link #mCameraId}.
 *///w w  w. j a  va  2s  .  co  m
private void openCamera() {
    if (!setUpCameraOutputs()) {
        return;
    }
    if (!hasAllPermissionsGranted()) {
        requestCameraPermissions();
        return;
    }

    Activity activity = getActivity();
    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
    try {
        // Wait for any previously running session to finish.
        if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
            throw new RuntimeException("Time out waiting to lock camera opening.");
        }

        String cameraId;
        Handler backgroundHandler;
        synchronized (mCameraStateLock) {
            cameraId = mCameraId;
            backgroundHandler = mBackgroundHandler;
        }

        // Attempt to open the camera. mStateCallback will be called on the background handler's
        // thread when this succeeds or fails.
        manager.openCamera(cameraId, mStateCallback, backgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        throw new RuntimeException("Interrupted while trying to lock camera opening.", e);
    }
}

From source file:com.quectel.camera2test.Camera2RawFragment.java

/**
 * Opens the camera specified by {@link #mCameraId}.
 *///from   ww  w .  j  a  v  a2s .  c o m
@SuppressWarnings("MissingPermission")
private void openCamera() {
    if (!setUpCameraOutputs()) {
        return;
    }
    if (!hasAllPermissionsGranted()) {
        requestCameraPermissions();
        return;
    }

    Activity activity = getActivity();
    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
    try {
        // Wait for any previously running session to finish.
        if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
            throw new RuntimeException("Time out waiting to lock camera opening.");
        }

        String cameraId;
        Handler backgroundHandler;
        synchronized (mCameraStateLock) {
            cameraId = mCameraId;
            backgroundHandler = mBackgroundHandler;
        }

        // Attempt to open the camera. mStateCallback will be called on the background handler's
        // thread when this succeeds or fails.
        manager.openCamera(cameraId, mStateCallback, backgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        throw new RuntimeException("Interrupted while trying to lock camera opening.", e);
    }
}

From source file:com.example.camera2apidemo.Camera2BasicFragment.java

/**
 * Opens the camera specified by {@link Camera2BasicFragment#mCameraId}.
 *//*from   w w w  .  ja va 2s.  c  o  m*/
private void openCamera(int width, int height) {
    if (ContextCompat.checkSelfPermission(getActivity(),
            Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
        requestCameraPermission();
        return;
    }
    setUpCameraOutputs(width, height); // 
    configureTransform(width, height); // ???

    //        ?CameraId???CameraIdCamera?CameraDevice
    Activity activity = getActivity();// ??CameraManager
    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
    try {
        // ??, 2500?
        if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
            throw new RuntimeException("Time out waiting to lock camera opening.");
        }
        //            , ??CameraId
        //            ?CameraDeviceStateCallback??CameraDevice
        //            ?Handler?null?
        manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        throw new RuntimeException("Interrupted while trying to lock camera opening.", e);
    }
}

From source file:com.example.abrahamsofer.ident.Camera2Fragment.java

/**
 * Capture a still picture. This method should be called when we get a response in
 * {@link #mCaptureCallback} from both {@link #lockFocus()}.
 *//*from w  w w  .  jav  a2  s .  c  o  m*/
@TargetApi(Build.VERSION_CODES.M)
private void captureStillPicture() {
    try {
        final Activity activity = getActivity();
        if (null == activity || null == mCameraDevice) {
            return;
        }
        // This is the CaptureRequest.Builder that we use to take a picture.
        final CaptureRequest.Builder captureBuilder = mCameraDevice
                .createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        captureBuilder.addTarget(mImageReader.getSurface());

        // Use the same AE and AF modes as the preview.
        captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
        setAutoFlash(captureBuilder);

        // Orientation
        // int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        // captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation));

        CameraManager manager = (CameraManager) getContext().getSystemService(Context.CAMERA_SERVICE);
        CameraCharacteristics cameraCharacteristics = manager
                .getCameraCharacteristics("" + mCameraDevice.getId());
        int rotation = cameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
        captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, rotation);

        CameraCaptureSession.CaptureCallback CaptureCallback = new CameraCaptureSession.CaptureCallback() {

            @Override
            public void onCaptureCompleted(@NonNull CameraCaptureSession session,
                    @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {

                // showToast("Saved: " + mFile);
                Log.d(TAG, mFile.toString());
                unlockFocus();
            }
        };

        mCaptureSession.stopRepeating();
        mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

From source file:org.eypgvnm.camera2api.CameraFragment.java

private void changeCamera() {
    //get the number of cameras
    int camerasNumber = Camera.getNumberOfCameras();
    if (camerasNumber > 1) {

        //if the camera preview is the front
        if (isCameraFront) {

            closeCamera();/*from  w  w  w  .j a  v a2 s . c o m*/
            stopBackgroundThread();
            startBackgroundThread();
            if (mTextureView.isAvailable()) {
                openCamera(mTextureView.getWidth(), mTextureView.getHeight());
            } else {
                mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
            }

            isCameraFront = false;

        } else {

            mCameraId = getFrontFacingCameraId();

            closeCamera();
            stopBackgroundThread();
            startBackgroundThread();

            setUpCameraOutputs(mTextureView.getWidth(), mTextureView.getHeight());
            configureTransform(mTextureView.getWidth(), mTextureView.getHeight());
            Activity activity = getActivity();
            CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
            try {
                if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
                    throw new RuntimeException("Time out waiting to lock camera opening.");
                }
                mCameraId = getFrontFacingCameraId();
                manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler);
            } catch (CameraAccessException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                throw new RuntimeException("Interrupted while trying to lock camera opening.", e);
            }
            isCameraFront = true;
        }

    } else {
        ///...
    }
}

From source file:org.eypgvnm.camera2api.CameraFragment.java

private String getFrontFacingCameraId() {
    Activity activity = getActivity();//from  w w  w  . j  a va 2s. c  om
    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
    try {
        for (final String cameraId : manager.getCameraIdList()) {
            CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
            int cOrientation = characteristics.get(CameraCharacteristics.LENS_FACING);
            if (cOrientation == CameraCharacteristics.LENS_FACING_FRONT)
                return cameraId;
        }
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
    return null;
}

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 w  w .ja va2s .c om
        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:com.android.launcher3.Utilities.java

public static void turnOffFlashLight(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        CameraManager camManager = (CameraManager) context.getApplicationContext()
                .getSystemService(Context.CAMERA_SERVICE);
        String cameraId = null;/*w  w  w. jav a2 s . c  om*/
        try {
            cameraId = camManager.getCameraIdList()[0];
            camManager.setTorchMode(cameraId, false);
            isFlashLightOn = false;
        } catch (CameraAccessException e) {
            isFlashLightOn = true;
            e.printStackTrace();
        }
    } else {
        try {
            if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
                cam.stopPreview();
                cam.release();
                cam = null;
                isFlashLightOn = false;
            }
        } catch (Exception e) {
            isFlashLightOn = true;
            e.printStackTrace();
        }
    }
}