use of androidx.camera.core.ImageCaptureException in project mollyim-android by mollyim.
the class CameraXFragment method onCaptureClicked.
private void onCaptureClicked() {
Stopwatch stopwatch = new Stopwatch("Capture");
CameraXSelfieFlashHelper flashHelper = new CameraXSelfieFlashHelper(requireActivity().getWindow(), camera, selfieFlash);
camera.takePicture(Executors.mainThreadExecutor(), new ImageCapture.OnImageCapturedCallback() {
@Override
public void onCaptureSuccess(@NonNull ImageProxy image) {
flashHelper.endFlash();
SimpleTask.run(CameraXFragment.this.getViewLifecycleOwner().getLifecycle(), () -> {
stopwatch.split("captured");
try {
return CameraXUtil.toJpeg(image, camera.getCameraLensFacing() == CameraSelector.LENS_FACING_FRONT);
} catch (IOException e) {
Log.w(TAG, "Failed to encode captured image.", e);
return null;
} finally {
image.close();
}
}, result -> {
stopwatch.split("transformed");
stopwatch.stop(TAG);
if (result != null) {
controller.onImageCaptured(result.getData(), result.getWidth(), result.getHeight());
} else {
controller.onCameraError();
}
});
}
@Override
public void onError(ImageCaptureException exception) {
Log.w(TAG, "Failed to capture image", exception);
flashHelper.endFlash();
controller.onCameraError();
}
});
flashHelper.startFlash();
}
use of androidx.camera.core.ImageCaptureException in project talk-android by nextcloud.
the class TakePhotoActivity method getImageCapture.
private ImageCapture getImageCapture(Boolean crop, Boolean lowres) {
final ImageCapture imageCapture;
if (lowres)
imageCapture = new ImageCapture.Builder().setTargetResolution(new Size(crop ? 1080 : 1440, 1920)).build();
else
imageCapture = new ImageCapture.Builder().setTargetAspectRatio(crop ? AspectRatio.RATIO_16_9 : AspectRatio.RATIO_4_3).build();
orientationEventListener = new OrientationEventListener(this) {
@Override
public void onOrientationChanged(int orientation) {
int rotation;
// Monitors orientation values to determine the target rotation value
if (orientation >= 45 && orientation < 135) {
rotation = Surface.ROTATION_270;
} else if (orientation >= 135 && orientation < 225) {
rotation = Surface.ROTATION_180;
} else if (orientation >= 225 && orientation < 315) {
rotation = Surface.ROTATION_90;
} else {
rotation = Surface.ROTATION_0;
}
imageCapture.setTargetRotation(rotation);
}
};
orientationEventListener.enable();
binding.takePhoto.setOnClickListener((v) -> {
binding.takePhoto.setEnabled(false);
final String photoFileName = dateFormat.format(new Date()) + ".jpg";
try {
final File photoFile = FileUtils.getTempCacheFile(this, "photos/" + photoFileName);
final ImageCapture.OutputFileOptions options = new ImageCapture.OutputFileOptions.Builder(photoFile).build();
imageCapture.takePicture(options, ContextCompat.getMainExecutor(this), new ImageCapture.OnImageSavedCallback() {
@Override
public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
setPreviewImage(photoFile);
showPictureProcessingElements();
}
@Override
public void onError(@NonNull ImageCaptureException e) {
Log.e(TAG, "Error", e);
if (!photoFile.delete()) {
Log.w(TAG, "Deleting picture failed");
}
binding.takePhoto.setEnabled(true);
}
});
} catch (Exception e) {
Toast.makeText(this, R.string.take_photo_error_deleting_picture, Toast.LENGTH_SHORT).show();
}
});
return imageCapture;
}
use of androidx.camera.core.ImageCaptureException in project J2ME-Loader by nikita36078.
the class CameraController method getSnapshot.
public byte[] getSnapshot() {
Executor executor = Executors.newSingleThreadExecutor();
imageCapture.takePicture(executor, new ImageCapture.OnImageCapturedCallback() {
@Override
public void onCaptureSuccess(@NonNull ImageProxy image) {
ImageInfo info = image.getImageInfo();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] temp = new byte[buffer.remaining()];
buffer.get(temp);
Bitmap bmp = BitmapFactory.decodeByteArray(temp, 0, temp.length);
try {
bmp = rotateImageIfRequired(bmp, info.getRotationDegrees());
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
result = stream.toByteArray();
synchronized (monitor) {
monitor.notifyAll();
}
}
@Override
public void onError(@NonNull ImageCaptureException exception) {
result = null;
synchronized (monitor) {
monitor.notifyAll();
}
}
});
synchronized (monitor) {
try {
monitor.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return result;
}
use of androidx.camera.core.ImageCaptureException in project Signal-Android by signalapp.
the class CameraXFragment method onCaptureClicked.
private void onCaptureClicked() {
Stopwatch stopwatch = new Stopwatch("Capture");
CameraXSelfieFlashHelper flashHelper = new CameraXSelfieFlashHelper(requireActivity().getWindow(), camera, selfieFlash);
camera.takePicture(Executors.mainThreadExecutor(), new ImageCapture.OnImageCapturedCallback() {
@Override
public void onCaptureSuccess(@NonNull ImageProxy image) {
flashHelper.endFlash();
SimpleTask.run(CameraXFragment.this.getViewLifecycleOwner().getLifecycle(), () -> {
stopwatch.split("captured");
try {
return CameraXUtil.toJpeg(image, camera.getCameraLensFacing() == CameraSelector.LENS_FACING_FRONT);
} catch (IOException e) {
Log.w(TAG, "Failed to encode captured image.", e);
return null;
} finally {
image.close();
}
}, result -> {
stopwatch.split("transformed");
stopwatch.stop(TAG);
if (result != null) {
controller.onImageCaptured(result.getData(), result.getWidth(), result.getHeight());
} else {
controller.onCameraError();
}
});
}
@Override
public void onError(ImageCaptureException exception) {
Log.w(TAG, "Failed to capture image", exception);
flashHelper.endFlash();
controller.onCameraError();
}
});
flashHelper.startFlash();
}
use of androidx.camera.core.ImageCaptureException in project nextcloud-deck by stefan-niedermann.
the class TakePhotoActivity method getCaptureUseCase.
private ImageCapture getCaptureUseCase() {
final ImageCapture captureUseCase = new ImageCapture.Builder().setTargetResolution(new Size(720, 1280)).build();
orientationEventListener = new OrientationEventListener(this) {
@Override
public void onOrientationChanged(int orientation) {
int rotation;
// Monitors orientation values to determine the target rotation value
if (orientation >= 45 && orientation < 135) {
rotation = Surface.ROTATION_270;
} else if (orientation >= 135 && orientation < 225) {
rotation = Surface.ROTATION_180;
} else if (orientation >= 225 && orientation < 315) {
rotation = Surface.ROTATION_90;
} else {
rotation = Surface.ROTATION_0;
}
captureUseCase.setTargetRotation(rotation);
}
};
orientationEventListener.enable();
binding.takePhoto.setOnClickListener((v) -> {
binding.takePhoto.setEnabled(false);
final String photoFileName = Instant.now().atZone(ZoneId.systemDefault()).format(fileNameFromCameraFormatter);
try {
final File photoFile = FilesUtil.getTempCacheFile(this, "photos/" + photoFileName);
final ImageCapture.OutputFileOptions options = new ImageCapture.OutputFileOptions.Builder(photoFile).build();
captureUseCase.takePicture(options, ContextCompat.getMainExecutor(this), new ImageCapture.OnImageSavedCallback() {
@Override
public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
final Uri savedUri = Uri.fromFile(photoFile);
DeckLog.info("onImageSaved - savedUri:", savedUri.toString());
setResult(RESULT_OK, new Intent().setDataAndType(savedUri, IMAGE_JPEG));
finish();
}
@Override
public void onError(@NonNull ImageCaptureException e) {
e.printStackTrace();
// noinspection ResultOfMethodCallIgnored
photoFile.delete();
binding.takePhoto.setEnabled(true);
}
});
} catch (Exception e) {
ExceptionDialogFragment.newInstance(e, null).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName());
}
});
return captureUseCase;
}
Aggregations