Code example for Bitmap

Methods: createScaledBitmapgetHeightgetWidth

0
     * @param height   preferred height of the new image 
     * @param upsample if true smaller images than the preferred size are increased, if false the origin bitmap is returned 
     * @return new bitmap if size has changed, otherwise original bitmap. 
     */ 
    public Bitmap scaleBitmap(Bitmap b, int width, int height, boolean upsampling) {
        int imageHeight = b.getHeight();
        int imageWidth = b.getWidth();
        if (!upsampling && imageHeight <= height && imageWidth <= width) {
            return b;
        } 
        int finalWidth = width;
        int finalHeight = height;
        if (imageHeight > imageWidth) {
            float factor = ((float) height) / ((float) imageHeight);
            finalHeight = new Float(imageHeight * factor).intValue();
            finalWidth = new Float(imageWidth * factor).intValue();
        } else { 
            float factor = ((float) width) / ((float) imageWidth);
            finalHeight = new Float(imageHeight * factor).intValue();
            finalWidth = new Float(imageWidth * factor).intValue();
        } 
        Bitmap scaled = null;
        try { 
            scaled = Bitmap.createScaledBitmap(b, finalWidth, finalHeight, true);
        } catch (final Throwable e) {
            // calling gc does not help as is called anyway 
            // http://code.google.com/p/android/issues/detail?id=8488#c80 
            // System.gc(); 
        }