This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.imageio.ImageIO; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
public class Image { | |
public static Image read(String name) throws IOException { | |
return new Image(ImageIO.read(new File(name))); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static BufferedImage comp(BufferedImage image, float comp) throws IOException { | |
ImageWriter jpgWriter = ImageIO.getImageWritersByFormatName("jpg").next(); | |
ImageWriteParam jpgWriteParam = jpgWriter.getDefaultWriteParam(); | |
jpgWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); | |
jpgWriteParam.setCompressionQuality(comp); | |
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | |
ImageOutputStream stream = ImageIO.createImageOutputStream(outputStream); | |
jpgWriter.setOutput(stream); | |
jpgWriter.write(null, new IIOImage(image, null, null), jpgWriteParam); |