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

/**
 * Shrink the bitmap to the specified scale.
 *
 * @param bitmap to shrink./*ww w.j  a v a2s.c om*/
 * @param scale  the shrink scale, must be < 1.0.
 * @return the shrunk bitmap.
 */
public static Bitmap shrink(Bitmap bitmap, float scale) {
    if (scale >= 1.0f) {
        return bitmap.copy(bitmap.getConfig(), false);
    }

    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);

    return Bitmap.createBitmap(bitmap, 0, 0, (int) (scale * bitmap.getWidth()),
            (int) (scale * bitmap.getHeight()), matrix, true);
}

From source file:Main.java

public static Bitmap forceConfig565(Bitmap original) {
    Bitmap convertedBitmap = original;//from   w ww  . ja v a2  s . c o  m
    if (original.getConfig() != Bitmap.Config.RGB_565) {
        convertedBitmap = Bitmap.createBitmap(original.getWidth(), original.getHeight(), Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(convertedBitmap);
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        canvas.drawBitmap(original, 0, 0, paint);

        if (convertedBitmap != original) {
            original.recycle();
        }
    }

    return convertedBitmap;
}

From source file:Main.java

public static Bitmap doInvert(Bitmap src) {
    Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
    int A, R, G, B;
    int pixelColor;
    int w = src.getWidth();
    int h = src.getHeight();
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            pixelColor = src.getPixel(x, y);
            A = Color.alpha(pixelColor);
            R = 255 - Color.red(pixelColor);
            G = 255 - Color.green(pixelColor);
            B = 255 - Color.blue(pixelColor);
            bmOut.setPixel(x, y, Color.argb(A, R, G, B));
        }//from   w  ww  . ja v a  2 s . c  om
    }
    return bmOut;
}

From source file:Main.java

/**
 * //from  w w  w .ja  v  a  2s  . co  m
 * @return Bitmap's RGBA byte array
 */
public static byte[] getImageRGBA(Bitmap inputBitmap) {
    Config config = inputBitmap.getConfig();
    ByteBuffer buffer;

    Bitmap bitmap;
    /**
     * if bitmap size is not 32*32 create scaled bitmap
     */

    if (inputBitmap.getWidth() != 32 || inputBitmap.getHeight() != 32) {
        Log.d(TAG, "bitmap resized to 32x32");
        bitmap = Bitmap.createScaledBitmap(inputBitmap, 32, 32, false);
    } else {
        bitmap = inputBitmap;
    }
    /**
     * if bitmap is not ARGB_8888 format, copy bitmap with ARGB_8888 format
     */
    if (!config.equals(Bitmap.Config.ARGB_8888)) {
        Bitmap bitmapARBG = bitmap.copy(Bitmap.Config.ARGB_8888, false);
        buffer = ByteBuffer.allocate(bitmapARBG.getByteCount());
        bitmapARBG.copyPixelsToBuffer(buffer);
        bitmapARBG.recycle();
    } else {
        buffer = ByteBuffer.allocate(bitmap.getByteCount());
        bitmap.copyPixelsToBuffer(buffer);
    }
    return buffer.array();
}

From source file:Main.java

public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
    Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
    Canvas canvas = new Canvas(bmOverlay);
    canvas.drawBitmap(bmp1, new Matrix(), null);
    canvas.drawBitmap(bmp2, 0, 0, null);
    return bmOverlay;
}

From source file:Main.java

public static Bitmap overlayToDownCenter(Bitmap bmp1, Bitmap bmp2) {
    Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
    Canvas canvas = new Canvas(bmOverlay);
    canvas.drawBitmap(bmp1, new Matrix(), null);
    Matrix matrix = new Matrix();
    matrix.setTranslate(((float) bmp1.getWidth() - bmp2.getWidth()) / 2, bmp1.getHeight() - bmp2.getHeight());
    canvas.drawBitmap(bmp2, matrix, null);
    return bmOverlay;
}

From source file:Main.java

public static Bitmap overlayToDownRightCorner(Bitmap bmp1, Bitmap bmp2) {
    Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
    Canvas canvas = new Canvas(bmOverlay);
    canvas.drawBitmap(bmp1, new Matrix(), null);
    Matrix matrix = new Matrix();
    matrix.setTranslate(bmp1.getWidth() - bmp2.getWidth(), bmp1.getHeight() - bmp2.getHeight());
    canvas.drawBitmap(bmp2, matrix, null);
    return bmOverlay;
}

From source file:Main.java

public static Bitmap drawTextToBitmap(Bitmap bitmap, String gText) {
    android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
    // set default bitmap config if none
    if (bitmapConfig == null) {
        bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
    }//from   www.j a  v  a 2 s.  co m
    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);
    int x = bitmap.getWidth() - 150;//bounds.width()) - 150;
    int y = bitmap.getHeight() - 27;//bounds.height()) - 30;
    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 Bitmap blurBitmap(Context context, Bitmap bitmap, float blur) {
    Bitmap bitmapCopy = bitmap.copy(bitmap.getConfig(), false);
    //Let's create an empty bitmap with the same size of the bitmap we want to blur
    Bitmap outBitmap = (Bitmap.createBitmap(bitmapCopy.getWidth(), bitmapCopy.getHeight(),
            Bitmap.Config.ARGB_8888));//from   w  ww  .  j  av  a2  s . c  o  m

    //Instantiate a new Renderscript
    RenderScript rs = RenderScript.create(context);

    //Create an Intrinsic Blur Script using the Renderscript
    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    //Create the in/out Allocations with the Renderscript and the in/out bitmaps
    Allocation allIn = Allocation.createFromBitmap(rs, bitmapCopy);
    Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);

    //Set the radius of the blur
    blurScript.setRadius(blur);

    //Perform the Renderscript
    blurScript.setInput(allIn);
    blurScript.forEach(allOut);

    //Copy the final bitmap created by the out Allocation to the outBitmap
    allOut.copyTo(outBitmap);

    bitmapCopy.recycle();
    //After finishing everything, we destroy the Renderscript.
    rs.destroy();
    return outBitmap;
}

From source file:Main.java

public static Bitmap getGrayscale(@NonNull Bitmap src) {

    Bitmap dest = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());

    Canvas canvas = new Canvas(dest);
    Paint paint = new Paint();
    ColorMatrixColorFilter filter = new ColorMatrixColorFilter(MATRIX);
    paint.setColorFilter(filter);//from   w  w  w  .  j  a va 2 s  .  com
    canvas.drawBitmap(src, 0, 0, paint);

    return dest;
}