use of javax.imageio.ImageWriter 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.ImageWriter 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.ImageWriter 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.ImageWriter 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.ImageWriter in project adempiere by adempiere.
the class ScreenShot method createJPEG.
/**
* Create JPEG file from window
* @param window window
* @param fileName optional file name
* @return true if created
*/
public static boolean createJPEG(Window window, String fileName) {
BufferedImage bi = null;
if (window == null || fileName == null)
new IllegalArgumentException("ScreenShot.createJPEG Window os NULL");
// Get File
File file = getJPGFile(window);
if (file == null)
return false;
log.config("File=" + file);
if (file.exists())
file.delete();
// Get Writer
Iterator writers = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = (ImageWriter) writers.next();
if (writer == null) {
log.log(Level.SEVERE, "no ImageWriter");
return false;
}
// Get Image
try {
Thread.sleep(1000);
bi = getImage(window);
} catch (InterruptedException ex) {
log.log(Level.SEVERE, "ex", ex);
}
// Write Image
try {
ImageOutputStream ios = ImageIO.createImageOutputStream(file);
writer.setOutput(ios);
writer.write(bi);
ios.flush();
ios.close();
} catch (IOException ex) {
log.log(Level.SEVERE, "ex", ex);
return false;
}
return true;
}
Aggregations