Example usage for android.graphics Bitmap createBitmap

List of usage examples for android.graphics Bitmap createBitmap

Introduction

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

Prototype

public static Bitmap createBitmap(int width, int height, @NonNull Config config) 

Source Link

Document

Returns a mutable bitmap with the specified width and height.

Usage

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);/*w  w  w.j  av  a  2s.c om*/

            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

public static Bitmap getViewBitmap(View v) {
    if (v.getWidth() == 0 || v.getHeight() == 0)
        return null;
    Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    v.draw(c);/*from   w  w w  .j  a  v  a 2 s. c om*/
    return b;
}

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;/*from ww w  .  j  av 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:Main.java

public static BitmapDrawable createDrawableFromView(Context context, View v) {
    Bitmap b = Bitmap.createBitmap(v.getLayoutParams().width, v.getLayoutParams().height,
            Bitmap.Config.ARGB_8888);//from w w  w  .  ja va 2  s .c om
    Canvas c = new Canvas(b);
    v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
    v.draw(c);
    return new BitmapDrawable(context.getResources(), b);
}

From source file:Main.java

public static Bitmap cutBitmap(Bitmap mBitmap, Rect r, Bitmap.Config config) {
    int width = r.width();
    int height = r.height();

    Bitmap croppedImage = Bitmap.createBitmap(width, height, config);

    Canvas cvs = new Canvas(croppedImage);
    Rect dr = new Rect(0, 0, width, height);
    cvs.drawBitmap(mBitmap, r, dr, null);
    return croppedImage;
}

From source file:Main.java

private static Bitmap toBitmap(Drawable drawable, int width, int height) {

    Bitmap bmp = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas c = new Canvas(bmp);
    drawable.setBounds(new Rect(0, 0, width, height));
    drawable.draw(c);//from w w w. j av  a  2 s.c o m

    return bmp;
}

From source file:Main.java

public static Bitmap toThreshold(Bitmap bmpOriginal) {
    int imageWidth = bmpOriginal.getWidth();
    int imageHeight = bmpOriginal.getHeight();
    Bitmap bmpThreshold = Bitmap.createBitmap(imageWidth, imageHeight, Bitmap.Config.RGB_565);
    int[] buffer = new int[imageWidth];

    int grayVal;/*from www.j a  v a2 s. c o m*/
    for (int row = 0; row < imageHeight; row++) {
        for (int col = 0; col < imageWidth; col++) {
            grayVal = rgbToGray(bmpOriginal.getPixel(col, row));
            if (grayVal > 125)
                buffer[col] = Color.rgb(255, 255, 255);
            else
                buffer[col] = Color.rgb(0, 0, 0);
        }
        bmpThreshold.setPixels(buffer, 0, imageWidth, 0, row, imageWidth, 1);
    }
    return bmpThreshold;
}

From source file:Main.java

public static Bitmap ARGBToBitmap(int width, int height, int[] data) {
    int ilength = data.length;
    int[] colors = new int[ilength];
    System.arraycopy(data, 0, colors, 0, ilength);
    Bitmap bmp = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    return bmp;//from  w  w w .j  a va2  s .c  om
}

From source file:Main.java

public static Bitmap captureScreen(Activity context) {
    View cv = context.getWindow().getDecorView();
    Bitmap bmp = Bitmap.createBitmap(cv.getWidth(), cv.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bmp);
    cv.draw(canvas);//from   ww w  .  ja v  a 2  s. c o m
    return bmp;
}

From source file:Main.java

public static Bitmap drawable2Bitmap(Drawable d) {
    if (d == null) {
        return null;
    }//from  w w w.java 2  s .  c  o m
    Bitmap bm = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bm);
    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
    d.draw(canvas);
    return bm;
}