use of javax.imageio.stream.ImageOutputStream in project jdk8u_jdk by JetBrains.
the class WriteProgressive method main.
public static void main(String[] args) throws IOException {
Iterator witer = ImageIO.getImageWritersByFormatName("png");
ImageWriter w = (ImageWriter) witer.next();
File f = File.createTempFile("WriteProgressive", ".png");
ImageOutputStream ios = ImageIO.createImageOutputStream(f);
w.setOutput(ios);
BufferedImage bi = new BufferedImage(100, 100, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g = bi.createGraphics();
Random r = new Random(10);
for (int i = 0; i < 10000; i++) {
Color c = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
g.setColor(c);
g.fillRect(r.nextInt(100), r.nextInt(100), 1, 1);
}
IIOImage iioimage = new IIOImage(bi, null, null);
ImageWriteParam param = w.getDefaultWriteParam();
param.setProgressiveMode(ImageWriteParam.MODE_DEFAULT);
try {
w.write(null, iioimage, param);
} catch (NullPointerException npe) {
throw new RuntimeException("Got NPE during write!");
}
ios.close();
BufferedImage bi2 = ImageIO.read(f);
f.delete();
ImageCompare.compare(bi, bi2);
}
use of javax.imageio.stream.ImageOutputStream in project vcell by virtualcell.
the class ITextWriter method encodeJPEG.
public static ByteArrayOutputStream encodeJPEG(BufferedImage bufferedImage) throws Exception {
ImageWriter imageWriter = ImageIO.getImageWritersBySuffix("jpeg").next();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(byteArrayOutputStream);
imageWriter.setOutput(imageOutputStream);
ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam();
imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
// quality 0(very compressed, lossy) -> 1.0(less compressed,loss-less)
imageWriteParam.setCompressionQuality(1.0f);
IIOImage iioImage = new IIOImage(bufferedImage, null, null);
imageWriter.write(null, iioImage, imageWriteParam);
imageOutputStream.close();
imageWriter.dispose();
return byteArrayOutputStream;
}
use of javax.imageio.stream.ImageOutputStream in project Lucee by lucee.
the class Image method writeOut.
public void writeOut(Resource destination, final String format, boolean overwrite, float quality) throws IOException, ExpressionException {
if (destination == null) {
if (source != null)
destination = source;
else
throw new IOException("missing destination file");
}
if (destination.exists()) {
if (!overwrite)
throw new IOException("can't overwrite existing image");
}
if (JAIUtil.isSupportedWriteFormat(format)) {
JAIUtil.write(getBufferedImage(), destination, format);
return;
}
OutputStream os = null;
ImageOutputStream ios = null;
try {
os = destination.getOutputStream();
ios = ImageIO.createImageOutputStream(os);
_writeOut(ios, format, quality, false);
} finally {
ImageUtil.closeEL(ios);
IOUtil.closeEL(os);
}
}
use of javax.imageio.stream.ImageOutputStream in project wcomponents by BorderTech.
the class DynamicImage method getBytes.
/**
* @return the bytes that make up the document content.
*/
@Override
public byte[] getBytes() {
if (text != null) {
try {
// Draw the image.
Dimension size = getSize();
BufferedImage image = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = image.createGraphics();
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setColor(new Color(255, 255, 255, 0));
graphics.fillRect(0, 0, size.width, size.height);
graphics.setColor(Color.BLUE.darker());
graphics.fillOval(0, 0, size.width, size.height);
graphics.setColor(Color.WHITE);
graphics.setFont(new Font("Arial", Font.BOLD, 24));
Rectangle2D bounds = graphics.getFontMetrics().getStringBounds(text, graphics);
int x = (int) (size.width - bounds.getWidth()) / 2;
int y = (int) (size.height + bounds.getHeight() / 2) / 2;
graphics.drawString(text, x, y);
// Write the image to a byte array.
Iterator<ImageWriter> writers = ImageIO.getImageWritersByMIMEType(getMimeType());
ImageWriter writer = writers.next();
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
writer.write(image);
return os.toByteArray();
} catch (IOException ex) {
LOG.error("Unable to generate client image.", ex);
}
}
return new byte[0];
}
Aggregations