Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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:

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

Add the following to the JVM Parameters section

-Xms256m -Xmx512m

like image 849
Sergio del Amo Avatar asked Aug 04 '09 16:08

Sergio del Amo


People also ask

What does byte [] do in Java?

A byte array is an array of bytes (tautology FTW!). You could use a byte array to store a collection of binary data, for example, the contents of a file. The downside to this is that the entire file contents must be loaded into memory.

What is Class for byte [] in Java?

Byte class is a wrapper class for the primitive type byte which contains several methods to effectively deal with a byte value like converting it to a string representation, and vice-versa. An object of Byte class can hold a single byte value. Byte class offers four constants in the form of Fields.

Can you print [] byte?

You can simply iterate the byte array and print the byte using System. out. println() method.

How do you scale an image in Java?

The simplest way to scale an image in Java is to use the AffineTransformOp class. You can load an image into Java as a BufferedImage and then apply the scaling operation to generate a new BufferedImage. You can use Java's ImageIO or a third-party image library such as JDeli to load and save the image.


2 Answers

Depends on the data format.

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

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");
    }
}
like image 194
Kevin Montrose Avatar answered Oct 17 '22 03:10

Kevin Montrose


See this:

Java 2D - How to convert byte[] to BufferedImage

Then see this:

How can I resize an image using Java?

like image 31
karim79 Avatar answered Oct 17 '22 04:10

karim79