@RequestMapping(value = "loadRiderContentImg")//窗口大小 width: 590, height: 500,
public void loadRiderContentImg(String RIDER_ID_, HttpServletResponse response, Map<String, Object> operator) {
try {
InputStream inputStream = riderService.loadRiderContent(RIDER_ID_);
//读取图片输入流为 BufferedImage
BufferedImage image = ImageIO.read(inputStream);
// System.out.println("Width: " + image.getWidth());
// System.out.println("Height: " + image.getHeight());
//图片给宽度小于高度,已宽度为准
int getImgMaxWidth = 540;
if (image.getWidth() <= image.getHeight()) {
if (image.getWidth() > getImgMaxWidth) {
//调整图片大小
Image resultingImage = image.getScaledInstance(Math.min(image.getWidth(), getImgMaxWidth), image.getHeight() * getImgMaxWidth / image.getWidth(), Image.SCALE_AREA_AVERAGING);
BufferedImage outputImage = new BufferedImage(Math.min(image.getWidth(), getImgMaxWidth), image.getHeight() * getImgMaxWidth / image.getWidth(), BufferedImage.TYPE_INT_RGB);
outputImage.getGraphics().drawImage(resultingImage, 0, 0, null);
//图像缓冲区图片保存为图片文件(文件不存在会自动创建文件保存,文件存在会覆盖原文件保存)
//ImageIO.write(outputImage, "jpg", new File("D:/pic2.jpg"));
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(outputImage, "jpg", os);
inputStream = new ByteArrayInputStream(os.toByteArray());
} else {
Image resultingImage = image.getScaledInstance(image.getWidth(), image.getHeight(), Image.SCALE_AREA_AVERAGING);
BufferedImage outputImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
outputImage.getGraphics().drawImage(resultingImage, 0, 0, null);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(outputImage, "jpg", os);
inputStream = new ByteArrayInputStream(os.toByteArray());
}
}
//图片给宽度大于高度,已高度为准
int getImgMaxHeight = 390;
if (image.getWidth() > image.getHeight()) {
if (image.getHeight() > getImgMaxHeight) {
Image resultingImage = image.getScaledInstance(image.getWidth() * getImgMaxHeight / image.getHeight(), Math.min(image.getWidth(), getImgMaxHeight), Image.SCALE_AREA_AVERAGING);
BufferedImage outputImage = new BufferedImage(image.getWidth() * getImgMaxHeight / image.getHeight(), Math.min(image.getWidth(), getImgMaxHeight), BufferedImage.TYPE_INT_RGB);
outputImage.getGraphics().drawImage(resultingImage, 0, 0, null);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(outputImage, "jpg", os);
inputStream = new ByteArrayInputStream(os.toByteArray());
} else {
Image resultingImage = image.getScaledInstance(image.getWidth(), image.getHeight(), Image.SCALE_AREA_AVERAGING);
BufferedImage outputImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
outputImage.getGraphics().drawImage(resultingImage, 0, 0, null);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(outputImage, "jpg", os);
inputStream = new ByteArrayInputStream(os.toByteArray());
}
}
response.reset();
ServletOutputStream out = response.getOutputStream();
byte[] content = new byte[65535];
int length = 0;
while ((length = inputStream.read(content)) != -1) {
out.write(content, 0, length);
}
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交