图片在Java中的大小调整,从byte[]到BufferedImage。

huangapple go评论74阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年4月6日 18:25:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/61057660.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定