Example usage for android.graphics Bitmap getConfig

List of usage examples for android.graphics Bitmap getConfig

Introduction

In this page you can find the example usage for android.graphics Bitmap getConfig.

Prototype

public final Config getConfig() 

Source Link

Document

If the bitmap's internal config is in one of the public formats, return that config, otherwise return null.

Usage

From source file:Main.java

public static Bitmap boost(Bitmap src, int type, float percent) {
    int width = src.getWidth();
    int height = src.getHeight();
    Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());

    int A, R, G, B;
    int pixel;//www  .  ja v a  2  s .c  o  m

    for (int x = 0; x < width; ++x) {
        for (int y = 0; y < height; ++y) {
            pixel = src.getPixel(x, y);
            A = Color.alpha(pixel);
            R = Color.red(pixel);
            G = Color.green(pixel);
            B = Color.blue(pixel);
            if (type == 1) {
                R = (int) (R * (1 + percent));
                if (R > 255)
                    R = 255;
            } else if (type == 2) {
                G = (int) (G * (1 + percent));
                if (G > 255)
                    G = 255;
            } else if (type == 3) {
                B = (int) (B * (1 + percent));
                if (B > 255)
                    B = 255;
            }
            bmOut.setPixel(x, y, Color.argb(A, R, G, B));
        }
    }
    return bmOut;
}

From source file:net.quduo.pixel.interfaces.android.common.ImageLoader.java

/**
 * Bitmap.getByteCount()?Bitmap??//from   w  w  w. ja va 2s. co  m
 *
 * @param bitmap Bitmap
 * @return Bitmap??
 */
public static int getByteCount(Bitmap bitmap) {

    Bitmap.Config config = bitmap.getConfig();
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int pixelByte = 4;

    // Config.ARGB_8888
    if (config == Bitmap.Config.ARGB_8888) {
        pixelByte = 4;
    } else if (config == Bitmap.Config.ALPHA_8) {
        pixelByte = 1;
    } else if (config == Bitmap.Config.ARGB_4444) {
        pixelByte = 2;
    } else if (config == Bitmap.Config.RGB_565) {
        pixelByte = 2;
    }
    return width * height * pixelByte;
}

From source file:Main.java

/**
 * Initialize the current texture and load the specified Bitmap into
 * it.//w  w w .j a  v a 2  s  . co m
 */
private static boolean loadTexture(Bitmap bmp, int allocated_width, int allocated_height) {
    int internalFormat, format, type;
    int unpackAlignment;

    switch (bmp.getConfig()) {
    case ARGB_4444:
    case ARGB_8888:
        internalFormat = format = GL_RGBA;
        type = GL_UNSIGNED_BYTE;
        unpackAlignment = 4;
        break;

    case RGB_565:
        internalFormat = format = GL_RGB;
        type = GL_UNSIGNED_SHORT_5_6_5;
        unpackAlignment = 2;
        break;

    case ALPHA_8:
        internalFormat = format = GL_ALPHA;
        type = GL_UNSIGNED_BYTE;
        unpackAlignment = 1;
        break;

    default:
        return false;
    }

    /* create an empty texture, and load the Bitmap into it */

    glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, allocated_width, allocated_height, 0, format, type, null);
    glPixelStorei(GL_UNPACK_ALIGNMENT, unpackAlignment);
    GLUtils.texSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bmp, format, type);
    return true;
}

From source file:Main.java

public static Bitmap doGray(Bitmap src) {
    final double GS_RED = 0.299;
    final double GS_GREEN = 0.587;
    final double GS_BLUE = 0.114;

    Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
    int A, R, G, B, pixel;
    int w = src.getWidth();
    int h = src.getHeight();
    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
            pixel = src.getPixel(x, y);/*from   ww  w. ja va  2  s  .co  m*/

            A = Color.alpha(pixel);
            R = Color.red(pixel);
            G = Color.green(pixel);
            B = Color.blue(pixel);
            R = G = B = (int) (GS_RED * R + GS_GREEN * G + GS_BLUE * B);
            bmOut.setPixel(x, y, Color.argb(A, R, G, B));
        }
    }
    return bmOut;
}

From source file:Main.java

/**
 * Given an input bitmap, scales it to the given width/height and makes it round.
 *
 * @param input {@link Bitmap} to scale and crop
 * @param targetWidth desired output width
 * @param targetHeight desired output height
 * @return output bitmap scaled to the target width/height and cropped to an oval. The
 *         cropping algorithm will try to fit as much of the input into the output as possible,
 *         while preserving the target width/height ratio.
 *///from w w  w . jav a 2s .  c  om
public static Bitmap getRoundedBitmap(Bitmap input, int targetWidth, int targetHeight) {
    if (input == null) {
        return null;
    }
    final Bitmap.Config inputConfig = input.getConfig();
    final Bitmap result = Bitmap.createBitmap(targetWidth, targetHeight,
            inputConfig != null ? inputConfig : Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(result);
    final Paint paint = new Paint();
    canvas.drawARGB(0, 0, 0, 0);
    paint.setAntiAlias(true);
    final RectF dst = new RectF(0, 0, targetWidth, targetHeight);
    canvas.drawOval(dst, paint);

    // Specifies that only pixels present in the destination (i.e. the drawn oval) should
    // be overwritten with pixels from the input bitmap.
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

    final int inputWidth = input.getWidth();
    final int inputHeight = input.getHeight();

    // Choose the largest scale factor that will fit inside the dimensions of the
    // input bitmap.
    final float scaleBy = Math.min((float) inputWidth / targetWidth, (float) inputHeight / targetHeight);

    final int xCropAmountHalved = (int) (scaleBy * targetWidth / 2);
    final int yCropAmountHalved = (int) (scaleBy * targetHeight / 2);

    final Rect src = new Rect(inputWidth / 2 - xCropAmountHalved, inputHeight / 2 - yCropAmountHalved,
            inputWidth / 2 + xCropAmountHalved, inputHeight / 2 + yCropAmountHalved);

    canvas.drawBitmap(input, src, dst, paint);
    return result;
}

From source file:Main.java

/**
 * Given an input bitmap, scales it to the given width/height and makes it round.
 *
 * @param input {@link Bitmap} to scale and crop
 * @param targetWidth desired output width
 * @param targetHeight desired output height
 * @return output bitmap scaled to the target width/height and cropped to an oval. The
 *         cropping algorithm will try to fit as much of the input into the output as possible,
 *         while preserving the target width/height ratio.
 *///ww w .j  a  v a  2s . c  o  m
public static Bitmap getRoundedBitmap(Bitmap input, int targetWidth, int targetHeight) {
    if (input == null) {
        return null;
    }
    final Bitmap.Config inputConfig = input.getConfig();
    final Bitmap result = Bitmap.createBitmap(targetWidth, targetHeight,
            inputConfig != null ? inputConfig : Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(result);
    final Paint paint = new Paint();
    canvas.drawARGB(0, 0, 0, 0);
    paint.setAntiAlias(true);
    canvas.drawOval(0, 0, targetWidth, targetHeight, paint);

    // Specifies that only pixels present in the destination (i.e. the drawn oval) should
    // be overwritten with pixels from the input bitmap.
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

    final int inputWidth = input.getWidth();
    final int inputHeight = input.getHeight();

    // Choose the largest scale factor that will fit inside the dimensions of the
    // input bitmap.
    final float scaleBy = Math.min((float) inputWidth / targetWidth, (float) inputHeight / targetHeight);

    final int xCropAmountHalved = (int) (scaleBy * targetWidth / 2);
    final int yCropAmountHalved = (int) (scaleBy * targetHeight / 2);

    final Rect src = new Rect(inputWidth / 2 - xCropAmountHalved, inputHeight / 2 - yCropAmountHalved,
            inputWidth / 2 + xCropAmountHalved, inputHeight / 2 + yCropAmountHalved);

    final RectF dst = new RectF(0, 0, targetWidth, targetHeight);
    canvas.drawBitmap(input, src, dst, paint);
    return result;
}

From source file:Main.java

public static Bitmap addPadding(Bitmap bmp, int color) {

    if (bmp == null) {
        return null;
    }//from   w w w .ja v  a 2s .  co m

    int biggerParam = Math.max(bmp.getWidth(), bmp.getHeight());
    Bitmap bitmap = Bitmap.createBitmap(biggerParam, biggerParam, bmp.getConfig());
    Canvas canvas = new Canvas(bitmap);
    canvas.drawColor(color);

    int top = bmp.getHeight() > bmp.getWidth() ? 0 : (bmp.getWidth() - bmp.getHeight()) / 2;
    int left = bmp.getWidth() > bmp.getHeight() ? 0 : (bmp.getHeight() - bmp.getWidth()) / 2;

    canvas.drawBitmap(bmp, left, top, null);
    return bitmap;
}

From source file:Main.java

public static Bitmap doSherpiaEffect(Bitmap src, int depth, double red, double green, double blue) {
    int width = src.getWidth();
    int height = src.getHeight();
    Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
    final double GS_RED = 0.3;
    final double GS_GREEN = 0.59;
    final double GS_BLUE = 0.11;
    int A, R, G, B;
    int pixel;//  ww w. ja  va  2  s  .  c  om

    for (int x = 0; x < width; ++x) {
        for (int y = 0; y < height; ++y) {
            pixel = src.getPixel(x, y);
            A = Color.alpha(pixel);
            R = Color.red(pixel);
            G = Color.green(pixel);
            B = Color.blue(pixel);
            B = G = R = (int) (GS_RED * R + GS_GREEN * G + GS_BLUE * B);

            R += (depth * red);
            if (R > 255) {
                R = 255;
            }

            G += (depth * green);
            if (G > 255) {
                G = 255;
            }

            B += (depth * blue);
            if (B > 255) {
                B = 255;
            }

            bmOut.setPixel(x, y, Color.argb(A, R, G, B));
        }
    }
    return bmOut;
}

From source file:Main.java

public static Bitmap drawTextToBitmap(Bitmap bitmap, String gText) {
    //Resources resources = gContext.getResources();
    //float scale = resources.getDisplayMetrics().density;

    android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
    // set default bitmap config if none
    if (bitmapConfig == null) {
        bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
    }// w  w w .  j  ava 2  s. co  m
    // resource bitmaps are imutable,
    // so we need to convert it to mutable one
    bitmap = bitmap.copy(bitmapConfig, true);
    Canvas canvas = new Canvas(bitmap);
    // new antialised Paint
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    // text color - #3D3D3D
    paint.setColor(Color.rgb(61, 61, 61));
    // text size in pixels
    paint.setTextSize((int) (21)); //* scale));
    // text shadow
    paint.setShadowLayer(2f, 1f, 1f, Color.WHITE);
    // draw text to the Canvas center
    //Rect bounds = new Rect();
    //paint.getTextBounds(gText, 0, gText.length(), bounds);
    int x = bitmap.getWidth() - 150;//bounds.width()) - 150;
    int y = bitmap.getHeight() - 27;//bounds.height()) - 30;
    // fill
    canvas.drawRect(x, y, x + 150, y + 27, paint);
    canvas.drawText(gText, x, y + 20, paint);
    return bitmap;
}

From source file:Main.java

public static Double getBitmapsize(Bitmap bitmap, int sizeType) {
    long fileS = 0;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
        //            fileS = bitmap.getByteCount();
        Bitmap.Config config = bitmap.getConfig();
        if (config == Bitmap.Config.RGB_565 || config == Bitmap.Config.ARGB_4444) {
            fileS = bitmap.getWidth() * bitmap.getHeight() * 2;
        } else if (config == Bitmap.Config.ALPHA_8) {
            fileS = bitmap.getWidth() * bitmap.getHeight();
        } else if (config == Bitmap.Config.ARGB_8888) {
            fileS = bitmap.getWidth() * bitmap.getHeight() * 4;
        }/*from   w  ww  .j a  v  a2  s  . c  o m*/
    } else {
        fileS = bitmap.getRowBytes() * bitmap.getHeight();// Pre HC-MR1
    }

    DecimalFormat df = new DecimalFormat("#.00");
    double fileSizeLong = 0;
    switch (sizeType) {
    case SIZETYPE_B:
        fileSizeLong = Double.valueOf(df.format((double) fileS));
        break;
    case SIZETYPE_KB:
        fileSizeLong = Double.valueOf(df.format((double) fileS / 1024));
        break;
    case SIZETYPE_MB:
        fileSizeLong = Double.valueOf(df.format((double) fileS / 1048576));
        break;
    case SIZETYPE_GB:
        fileSizeLong = Double.valueOf(df.format((double) fileS / 1073741824));
        break;
    default:
        break;
    }
    return fileSizeLong;
}