我在获取正确尺寸的图像时遇到问题。
为此,我有这种方法来调整图像的大小,不知何故逻辑需要一个小的改变来获得正确的图像大小:
private final Integer width = 170;
private final Integer height = 140;
private final Integer maxFeatImageHeight = 600;
private final Integer maxFeatImageWidth = 600;
/**
* @param featureImage .
* @return byte[]
* @throws Exception
*/
private byte[] resizeFeatureImage(MultipartFile featureImage) throws Exception
{
try
{
BufferedImage originalImage = ImageIO.read(featureImage.getInputStream());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
double featImageWidth = originalImage.getWidth();
double featImageHeight = originalImage.getHeight();
if (featImageHeight > maxFeatImageHeight || featImageWidth > maxFeatImageWidth)
{
// Sanity check on the input (division by zero, infinity):
if (featImageWidth <= 1 || featImageHeight <= 1) {
throw new IllegalArgumentException("Error ..." + featureImage);
}
// The scaling factors to reach to maxima on width and height:
double xScale = maxFeatImageWidth / featImageWidth;
double yScale = maxFeatImageHeight / featImageHeight;
// Proportional (scale width and height by the same factor):
double scale = Math.min(xScale, yScale);
// (Possibly) Do not enlarge:
scale = Math.min(1.0, scale);
int finalWidth = Math.min((int) Math.round(scale * featImageWidth), maxFeatImageWidth);
int finalHeight = Math.min((int) Math.round(scale * featImageHeight), maxFeatImageHeight);
double ratio = featImageWidth / featImageHeight;
// width is bigger then height
if (ratio > 1)
{
finalWidth = maxFeatImageWidth;
finalHeight = (int) Math.round(maxFeatImageHeight / ratio);
}
所以每张图片都显示得比她在下图中的空间小,你可以看到有很多空白没有被使用,这绝对意味着我在我的方法中做错了什么导致了这个,我已经检查了很多次这种方法,但我真的不知道为什么会发生这种情况,如果需要的话,任何人都可以帮助我编辑这种方法我真的很感激。图像显示如下:

斯蒂芬大帝
蛊毒传说
茅侃侃
慕神8447489
随时随地看视频慕课网APP
相关分类