英文:
image resizing in java from byte [] to buffered Image
问题
以下是已翻译的内容:
我正在尝试在Java中按比例缩放以byte[]
形式表示的图像,
但是当我设置比例参数并且出现异常时,我的缩放变量返回空值。
public static byte[] resizeImage(byte[] image) throws IOException {
ByteArrayInputStream bais = new ByteArrayInputStream(image);
Image tmpImage = ImageIO.read(bais);
ImageIcon imageIcon = new ImageIcon(tmpImage);
Image scaled = imageIcon.getImage().getScaledInstance(100, 100, Image.SCALE_SMOOTH);
BufferedImage buffered = ((ToolkitImage) scaled).getBufferedImage();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(buffered, "jpeg", baos);
baos.flush();
image = baos.toByteArray();
baos.close();
return image;
}
英文:
I am trying to scale an image in byte[]
form in java
but my scaled variable returns null when I set scale parameters and get exception
public static byte[] resizeImage(byte[] image) throws IOException {
ByteArrayInputStream bais = new ByteArrayInputStream(image);
Image tmpImage = ImageIO.read(bais);
ImageIcon imageIcon = new ImageIcon(tmpImage);
Image scaled = imageIcon.getImage().getScaledInstance(100, 100, Image.SCALE_SMOOTH);
BufferedImage buffered = ((ToolkitImage) scaled).getBufferedImage();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(buffered, "jpeg", baos);
baos.flush();
image = baos.toByteArray();
baos.close();
return image;
}
答案1
得分: 0
使用BufferedImage buffered = ImageIO.read(new ByteArrayInputStream(byte));
创建BufferedImage
,然后查看https://stackoverflow.com/questions/15558202/how-to-resize-image-in-java以了解如何调整大小。
英文:
Use BufferedImage buffered = ImageIO.read(new ByteArrayInputStream(byte));
to create the BufferedImage
and then check https://stackoverflow.com/questions/15558202/how-to-resize-image-in-java to understand how to resize it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论