use of javax.imageio.ImageWriter 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
*/
public static boolean writeTo(BufferedImage image, File 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 FileImageOutputStream(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.ImageWriter in project imageio-ext by geosolutions-it.
the class JPEGWriterTest method writerTestRefineLayout.
@Test
public void writerTestRefineLayout() throws IOException {
if (SKIP_TESTS) {
LOGGER.warning(ERROR_LIB_MESSAGE);
assumeTrue(!SKIP_TESTS);
return;
}
// test-data
final File input = TestData.file(this, "testme.jpg");
FileImageInputStream stream = null;
ImageReader reader = null;
try {
stream = new FileImageInputStream(input);
ImageLayout layout = new ImageLayout();
layout.setTileGridXOffset(-2);
layout.setTileGridYOffset(-2);
layout.setTileWidth(228);
layout.setTileHeight(104);
reader = ImageIO.getImageReaders(stream).next();
RenderingHints hints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout);
BufferedImage sourceImage = ImageIO.read(input);
sourceImage.getWidth();
RenderedImage inputImage = BandSelectDescriptor.create(sourceImage, new int[] { 0 }, hints);
// get the SPI for writer\
final Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName(TurboJpegImageWriterSpi.formatNames[0]);
assertTrue(it.hasNext());
TurboJpegImageWriter writer = null;
while (it.hasNext()) {
ImageWriterSpi writer_ = it.next().getOriginatingProvider();
if (writer_ instanceof TurboJpegImageWriterSpi) {
writer = (TurboJpegImageWriter) writer_.createWriterInstance();
break;
}
}
assertNotNull("Unable to find TurboJpegImageWriter", writer);
IIOImage image = new IIOImage(inputImage, null, null);
// create write param
ImageWriteParam wParam_ = writer.getDefaultWriteParam();
assertTrue(wParam_ instanceof TurboJpegImageWriteParam);
TurboJpegImageWriteParam wParam = (TurboJpegImageWriteParam) wParam_;
wParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
wParam.setCompressionType("JPEG");
wParam.setCompressionQuality(.75f);
// create output file
final File output = TestData.temp(this, "output.jpeg", false);
LOGGER.info("output file is " + output);
writer.setOutput(output);
writer.write(null, image, wParam);
writer.dispose();
assertTrue("Unable to create output file", output.exists() && output.isFile());
} catch (Throwable t) {
} finally {
if (stream != null) {
try {
stream.close();
} catch (Throwable t) {
}
}
if (reader != null) {
try {
reader.dispose();
} catch (Throwable t) {
}
}
}
}
use of javax.imageio.ImageWriter in project imageio-ext by geosolutions-it.
the class JPEGWriterTest method writerTest.
@Test
public void writerTest() throws IOException {
if (SKIP_TESTS) {
LOGGER.warning(ERROR_LIB_MESSAGE);
assumeTrue(!SKIP_TESTS);
return;
}
// test-data
final File input = TestData.file(this, "testmergb.png");
assertTrue("Unable to find test data", input.exists() && input.isFile() && input.canRead());
// get the SPI for writer\
final Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName(TurboJpegImageWriterSpi.formatNames[0]);
assertTrue(it.hasNext());
TurboJpegImageWriter writer = null;
while (it.hasNext()) {
ImageWriterSpi writer_ = it.next().getOriginatingProvider();
if (writer_ instanceof TurboJpegImageWriterSpi) {
writer = (TurboJpegImageWriter) writer_.createWriterInstance();
break;
}
}
assertNotNull("Unable to find TurboJpegImageWriter", writer);
// create write param
ImageWriteParam wParam_ = writer.getDefaultWriteParam();
assertTrue(wParam_ instanceof TurboJpegImageWriteParam);
TurboJpegImageWriteParam wParam = (TurboJpegImageWriteParam) wParam_;
wParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
try {
wParam.setCompressionType("");
assertTrue("We should not be allowed to set an empty compression type", false);
} catch (Exception e) {
// TODO: handle exception
}
try {
wParam.setCompressionType("aaa");
assertTrue("We should not be allowed to set a generic compression type", false);
} catch (Exception e) {
// TODO: handle exception
}
wParam.setCompressionType("JPEG");
wParam.setCompressionQuality(.75f);
// create output file
final File output = TestData.temp(this, "output.jpeg", true);
writer.setOutput(output);
writer.write(null, new IIOImage(ImageIO.read(input), null, null), wParam);
assertTrue("Unable to create output file", output.exists() && output.isFile());
}
use of javax.imageio.ImageWriter in project imageio-ext by geosolutions-it.
the class JPEGWriterTest method basicWriterTest.
@Test
public void basicWriterTest() throws IOException {
if (SKIP_TESTS) {
LOGGER.warning(ERROR_LIB_MESSAGE);
assumeTrue(!SKIP_TESTS);
return;
}
// test-data
final File input = TestData.file(this, "testmergb.png");
assertTrue("Unable to find test data", input.exists() && input.isFile() && input.canRead());
// get the SPI for writer\
final Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName(TurboJpegImageWriterSpi.formatNames[0]);
assertTrue(it.hasNext());
TurboJpegImageWriter writer = null;
while (it.hasNext()) {
ImageWriterSpi writer_ = it.next().getOriginatingProvider();
if (writer_ instanceof TurboJpegImageWriterSpi) {
writer = (TurboJpegImageWriter) writer_.createWriterInstance();
break;
}
}
assertNotNull("Unable to find TurboJpegImageWriter", writer);
// create output file
final File output = TestData.temp(this, "output.jpeg", false);
writer.setOutput(output);
writer.write(ImageIO.read(input));
LOGGER.info("Writing output to " + output);
assertTrue("Unable to create output file", output.exists() && output.isFile());
}
use of javax.imageio.ImageWriter in project imageio-ext by geosolutions-it.
the class JP2KWriteTest method write_Cprecincts.
// ////////////////////////////////////////////////////////////////////////
//
// Testing "Cprecincts" Create Option.
//
// ////////////////////////////////////////////////////////////////////////
@Test
public void write_Cprecincts() throws IOException, FileNotFoundException {
if (!isJp2KakDriverAvailable)
return;
LOGGER.info("Testing JP2 Write operation with Cprecincts option setting");
// //
// Preparing input/output files
// //
final File inputFile = TestData.file(this, testFileName);
Assert.assertTrue(inputFile.exists());
// Output files resulting from 2 different values of the same create
// option. In this test, the create option is Cprecincts.
final File outputFile1 = TestData.temp(this, "CprecintsA-.jp2", deleteTempFilesOnExit);
final File outputFile2 = TestData.temp(this, "CprecintsB-.jp2", deleteTempFilesOnExit);
// //
// Preparing to read
// //
final ParameterBlockJAI pbjImageRead = new ParameterBlockJAI("ImageRead");
pbjImageRead.setParameter("Input", inputFile);
if (ENABLE_SUBSAMPLING) {
ImageReadParam readParam = new ImageReadParam();
readParam.setSourceSubsampling(4, 4, 0, 0);
pbjImageRead.setParameter("readParam", readParam);
}
pbjImageRead.setParameter("Reader", new JP2GDALKakaduImageReaderSpi().createReaderInstance());
RenderedOp image = JAI.create("ImageRead", pbjImageRead);
// ////////////////////////////////////////////////////////////////////
//
// preparing to write (1ST version of the create option test)
//
// ////////////////////////////////////////////////////////////////////
// Setting output and writer
final ParameterBlockJAI pbjImageWrite = new ParameterBlockJAI("ImageWrite");
pbjImageWrite.setParameter("Output", new FileImageOutputStreamExtImpl(outputFile1));
ImageWriter writer = new JP2GDALKakaduImageWriterSpi().createWriterInstance();
pbjImageWrite.setParameter("Writer", writer);
// Specifying image source to write
pbjImageWrite.addSource(image);
ImageWriteParam param = writer.getDefaultWriteParam();
// Specifying the required create option
((JP2GDALKakaduImageWriteParam) param).setCprecincts("{256,256},{256,256},{128,128}");
pbjImageWrite.setParameter("writeParam", param);
// Writing
final RenderedOp op = JAI.create("ImageWrite", pbjImageWrite);
// ////////////////////////////////////////////////////////////////////
//
// preparing to write (2ND version of the create option test)
//
// ////////////////////////////////////////////////////////////////////
// Setting output and writer
final ParameterBlockJAI pbjImageWrite2 = new ParameterBlockJAI("ImageWrite");
pbjImageWrite2.setParameter("Output", new FileImageOutputStreamExtImpl(outputFile2));
ImageWriter writer2 = new JP2GDALKakaduImageWriterSpi().createWriterInstance();
pbjImageWrite2.setParameter("Writer", writer2);
// Specifying image source to write
pbjImageWrite2.addSource(image);
ImageWriteParam param2 = writer2.getDefaultWriteParam();
// Specifying the required create option
((JP2GDALKakaduImageWriteParam) param2).setCprecincts("{512,512},{256,512},{128,512},{64,512},{32,512},{16,512},{8,512},{4,512},{2,512}");
pbjImageWrite2.setParameter("writeParam", param2);
// Writing
final RenderedOp op2 = JAI.create("ImageWrite", pbjImageWrite2);
}
Aggregations