use of javax.imageio.IIOImage in project basicv2 by EgonOlsen71.
the class GraphicsDevice method save.
/**
* Saves the current image into a PNG file.
*
* @param machine
* the machine
* @param name
* the name of the target file
*/
public void save(Machine machine, String name) {
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(name));
ImageOutputStream ios = ImageIO.createImageOutputStream(bos)) {
Iterator<ImageWriter> itty = ImageIO.getImageWritersBySuffix("png");
if (itty.hasNext()) {
ImageWriter iw = (ImageWriter) itty.next();
ImageWriteParam iwp = iw.getDefaultWriteParam();
iw.setOutput(ios);
iw.write(null, new IIOImage((RenderedImage) getScreen(), null, null), iwp);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of javax.imageio.IIOImage in project chordatlas by twak.
the class Imagez method writeJPG.
public static void writeJPG(BufferedImage rendered, float quality, File f) {
try {
JPEGImageWriteParam jpegParams = new JPEGImageWriteParam(null);
jpegParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
jpegParams.setCompressionQuality(1f);
ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
ImageWriteParam param = writer.getDefaultWriteParam();
// Needed see javadoc
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
// Highest quality
param.setCompressionQuality(quality);
writer.setOutput(new FileImageOutputStream(f));
writer.write(null, new IIOImage(rendered, null, null), jpegParams);
} catch (Throwable th) {
th.printStackTrace();
}
}
use of javax.imageio.IIOImage in project OpenOLAT by OpenOLAT.
the class ImageHelperImpl method writeTo.
/**
* Can change this to choose a better compression level as the default
* @param image
* @param scaledImage
* @return
*/
private static boolean writeTo(BufferedImage image, OutputStream scaledImage, Size scaledSize, String outputFormat) {
try {
if (!StringHelper.containsNonWhitespace(outputFormat)) {
outputFormat = OUTPUT_FORMAT;
}
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(outputFormat);
if (writers.hasNext()) {
ImageWriter writer = writers.next();
ImageWriteParam iwp = getOptimizedImageWriteParam(writer, scaledSize);
IIOImage iiOImage = new IIOImage(image, null, null);
ImageOutputStream iOut = new MemoryCacheImageOutputStream(scaledImage);
writer.setOutput(iOut);
writer.write(null, iiOImage, iwp);
writer.dispose();
iOut.flush();
iOut.close();
return true;
} else {
return ImageIO.write(image, outputFormat, scaledImage);
}
} catch (IOException e) {
return false;
}
}
use of javax.imageio.IIOImage in project xwiki-platform by xwiki.
the class DefaultImageProcessor method writeImage.
@Override
public void writeImage(RenderedImage image, String mimeType, float quality, OutputStream out) throws IOException {
if ("image/jpeg".equals(mimeType)) {
// Find a JPEG writer.
ImageWriter writer = null;
Iterator<ImageWriter> iter = ImageIO.getImageWritersByMIMEType(mimeType);
if (iter.hasNext()) {
writer = iter.next();
}
JPEGImageWriteParam iwp = new JPEGImageWriteParam(null);
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(quality);
// Prepare output file.
ImageOutputStream ios = ImageIO.createImageOutputStream(out);
writer.setOutput(ios);
// Write the image.
writer.write(null, new IIOImage(image, null, null), iwp);
// Cleanup.
ios.flush();
writer.dispose();
ios.close();
} else {
ImageIO.write(image, "png", out);
}
}
use of javax.imageio.IIOImage in project polymap4-core by Polymap4.
the class ImageEncodeProcessor method imageioEncodeJPEG.
private void imageioEncodeJPEG(Image image, ChunkedResponseOutputStream out) throws IOException {
// this code is from http://forums.sun.com/thread.jspa?threadID=5197061
ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
ImageWriteParam param = writer.getDefaultWriteParam();
param.setSourceBands(new int[] { 0, 1, 2 });
ColorModel cm = new DirectColorModel(24, // Red
0x00ff0000, // Green
0x0000ff00, // Blue
0x000000ff, // Alpha
0x0);
param.setDestinationType(new ImageTypeSpecifier(cm, cm.createCompatibleSampleModel(1, 1)));
ImageOutputStream imageOut = ImageIO.createImageOutputStream(out);
writer.setOutput(imageOut);
writer.write(null, new IIOImage((RenderedImage) image, null, null), param);
writer.dispose();
imageOut.close();
}
Aggregations