缩放在 Java 中存储为 byte[] 的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1228381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:42:46  来源:igfitidea点击:

Scale an image which is stored as a byte[] in Java

javaimageimage-scaling

提问by Sergio del Amo

I upload a file with a struts form. I have the image as a byte[] and I would like to scale it.

我上传了一个带有 struts 表单的文件。我有一个字节 [] 的图像,我想缩放它。

FormFile file = (FormFile) dynaform.get("file");
byte[] fileData = file.getFileData(); 
fileData = scale(fileData,200,200);

public byte[] scale(byte[] fileData, int width, int height) {
// TODO 
}

Anyone knows an easy function to do this?

任何人都知道一个简单的功能来做到这一点?

public byte[] scale(byte[] fileData, int width, int height) {
        ByteArrayInputStream in = new ByteArrayInputStream(fileData);
        try {
            BufferedImage img = ImageIO.read(in);
            if(height == 0) {
                height = (width * img.getHeight())/ img.getWidth(); 
            }
            if(width == 0) {
                width = (height * img.getWidth())/ img.getHeight();
            }
            Image scaledImage = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
            BufferedImage imageBuff = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            imageBuff.getGraphics().drawImage(scaledImage, 0, 0, new Color(0,0,0), null);

            ByteArrayOutputStream buffer = new ByteArrayOutputStream();

            ImageIO.write(imageBuff, "jpg", buffer);

            return buffer.toByteArray();
        } catch (IOException e) {
            throw new ApplicationException("IOException in scale");
        }
    }

If you run out of Java Heap Space in tomcat as I did, increase the heap space which is used by tomcat. In case you use the tomcat plugin for Eclipse, next should apply:

如果你像我一样在 tomcat 中用完了 Java 堆空间,请增加 tomcat 使用的堆空间。如果您使用 Eclipse 的 tomcat 插件,下一步应该适用:

In Eclipse, choose Window > Preferences > Tomcat > JVM Settings

Add the following to the JVM Parameters section

-Xms256m -Xmx512m

在 Eclipse 中,选择窗口 > 首选项 > Tomcat > JVM 设置

将以下内容添加到 JVM 参数部分

-Xms256m -Xmx512m

回答by Kevin Montrose

Depends on the data format.

取决于数据格式。

However, if you're using something like JPEG, GIF, PNG, or BMP you can use the ImageIOclass.

但是,如果您使用的是 JPEG、GIF、PNG 或 BMP 之类的内容,则可以使用ImageIO类。

Something like:

就像是:

public byte[] scale(byte[] fileData, int width, int height) {
    ByteArrayInputStream in = new ByteArrayInputStream(fileData);
    try {
        BufferedImage img = ImageIO.read(in);
        if(height == 0) {
            height = (width * img.getHeight())/ img.getWidth(); 
        }
        if(width == 0) {
            width = (height * img.getWidth())/ img.getHeight();
        }
        Image scaledImage = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        BufferedImage imageBuff = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        imageBuff.getGraphics().drawImage(scaledImage, 0, 0, new Color(0,0,0), null);

        ByteArrayOutputStream buffer = new ByteArrayOutputStream();

        ImageIO.write(imageBuff, "jpg", buffer);

        return buffer.toByteArray();
    } catch (IOException e) {
        throw new ApplicationException("IOException in scale");
    }
}