Search in sources :

Example 6 with FileImageOutputStream

use of javax.imageio.stream.FileImageOutputStream 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;
    }
}
Also used : FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) ImageWriter(javax.imageio.ImageWriter) IOException(java.io.IOException) ImageWriteParam(javax.imageio.ImageWriteParam) ImageOutputStream(javax.imageio.stream.ImageOutputStream) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 7 with FileImageOutputStream

use of javax.imageio.stream.FileImageOutputStream in project imageio-ext by geosolutions-it.

the class JP2KKakaduQualityLayersWriteTest method write.

private void write(final String inputFileName, final RenderedImage originalImage, final Compression type, final CompressionProfile profile) throws IOException {
    JP2KKakaduImageWriter kakaduWriter = (JP2KKakaduImageWriter) JP2K_WSPI.createWriterInstance();
    ImageReader reader = JP2K_RSPI.createReaderInstance();
    FileImageOutputStream fos = null;
    FileImageInputStream fis = null;
    String suffix;
    switch(type) {
        case NUMERICALLY_LOSSLESS:
            suffix = "NL";
            break;
        case LOSSY:
            suffix = "VL";
            break;
        default:
            suffix = "Lossy";
    }
    final String outputFileName = inputFileName + "rewrittenAs_" + suffix + "_" + profile + ".jp2";
    final File outputFile = new File(outputFileName);
    try {
        fos = new FileImageOutputStream(outputFile);
        kakaduWriter.setOutput(fos);
        JP2KKakaduImageWriteParam param = setupWriteParameter(kakaduWriter, type, profile);
        kakaduWriter.write(null, new IIOImage(originalImage, null, null), param);
        kakaduWriter.dispose();
        kakaduWriter = null;
        fis = new FileImageInputStream(outputFile);
        RenderedImage readBack = ImageReadDescriptor.create(fis, 0, false, false, false, null, null, null, reader, null);
        RenderedImage difference = SubtractDescriptor.create(readBack, originalImage, null);
        double[][] extrema = (double[][]) ExtremaDescriptor.create(difference, null, 1, 1, false, 1, null).getProperty("Extrema");
        if (type == Compression.NUMERICALLY_LOSSLESS) {
            for (int i = 0; i < extrema.length; i++) {
                for (int j = 0; j < extrema[i].length; j++) {
                    assertEquals(extrema[i][j], 0, THRESHOLD);
                }
            }
            LOGGER.info("Numerically LossLess successfull: No differences with " + "respect to the original image");
        } else {
            StringBuilder sb = new StringBuilder("Extrema values on VISUALLY_LOSSLESS (LOSSY)" + " compressions for: " + outputFileName);
            for (int i = 0; i < extrema.length; i++) {
                for (int j = 0; j < extrema[i].length; j++) {
                    sb.append(extrema[i][j]).append(" ");
                }
            }
            LOGGER.info(sb.toString());
        }
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (Throwable t) {
            }
        }
        if (kakaduWriter != null) {
            try {
                kakaduWriter.dispose();
            } catch (Throwable t) {
            }
        }
        if (fis != null) {
            try {
                fis.close();
            } catch (Throwable t) {
            }
        }
        if (reader != null) {
            try {
                reader.dispose();
            } catch (Throwable t) {
            }
        }
    }
}
Also used : FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) IIOImage(javax.imageio.IIOImage) FileImageInputStream(javax.imageio.stream.FileImageInputStream) ImageReader(javax.imageio.ImageReader) RenderedImage(java.awt.image.RenderedImage) File(java.io.File)

Example 8 with FileImageOutputStream

use of javax.imageio.stream.FileImageOutputStream in project imageio-ext by geosolutions-it.

the class JP2KKakaduWriteTest method testOutputStream.

public void testOutputStream() throws IOException {
    if (!isKakaduAvailable) {
        LOGGER.warning("Kakadu libs not found: test are skipped ");
        return;
    }
    final File outFile = File.createTempFile("stream", "temp");
    final FileImageOutputStream stream = new FileImageOutputStream(outFile);
    stream.writeBytes("This is an Image Header written before the j2c raw codestream");
    final ImageReader reader = new BMPImageReaderSpi().createReaderInstance();
    final File file = TestData.file(this, "RGB24.bmp");
    reader.setInput(ImageIO.createImageInputStream(file));
    BufferedImage bi = reader.read(0);
    final ImageWriter writer = new JP2KKakaduImageWriterSpi().createWriterInstance();
    JP2KKakaduImageWriteParam param = new JP2KKakaduImageWriteParam();
    param.setQuality(0.8);
    param.setWriteCodeStreamOnly(true);
    writer.setOutput(stream);
    writer.write(null, new IIOImage(bi, null, null), param);
    writer.dispose();
    LOGGER.info(writeOperations + " write operations performed");
}
Also used : FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) ImageWriter(javax.imageio.ImageWriter) BMPImageReaderSpi(com.sun.imageio.plugins.bmp.BMPImageReaderSpi) ImageReader(javax.imageio.ImageReader) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage)

Example 9 with FileImageOutputStream

use of javax.imageio.stream.FileImageOutputStream in project imageio-ext by geosolutions-it.

the class JP2KakaduReadTest method inputsTest.

@org.junit.Test
public void inputsTest() throws IOException {
    if (!runTests)
        return;
    // //
    // 
    // Testing base reader methods
    // 
    // //
    final File file = TestData.file(this, "CB_TM432.jp2");
    final ImageReader reader = new JP2KKakaduImageReaderSpi().createReaderInstance();
    reader.setInput(file);
    Assert.assertEquals(1, reader.getNumImages(false));
    Assert.assertEquals(488, reader.getTileHeight(0));
    Assert.assertEquals(361, reader.getTileWidth(0));
    Assert.assertEquals(488, reader.getHeight(0));
    Assert.assertEquals(361, reader.getWidth(0));
    Assert.assertNotNull(reader.getStreamMetadata());
    Assert.assertNotNull(reader.getImageMetadata(0));
    Assert.assertNotNull(reader.getImageTypes(0));
    // //
    // 
    // Quick Test on wrong image index
    // 
    // //
    boolean isValidImageIndex = false;
    try {
        reader.getWidth(99);
        isValidImageIndex = true;
    } catch (IndexOutOfBoundsException e) {
        Assert.assertFalse(isValidImageIndex);
    }
    // //
    // 
    // Testing raw jp2 file
    // 
    // //
    final File rawfile = TestData.file(this, "raw.j2c");
    final ImageReader rawreader = new JP2KKakaduImageReaderSpi().createReaderInstance();
    rawreader.setInput(rawfile);
    rawreader.read(0);
    boolean hasStreamMetadata = false;
    try {
        rawreader.getStreamMetadata();
        hasStreamMetadata = true;
    } catch (UnsupportedOperationException e) {
        Assert.assertFalse(hasStreamMetadata);
    }
    // //
    // 
    // Testing a file which isn't a jp2 one
    // 
    // //
    boolean isValidInput = false;
    final File badfile = File.createTempFile("bad", ".jp2");
    badfile.deleteOnExit();
    final FileImageOutputStream fios = new FileImageOutputStream(badfile);
    fios.writeChars("BAD");
    fios.close();
    final ImageReader badFileReader = new JP2KKakaduImageReaderSpi().createReaderInstance();
    try {
        badFileReader.setInput(badfile);
        isValidInput = true;
    } catch (Throwable t) {
        Assert.assertFalse(isValidInput);
    }
}
Also used : FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) ImageReader(javax.imageio.ImageReader) File(java.io.File)

Example 10 with FileImageOutputStream

use of javax.imageio.stream.FileImageOutputStream in project Denizen-For-Bukkit by DenizenScript.

the class DenizenMapManager method downloadImage.

private static String downloadImage(URL url) {
    try {
        if (failedUrls.contains(url.toString())) {
            return null;
        }
        if (!imageDownloads.exists()) {
            imageDownloads.mkdirs();
        }
        String urlString = CoreUtilities.toLowerCase(url.toString());
        if (downloadedByUrl.containsKey(urlString)) {
            File image = new File(imageDownloads, downloadedByUrl.get(urlString));
            if (image.exists()) {
                return image.getPath();
            }
        }
        URLConnection connection = url.openConnection();
        BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
        int lastDot = urlString.lastIndexOf('.');
        String fileName = String.format("%0" + (6 - String.valueOf(downloadCount).length()) + "d", downloadCount) + (lastDot > 0 ? allowedExtensionText.trimToMatches(urlString.substring(lastDot)) : "");
        File output = new File(imageDownloads, fileName);
        FileImageOutputStream out = new FileImageOutputStream(output);
        int i;
        while ((i = in.read()) != -1) {
            out.write(i);
        }
        out.flush();
        out.close();
        in.close();
        downloadedByUrl.put(urlString, fileName);
        downloadCount++;
        return output.getPath();
    } catch (IOException e) {
        failedUrls.add(url.toString());
        Debug.echoError(e);
    }
    return null;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) IOException(java.io.IOException) File(java.io.File) URLConnection(java.net.URLConnection)

Aggregations

FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)21 File (java.io.File)18 ImageWriter (javax.imageio.ImageWriter)14 IOException (java.io.IOException)13 IIOImage (javax.imageio.IIOImage)13 ImageWriteParam (javax.imageio.ImageWriteParam)10 BufferedImage (java.awt.image.BufferedImage)9 ImageOutputStream (javax.imageio.stream.ImageOutputStream)8 Rectangle (java.awt.Rectangle)4 Iterator (java.util.Iterator)3 ImageReader (javax.imageio.ImageReader)3 FileImageInputStream (javax.imageio.stream.FileImageInputStream)3 Test (org.junit.Test)3 Graphics2D (java.awt.Graphics2D)2 Robot (java.awt.Robot)2 BufferedInputStream (java.io.BufferedInputStream)2 URLConnection (java.net.URLConnection)2 DataFormatException (java.util.zip.DataFormatException)2 JPEGImageWriteParam (javax.imageio.plugins.jpeg.JPEGImageWriteParam)2 ImageWriterSpi (javax.imageio.spi.ImageWriterSpi)2