Java Code Examples for android.graphics.ImageDecoder#decodeBitmap()

The following examples show how to use android.graphics.ImageDecoder#decodeBitmap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BitmapUtils.java    From picasso with Apache License 2.0 6 votes vote down vote up
@RequiresApi(28)
private static Bitmap decodeImageSource(ImageDecoder.Source imageSource, final Request request)
    throws IOException {
  return ImageDecoder.decodeBitmap(imageSource, new ImageDecoder.OnHeaderDecodedListener() {
    @Override
    public void onHeaderDecoded(@NonNull ImageDecoder imageDecoder,
        @NonNull ImageDecoder.ImageInfo imageInfo, @NonNull ImageDecoder.Source source) {
      if (request.hasSize()) {
        Size size = imageInfo.getSize();
        if (shouldResize(request.onlyScaleDown, size.getWidth(), size.getHeight(),
            request.targetWidth, request.targetHeight)) {
          imageDecoder.setTargetSize(request.targetWidth, request.targetHeight);
        }
      }
    }
  });
}
 
Example 2
Source File: ContentResolver.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
/** {@hide} */
public static Bitmap loadThumbnail(@NonNull ContentInterface content, @NonNull Uri uri,
        @NonNull Size size, @Nullable CancellationSignal signal, int allocator)
        throws IOException {
    Objects.requireNonNull(content);
    Objects.requireNonNull(uri);
    Objects.requireNonNull(size);

    // Convert to Point, since that's what the API is defined as
    final Bundle opts = new Bundle();
    opts.putParcelable(EXTRA_SIZE, Point.convert(size));
    final Int32Ref orientation = new Int32Ref(0);

    Bitmap bitmap = ImageDecoder.decodeBitmap(ImageDecoder.createSource(() -> {
        final AssetFileDescriptor afd = content.openTypedAssetFile(uri, "image/*", opts,
                signal);
        final Bundle extras = afd.getExtras();
        orientation.value = (extras != null) ? extras.getInt(EXTRA_ORIENTATION, 0) : 0;
        return afd;
    }), (ImageDecoder decoder, ImageInfo info, Source source) -> {
        decoder.setAllocator(allocator);

        // One last-ditch check to see if we've been canceled.
        if (signal != null) signal.throwIfCanceled();

        // We requested a rough thumbnail size, but the remote size may have
        // returned something giant, so defensively scale down as needed.
        final int widthSample = info.getSize().getWidth() / size.getWidth();
        final int heightSample = info.getSize().getHeight() / size.getHeight();
        final int sample = Math.min(widthSample, heightSample);
        if (sample > 1) {
            decoder.setTargetSampleSize(sample);
        }
    });

    // Transform the bitmap if requested. We use a side-channel to
    // communicate the orientation, since EXIF thumbnails don't contain
    // the rotation flags of the original image.
    if (orientation.value != 0) {
        final int width = bitmap.getWidth();
        final int height = bitmap.getHeight();

        final Matrix m = new Matrix();
        m.setRotate(orientation.value, width / 2, height / 2);
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, m, false);
    }

    return bitmap;
}
 
Example 3
Source File: StillImageActivity.java    From quickstart-android with Apache License 2.0 4 votes vote down vote up
private void tryReloadAndDetectInImage() {
  try {
    if (imageUri == null) {
      return;
    }

    // Clear the overlay first
    binding.previewOverlay.clear();

    Bitmap imageBitmap;
    if (Build.VERSION.SDK_INT < 29) {
      imageBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
    } else {
      ImageDecoder.Source source = ImageDecoder.createSource(getContentResolver(), imageUri);
      imageBitmap = ImageDecoder.decodeBitmap(source);
    }

    // Get the dimensions of the View
    Pair<Integer, Integer> targetedSize = getTargetedWidthHeight();

    int targetWidth = targetedSize.first;
    int maxHeight = targetedSize.second;

    // Determine how much to scale down the image
    float scaleFactor =
        Math.max(
            (float) imageBitmap.getWidth() / (float) targetWidth,
            (float) imageBitmap.getHeight() / (float) maxHeight);

    Bitmap resizedBitmap =
        Bitmap.createScaledBitmap(
            imageBitmap,
            (int) (imageBitmap.getWidth() / scaleFactor),
            (int) (imageBitmap.getHeight() / scaleFactor),
            true);

    binding.previewPane.setImageBitmap(resizedBitmap);

    imageProcessor.process(resizedBitmap, binding.previewOverlay);
  } catch (IOException e) {
    Log.e(TAG, "Error retrieving saved image");
  }
}