Bitmap source code instance in Android Development
Package android. graphics;
Import java. awt. image. BufferedImage;
Import java. io. File;
Import java. io. IOException;
Import java. io. InputStream;
Import javax. imageio. ImageIO;
Public final class Bitmap extends _ Original_Bitmap {
Private BufferedImage mImage;
Public Bitmap (File input) throws IOException {
Super (1, true, null,-1 );
MImage = ImageIO. read (input );
}
Public Bitmap (InputStream is) throws IOException {
Super (1, true, null,-1 );
MImage = ImageIO. read (is );
}
Bitmap (BufferedImage image ){
Super (1, true, null,-1 );
MImage = image;
}
Public BufferedImage getImage (){
Return mImage;
}
// ----- Overriden methods
Public enum Config {
// These native values must match up with the enum in SkBitmap. h
ALPHA_8 (2 ),
RGB_565 (4 ),
ARGB_4444 (5 ),
ARGB_8888 (6 );
Config (int ni ){
This. nativeInt = ni;
}
Final int nativeInt;
/* Package */static Config nativeToConfig (int ni ){
Return sConfigs [ni];
}
Private static Config sConfigs [] = {
Null, null, ALPHA_8, null, RGB_565, ARGB_4444, ARGB_8888
};
}
@ Override
Public int getWidth (){
Return mImage. getWidth ();
}
@ Override
Public int getHeight (){
Return mImage. getHeight ();
}
/**
* Returns an immutable bitmap from the source bitmap. The new bitmap may
* Be the same object as source, or a copy may have been made.
*/
Public static Bitmap createBitmap (Bitmap src ){
Return createBitmap (src, 0, 0, src. getWidth (), src. getHeight (), null, false );
}
/**
* Returns an immutable bitmap from the specified subset of the source
* Bitmap. The new bitmap may be the same object as source, or a copy may
* Have been made.
*
* @ Param source The bitmap we are subsetting
* @ Param x The x coordinate of the first pixel in source
* @ Param y The y coordinate of the first pixel in source
* @ Param width The number of pixels in each row
* @ Param height The number of rows
*/
Public static Bitmap createBitmap (Bitmap source, int x, int y,
Int width, int height ){
Return new Bitmap (source. mImage. getSubimage (x, y, width, height ));
}
/**
* Returns an immutable bitmap from subset of the source bitmap,
* Transformed by the optional matrix.
*
* @ Param source The bitmap we are subsetting
* @ Param x The x coordinate of the first pixel in source
* @ Param y The y coordinate of the first pixel in source
* @ Param width The number of pixels in each row
* @ Param height The number of rows
* @ Param m Option matrix to be applied to the pixels
* @ Param filter true if the source shoshould be filtered.
* Only applies if the matrix contains more than just
* Translation.
* @ Return A bitmap that represents the specified subset of source
* @ Throws IllegalArgumentException if the x, y, width, height values are
* Outside of the dimensions of the source bitmap.
*/
Public static Bitmap createBitmap (Bitmap source, int x, int y, int width,
Int height, Matrix m, boolean filter ){
CheckXYSign (x, y );
CheckWidthHeight (width, height );
If (x + width> source. getWidth ()){
Throw new IllegalArgumentException (
"X + width must be <= bitmap. width ()");
}
If (y + height> source. getHeight ()){
Throw new IllegalArgumentException (
"Y + height must be <= bitmap. height ()");
}
// Check if we can just return our argument unchanged
If (! Source. isMutable () & x = 0 & y = 0
& Width = source. getWidth () & height = source. getHeight ()
& (M = null | m. isIdentity ())){
Return source;
}
If (m = null | m. isIdentity ()){
Return new Bitmap (source. mImage. getSubimage (x, y, width, height ));
}
Int neww = width;
Int newh = height;
Paint paint;
Rect srcR = new Rect (x, y, x + width, y + height );
RectF dstR = new RectF (0, 0, width, height );
/* The dst shoshould have alpha if the src does, or if our matrix
Doesn' t preserve rectness
*/
Boolean hasAlpha = source. hasAlpha () |! M. rectStaysRect ();
RectF deviceR = new RectF ();
M. mapRect (deviceR, dstR );
Neww = Math. round (deviceR. width ());
Newh = Math. round (deviceR. height ());
Canvas canvas = new Canvas (neww, newh );
Canvas. translate (-deviceR. left,-deviceR. top );
Canvas. concat (m );
Paint = new Paint ();
Paint. setFilterBitmap (filter );
If (! M. rectStaysRect ()){
Paint. setAntiAlias (true );
}
Canvas. drawBitmap (source, srcR, dstR, paint );
Return new Bitmap (canvas. getImage ());
}
/**
* Returns a mutable bitmap with the specified width and height.
*
* @ Param width The width of the bitmap
* @ Param height The height of the bitmap
* @ Param config The bitmap config to create.
* @ Throws IllegalArgumentException if the width or height are <= 0
*/
Public static Bitmap createBitmap (int width, int height, Config config ){
Return new Bitmap (new BufferedImage (width, height, BufferedImage. TYPE_INT_ARGB ));
}
/**
* Returns a immutable bitmap with the specified width and height, with each
* Pixel value set to the corresponding value in the colors array.
*
* @ Param colors Array of {@ link Color} used to initialize the pixels.
* @ Param offset Number of values to skip before the first color in
* Array of colors.
* @ Param stride Number of colors in the array between rows (must be> =
* Width or <=-width ).
* @ Param width The width of the bitmap
* @ Param height The height of the bitmap
* @ Param config The bitmap config to create. If the config does not
* Support per-pixel alpha (e.g. RGB_565), then the alpha
* Bytes in the colors [] will be ignored (assumed to be FF)
* @ Throws IllegalArgumentException if the width or height are <= 0, or if
* The color array's length is less than the number of pixels.
*/
Public static Bitmap createBitmap (int colors [], int offset, int stride,
Int width, int height, Config config ){
CheckWidthHeight (width, height );
If (Math. abs (stride) <width ){
Throw new IllegalArgumentException ("abs (stride) must be> = width ");
}
Int lastScanline = offset + (height-1) * stride;
Int length = colors. length;
If (offset <0 | (offset + width> length)
| LastScanline <0
| (LastScanline + width> length )){
Throw new ArrayIndexOutOfBoundsException ();
}
// TODO: create an immutable bitmap...
Throw new UnsupportedOperationException ();
}
/**
* Returns a immutable bitmap with the specified width and height, with each
* Pixel value set to the corresponding value in the colors array.
*
* @ Param colors Array of {@ link Color} used to initialize the pixels.
* This array must be at least as large as width * height.
* @ Param width The width of the bitmap
* @ Param height The height of the bitmap
* @ Param config The bitmap config to create. If the config does not
* Support per-pixel alpha (e.g. RGB_565), then the alpha
* Bytes in the colors [] will be ignored (assumed to be FF)
* @ Throws IllegalArgumentException if the width or height are <= 0, or if
* The color array's length is less than the number of pixels.
*/
Public static Bitmap createBitmap (int colors [], int width, int height,
Config config ){
Return createBitmap (colors, 0, width, width, height, config );
}
Public static Bitmap createScaledBitmap (Bitmap src, int dstWidth,
Int dstHeight, boolean filter ){
Matrix m;
Synchronized (Bitmap. class ){
// Small pool of just 1 matrix
M = sScaleMatrix;
SScaleMatrix = null;
}
If (m = null ){
M = new Matrix ();
}
Final int width = src. getWidth ();
Final int height = src. getHeight ();
Final float sx = dstWidth/(float) width;
Final float sy = dstHeight/(float) height;
M. setScale (sx, sy );
Bitmap B = Bitmap. createBitmap (src, 0, 0, width, height, m, filter );
Synchronized (Bitmap. class ){
// Do we need to check for null? Why not just assign everytime?
If (sScaleMatrix = null ){
SScaleMatrix = m;
}
}
Return B;
}
}