Java Code Examples for android.graphics.ImageDecoder#Source

The following examples show how to use android.graphics.ImageDecoder#Source . 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: 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 6
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);
}