android.graphics.ImageDecoder Java Examples

The following examples show how to use android.graphics.ImageDecoder. 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: MainActivity.java    From Android-9-Development-Cookbook with MIT License 7 votes vote down vote up
private void loadGif() {
    try {
        ImageDecoder.Source source = ImageDecoder.createSource(getResources(),
                R.drawable.giphy);
        Drawable decodedAnimation = ImageDecoder.decodeDrawable(source);

        ImageView imageView = findViewById(R.id.imageView);
        imageView.setImageDrawable(decodedAnimation);

        if (decodedAnimation instanceof AnimatedImageDrawable) {
            ((AnimatedImageDrawable) decodedAnimation).start();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #2
Source File: ResourcesImpl.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Loads a Drawable from an encoded image stream, or null.
 *
 * This call will handle closing ais.
 */
@Nullable
private Drawable decodeImageDrawable(@NonNull AssetInputStream ais,
        @NonNull Resources wrapper, @NonNull TypedValue value) {
    ImageDecoder.Source src = new ImageDecoder.AssetInputStreamSource(ais,
                        wrapper, value);
    try {
        return ImageDecoder.decodeDrawable(src, (decoder, info, s) -> {
            decoder.setAllocator(ImageDecoder.ALLOCATOR_SOFTWARE);
        });
    } catch (IOException ioe) {
        // This is okay. This may be something that ImageDecoder does not
        // support, like SVG.
        return null;
    }
}
 
Example #3
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 #4
Source File: BitmapUtils.java    From picasso with Apache License 2.0 5 votes vote down vote up
@RequiresApi(28)
@SuppressLint("Override")
private static Bitmap decodeStreamP(Request request, BufferedSource bufferedSource)
    throws IOException {
  ImageDecoder.Source imageSource =
      ImageDecoder.createSource(ByteBuffer.wrap(bufferedSource.readByteArray()));
  return decodeImageSource(imageSource, request);
}
 
Example #5
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 #6
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");
  }
}
 
Example #7
Source File: BitmapUtils.java    From picasso with Apache License 2.0 4 votes vote down vote up
@RequiresApi(28)
private static Bitmap decodeResourceP(Context context, final Request request) throws IOException {
  ImageDecoder.Source imageSource =
      ImageDecoder.createSource(context.getResources(), request.resourceId);
  return decodeImageSource(imageSource, request);
}
 
Example #8
Source File: ContentResolver.java    From AndroidComponentPlugin with Apache License 2.0 2 votes vote down vote up
/**
 * Convenience method that efficiently loads a visual thumbnail for the
 * given {@link Uri}. Internally calls
 * {@link ContentProvider#openTypedAssetFile} on the remote provider, but
 * also defensively resizes any returned content to match the requested
 * target size.
 *
 * @param uri The item that should be visualized as a thumbnail.
 * @param size The target area on the screen where this thumbnail will be
 *            shown. This is passed to the provider as {@link #EXTRA_SIZE}
 *            to help it avoid downloading or generating heavy resources.
 * @param signal A signal to cancel the operation in progress.
 * @return Valid {@link Bitmap} which is a visual thumbnail.
 * @throws IOException If any trouble was encountered while generating or
 *             loading the thumbnail, or if
 *             {@link CancellationSignal#cancel()} was invoked.
 */
public @NonNull Bitmap loadThumbnail(@NonNull Uri uri, @NonNull Size size,
        @Nullable CancellationSignal signal) throws IOException {
    return loadThumbnail(this, uri, size, signal, ImageDecoder.ALLOCATOR_SOFTWARE);
}