【问题标题】:Why the image gets trimmed off from the top?为什么图像从顶部被修剪掉?
【发布时间】:2014-08-03 02:32:32
【问题描述】:

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

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

代码:

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

}

非常感谢...

【问题讨论】:

  • 您的计算有多个代码路径。你能描述一下你使用的是哪一个吗?目标大小与源大小相同还是有缩放比例?
  • 我使用 decode_imagePath_String();谢谢

标签: image bitmapfactory android-bitmap bitmapencoder


【解决方案1】:

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

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

【讨论】:

  • 图像的大小取决于用户的选择。因此,图像可能是 3000px x4000px。将发生 OOM。我从其他地方下载了代码,我一直在尝试修改它,但由于某些原因,它只修剪顶部,而不是其他任何地方。
  • 您应该尝试在返回 unscaledBitmap 时设置断点并验证 inSampleSize 是否大于 1。
  • 非常感谢您的帮助。我对 Android 还是很陌生。你能给我一些代码吗?谢谢
  • 你应该学会使用调试器。我不是凭空说这话的。掌握调试器是 IMO 开发人员可以获得的最佳技能之一
  • 好的,谢谢您的提示。有时间我一定会调查的。 Ugg,有很多关于开发的知识。
猜你喜欢
  • 1970-01-01
  • 2017-06-03
  • 2014-01-28
  • 1970-01-01
  • 1970-01-01
  • 2015-08-13
  • 1970-01-01
  • 2016-12-03
  • 1970-01-01
相关资源
最近更新 更多