【发布时间】:2018-03-18 10:28:25
【问题描述】:
我想在点击localhost:8080:/getImage/app/path={imagePath} 之类的 API 时获取图像
当我点击这个 API 时,它会返回一个图片。
这可能吗?
实际上,我已经尝试过了,但它给了我一个错误。 这是我的代码,
@GET
@Path("/app")
public BufferedImage getFullImage(@Context UriInfo info) throws MalformedURLException, IOException {
String objectKey = info.getQueryParameters().getFirst("path");
return resizeImage(300, 300, objectKey);
}
public static BufferedImage resizeImage(int width, int height, String imagePath)
throws MalformedURLException, IOException {
BufferedImage bufferedImage = ImageIO.read(new URL(imagePath));
final Graphics2D graphics2D = bufferedImage.createGraphics();
graphics2D.setComposite(AlphaComposite.Src);
// below three lines are for RenderingHints for better image quality at cost of
// higher processing time
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics2D.drawImage(bufferedImage, 0, 0, width, height, null);
graphics2D.dispose();
System.out.println(bufferedImage.getWidth());
return bufferedImage;
}
我的错误,
java.io.IOException: The image-based media type image/webp is not supported for writing
有什么方法可以在点击 java 中的任何 URL 时返回图像?
【问题讨论】:
-
将图像存储为 base64 编码字符串并返回该字符串。如果需要,我可以提供示例代码
标签: spring restful-url java