类android.graphics.ImageDecoder源码实例Demo

下面列出了怎么用android.graphics.ImageDecoder的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: android_9.0.0_r45   文件: ResourcesImpl.java

/**
 * 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;
    }
}
 

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();
    }
}
 
源代码3 项目: picasso   文件: BitmapUtils.java

@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);
        }
      }
    }
  });
}
 
源代码4 项目: picasso   文件: BitmapUtils.java

@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);
}
 

/** {@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;
}
 

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");
  }
}
 
源代码7 项目: picasso   文件: BitmapUtils.java

@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);
}
 

/**
 * 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);
}
 
 类所在包
 同包方法