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 scaleBitmap(Bitmap bitmap, float scale) {
    int width = (int) ((float) bitmap.getWidth() * scale);
    int height = (int) ((float) bitmap.getHeight() * scale);
    if (bitmap.getWidth() != width || bitmap.getHeight() != height) {
        return Bitmap.createScaledBitmap(bitmap, width, height, true /* filter */);
    } else {//from  www . jav a  2s  .  c  o m
        return bitmap;
    }
}

From source file:Main.java

/**
 * draw the image to viewBitmap in scale mode.
 * @param viewBitmap Bitmap to be displayed on the device.
 * @param bitmap Bitmap image./*from  w  w  w  .  ja va  2s.  com*/
 */
public static void drawImageForScalesMode(final Bitmap viewBitmap, final Bitmap bitmap) {

    float startGridX = 0;
    float startGridY = 0;

    float getSizeW = bitmap.getWidth();
    float getSizeH = bitmap.getHeight();

    float scale;
    final int width = viewBitmap.getWidth();
    final int height = viewBitmap.getHeight();
    if ((getSizeW / width) > (getSizeH / height)) {
        scale = width / getSizeW;
    } else {
        scale = height / getSizeH;
    }

    int targetW = (int) Math.ceil(scale * getSizeW);
    int targetH = (int) Math.ceil(scale * getSizeH);

    Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, targetW, targetH, false);

    if ((getSizeW / width) > (getSizeH / height)) {
        startGridY = (height / 2 - targetH / 2);
    } else {
        startGridX = (width / 2 - targetW / 2);
    }

    Canvas canvas = new Canvas(viewBitmap);
    canvas.drawBitmap(resizedBitmap, startGridX, startGridY, null);
}

From source file:Main.java

static private BitmapDrawable bitmapDrawableFromURL(Resources resources, String str_url, boolean scale, int w,
        int h) {/*w  w  w.j a  v a 2  s. co m*/
    if (str_url == null || str_url.length() <= 0)
        return null;

    BitmapDrawable thumb = null;
    try {
        URL url = new URL(str_url);
        URLConnection connection = url.openConnection();
        connection.connect();

        InputStream stream = connection.getInputStream();
        BufferedInputStream data = new BufferedInputStream(stream);

        Bitmap bitmap = BitmapFactory.decodeStream(data);
        if (bitmap != null) {
            if (scale)
                thumb = new BitmapDrawable(resources, Bitmap.createScaledBitmap(bitmap, w, h, true));
            else
                thumb = new BitmapDrawable(resources, bitmap);
        }

    } catch (MalformedURLException e) {
        //e.printStackTrace();
    } catch (IOException e) {
        //e.printStackTrace();
    }

    return thumb;
}

From source file:Main.java

/**
 * /*from   w  w  w.ja va2 s .c  o  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 createScaledToFillBitmap(Bitmap bitmap, int reqWidth, int reqHeight) {
    final int height = bitmap.getHeight();
    final int width = bitmap.getWidth();

    float widthScaleF = (float) width / (float) reqWidth;
    float heightScaleF = (float) height / (float) reqHeight;
    if (widthScaleF < heightScaleF) {
        return Bitmap.createScaledBitmap(bitmap, reqWidth, (int) (height / widthScaleF), true);
    } else {//from w ww .j  av  a  2s .  c  om
        return Bitmap.createScaledBitmap(bitmap, (int) (width / heightScaleF), reqHeight, true);
    }
}

From source file:Main.java

protected static Uri scaleDown(Uri imageUri, int targetSize, Context context) {
    System.gc();// w  w  w .  j a va2s  .c  om
    String imagePath = getImagePath(imageUri, context);
    Bitmap currentImage = BitmapFactory.decodeFile(imagePath);

    int targetWidth = targetSize;
    int targetHeight = targetSize;

    int width = currentImage.getWidth();
    int height = currentImage.getHeight();

    if (width < targetWidth || height < targetHeight) {
        currentImage.recycle();
        currentImage = null;
        System.gc();
        return Uri.parse(imageUri.toString());
    }

    height = (int) (height * (float) targetWidth / width);

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(currentImage, targetWidth, height, false);

    if (currentImage != scaledBitmap) {
        currentImage.recycle();
        currentImage = null;
    }
    System.gc();
    File imageFile;
    try {
        imageFile = new File(context.getCacheDir(), "vumatch-upload-00.jpeg");
        FileOutputStream output;

        output = new FileOutputStream(imageFile);
        boolean result = scaledBitmap.compress(CompressFormat.JPEG, 90, output);
        if (result) {
            scaledBitmap.recycle();
            scaledBitmap = null;
            return Uri.fromFile(imageFile);
        } else {
            return null;
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

/**
 * Scales an image maintaining aspect ratio
 *
 * @param original  original image/*w  ww .j a  va 2s.  co  m*/
 * @param maxWidth  maximum width
 * @param maxHeight maximum height
 * @return a Bitmap representing the thumbnail
 */
public static Bitmap createThumbnailForImage(Bitmap original, int maxWidth, int maxHeight) {
    int width = original.getWidth();
    int height = original.getHeight();

    Log.v("Pictures", "Width and height are " + width + "--" + height);

    if (width > height) {
        // landscape
        float ratio = (float) width / maxWidth;
        width = maxWidth;
        height = (int) (height / ratio);
    } else if (height > width) {
        // portrait
        float ratio = (float) height / maxHeight;
        height = maxHeight;
        width = (int) (width / ratio);
    } else {
        // square
        height = maxHeight;
        width = maxWidth;
    }

    Log.v("Pictures", "after scaling Width and height are " + width + "--" + height);

    return Bitmap.createScaledBitmap(original, width, height, true);
}

From source file:Main.java

/**
 * This method of image pixelization utilizes the bitmap scaling operations built
 * into the framework. By downscaling the bitmap and upscaling it back to its
 * original size (while setting the filter flag to false), the same effect can be
 * achieved with much better performance.
 *//*from   w  ww  . ja  v  a  2 s  .  c o  m*/
public static BitmapDrawable builtInPixelization(Context context, float pixelizationFactor, Bitmap bitmap) {

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    int downScaleFactorWidth = (int) (pixelizationFactor * width);
    downScaleFactorWidth = downScaleFactorWidth > 0 ? downScaleFactorWidth : 1;
    int downScaleFactorHeight = (int) (pixelizationFactor * height);
    downScaleFactorHeight = downScaleFactorHeight > 0 ? downScaleFactorHeight : 1;

    int downScaledWidth = width / downScaleFactorWidth;
    int downScaledHeight = height / downScaleFactorHeight;

    Bitmap pixelatedBitmap = Bitmap.createScaledBitmap(bitmap, downScaledWidth, downScaledHeight, false);

    /* Bitmap's createScaledBitmap method has a filter parameter that can be set to either
     * true or false in order to specify either bilinear filtering or point sampling
     * respectively when the bitmap is scaled up or now.
     *
     * Similarly, a BitmapDrawable also has a flag to specify the same thing. When the
     * BitmapDrawable is applied to an ImageView that has some scaleType, the filtering
     * flag is taken into consideration. However, for optimization purposes, this flag was
     * ignored in BitmapDrawables before Jelly Bean MR1.
     *
     * Here, it is important to note that prior to JBMR1, two bitmap scaling operations
     * are required to achieve the pixelization effect. Otherwise, a BitmapDrawable
     * can be created corresponding to the downscaled bitmap such that when it is
     * upscaled to fit the ImageView, the upscaling operation is a lot faster since
     * it uses internal optimizations to fit the ImageView.
     * */
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        BitmapDrawable bitmapDrawable = new BitmapDrawable(context.getResources(), pixelatedBitmap);
        bitmapDrawable.setFilterBitmap(false);
        return bitmapDrawable;
    } else {
        Bitmap upscaled = Bitmap.createScaledBitmap(pixelatedBitmap, width, height, false);
        return new BitmapDrawable(context.getResources(), upscaled);
    }
}

From source file:Main.java

public static Bitmap createScaledBitmap(String path, float scale, boolean filtering) {
    Bitmap src = BitmapFactory.decodeFile(path);
    int width = (int) (src.getWidth() * scale + 0.5f);
    int height = (int) (src.getHeight() * scale + 0.5f);
    return Bitmap.createScaledBitmap(src, width, height, filtering);
}

From source file:Main.java

/**
 * Scale down the bitmap in order to make color analysis faster. Taken from Palette.
 *///w  w w.jav a2  s. c o  m
private static Bitmap scaleBitmapDown(Bitmap bitmap) {
    final int CALCULATE_BITMAP_MIN_DIMENSION = 100;
    final int minDimension = Math.min(bitmap.getWidth(), bitmap.getHeight());

    if (minDimension <= CALCULATE_BITMAP_MIN_DIMENSION) {
        // If the bitmap is small enough already, just return it
        return bitmap;
    }

    final float scaleRatio = CALCULATE_BITMAP_MIN_DIMENSION / (float) minDimension;
    return Bitmap.createScaledBitmap(bitmap, Math.round(bitmap.getWidth() * scaleRatio),
            Math.round(bitmap.getHeight() * scaleRatio), false);
}