Example usage for android.hardware.camera2 CameraManager openCamera

List of usage examples for android.hardware.camera2 CameraManager openCamera

Introduction

In this page you can find the example usage for android.hardware.camera2 CameraManager openCamera.

Prototype

@RequiresPermission(android.Manifest.permission.CAMERA)
public void openCamera(@NonNull String cameraId, @NonNull @CallbackExecutor Executor executor,
        @NonNull final CameraDevice.StateCallback callback) throws CameraAccessException 

Source Link

Document

Open a connection to a camera with the given ID.

Usage

From source file:com.google.android.apps.watchme.StreamerActivity.java

private void connectCamera() {
    CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    try {/*from  ww w . j  a  va  2 s.  c o m*/
        cameraManager.openCamera(cameraId, cameraDeviceCallback, backgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

From source file:MainActivity.java

private void openCamera() {
    CameraManager manager = (CameraManager) getSystemService(CAMERA_SERVICE);
    try {/*from  w ww .  j  a  v a2 s.c  om*/
        String cameraId = manager.getCameraIdList()[0];
        CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
        StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
        mPreviewSize = map.getOutputSizes(SurfaceTexture.class)[0];
        manager.openCamera(cameraId, mStateCallback, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    }
}

From source file:com.example.android.tflitecamerademo.Camera2BasicFragment.java

/** Opens the camera specified by {@link Camera2BasicFragment#cameraId}. */
private void openCamera(int width, int height) {
    if (!checkedPermissions && !allPermissionsGranted()) {
        FragmentCompat.requestPermissions(this, getRequiredPermissions(), PERMISSIONS_REQUEST_CODE);
        return;//from  w w w.  java2s  .  c  o  m
    } else {
        checkedPermissions = true;
    }
    setUpCameraOutputs(width, height);
    configureTransform(width, height);
    Activity activity = getActivity();
    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
    try {
        if (!cameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
            throw new RuntimeException("Time out waiting to lock camera opening.");
        }
        manager.openCamera(cameraId, stateCallback, backgroundHandler);
    } catch (CameraAccessException e) {
        Log.e(TAG, "Failed to open Camera", e);
    } catch (InterruptedException e) {
        throw new RuntimeException("Interrupted while trying to lock camera opening.", e);
    }
}

From source file:fr.xebia.magritte.classifier.lite.Camera2BasicFragment.java

/** Opens the camera specified by {@link Camera2BasicFragment#cameraId}. */
private void openCamera(int width, int height) {
    if (!checkedPermissions && !allPermissionsGranted()) {
        FragmentCompat.requestPermissions(this, getRequiredPermissions(), PERMISSIONS_REQUEST_CODE);
        return;/*from ww w .j a  v a 2s  .  com*/
    } else {
        checkedPermissions = true;
    }
    setUpCameraOutputs(width, height);
    configureTransform(width, height);
    Activity activity = getActivity();
    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
    try {
        if (!cameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
            throw new RuntimeException("Time out waiting to lock camera opening.");
        }
        manager.openCamera(cameraId, stateCallback, backgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        throw new RuntimeException("Interrupted while trying to lock camera opening.", e);
    }
}

From source file:com.manufacton.cordova.MyCameraManager.java

/**
 * Opens the camera specified by {@link Camera2BasicFragment#mCameraId}.
 *///  ww w .  j a  va  2 s. co m
public void openCamera(int width, int height) {
    Log.d(TAG, "OPENING THREAD");
    qrCode = null;
    startBackgroundThread();
    Log.d(TAG, "OPENING CAMERA");
    if (ContextCompat.checkSelfPermission(activity,
            Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
        // requestCameraPermission();
        return;
    }
    Log.d(TAG, "SETTING OUTPUTS");
    setUpCameraOutputs(width, height);
    Log.d(TAG, "OUTPUTS SET");
    // configureTransform(width, height);
    // Activity activity = getActivity();
    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
    Log.d(TAG, "GOT MANAGER");
    try {
        if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
            throw new RuntimeException("Time out waiting to lock camera opening.");
        }
        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.microblink.barcode.customcamera.camera2.Camera2Fragment.java

/**
 * Opens the camera specified by {@link Camera2Fragment#mCameraId}.
 *//*from   www .  j a  v  a  2 s  . c  om*/
private void openCamera(int width, int height) {
    if (ContextCompat.checkSelfPermission(getActivity(),
            Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
        getActivity().finish();
        return;
    }
    setUpCameraOutputs(width, height);
    configureTransform(width, height);
    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.");
        }
        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.android.learnmore.Camera2BasicFragment.java

private void openCamera(int width, int height) {
    if (ContextCompat.checkSelfPermission(getActivity(),
            Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
        requestCameraPermission();/*from w  w w.  j a v a 2  s. com*/
        return;
    }
    setUpCameraOutputs(width, height);
    configureTransform(width, height);
    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.");
        }
        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.android.camera2basic.Fragment.Camera2BasicFragment.java

/**
 * Opens the camera specified by {@link Camera2BasicFragment#mCameraId}.
 *///w  w w  .  j  a  va 2 s. co  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);

    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.");
        }
        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.tenth.space.ui.fragment.HomeFragment.java

private void openCamera(int width, int height) {
    //        if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
    //                ){
    //            new AlertDialog.Builder(getActivity(),AlertDialog.THEME_DEVICE_DEFAULT_LIGHT)
    //            .setTitle("???")
    //            .setPositiveButton("", new DialogInterface.OnClickListener() {
    //                 @Override
    //                public void onClick(DialogInterface dialog, int which) {
    //                    Log.i("GTAG","?");
    //                    requestPermissions(new String[]{Manifest.permission.CAMERA,Manifest.permission.LOCATION_HARDWARE},REQUEST_CAMERA_PERMISSION);
    //                    requestPermissions(new String[]{Manifest.permission.LOCATION_HARDWARE},2);
    //                    ToastUtils.show("???");
    //                }
    //            })
    //            .setNegativeButton("?",null)
    //            .create()
    //            .show();
    //        }//from   w  ww. j a v a2 s . c om
    setUpCameraOutputs(width, height);
    configureTransform(width, height);
    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.");
        }
        manager.openCamera("1", 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.tzutalin.dlibtest.CameraConnectionFragment.java

/**
 * Opens the camera specified by {@link CameraConnectionFragment#cameraId}.
 *///from w  ww. j ava2  s .  c  om
@SuppressLint("LongLogTag")
@DebugLog
private void openCamera(final int width, final int height) {
    Log.d(TAG, "width " + width + "height " + height);
    setUpCameraOutputs(width, height);
    configureTransform(width, height);
    final Activity activity = getActivity();
    final CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
    try {
        if (!cameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
            throw new RuntimeException("Time out waiting to lock camera opening.");
        }
        if (ActivityCompat.checkSelfPermission(this.getActivity(),
                Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            Timber.tag(TAG).w("checkSelfPermission CAMERA");
        }
        manager.openCamera("1", stateCallback, backgroundHandler);
        Timber.tag(TAG).d("open Camera");
    } catch (final CameraAccessException e) {
        Timber.tag(TAG).e("Exception!", e);
    } catch (final InterruptedException e) {
        throw new RuntimeException("Interrupted while trying to lock camera opening.", e);
    }
}