Example usage for android.graphics Bitmap createScaledBitmap

List of usage examples for android.graphics Bitmap createScaledBitmap

Introduction

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

Prototype

public static Bitmap createScaledBitmap(@NonNull Bitmap src, int dstWidth, int dstHeight, boolean filter) 

Source Link

Document

Creates a new bitmap, scaled from an existing bitmap, when possible.

Usage

From source file:Main.java

public static Bitmap scaleAndFrame(Bitmap bitmap, int width, int height) {
    final int bitmapWidth = bitmap.getWidth();
    final int bitmapHeight = bitmap.getHeight();

    final float scale = Math.min((float) width / (float) bitmapWidth, (float) height / (float) bitmapHeight);

    final int scaledWidth = (int) (bitmapWidth * scale);
    final int scaledHeight = (int) (bitmapHeight * scale);

    final Bitmap decored = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true);
    final Canvas canvas = new Canvas(decored);

    final int offset = (int) (PHOTO_BORDER_WIDTH / 2);
    sStrokePaint.setAntiAlias(false);/*from   w ww.j a  va  2 s .  co  m*/
    canvas.drawRect(offset, offset, scaledWidth - offset - 1, scaledHeight - offset - 1, sStrokePaint);
    sStrokePaint.setAntiAlias(true);

    return decored;

}

From source file:Main.java

public static Bitmap load_bitmap_from_uri_string(Context context, String uri, int sizeX, int sizeY) {
    Bitmap small_bitmap = null;/*from   w  w  w  .  ja  v a 2  s  . c  om*/

    Uri img_uri = Uri.parse(uri);
    if (uri != null) {
        try {

            InputStream inputStream = context.getContentResolver().openInputStream(img_uri);
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            small_bitmap = Bitmap.createScaledBitmap(bitmap, sizeX, sizeY, false);
            bitmap.recycle();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    return small_bitmap;
}

From source file:Main.java

public static Bitmap getCroppedRoundBitmap(Bitmap bmp, int radius) {
    Bitmap scaledSrcBmp;/*from  w ww .ja  va2  s  .co m*/
    int diameter = radius * 2;
    int bmpWidth = bmp.getWidth();
    int bmpHeight = bmp.getHeight();
    int squareWidth = 0, squareHeight = 0;
    int x = 0, y = 0;
    Bitmap squareBitmap;
    if (bmpHeight > bmpWidth) {
        squareWidth = squareHeight = bmpWidth;
        x = 0;
        y = (bmpHeight - bmpWidth) / 2;
        squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth, squareHeight);
    } else if (bmpHeight < bmpWidth) {
        squareWidth = squareHeight = bmpHeight;
        x = (bmpWidth - bmpHeight) / 2;
        y = 0;
        squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth, squareHeight);
    } else {
        squareBitmap = bmp;
    }

    if (squareBitmap.getWidth() != diameter || squareBitmap.getHeight() != diameter) {
        scaledSrcBmp = Bitmap.createScaledBitmap(squareBitmap, diameter, diameter, true);

    } else {
        scaledSrcBmp = squareBitmap;
    }
    Bitmap output = Bitmap.createBitmap(scaledSrcBmp.getWidth(), scaledSrcBmp.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    Paint paint = new Paint();
    Rect rect = new Rect(0, 0, scaledSrcBmp.getWidth(), scaledSrcBmp.getHeight());

    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawCircle(scaledSrcBmp.getWidth() / 2, scaledSrcBmp.getHeight() / 2, scaledSrcBmp.getWidth() / 2,
            paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(scaledSrcBmp, rect, rect, paint);
    return output;
}

From source file:Main.java

public static Bitmap getScaledBitmapFromBitmap(Bitmap bmp, int maxWidth, int maxHeight, boolean keepAspectRatio,
        boolean freeOldBitmap) {
    if (keepAspectRatio) {
        float ratio = bmp.getWidth() == 0 ? 1.f : (bmp.getWidth() / (float) bmp.getHeight());
        float newRatio = maxHeight == 0 ? 1.f : (maxWidth / (float) maxHeight);

        if (newRatio > ratio) {
            maxWidth = (int) ((float) maxHeight * ratio);
        } else if (newRatio < ratio) {
            maxHeight = (int) ((float) maxWidth / ratio);
        }/*from   www  . j  a va  2s  .  c  om*/
    }

    Bitmap scaled = Bitmap.createScaledBitmap(bmp, maxWidth, maxHeight, true);

    if (freeOldBitmap && scaled != bmp) {
        bmp.recycle();
    }

    return scaled;
}

From source file:Main.java

public static Bitmap compressPixel1(String imgPath, float pixelW, float pixelH) {
    BitmapFactory.Options newOpts = new BitmapFactory.Options();
    newOpts.inJustDecodeBounds = true;/*from  w  ww.j  av a 2 s  .  c om*/
    newOpts.inPreferredConfig = Config.RGB_565;
    Bitmap bitmap = BitmapFactory.decodeFile(imgPath, newOpts);
    newOpts.inJustDecodeBounds = false;
    int w = newOpts.outWidth;
    int h = newOpts.outHeight;
    float hh = pixelH;
    float ww = pixelW;
    int height = h;
    int width = w;
    int inSampleSize = 1;
    if (h > hh || w > ww) {
        final int halfHeight = h / 2;
        final int halfWidth = w / 2;
        while ((halfHeight / inSampleSize) > hh && (halfWidth / inSampleSize) > ww) {
            inSampleSize *= 2;
        }

        if (h > w) {
            height = 1280;
            width = (1280 * w) / h;
        } else {
            width = 1280;
            height = (1280 * h) / w;
        }
    }
    newOpts.inSampleSize = inSampleSize;
    bitmap = BitmapFactory.decodeFile(imgPath, newOpts);

    Bitmap dst = Bitmap.createScaledBitmap(bitmap, width, height, false);
    if (bitmap != dst) {
        bitmap.recycle();
    }

    return dst;
}

From source file:Main.java

public static Drawable load_drawable_from_uri_string(Context context, String uri, int sizeX, int sizeY) {
    Drawable drawable = null;/* w  ww.ja  v a2 s .co m*/
    Uri img_uri = Uri.parse(uri);
    if (uri != null) {
        try {

            InputStream inputStream = context.getContentResolver().openInputStream(img_uri);
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            Bitmap small_bitmap = Bitmap.createScaledBitmap(bitmap, sizeX, sizeY, false);
            bitmap.recycle();
            drawable = new BitmapDrawable(context.getResources(), small_bitmap);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    return drawable;
}

From source file:Main.java

public static Bitmap resize(Bitmap bMap) {
    if ((float) bMap.getHeight() / (float) bMap.getWidth() == .5625
            || (float) bMap.getWidth() / (float) bMap.getHeight() == .5625) { //16/9
        if (bMap.getHeight() < bMap.getWidth()) {
            //bMap = Bitmap.createScaledBitmap(bMap, 1024, 576, false);
            bMap = Bitmap.createScaledBitmap(bMap, 1280, 720, false); //720p
        } else {//w ww  . jav a2s  . c o m
            //bMap = Bitmap.createScaledBitmap(bMap, 576, 1024, false);
            bMap = Bitmap.createScaledBitmap(bMap, 720, 1280, false); //720p
        }
    } else if ((float) bMap.getHeight() / (float) bMap.getWidth() == .625
            || (float) bMap.getWidth() / (float) bMap.getHeight() == .625) { //16/10
        if (bMap.getHeight() < bMap.getWidth()) {
            //bMap = Bitmap.createScaledBitmap(bMap, 960, 600, false);
            bMap = Bitmap.createScaledBitmap(bMap, 1152, 720, false); //720p
        } else {
            //bMap = Bitmap.createScaledBitmap(bMap, 600, 960, false);
            bMap = Bitmap.createScaledBitmap(bMap, 720, 1152, false); //720p
        }
    } else if ((float) bMap.getHeight() / (float) bMap.getWidth() == .75
            || (float) bMap.getWidth() / (float) bMap.getHeight() == .75) { //4/3
        if (bMap.getHeight() < bMap.getWidth()) {
            bMap = Bitmap.createScaledBitmap(bMap, 960, 720, false);
        } else {
            bMap = Bitmap.createScaledBitmap(bMap, 720, 960, false);
        }
    }
    return bMap;
}

From source file:gowtham.com.desknote.MyListener.java

private String bitmap2Base64(Bitmap b) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    //Bitmap gray = toGrayscale(b);
    // Windows needs atleast 48x48 image, otherwise, notification is not shown
    Bitmap smaller = b.createScaledBitmap(b, 48, 48, false);
    // PNG is lossless. So, quality setting is unused
    smaller.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] buf = baos.toByteArray();
    return new String(Base64.encode(buf, Base64.NO_WRAP));
}

From source file:Main.java

private static float[] getMainHSV(Bitmap bm) {
    Bitmap onePixelBitmap = Bitmap.createScaledBitmap(bm, 1, 1, true);
    int pixel = onePixelBitmap.getPixel(0, 0);

    int red = Color.red(pixel);
    int blue = Color.blue(pixel);
    int green = Color.green(pixel);

    float[] hsvVals = new float[3];
    Color.RGBToHSV(red, green, blue, hsvVals);
    return hsvVals;
}

From source file:Main.java

public static Bitmap cropMaxVisibleBitmap(Drawable drawable, int iconSize) {
    Bitmap tmp = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            Bitmap.Config.ARGB_8888);//from   ww w .j a  va 2s  . c o  m
    Canvas canvas = new Canvas(tmp);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    Rect crop = new Rect(tmp.getWidth(), tmp.getHeight(), -1, -1);
    for (int y = 0; y < tmp.getHeight(); y++) {
        for (int x = 0; x < tmp.getWidth(); x++) {
            int alpha = (tmp.getPixel(x, y) >> 24) & 255;
            if (alpha > 0) { // pixel is not 100% transparent
                if (x < crop.left)
                    crop.left = x;
                if (x > crop.right)
                    crop.right = x;
                if (y < crop.top)
                    crop.top = y;
                if (y > crop.bottom)
                    crop.bottom = y;
            }
        }
    }

    if (crop.width() <= 0 || crop.height() <= 0) {
        return Bitmap.createScaledBitmap(tmp, iconSize, iconSize, true);
    }

    // We want to crop a square region.
    float size = Math.max(crop.width(), crop.height());
    float xShift = (size - crop.width()) * 0.5f;
    crop.left -= Math.floor(xShift);
    crop.right += Math.ceil(xShift);

    float yShift = (size - crop.height()) * 0.5f;
    crop.top -= Math.floor(yShift);
    crop.bottom += Math.ceil(yShift);

    Bitmap finalImage = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888);
    canvas.setBitmap(finalImage);
    float scale = iconSize / size;

    canvas.scale(scale, scale);
    canvas.drawBitmap(tmp, -crop.left, -crop.top, new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
    canvas.setBitmap(null);
    return finalImage;
}