Search in sources :

Example 1 with ImageCaptureException

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();
}
Also used : RequiresApi(androidx.annotation.RequiresApi) Bundle(android.os.Bundle) NonNull(androidx.annotation.NonNull) WindowManager(android.view.WindowManager) ImageView(android.widget.ImageView) Animator(android.animation.Animator) R(org.thoughtcrime.securesms.R) ProcessCameraProvider(androidx.camera.lifecycle.ProcessCameraProvider) VideoUtil(org.thoughtcrime.securesms.video.VideoUtil) View(android.view.View) Animation(android.view.animation.Animation) ContextCompat(androidx.core.content.ContextCompat) Executors(com.bumptech.glide.util.Executors) ImageProxy(androidx.camera.core.ImageProxy) Surface(android.view.Surface) ImageCapture(androidx.camera.core.ImageCapture) ViewGroup(android.view.ViewGroup) MediaCountIndicatorButton(org.thoughtcrime.securesms.mediasend.v2.MediaCountIndicatorButton) Log(org.signal.core.util.logging.Log) Nullable(androidx.annotation.Nullable) AnimationCompleteListener(org.thoughtcrime.securesms.animation.AnimationCompleteListener) DecelerateInterpolator(android.view.animation.DecelerateInterpolator) Context(android.content.Context) GestureDetector(android.view.GestureDetector) PreviewView(androidx.camera.view.PreviewView) CameraXUtil(org.thoughtcrime.securesms.mediasend.camerax.CameraXUtil) CameraSelector(androidx.camera.core.CameraSelector) AnimationUtils(android.view.animation.AnimationUtils) TextSecurePreferences(org.thoughtcrime.securesms.util.TextSecurePreferences) TooltipPopup(org.thoughtcrime.securesms.components.TooltipPopup) SuppressLint(android.annotation.SuppressLint) MotionEvent(android.view.MotionEvent) Toast(android.widget.Toast) ImageCaptureException(androidx.camera.core.ImageCaptureException) MemoryFileDescriptor(org.thoughtcrime.securesms.util.MemoryFileDescriptor) Build(android.os.Build) CameraXFlashToggleView(org.thoughtcrime.securesms.mediasend.camerax.CameraXFlashToggleView) SimpleTask(org.thoughtcrime.securesms.util.concurrent.SimpleTask) LayoutInflater(android.view.LayoutInflater) MediaAnimations(org.thoughtcrime.securesms.mediasend.v2.MediaAnimations) IOException(java.io.IOException) SignalCameraView(androidx.camera.view.SignalCameraView) RotateAnimation(android.view.animation.RotateAnimation) Optional(org.whispersystems.libsignal.util.guava.Optional) Glide(com.bumptech.glide.Glide) MediaConstraints(org.thoughtcrime.securesms.mms.MediaConstraints) Configuration(android.content.res.Configuration) DecryptableUri(org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader.DecryptableUri) Stopwatch(org.thoughtcrime.securesms.util.Stopwatch) FileDescriptor(java.io.FileDescriptor) LoggingFragment(org.thoughtcrime.securesms.LoggingFragment) ImageCaptureException(androidx.camera.core.ImageCaptureException) Stopwatch(org.thoughtcrime.securesms.util.Stopwatch) ImageCapture(androidx.camera.core.ImageCapture) IOException(java.io.IOException) ImageProxy(androidx.camera.core.ImageProxy)

Example 2 with ImageCaptureException

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;
}
Also used : ImageCaptureException(androidx.camera.core.ImageCaptureException) OrientationEventListener(android.view.OrientationEventListener) Size(android.util.Size) ImageCapture(androidx.camera.core.ImageCapture) Date(java.util.Date) ImageCaptureException(androidx.camera.core.ImageCaptureException) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File)

Example 3 with ImageCaptureException

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;
}
Also used : Bitmap(android.graphics.Bitmap) Executor(java.util.concurrent.Executor) ImageCaptureException(androidx.camera.core.ImageCaptureException) ImageCapture(androidx.camera.core.ImageCapture) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageInfo(androidx.camera.core.ImageInfo) ImageProxy(androidx.camera.core.ImageProxy) ByteBuffer(java.nio.ByteBuffer)

Example 4 with ImageCaptureException

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();
}
Also used : RequiresApi(androidx.annotation.RequiresApi) Bundle(android.os.Bundle) NonNull(androidx.annotation.NonNull) WindowManager(android.view.WindowManager) ImageView(android.widget.ImageView) Animator(android.animation.Animator) R(org.thoughtcrime.securesms.R) ProcessCameraProvider(androidx.camera.lifecycle.ProcessCameraProvider) VideoUtil(org.thoughtcrime.securesms.video.VideoUtil) ActivityInfo(android.content.pm.ActivityInfo) View(android.view.View) Animation(android.view.animation.Animation) ContextCompat(androidx.core.content.ContextCompat) Executors(com.bumptech.glide.util.Executors) ImageProxy(androidx.camera.core.ImageProxy) Surface(android.view.Surface) ImageCapture(androidx.camera.core.ImageCapture) ViewGroup(android.view.ViewGroup) MediaCountIndicatorButton(org.thoughtcrime.securesms.mediasend.v2.MediaCountIndicatorButton) Log(org.signal.core.util.logging.Log) StoryDisplay(org.thoughtcrime.securesms.stories.viewer.page.StoryDisplay) FeatureFlags(org.thoughtcrime.securesms.util.FeatureFlags) Nullable(androidx.annotation.Nullable) Optional(java.util.Optional) AnimationCompleteListener(org.thoughtcrime.securesms.animation.AnimationCompleteListener) DecelerateInterpolator(android.view.animation.DecelerateInterpolator) Context(android.content.Context) GestureDetector(android.view.GestureDetector) PreviewView(androidx.camera.view.PreviewView) CameraXUtil(org.thoughtcrime.securesms.mediasend.camerax.CameraXUtil) CameraSelector(androidx.camera.core.CameraSelector) SimpleTask(org.signal.core.util.concurrent.SimpleTask) AnimationUtils(android.view.animation.AnimationUtils) TextSecurePreferences(org.thoughtcrime.securesms.util.TextSecurePreferences) TooltipPopup(org.thoughtcrime.securesms.components.TooltipPopup) SuppressLint(android.annotation.SuppressLint) MotionEvent(android.view.MotionEvent) Toast(android.widget.Toast) ImageCaptureException(androidx.camera.core.ImageCaptureException) MemoryFileDescriptor(org.thoughtcrime.securesms.util.MemoryFileDescriptor) Build(android.os.Build) CameraXFlashToggleView(org.thoughtcrime.securesms.mediasend.camerax.CameraXFlashToggleView) Stories(org.thoughtcrime.securesms.stories.Stories) LayoutInflater(android.view.LayoutInflater) MediaAnimations(org.thoughtcrime.securesms.mediasend.v2.MediaAnimations) IOException(java.io.IOException) SignalCameraView(androidx.camera.view.SignalCameraView) RotateAnimation(android.view.animation.RotateAnimation) Glide(com.bumptech.glide.Glide) MediaConstraints(org.thoughtcrime.securesms.mms.MediaConstraints) Configuration(android.content.res.Configuration) DecryptableUri(org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader.DecryptableUri) Stopwatch(org.thoughtcrime.securesms.util.Stopwatch) FileDescriptor(java.io.FileDescriptor) LoggingFragment(org.thoughtcrime.securesms.LoggingFragment) ImageCaptureException(androidx.camera.core.ImageCaptureException) Stopwatch(org.thoughtcrime.securesms.util.Stopwatch) ImageCapture(androidx.camera.core.ImageCapture) IOException(java.io.IOException) ImageProxy(androidx.camera.core.ImageProxy)

Example 5 with ImageCaptureException

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;
}
Also used : ImageCaptureException(androidx.camera.core.ImageCaptureException) OrientationEventListener(android.view.OrientationEventListener) Size(android.util.Size) ImageCapture(androidx.camera.core.ImageCapture) Intent(android.content.Intent) Uri(android.net.Uri) ImageCaptureException(androidx.camera.core.ImageCaptureException) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File)

Aggregations

ImageCapture (androidx.camera.core.ImageCapture)8 ImageCaptureException (androidx.camera.core.ImageCaptureException)8 ImageProxy (androidx.camera.core.ImageProxy)4 File (java.io.File)4 IOException (java.io.IOException)4 Animator (android.animation.Animator)3 SuppressLint (android.annotation.SuppressLint)3 Context (android.content.Context)3 Configuration (android.content.res.Configuration)3 Build (android.os.Build)3 Bundle (android.os.Bundle)3 GestureDetector (android.view.GestureDetector)3 LayoutInflater (android.view.LayoutInflater)3 MotionEvent (android.view.MotionEvent)3 Surface (android.view.Surface)3 View (android.view.View)3 ViewGroup (android.view.ViewGroup)3 WindowManager (android.view.WindowManager)3 Animation (android.view.animation.Animation)3 AnimationUtils (android.view.animation.AnimationUtils)3