Why the image gets trimmed off from the top?

65 Views Asked by At

The code works, but the images get trimmed off from the top. I have tried everything, but I still can't figure it out.

Can someone please take a look at it? Thanks in advance.

Code:

public class ScalingUtilities {


 public static Bitmap decodeResource(Resources res, int resId, int ReqWidth, int ReqHeight, ScalingLogic scalingLogic) {
    Options options = new Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);
    options.inJustDecodeBounds = false;
    options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, ReqWidth,
            ReqHeight, scalingLogic);
    Bitmap unscaledBitmap = BitmapFactory.decodeResource(res, resId, options);

    return unscaledBitmap;
}


public static Bitmap decode_imagePath_String(String image_path, int ReqWidth, int ReqHeight, ScalingLogic scalingLogic) {
    Options options = new Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(image_path, options);
    options.inJustDecodeBounds = false;
    options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, ReqWidth,
            ReqHeight, scalingLogic);
    Bitmap unscaledBitmap = BitmapFactory.decodeFile(image_path, options);

    return unscaledBitmap;
}



public static Bitmap createScaledBitmap(Bitmap unscaledBitmap, int ReqWidth, int ReqHeight,
        ScalingLogic scalingLogic) {
    Rect srcRect = calculateSrcRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(),
            ReqWidth, ReqHeight, scalingLogic);
    Rect dstRect = calculateDstRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(),
            ReqWidth, ReqHeight, scalingLogic);
    Bitmap scaledBitmap = Bitmap.createBitmap(dstRect.width(), dstRect.height(),
            Config.RGB_565);
    Canvas canvas = new Canvas(scaledBitmap);
    canvas.drawBitmap(unscaledBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));

    return scaledBitmap;
}


public static enum ScalingLogic {
    CROP, FIT
}


public static int calculateSampleSize(int srcWidth, int srcHeight, int ReqWidth, int ReqHeight,
        ScalingLogic scalingLogic) {
    if (scalingLogic == ScalingLogic.FIT) {
        final float srcAspect = (float)srcWidth / (float)srcHeight;
        final float dstAspect = (float)ReqWidth / (float)ReqHeight;

        if (srcAspect > dstAspect) {
            return srcWidth / ReqWidth;
        } else {
            return srcHeight / ReqHeight;
        }
    } else {
        final float srcAspect = (float)srcWidth / (float)srcHeight;
        final float dstAspect = (float)ReqWidth / (float)ReqHeight;

        if (srcAspect > dstAspect) {
            return srcHeight / ReqHeight;
        } else {
            return srcWidth / ReqWidth;
        }
    }
}

public static Rect calculateSrcRect(int srcWidth, int srcHeight, int ReqWidth, int ReqHeight,
        ScalingLogic scalingLogic) {
    if (scalingLogic == ScalingLogic.CROP) {
        final float srcAspect = (float)srcWidth / (float)srcHeight;
        final float dstAspect = (float)ReqWidth / (float)ReqHeight;

        if (srcAspect > dstAspect) {
            final int srcRectWidth = (int)(srcHeight * dstAspect);
            final int srcRectLeft = (srcWidth - srcRectWidth) / 2;
            return new Rect(srcRectLeft, 0, srcRectLeft + srcRectWidth, srcHeight);
        } else {
            final int srcRectHeight = (int)(srcWidth / dstAspect);
            final int scrRectTop = (int)(srcHeight - srcRectHeight) / 2;
            return new Rect(0, scrRectTop, srcWidth, scrRectTop + srcRectHeight);
        }
    } else {
        return new Rect(0, 0, srcWidth, srcHeight);
    }
}


public static Rect calculateDstRect(int srcWidth, int srcHeight, int ReqWidth, int ReqHeight,
        ScalingLogic scalingLogic) {
    if (scalingLogic == ScalingLogic.FIT) {
        final float srcAspect = (float)srcWidth / (float)srcHeight;
        final float dstAspect = (float)ReqWidth / (float)ReqHeight;

        if (srcAspect > dstAspect) {
            return new Rect(0, 0, ReqWidth, (int)(ReqWidth / srcAspect));
        } else {
            return new Rect(0, 0, (int)(ReqHeight * srcAspect), ReqHeight);
        }
    } else {
        return new Rect(0, 0, ReqWidth, ReqHeight);
    }
  }

}

I very appreciate it...

1

There are 1 best solutions below

5
On

In calculateSampleSize, you are returning int. Depending on the ratio of your image, this return value may very well be 0 and options.inSampleSize will be 0, so no scaling will take place.

Then the decode will decode with the size values you passed, so cropping will occur. I think that you need to use the density parameters instead. Please take a look at this post