0

该代码有效,但图像从顶部被修剪掉。我已经尝试了一切,但我仍然无法弄清楚。

有人可以看看吗?提前致谢。

代码:

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

}

我非常感谢...

4

1 回答 1

0

在 calculateSampleSize 中,您返回的是 int。根据您图像的比例,此返回值很可能为 0,而 options.inSampleSize 将为 0,因此不会发生缩放。

然后解码将使用您传递的大小值进行解码,因此会发生裁剪。我认为您需要改用密度参数。请看一下这个帖子

于 2014-08-03T03:01:41.907 回答