Bitmap Source code example in Android development

Source: Internet
Author: User
Tags abs config final

Bitmap Source code example 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 of 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 is the same object as source, or a copy may

* have been made.

*

* @param source The bitmap we are subsetting

* @param x The coordinate of the the ' the ' pixel in source

* @param y coordinate of the ' the ' the ' 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 coordinate of the the ' the ' pixel in source

* @param y coordinate of the ' the ' the ' pixel in source

* @param width The number of pixels in each row

* @param height the number of rows

* @param m Option matrix to is applied to the pixels

* @param filter True if the source should 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 to 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 should 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 of the bitmap

* @param 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 the

* Array of colors.

* @param stride Number of colors in the array between rows (must is >=

* Width or <=-width).

* @param width of the bitmap

* @param 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[] 'll 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 is at least as large as width * height.

* @param width of the bitmap

* @param 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[] 'll 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;

}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.