use of javax.imageio.ImageWriteParam in project screenbird by adamhub.
the class Frame method writeJPEG.
/** Writes a JPEG image to a given OutputStream
*
* @param bi an image
* @param quality the quality (between zero and one)
* @return the amount of bytes written
*/
protected static long writeJPEG(OutputStream out, BufferedImage bi, float quality) throws IOException {
MeasuredOutputStream mOut = new MeasuredOutputStream(out);
MemoryCacheImageOutputStream iOut = null;
iOut = new MemoryCacheImageOutputStream(mOut);
ImageWriter iw = (ImageWriter) ImageIO.getImageWritersByMIMEType("image/jpeg").next();
ImageWriteParam iwParam = iw.getDefaultWriteParam();
iwParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwParam.setCompressionQuality(quality);
iw.setOutput(iOut);
IIOImage img = new IIOImage(bi, null, null);
iw.write(null, img, iwParam);
return mOut.getBytesWritten();
}
use of javax.imageio.ImageWriteParam in project spring-framework by spring-projects.
the class BufferedImageHttpMessageConverter method writeInternal.
private void writeInternal(BufferedImage image, MediaType contentType, OutputStream body) throws IOException, HttpMessageNotWritableException {
ImageOutputStream imageOutputStream = null;
ImageWriter imageWriter = null;
try {
Iterator<ImageWriter> imageWriters = ImageIO.getImageWritersByMIMEType(contentType.toString());
if (imageWriters.hasNext()) {
imageWriter = imageWriters.next();
ImageWriteParam iwp = imageWriter.getDefaultWriteParam();
process(iwp);
imageOutputStream = createImageOutputStream(body);
imageWriter.setOutput(imageOutputStream);
imageWriter.write(null, new IIOImage(image, null, null), iwp);
} else {
throw new HttpMessageNotWritableException("Could not find javax.imageio.ImageWriter for Content-Type [" + contentType + "]");
}
} finally {
if (imageWriter != null) {
imageWriter.dispose();
}
if (imageOutputStream != null) {
try {
imageOutputStream.close();
} catch (IOException ex) {
// ignore
}
}
}
}
use of javax.imageio.ImageWriteParam in project jdk8u_jdk by JetBrains.
the class LUTCompareTest method createTestImage.
/* Create test image with two frames:
* 1) with {red, red} palette
* 2) with {blue, red } palette
*/
private static Image createTestImage() throws IOException {
BufferedImage frame1 = createFrame(new int[] { 0xffff0000, 0xffff0000 });
BufferedImage frame2 = createFrame(new int[] { 0xff0000ff, 0xffff0000 });
ImageWriter writer = ImageIO.getImageWritersByFormatName("GIF").next();
ImageOutputStream ios = ImageIO.createImageOutputStream(new File("lut_test.gif"));
ImageWriteParam param = writer.getDefaultWriteParam();
writer.setOutput(ios);
writer.prepareWriteSequence(null);
writer.writeToSequence(new IIOImage(frame1, null, null), param);
writer.writeToSequence(new IIOImage(frame2, null, null), param);
writer.endWriteSequence();
writer.reset();
writer.dispose();
ios.flush();
ios.close();
return Toolkit.getDefaultToolkit().createImage("lut_test.gif");
}
use of javax.imageio.ImageWriteParam in project jdk8u_jdk by JetBrains.
the class CompressionModeTest method doTest.
private static void doTest(int mode) {
String fileFormat = "bmp";
try {
ImageWriter iw = (ImageWriter) ImageIO.getImageWritersBySuffix(fileFormat).next();
if (iw == null) {
throw new RuntimeException("No available image writer for " + fileFormat + " Test failed.");
}
File file = new File("image." + fileFormat);
ImageOutputStream ios = ImageIO.createImageOutputStream(file);
iw.setOutput(ios);
BufferedImage bimg = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
Graphics g = bimg.getGraphics();
g.setColor(Color.green);
g.fillRect(0, 0, 100, 100);
ImageWriteParam param = iw.getDefaultWriteParam();
param.setCompressionMode(mode);
IIOMetadata meta = iw.getDefaultImageMetadata(new ImageTypeSpecifier(bimg), param);
IIOImage iioImg = new IIOImage(bimg, null, meta);
iw.write(null, iioImg, param);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Test failed.");
}
}
use of javax.imageio.ImageWriteParam in project jdk8u_jdk by JetBrains.
the class AnimationTest method main.
public static void main(String[] args) {
try {
AnimationTest t = new AnimationTest();
t.initFrame();
ImageWriter w = t.initWriter();
ImageWriteParam p = w.getDefaultWriteParam();
IIOMetadata streamMetadata = w.getDefaultStreamMetadata(p);
w.prepareWriteSequence(streamMetadata);
for (int i = 0; i < 50; i++) {
BufferedImage f = t.createNextFrame();
ImageTypeSpecifier type = new ImageTypeSpecifier(f);
IIOMetadata m = w.getDefaultImageMetadata(type, p);
w.writeToSequence(new IIOImage(f, null, m), p);
}
w.endWriteSequence();
t.checkAnimation();
} catch (Exception e) {
throw new RuntimeException("Test failed.", e);
}
}
Aggregations