Android Bitmap Corner Round getRoundBitmapFromUrl(String url, int pixels)

Here you can find the source of getRoundBitmapFromUrl(String url, int pixels)

Description

get Round Bitmap From Url

Declaration

public static Bitmap getRoundBitmapFromUrl(String url, int pixels)
            throws Exception 

Method Source Code

//package com.java2s;

import java.io.ByteArrayOutputStream;

import java.io.InputStream;
import java.net.HttpURLConnection;

import java.net.URL;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

import android.graphics.Paint;

import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;

public class Main {
    public static Bitmap getRoundBitmapFromUrl(String url, int pixels)
            throws Exception {
        byte[] bytes = getBytesFromUrl(url);
        Bitmap bitmap = byteToBitmap(bytes);
        return toRoundCorner(bitmap, pixels);
    }/*from w ww .ja  v  a  2s  . com*/

    public static byte[] getBytesFromUrl(String url) throws Exception {
        return readInputStream(getRequest(url));
    }

    public static Bitmap byteToBitmap(byte[] byteArray) {
        if (byteArray.length != 0) {
            return BitmapFactory.decodeByteArray(byteArray, 0,
                    byteArray.length);
        } else {
            return null;
        }
    }

    public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {

        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 = pixels;

        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;
    }

    public static BitmapDrawable toRoundCorner(
            BitmapDrawable bitmapDrawable, int pixels) {
        Bitmap bitmap = bitmapDrawable.getBitmap();
        bitmapDrawable = new BitmapDrawable(toRoundCorner(bitmap, pixels));
        return bitmapDrawable;
    }

    public static byte[] readInputStream(InputStream inStream)
            throws Exception {
        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outSteam.write(buffer, 0, len);
        }
        outSteam.close();
        inStream.close();
        return outSteam.toByteArray();
    }

    public static InputStream getRequest(String path) throws Exception {
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setConnectTimeout(5000);
        if (conn.getResponseCode() == 200) {
            return conn.getInputStream();
        }
        return null;
    }
}

Related

  1. roundCornersRGB888(Bitmap src, int roundPixles)
  2. getRoundedCornerBitmap(Context context, Bitmap bitmap, Boolean create_circle)
  3. getRoundedCornerBitmap(Bitmap bitmap)
  4. getRoundedCornerBitmap(Bitmap bitmap, float roundPx)
  5. getRoundedCornerBitmap(Bitmap bitmap, int roundPx)
  6. toRoundCorner(Bitmap bitmap, int pixels)
  7. toRoundCorner( BitmapDrawable bitmapDrawable, int pixels)
  8. getRoundedCornerBitmap(Bitmap bitmap, float roundPx)
  9. getRoundedCornerBitmap(Bitmap bitmap, int maxHeight, int pixels)