use of javax.imageio.IIOImage 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.IIOImage in project freeplane by freeplane.
the class ExportToImage method exportToImage.
/**
* Export image.
* @param toFile
*/
public boolean exportToImage(final RenderedImage image, File chosenFile) {
try {
Controller.getCurrentController().getViewController().setWaitingCursor(true);
Iterator<ImageWriter> imageWritersByFormatName = ImageIO.getImageWritersByFormatName(imageType);
for (; ; ) {
ImageWriter writer = imageWritersByFormatName.next();
ImageWriteParam writeParam = writer.getDefaultWriteParam();
ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, writeParam);
if ((metadata.isReadOnly() || !metadata.isStandardMetadataFormatSupported()) && imageWritersByFormatName.hasNext()) {
continue;
}
addDpiToMetadata(metadata);
final FileOutputStream outFile = new FileOutputStream(chosenFile);
final ImageOutputStream stream = ImageIO.createImageOutputStream(outFile);
try {
writer.setOutput(ImageIO.createImageOutputStream(outFile));
writer.write(metadata, new IIOImage(image, null, metadata), writeParam);
break;
} finally {
stream.close();
outFile.close();
}
}
} catch (final IOException e1) {
LogUtils.warn(e1);
UITools.errorMessage(TextUtils.getText("export_failed"));
} finally {
Controller.getCurrentController().getViewController().setWaitingCursor(false);
}
return true;
}
use of javax.imageio.IIOImage in project artisynth_core by artisynth.
the class AnimatedGifWriter method write.
/**
* Writes a sequence of images to an animated GIF
* @param file output file
* @param frames input image sequence
* @param delayTime time between frames (s)
* @param loopCount number of times to loop (-1 for infinite)
* @throws IOException if cannot write to the output file
*/
public static void write(File file, List<? extends BufferedImage> frames, double delayTime, int loopCount) throws IOException {
ImageWriter iw;
try {
iw = ImageIO.getImageWritersByFormatName("gif").next();
} catch (Exception e) {
throw new IOException("Cannot write GIF format", e);
}
ImageOutputStream ios = ImageIO.createImageOutputStream(file);
iw.setOutput(ios);
iw.prepareWriteSequence(null);
int count = loopCount + 1;
if (count < 0) {
count = 0;
}
for (int i = 0; i < frames.size(); i++) {
BufferedImage src = frames.get(i);
ImageWriteParam iwp = iw.getDefaultWriteParam();
IIOMetadata metadata = iw.getDefaultImageMetadata(new ImageTypeSpecifier(src), iwp);
configure(metadata, delayTime, count, i);
IIOImage ii = new IIOImage(src, null, metadata);
iw.writeToSequence(ii, null);
}
iw.endWriteSequence();
ios.close();
}
use of javax.imageio.IIOImage in project imageio-ext by geosolutions-it.
the class EmptyImage method prepareWriteEmpty.
public void prepareWriteEmpty(IIOMetadata streamMetadata, ImageTypeSpecifier imageType, int width, int height, IIOMetadata imageMetadata, List thumbnails, ImageWriteParam param) throws IOException {
checkParamsEmpty(imageType, width, height, thumbnails);
this.isWritingEmpty = true;
SampleModel emptySM = imageType.getSampleModel();
RenderedImage emptyImage = new EmptyImage(0, 0, width, height, 0, 0, emptySM.getWidth(), emptySM.getHeight(), emptySM, imageType.getColorModel());
write(streamMetadata, new IIOImage(emptyImage, null, imageMetadata), param, true, false);
}
use of javax.imageio.IIOImage in project imageio-ext by geosolutions-it.
the class JPEGWriterCompareTest method writeJPEG.
/**
* Writes outs the image contained into this {@link ImageWorker} as a JPEG using the provided
* destination , compression and compression rate.
* <p>
* The destination object can be anything providing that we have an {@link ImageOutputStreamSpi}
* that recognizes it.
*
* @param destination
* where to write the internal {@link #image} as a JPEG.
* @param compression
* algorithm.
* @param compressionRate
* percentage of compression.
* @param nativeAcc
* should we use native acceleration.
* @return this {@link ImageWorker}.
* @throws IOException
* In case an error occurs during the search for an {@link ImageOutputStream} or
* during the eoncding process.
*/
public static final void writeJPEG(final RenderedImage image, final Object destination, final String compression, final float compressionRate, final boolean nativeAcc) throws IOException {
ImageWriterSpi spi = nativeAcc ? clibSPI : turboSPI;
ImageWriter writer = spi.createWriterInstance();
// Compression is available on both lib
final ImageWriteParam iwp = writer.getDefaultWriteParam();
final ImageOutputStream outStream = nativeAcc ? new MemoryCacheImageOutputStream((OutputStream) destination) : new ImageOutputStreamAdapter((OutputStream) destination);
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionType("JPEG");
// We can control quality here.
iwp.setCompressionQuality(compressionRate);
if (nativeAcc) {
// Lossy compression.
iwp.setCompressionType(compression);
}
try {
if (iwp instanceof JPEGImageWriteParam) {
final JPEGImageWriteParam param = (JPEGImageWriteParam) iwp;
param.setOptimizeHuffmanTables(true);
param.setProgressiveMode(JPEGImageWriteParam.MODE_DEFAULT);
}
writer.setOutput(outStream);
writer.write(null, new IIOImage(image, null, null), iwp);
} finally {
if (writer != null) {
try {
writer.dispose();
} catch (Throwable e) {
System.out.println(e.getLocalizedMessage());
}
}
if (outStream != null) {
try {
((ImageOutputStream) outStream).close();
} catch (Throwable e) {
System.out.println(e.getLocalizedMessage());
}
}
}
}
Aggregations