time

How to work with Bitmap in Android

How to work with Bitmap in Android

A bitmap (or raster graphic) is a digital image composed of a matrix of dots. When viewed at 100%, each dot corresponds to an individual pixel on a display. In a standard bitmap image, each dot can be assigned a different color.

In this instance we will simply create a Bitmap directly:

Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);

A bitmap is simply a rectangle of pixels. Each pixel can be set to a given color but exactly what color depends on the type of the pixel. The first two parameters give the width and the height in pixels. The third parameter specifies the type of pixel you want to use. This is where things can get complicated. The specification ARGB_8888 means create a pixel with four channels ARGB - Alpha, Red, Green, Blue and allocate each 8 bits of storage. As four eights are 32 this is 32 bit graphics. The alpha channel lets you set an opacity.

You can get Bitmap from Drawables via following code.

ImageView image = (ImageView) findViewById(R.id.image);
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.car);
image.setImageBitmap(b);

To convert a Bitmap object into a Drawable you can use the following code.

Drawable d = new BitmapDrawable(getResources(), bitmap);

If you want get Bitmap from gallery or camera read How to get image from Gallery or Camera on Android.

Scaling a Bitmap

If we need to resize a Bitmap, we can call the createScaledBitmap method to resize any bitmap to our desired width and height:

// load a bitmap from the drawable folder
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.car);

// resize the bitmap to 150x100 (width x height)
Bitmap scaled = Bitmap.createScaledBitmap(b, 150, 100, true);

// loads the resized Bitmap into an ImageView
ImageView image = (ImageView) findViewById(R.id.image);
image.setImageBitmap(scaled);

You often want to resize a bitmap but preserve the aspect ratio using a BitmapScaler utility class with code like this:

public class BitmapScaler{
    // Scale and maintain aspect ratio given a desired width
    // BitmapScaler.scaleToFitWidth(bitmap, 100);
    public static Bitmap scaleToFitWidth(Bitmap b, int width) {
        float factor = width / (float) b.getWidth();
        return Bitmap.createScaledBitmap(b, width, (int) (b.getHeight() * factor), true);
    }

    // Scale and maintain aspect ratio given a desired height
    // BitmapScaler.scaleToFitHeight(bitmap, 100);
    public static Bitmap scaleToFitHeight(Bitmap b, int height) {
        float factor = height / (float) b.getHeight();
        return Bitmap.createScaledBitmap(b, (int) (b.getWidth() * factor), height, true);
    }

    // ...
}

Combine multiple bitmaps to a single bitmap

Bitmap image1 = BitmapFactory.decodeResource(getResources(), R.drawable.image1);
Bitmap image2 = BitmapFactory.decodeResource(getResources(), R.drawable.image2);

Bitmap merged = Bitmap.createBitmap(image1.getWidth(), image1.getHeight(), image1.getConfig());
Canvas canvas = new Canvas(merged);
canvas.drawBitmap(image1, 0f, 0f, null);
canvas.drawBitmap(image2, 10, 10, null);

imageView.setImageBitmap(merged);

Rounded corners

In Android, you can create bitmaps with anti-aliased rounded corners on the fly using the code snippet below.

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = 12;

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

How to create a circle bitmap

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_photo);

    Bitmap img = BitmapFactory.decodeResource(getResources(), R.drawable.girl);
    ImageView iv = findViewById(R.id.imageView);
    iv.setImageBitmap(getCircleBitmap1(img));
}


public Bitmap getCircleBitmap1(Bitmap source) {
    int size = Math.min(source.getHeight(), source.getWidth());

    Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    int color = Color.RED;
    Paint paint = new Paint();
    Rect rect = new Rect(0, 0, size, size);
    RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);

    canvas.drawOval(rectF, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(source, rect, rect, paint);

    return output;
}

public Bitmap getCircleBitmap2(Bitmap source) {
    int size = Math.min(source.getWidth(), source.getHeight());

    int x = (source.getWidth() - size) / 2;
    int y = (source.getHeight() - size) / 2;

    Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
    if (squaredBitmap != source) {
        source.recycle();
    }

    Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());

    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
    paint.setShader(shader);
    paint.setAntiAlias(true);

    float r = size / 2f;
    canvas.drawCircle(r, r, r, paint);

    squaredBitmap.recycle();
    return bitmap;
}

public Bitmap getCircleBitmap3(Bitmap source) {
    RoundedBitmapDrawable roundedBitmapDrawable= RoundedBitmapDrawableFactory.create(getResources(), source);
    roundedBitmapDrawable.setCircular(true);
    //int size = Math.min(source.getWidth(), source.getHeight());
    //float r = size/2.0f;
    //roundedBitmapDrawable.setCornerRadius(r);
    return drawableToBitmap(roundedBitmapDrawable);
}

public static Bitmap drawableToBitmap (Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable)drawable).getBitmap();
    }

    int width = drawable.getIntrinsicWidth();
    width = width > 0 ? width : 1;
    int height = drawable.getIntrinsicHeight();
    height = height > 0 ? height : 1;

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}

Converting a File object to Bitmap

Bitmap bitmap = BitmapFactory.decodeFile(fileObj.getAbsolutePath());
imageView.setImageBitmap(bitmap);

Save Bitmap to file

Bitmap bitmap = Bitmap.createScaledBitmap(b, 150, 150, false);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();

bitmap.compress(Bitmap.CompressFormat.JPEG, 70, outStream);
//bitmap.compress(CompressFormat.PNG, 0, outStream);

File f = new File(Environment.getExternalStorageDirectory() + File.separator + "filename.jpg");
f.createNewFile();

try {
    FileOutputStream fo = new FileOutputStream(f);
    fo.write(outStream.toByteArray());

    fo.flush();
    fo.close();
} catch (FileNotFoundException e) {
    Log.w("TAG", "Error saving image file: " + e.getMessage());
    return false;
} catch (IOException e) {
    Log.w("TAG", "Error saving image file: " + e.getMessage());
    return false;
}

Send Bitmap using Intent

In your calling activity.

Intent i = new Intent(this, NextActivity.class);
Bitmap b; // your bitmap
ByteArrayOutputStream bs = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 50, bs);
i.putExtra("byteArray", bs.toByteArray());
startActivity(i);

In your receiving activity

if (getIntent().hasExtra("byteArray")) {
    ImageView preview = new ImageView(this);
    byte[] byteArray = getIntent().getByteArrayExtra("byteArray");
    Bitmap b = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
    preview.setImageBitmap(b);
}

Download Bitmap from URL

public Bitmap getBitmapFromURL(String strURL) {
    try {
        URL url = new URL(strURL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

How to draw linear gradient, rectangle and circle on Bitmap

iv = (ImageView) findViewById(R.id.iv);

Bitmap bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);

// BG
Paint paintBg = new Paint();
paintBg.setColor(Color.GRAY);
canvas.drawRect(0, 0, 200, 200, paintBg);

// GRADIENT
int gradientWidth = bitmap.getWidth();
int gradientHeight = 40;

Shader shader = new LinearGradient(0, 0, gradientWidth, gradientHeight, 
    new int[] {Color.RED, Color.TRANSPARENT}, null, Shader.TileMode.CLAMP);
Paint paintGradient = new Paint();
paintGradient.setShader(shader);

canvas.drawRect(0, 0, gradientWidth, gradientHeight, paintGradient);

// CIRCLE
Paint paintCircle = new Paint();
paintCircle.setColor(Color.GREEN);
paintCircle.setAntiAlias(true);
canvas.drawCircle(100, 100, 40, paintCircle);

Result

android_bitmap_gradient.png

How to draw text on Bitmap

iv = (ImageView) findViewById(R.id.iv);

Bitmap bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);

// BG
Paint paintBg = new Paint();
paintBg.setColor(Color.GRAY);
canvas.drawRect(0, 0, 200, 200, paintBg);

String string = "Hello Android";
float scale = getResources().getDisplayMetrics().density;
Paint paintText = new Paint(Paint.ANTI_ALIAS_FLAG);
paintText.setColor(Color.rgb(0, 0, 0));
paintText.setTextSize((int) (14 * scale));
paintText.setShadowLayer(1f, 0f, 1f, Color.WHITE);

// draw text in the center
Rect bounds = new Rect();
paintText.getTextBounds(string, 0, string.length(), bounds);

int x = (bitmap.getWidth() - bounds.width())/2;
int y = (bitmap.getHeight() + bounds.height())/2;

canvas.drawText(string, x, y, paintText);

iv.setImageBitmap(bitmap);    

Result

android_bitmap_text.png

How to center and crop Bitmap

public static Bitmap cropCenter(Bitmap bmp) {
    // use the smallest dimension of the image to crop to
    int dimension = Math.min(bmp.getWidth(), bmp.getHeight());
    return ThumbnailUtils.extractThumbnail(bmp, dimension, dimension);
}

Here extractThumbnail creates a centered bitmap of the desired size.

How to convert dp to px

public static int dp2px(int dp, Context context) {
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, metrics);
}

public static float px2dp(float px, Context context)  {
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    return (float) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, px, metrics);
}
comments powered by Disqus