У меня проблема с получением нужного размера изображения.
Для этого у меня есть этот метод, который делает изменение размера изображения, и каким-то образом логике нужно небольшое изменение, чтобы получить правильный размер изображения:
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);
}
// height is bigger then width
else if (ratio < 1)
{
finalWidth = (int) Math.round(maxFeatImageWidth / ratio);
finalHeight = maxFeatImageHeight;
}
// width and height are equal
else
{
finalHeight = maxFeatImageHeight;
finalWidth = maxFeatImageWidth;
}
logger.info("[resizeFeatureImage] [FEATURE IMAGE RESIZE] Starting to resize feature Image");
Graphics2D g2d;
BufferedImage resizedImage;
if (featureImage.getContentType().contains("png"))
{
resizedImage = new BufferedImage(finalWidth, finalHeight, BufferedImage.TYPE_INT_ARGB);
}
else
{
resizedImage = new BufferedImage(finalWidth, finalHeight, BufferedImage.TYPE_3BYTE_BGR);
}
g2d = resizedImage.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.drawImage(ImageIO.read(featureImage.getInputStream()), 0, 0, finalWidth, finalHeight,
null);
g2d.dispose();
ImageIO.write(resizedImage, featureImage.getContentType().split("/")[1], baos);
logger.info("[resizeFeatureImage] [FEATURE IMAGE RESIZE] Feature image resized!");
return baos.toByteArray();
}
else
{
ImageIO.write(originalImage, featureImage.getContentType().split("/")[1], baos);
return baos.toByteArray();
}
}
catch (Exception e)
{
logger.warn("[resizeFeatureImage] [STATUS] - ERROR ");
logger.warn("[resizeFeatureImage] [EXCEPTION] " + e.getMessage(), e);
throw new IOException("The file you uploaded can be damaged or has incorrect encoding.");
}
}
Таким образом, каждое изображение отображается меньше, чем пространство, которое она имеет
на изображении ниже вы можете видеть, что есть много пустого пространства, которое не используется, и определенно это означает, что я делаю что-то не так в своем методе, который вызывает это, я много раз проверял этот метод, и я надеваю Не знаю, почему или это происходит, может кто-нибудь помочь мне отредактировать этот метод, если это необходимо, я был бы очень признателен.
изображения показываются так:
![enter image description here](https://i.stack.imgur.com/Aijlk.png)