use of javax.imageio.ImageWriteParam in project jdk8u_jdk by JetBrains.
the class BMPImageWriter method writeEmbedded.
private void writeEmbedded(IIOImage image, ImageWriteParam bmpParam) throws IOException {
String format = compressionType == BI_JPEG ? "jpeg" : "png";
Iterator iterator = ImageIO.getImageWritersByFormatName(format);
ImageWriter writer = null;
if (iterator.hasNext())
writer = (ImageWriter) iterator.next();
if (writer != null) {
if (embedded_stream == null) {
throw new RuntimeException("No stream for writing embedded image!");
}
writer.addIIOWriteProgressListener(new IIOWriteProgressAdapter() {
public void imageProgress(ImageWriter source, float percentageDone) {
processImageProgress(percentageDone);
}
});
writer.addIIOWriteWarningListener(new IIOWriteWarningListener() {
public void warningOccurred(ImageWriter source, int imageIndex, String warning) {
processWarningOccurred(imageIndex, warning);
}
});
writer.setOutput(ImageIO.createImageOutputStream(embedded_stream));
ImageWriteParam param = writer.getDefaultWriteParam();
//param.setDestinationBands(bmpParam.getDestinationBands());
param.setDestinationOffset(bmpParam.getDestinationOffset());
param.setSourceBands(bmpParam.getSourceBands());
param.setSourceRegion(bmpParam.getSourceRegion());
param.setSourceSubsampling(bmpParam.getSourceXSubsampling(), bmpParam.getSourceYSubsampling(), bmpParam.getSubsamplingXOffset(), bmpParam.getSubsamplingYOffset());
writer.write(null, image, param);
} else
throw new RuntimeException(I18N.getString("BMPImageWrite5") + " " + format);
}
use of javax.imageio.ImageWriteParam in project jmonkeyengine by jMonkeyEngine.
the class MjpegFileWriter method writeImageToBytes.
public byte[] writeImageToBytes(Image image, float quality) throws Exception {
BufferedImage bi;
if (image instanceof BufferedImage && ((BufferedImage) image).getType() == BufferedImage.TYPE_INT_RGB) {
bi = (BufferedImage) image;
} else {
bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
g.drawImage(image, 0, 0, width, height, null);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageWriter imgWrtr = ImageIO.getImageWritersByFormatName("jpg").next();
ImageOutputStream imgOutStrm = ImageIO.createImageOutputStream(baos);
imgWrtr.setOutput(imgOutStrm);
ImageWriteParam jpgWrtPrm = imgWrtr.getDefaultWriteParam();
jpgWrtPrm.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
jpgWrtPrm.setCompressionQuality(quality);
imgWrtr.write(null, new IIOImage(bi, null, null), jpgWrtPrm);
imgOutStrm.close();
return baos.toByteArray();
}
use of javax.imageio.ImageWriteParam in project jmonkeyengine by jMonkeyEngine.
the class JmeDesktopSystem method writeImageFile.
@Override
public void writeImageFile(OutputStream outStream, String format, ByteBuffer imageData, int width, int height) throws IOException {
BufferedImage awtImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
Screenshots.convertScreenShot2(imageData.asIntBuffer(), awtImage);
ImageWriter writer = ImageIO.getImageWritersByFormatName(format).next();
ImageWriteParam writeParam = writer.getDefaultWriteParam();
if (format.equals("jpg")) {
JPEGImageWriteParam jpegParam = (JPEGImageWriteParam) writeParam;
jpegParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
jpegParam.setCompressionQuality(0.95f);
}
awtImage = verticalFlip(awtImage);
ImageOutputStream imgOut = new MemoryCacheImageOutputStream(outStream);
writer.setOutput(imgOut);
IIOImage outputImage = new IIOImage(awtImage, null, null);
try {
writer.write(null, outputImage, writeParam);
} finally {
imgOut.close();
writer.dispose();
}
}
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
}
}
}
}
Aggregations