[ACCEPTED]-Scale an image which is stored as a byte[] in Java-image-scaling
Accepted answer
Depends on the data format.
However, if you're 2 using something like JPEG, GIF, PNG, or 1 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");
}
}
See this:
Java 2D - How to convert byte[] to BufferedImage
Then see this:
How can I resize an image using Java?
0
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.