public final class

BitmapUtil

extends java.lang.Object

 java.lang.Object

↳androidx.media3.datasource.BitmapUtil

Gradle dependencies

compile group: 'androidx.media3', name: 'media3-datasource', version: '1.5.0-alpha01'

  • groupId: androidx.media3
  • artifactId: media3-datasource
  • version: 1.5.0-alpha01

Artifact androidx.media3:media3-datasource:1.5.0-alpha01 it located at Google repository (https://maven.google.com/)

Overview

Utility methods for Bitmap instances.

Summary

Methods
public static Bitmapdecode(byte[] data[], int length, BitmapFactory.Options options)

Decodes a Bitmap from a byte array using BitmapFactory and the ExifInterface.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Methods

public static Bitmap decode(byte[] data[], int length, BitmapFactory.Options options)

Decodes a Bitmap from a byte array using BitmapFactory and the ExifInterface.

Parameters:

data: Byte array of compressed image data.
length: The number of bytes to parse.
options: the to decode the data with.

Source

/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package androidx.media3.datasource;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import androidx.annotation.Nullable;
import androidx.exifinterface.media.ExifInterface;
import androidx.media3.common.ParserException;
import androidx.media3.common.util.UnstableApi;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/** Utility methods for {@link Bitmap} instances. */
@UnstableApi
public final class BitmapUtil {

  private BitmapUtil() {}

  /**
   * Decodes a {@link Bitmap} from a byte array using {@link BitmapFactory} and the {@link
   * ExifInterface}.
   *
   * @param data Byte array of compressed image data.
   * @param length The number of bytes to parse.
   * @param options the {@link BitmapFactory.Options} to decode the {@code data} with.
   * @throws ParserException if the {@code data} could not be decoded.
   */
  // BitmapFactory's options parameter is null-ok.
  @SuppressWarnings("nullness:argument.type.incompatible")
  public static Bitmap decode(byte[] data, int length, @Nullable BitmapFactory.Options options)
      throws IOException {
    @Nullable Bitmap bitmap = BitmapFactory.decodeByteArray(data, /* offset= */ 0, length, options);
    if (bitmap == null) {
      throw ParserException.createForMalformedContainer(
          "Could not decode image data", new IllegalStateException());
    }
    // BitmapFactory doesn't read the exif header, so we use the ExifInterface to this do ensure the
    // bitmap is correctly orientated.
    ExifInterface exifInterface;
    try (InputStream inputStream = new ByteArrayInputStream(data)) {
      exifInterface = new ExifInterface(inputStream);
    }
    int rotationDegrees = exifInterface.getRotationDegrees();
    if (rotationDegrees != 0) {
      Matrix matrix = new Matrix();
      matrix.postRotate(rotationDegrees);
      bitmap =
          Bitmap.createBitmap(
              bitmap,
              /* x= */ 0,
              /* y= */ 0,
              bitmap.getWidth(),
              bitmap.getHeight(),
              matrix,
              /* filter= */ false);
    }
    return bitmap;
  }
}