use of android.hardware.camera2.CameraDevice.StateCallback in project collect by opendatakit.
the class Camera2VideoFragment method openCamera.
/**
* Tries to open a {@link CameraDevice}. The result is listened by `stateCallback`.
*/
@SuppressWarnings("MissingPermission")
private void openCamera(int width, int height) {
final Activity activity = getActivity();
if (null == activity || activity.isFinishing()) {
return;
}
CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
try {
Timber.d("tryAcquire");
if (!cameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
throw new RuntimeException("Time out waiting to lock camera opening.");
}
String cameraId = null;
for (String id : manager.getCameraIdList()) {
CameraCharacteristics characteristics = manager.getCameraCharacteristics(id);
Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
if (facing != null && facing != CameraCharacteristics.LENS_FACING_FRONT) {
continue;
}
// Choose the sizes for camera preview and video recording
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
sensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
if (map == null) {
continue;
}
videoSize = chooseVideoSize(map.getOutputSizes(MediaRecorder.class));
previewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), width, height, videoSize);
cameraId = id;
break;
}
configureTransform(width, height);
mediaRecorder = new MediaRecorder();
manager.openCamera(cameraId, stateCallback, null);
} catch (CameraAccessException e) {
ToastUtils.showShortToast(getActivity(), "Cannot access the camera.");
activity.finish();
} catch (NullPointerException e) {
// Currently an NPE is thrown when the Camera2API is used but not supported on the
// device this code runs.
ErrorDialog.newInstance(getString(R.string.camera_error)).show(getChildFragmentManager(), FRAGMENT_DIALOG);
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while trying to lock camera opening.");
}
}
use of android.hardware.camera2.CameraDevice.StateCallback in project robolectric by robolectric.
the class ShadowCameraManager method openCameraDeviceUserAsync.
/**
* Enables {@link CameraManager#openCamera(String, StateCallback, Handler)} to open a
* {@link CameraDevice}.
*
* <p>If the provided cameraId exists, this will always post
* {@link CameraDevice.StateCallback#onOpened(CameraDevice) to the provided {@link Handler}.
* Unlike on real Android, this will not check if the camera has been disabled by device policy
* and does not attempt to connect to the camera service, so
* {@link CameraDevice.StateCallback#onError(CameraDevice, int)} and
* {@link CameraDevice.StateCallback#onDisconnected(CameraDevice)} will not be triggered by
* {@link CameraManager#openCamera(String, StateCallback, Handler)}.
*/
@Implementation(minSdk = VERSION_CODES.LOLLIPOP, maxSdk = VERSION_CODES.N)
protected CameraDevice openCameraDeviceUserAsync(String cameraId, CameraDevice.StateCallback callback, Handler handler) throws CameraAccessException {
CameraCharacteristics characteristics = getCameraCharacteristics(cameraId);
CameraDeviceImpl deviceImpl = ReflectionHelpers.callConstructor(CameraDeviceImpl.class, ClassParameter.from(String.class, cameraId), ClassParameter.from(CameraDevice.StateCallback.class, callback), ClassParameter.from(Handler.class, handler), ClassParameter.from(CameraCharacteristics.class, characteristics));
createdCameras.add(deviceImpl);
updateCameraCallback(deviceImpl, callback, handler, null);
handler.post(() -> callback.onOpened(deviceImpl));
return deviceImpl;
}
use of android.hardware.camera2.CameraDevice.StateCallback in project collect by opendatakit.
the class Camera2Fragment method openCamera.
/**
* Opens the camera specified by {@link Camera2Fragment#cameraId}.
*/
// Permission is handled in ImageWidget,
@SuppressLint("MissingPermission")
private void openCamera(int width, int height) {
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) {
Timber.e(e);
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while trying to lock camera opening.", e);
}
}
use of android.hardware.camera2.CameraDevice.StateCallback in project MazeSolver by Thukor.
the class Camera2 method openCamera.
private void openCamera() {
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
Log.e(TAG, "is camera open");
try {
cameraId = manager.getCameraIdList()[0];
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
assert map != null;
imageDimension = map.getOutputSizes(SurfaceTexture.class)[0];
// Add permission for camera and let user grant the permission
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(Camera2.this, new String[] { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE }, REQUEST_CAMERA_PERMISSION);
return;
}
Boolean available = characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
isFlashSupported = available == null ? false : available;
setupFlashButton();
manager.openCamera(cameraId, stateCallback, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
Log.e(TAG, "openCamera X");
}
Aggregations