public class

BitmapUtils

extends Object
java.lang.Object
   ↳ ai.fritz.core.utils.BitmapUtils

Class Overview

Provides static functions to decode bitmaps at the optimal size

Summary

Public Constructors
BitmapUtils()
Public Methods
static Bitmap centerCrop(Bitmap src, int w, int h)
Returns a new Bitmap copy with a center-crop effect a la android.widget.ImageView.ScaleType#CENTER_CROP.
static Bitmap centerCropSquare(Bitmap bitmap)
Crop a center square in the image.
static Bitmap crop(Bitmap src, int w, int h, float horizontalCenterPercent, float verticalCenterPercent)
Returns a new Bitmap copy with a crop effect depending on the crop anchor given.
static Bitmap decodeByteArray(byte[] src, int w, int h)
Decode an image into a Bitmap, using sub-sampling if the hinted dimensions call for it.
static Bitmap decodeByteArrayWithCenterCrop(byte[] src, int w, int h)
Decode an image into a Bitmap, using sub-sampling if the desired dimensions call for it.
static void drawOnCanvas(Bitmap bitmap, Canvas canvas)
Draw the image onto a canvas.
static Bitmap resize(Bitmap bitmap, int width, int height)
Resize the Bitmap for the underlying model.
static Bitmap rotate(Bitmap bitmap, int degrees)
Rotate the bitmap for the model.
static Bitmap scale(Bitmap bitmap, int targetWidth, int targetHeight)
Scale the image while maintaining aspect ratio.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public BitmapUtils ()

Public Methods

public static Bitmap centerCrop (Bitmap src, int w, int h)

Returns a new Bitmap copy with a center-crop effect a la android.widget.ImageView.ScaleType#CENTER_CROP. May return the input bitmap if no scaling is necessary.

Parameters
src original bitmap of any size
w desired width in px
h desired height in px
Returns
  • a copy of src conforming to the given width and height, or src itself if it already matches the given width and height

public static Bitmap centerCropSquare (Bitmap bitmap)

Crop a center square in the image.

Uses the larger of the height or width to create the cropped square image. *

Returns
  • Return the newly cropped bitmap (centered)

public static Bitmap crop (Bitmap src, int w, int h, float horizontalCenterPercent, float verticalCenterPercent)

Returns a new Bitmap copy with a crop effect depending on the crop anchor given. 0.5f is like android.widget.ImageView.ScaleType#CENTER_CROP. The crop anchor will be be nudged so the entire cropped bitmap will fit inside the src. May return the input bitmap if no scaling is necessary.

Example of changing verticalCenterPercent: _________ _________ | | | | | | |_________| | | | |/___0.3f |---------| |_________|\ | |<---0.5f | | |---------| | | | | | | | | | | |_________| |_________|

Parameters
src original bitmap of any size
w desired width in px
h desired height in px
horizontalCenterPercent determines which part of the src to crop from. Range from 0 .0f to 1.0f. The value determines which part of the src maps to the horizontal center of the resulting bitmap.
verticalCenterPercent determines which part of the src to crop from. Range from 0 .0f to 1.0f. The value determines which part of the src maps to the vertical center of the resulting bitmap.
Returns
  • a copy of src conforming to the given width and height, or src itself if it already matches the given width and height

public static Bitmap decodeByteArray (byte[] src, int w, int h)

Decode an image into a Bitmap, using sub-sampling if the hinted dimensions call for it. Does not crop to fit the hinted dimensions.

Parameters
src an encoded image
w hint width in px
h hint height in px
Returns
  • a decoded Bitmap that is not exactly sized to the hinted dimensions.

public static Bitmap decodeByteArrayWithCenterCrop (byte[] src, int w, int h)

Decode an image into a Bitmap, using sub-sampling if the desired dimensions call for it. Also applies a center-crop a la android.widget.ImageView.ScaleType#CENTER_CROP.

Parameters
src an encoded image
w desired width in px
h desired height in px
Returns
  • an exactly-sized decoded Bitmap that is center-cropped.

public static void drawOnCanvas (Bitmap bitmap, Canvas canvas)

Draw the image onto a canvas.

public static Bitmap resize (Bitmap bitmap, int width, int height)

Resize the Bitmap for the underlying model.

Warning: this may change the aspect ratio of the image. If you'd like to maintain aspect, use scale(Bitmap, int, int)

Returns
  • the newly resized bitmap.

public static Bitmap rotate (Bitmap bitmap, int degrees)

Rotate the bitmap for the model.

public static Bitmap scale (Bitmap bitmap, int targetWidth, int targetHeight)

Scale the image while maintaining aspect ratio.