Java Code Examples for javax.imageio.stream.FileImageOutputStream#close()

The following examples show how to use javax.imageio.stream.FileImageOutputStream#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UInt16ParamChoiceTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void writeUInt16ParamChoiceToFile(File file, int selector, int value) throws IOException
{
    final FileImageOutputStream stream = new FileImageOutputStream(file);

    switch (selector)
    {
    case 1:
        stream.writeByte(value);
        break;

    case 2:
    case 3:
    case 4:
        stream.writeShort(value);
        break;

    case 5:
    case 6:
        break;

    default:
        stream.writeInt(value);
    }

    stream.close();
}
 
Example 2
Source File: TeXFormula.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public void createImage(String format, int style, float size, String out, Color bg, Color fg, boolean transparency) {
    TeXIcon icon = createTeXIcon(style, size);
    icon.setInsets(new Insets(1, 1, 1, 1));
    int w = icon.getIconWidth(), h = icon.getIconHeight();

    BufferedImage image = new BufferedImage(w, h, transparency ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = image.createGraphics();
    if (bg != null && !transparency) {
        g2.setColor(bg);
        g2.fillRect(0, 0, w, h);
    }

    icon.setForeground(fg);
    icon.paintIcon(null, g2, 0, 0);
    try {
        FileImageOutputStream imout = new FileImageOutputStream(new File(out));
        ImageIO.write(image, format, imout);
        imout.flush();
        imout.close();
    } catch (IOException ex) {
        System.err.println("I/O error : Cannot generate " + out);
    }

    g2.dispose();
}
 
Example 3
Source File: UInt64ParamChoiceTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void writeUInt64ParamChoiceToFile(File file, BigInteger selector, int value) throws IOException
{
    final FileImageOutputStream stream = new FileImageOutputStream(file);

    if (selector.compareTo(new BigInteger("1")) == 0)
        stream.writeByte(value);
    else if (selector.compareTo(new BigInteger("2")) == 0 || selector.compareTo(new BigInteger("3")) == 0 ||
             selector.compareTo(new BigInteger("4")) == 0)
        stream.writeShort(value);
    else if (selector.compareTo(new BigInteger("5")) == 0 || selector.compareTo(new BigInteger("6")) == 0)
        ;
    else
        stream.writeInt(value);

    stream.close();
}
 
Example 4
Source File: DefaultEmptyChoiceTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void writeDefaultEmptyChoiceToFile(File file, byte tag, short value) throws IOException
{
    final FileImageOutputStream stream = new FileImageOutputStream(file);

    switch (tag)
    {
    case 1:
        stream.writeByte(value);
        break;

    case 2:
        stream.writeShort(value);
        break;

    default:
        break;
    }

    stream.close();
}
 
Example 5
Source File: CompoundAndFieldWithSameParamTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void writeToFile(File file) throws IOException
{
    final FileImageOutputStream stream = new FileImageOutputStream(file);
    stream.writeBits(FIELD1, 32);
    stream.writeBits(FIELD2, 32);
    stream.close();
}
 
Example 6
Source File: WaveImporter.java    From proarc with GNU General Public License v3.0 5 votes vote down vote up
private static File writeImage(BufferedImage image, File folder, String filename, ImageMimeType imageType) throws IOException {
    File imgFile = new File(folder, filename);
    FileImageOutputStream fos = new FileImageOutputStream(imgFile);
    try {
        ImageSupport.writeImageToStream(image, imageType.getDefaultFileExtension(), fos, 1.0f);
        return imgFile;
    } finally {
        fos.close();
    }
}
 
Example 7
Source File: TiffImporter.java    From proarc with GNU General Public License v3.0 5 votes vote down vote up
private static File writeImage(BufferedImage image, File folder, String filename, ImageMimeType imageType) throws IOException {
    File imgFile = new File(folder, filename);
    FileImageOutputStream fos = new FileImageOutputStream(imgFile);
    try {
        ImageSupport.writeImageToStream(image, imageType.getDefaultFileExtension(), fos, 1.0f);
        return imgFile;
    } finally {
        fos.close();
    }
}
 
Example 8
Source File: ThumbnailatorTest.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
@Test
public void testOutput() throws IOException {
    final String oldFile = System.getProperty("user.dir") + "/src/test/resources/images/lion2.jpg";
    File file = new File(oldFile);
    BufferedImage bufferedImage = ImageIO.read(file);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    // 输入形式:文件名
    Thumbnails.of(bufferedImage).scale(1.0).outputFormat("png").toOutputStream(outputStream);
    byte[] bytes = outputStream.toByteArray();
    FileImageOutputStream imageOutput = new FileImageOutputStream(new File("d:\\lion_output.png"));
    imageOutput.write(bytes, 0, bytes.length);
    imageOutput.close();
}
 
Example 9
Source File: OneStringStructureTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void writeOneStringStructureToFile(File file, String oneString) throws IOException
{
    final FileImageOutputStream stream = new FileImageOutputStream(file);

    stream.writeBits(oneString.length(), 8);
    stream.writeBytes(oneString);

    stream.close();
}
 
Example 10
Source File: StructureBitmaskConstraintsTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void writeStructureBitmaskConstraintsToFile(File file, Availability mask,
        short x, short y, short z) throws IOException
{
    final FileImageOutputStream stream = new FileImageOutputStream(file);

    stream.writeBits(mask.getValue(), 3);
    stream.writeBits(x, 8);
    stream.writeBits(y, 8);
    stream.writeBits(z, 8);

    stream.close();
}
 
Example 11
Source File: ArrayLengthofConstraintTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void writeArrayLengthofConstraintToFile(File file, int length)
        throws IOException
{
    final FileImageOutputStream stream = new FileImageOutputStream(file);

    stream.writeBits(length, 8); // all lengths in this test fits in a single byte

    for (int i = 0; i < length; ++i)
        stream.writeBits(i, 32);

    stream.close();
}
 
Example 12
Source File: FullEnumParamChoiceTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void writeFullEnumParamChoiceToFile(File file, Selector selector, int value) throws IOException
{
    final FileImageOutputStream stream = new FileImageOutputStream(file);

    if (selector == Selector.BLACK)
        stream.writeByte(value);
    else if (selector == Selector.GREY)
        stream.writeShort(value);
    else if (selector == Selector.WHITE)
        stream.writeInt(value);
    else
        fail("Invalid selector: " + selector);

    stream.close();
}
 
Example 13
Source File: SimpleParamTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void writeItemToFile(File file, long version, short param, int extraParam) throws IOException
{
    final FileImageOutputStream stream = new FileImageOutputStream(file);
    stream.writeShort(param);
    if (version >= HIGHER_VERSION)
        stream.writeInt(extraParam);
    stream.close();
}
 
Example 14
Source File: BitmaskParamChoiceTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void writeBitmaskParamChoiceToFile(File file, Selector selector, int value) throws IOException
{
    final FileImageOutputStream stream = new FileImageOutputStream(file);

    if (selector == Selector.Values.BLACK)
        stream.writeByte(value);
    else if (selector == Selector.Values.WHITE)
        stream.writeByte(value);
    else if (selector == Selector.Values.BLACK_AND_WHITE)
        stream.writeShort(value);
    else
        fail("Invalid selector: " + selector);

    stream.close();
}
 
Example 15
Source File: FullBitmaskParamChoiceTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void writeFullBitmaskParamChoiceToFile(File file, Selector selector, int value) throws IOException
{
    final FileImageOutputStream stream = new FileImageOutputStream(file);

    if (selector == Selector.Values.BLACK)
        stream.writeByte(value);
    else if (selector == Selector.Values.WHITE)
        stream.writeByte(value);
    else if (selector == Selector.Values.BLACK_AND_WHITE)
        stream.writeShort(value);
    else
        fail("Invalid selector: " + selector);

    stream.close();
}
 
Example 16
Source File: BoolParamChoiceTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void writeBoolParamChoiceToFile(File file, boolean selector, int value) throws IOException
{
    final FileImageOutputStream stream = new FileImageOutputStream(file);

    if (selector)
        stream.writeByte(value);
    else
        stream.writeShort(value);

    stream.close();
}
 
Example 17
Source File: BandWriterMain.java    From snap-examples with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Runs this program with the given parameters.
 */
private static void run(String inputPath, String outputPath, String bandName)
        throws IOException {

    // Read the product (note that only 'nodes' are read, not the entire data!)
    Product product = ProductIO.readProduct(inputPath);
    // Get the scene width
    int w = product.getSceneRasterWidth();
    // Get the scene height
    int h = product.getSceneRasterHeight();

    // Get the band with the given name
    Band band = product.getBand(bandName);
    if (band == null) {
        throw new IOException("band not found: " + bandName);
    }

    // Print out, what we are going to do...
    System.out.println("writing band raw image file "
                       + outputPath
                       + " containing " + w + " x " + h + " pixels of type "
                       + ProductData.getTypeString(band.getDataType()) + "...");

    // Create an output stream for the band's raw data
    FileImageOutputStream outputStream = new FileImageOutputStream(new File(outputPath));

    // Create a buffer for reading a single scan line of the band
    ProductData bandScanLine = band.createCompatibleRasterData(w, 1);

    // For all scan lines in the product...
    for (int y = 0; y < h; y++) {
        // Read the scan line at y
        band.readRasterData(0, y,
                            w, 1,
                            bandScanLine,
                            ProgressMonitor.NULL);

        // write band scan line to raw image file
        bandScanLine.writeTo(outputStream);
    }

    // close raw image file
    outputStream.close();

    // Done!
    System.out.println("OK");
}
 
Example 18
Source File: TiePointGridWriterMain.java    From snap-examples with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Runs this program with the given parameters.
 */
private static void run(String inputPath, String outputPath, String gridName)
        throws IOException {

    // Read the product (note that only 'nodes' are read, not the entire data!)
    Product product = ProductIO.readProduct(inputPath);
    // Get the scene width
    int w = product.getSceneRasterWidth();
    // Get the scene height
    int h = product.getSceneRasterHeight();

    // Get the tie-point grid with the given name
    TiePointGrid grid = product.getTiePointGrid(gridName);
    if (grid == null) {
        throw new IOException("tie-point grid not found: " + gridName);
    }

    // Print out, what we are going to do...
    System.out.println("writing tie-point grid raw image file "
                       + outputPath
                       + " containing " + w + " x " + h + " interpolated pixels of type "
                       + ProductData.getTypeString(grid.getDataType()) + "...");

    // Create an output stream for the grid's raw data
    FileImageOutputStream outputStream = new FileImageOutputStream(new File(outputPath));

    // Create a buffer for reading a single scan line of the grid
    float[] gridScanLine = new float[w];

    // For all scan lines in the product...
    for (int y = 0; y < h; y++) {
        // Read the scan line at y
        grid.readPixels(0, y, // x (=0) & y offsets
                        w, 1, // width (=w) & height (=1)
                        gridScanLine,
                        ProgressMonitor.NULL);

        // write grid scan line to raw image file
        outputStream.writeFloats(gridScanLine, 0, w);
    }

    // close raw image file
    outputStream.close();

    // Done!
    System.out.println("OK");
}
 
Example 19
Source File: NdviSimpleMain.java    From snap-examples with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Runs this program with the given parameters.
 */
private static void run(String inputPath, String outputPath)
        throws IOException {

    // Read the product (note that only 'nodes' are read, not the entire data!)
    Product product = ProductIO.readProduct(inputPath);
    // Get the scene width
    int w = product.getSceneRasterWidth();
    // Get the scene height
    int h = product.getSceneRasterHeight();

    // Get the "low" band
    Band lowBand = product.getBand("radiance_6");
    if (lowBand == null) {
        throw new IOException("low-band 'radiance_6' not found");
    }

    // Get the "high" band
    Band hiBand = product.getBand("radiance_10");
    if (hiBand == null) {
        throw new IOException("hi-band 'radiance_10' not found");
    }

    // Print out, what we are going to do...
    System.out.println("writing NDVI raw image file "
                       + outputPath
                       + " containing " + w + " x " + h
                       + " pixels of type byte (value range 0-255)...");

    // Create an output stream for the NDVI raw data
    FileImageOutputStream outputStream = new FileImageOutputStream(new File(outputPath));

    // Create a buffer for reading a single scan line of the low-band
    float[] lowBandPixels = new float[w];
    // Create a buffer for reading a single scan line of the hi-band
    float[] hiBandPixels = new float[w];

    // Hi/Low-band sum and difference of the NDVI quotient
    float sum, dif;
    // NDVI value
    float ndvi;
    // NDVI value in the range 0 to 255
    int ndviByte;

    // For all scan lines in the product...
    for (int y = 0; y < h; y++) {
        // Read low-band pixels for line y
        lowBand.readPixels(0, y, w, 1, lowBandPixels, new PrintWriterProgressMonitor(System.out));
        // Read hi-band pixels for line y
        hiBand.readPixels(0, y, w, 1, hiBandPixels, new PrintWriterProgressMonitor(System.out));

        // Compute NDVI for all x
        for (int x = 0; x < w; x++) {
            dif = lowBandPixels[x] - hiBandPixels[x];
            sum = lowBandPixels[x] + hiBandPixels[x];

            if (sum != 0.0F) {
                ndvi = dif / sum;
            } else {
                ndvi = 0.0F;
            }

            if (ndvi < 0.0F) {
                ndvi = 0.0F;
            } else if (ndvi > 1.0F) {
                ndvi = 1.0F;
            }

            // Convert NDVI to integer in the range 0 to 255
            ndviByte = (int) (255 * ndvi);

            // write NDVI byte to raw image file
            outputStream.writeByte(ndviByte);
        }
    }

    // close raw image file
    outputStream.close();

    // Done!
    System.out.println("OK");
}
 
Example 20
Source File: Images.java    From restcommander with Apache License 2.0 4 votes vote down vote up
/**
 * Resize an image
 * @param originalImage The image file
 * @param to The destination file
 * @param w The new width (or -1 to proportionally resize) or the maxWidth if keepRatio is true
 * @param h The new height (or -1 to proportionally resize) or the maxHeight if keepRatio is true
 * @param keepRatio : if true, resize will keep the original image ratio and use w and h as max dimensions
 */
public static void resize(File originalImage, File to, int w, int h, boolean keepRatio) {
    try {
        BufferedImage source = ImageIO.read(originalImage);
        int owidth = source.getWidth();
        int oheight = source.getHeight();
        double ratio = (double) owidth / oheight;
        
        int maxWidth = w;
        int maxHeight = h;
        
        if (w < 0 && h < 0) {
            w = owidth;
            h = oheight;
        }
        if (w < 0 && h > 0) {
            w = (int) (h * ratio);
        }
        if (w > 0 && h < 0) {
            h = (int) (w / ratio);
        }
        
        if(keepRatio) {
            h = (int) (w / ratio);
            if(h > maxHeight) {
                h = maxHeight;
                w = (int) (h * ratio);
            }
            if(w > maxWidth) {
                w = maxWidth;
                h = (int) (w / ratio);
            }
        }

        String mimeType = "image/jpeg";
        if (to.getName().endsWith(".png")) {
            mimeType = "image/png";
        }
        if (to.getName().endsWith(".gif")) {
            mimeType = "image/gif";
        }

        // out
        BufferedImage dest = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Image srcSized = source.getScaledInstance(w, h, Image.SCALE_SMOOTH);
        Graphics graphics = dest.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, w, h);
        graphics.drawImage(srcSized, 0, 0, null);
        ImageWriter writer = ImageIO.getImageWritersByMIMEType(mimeType).next();
        ImageWriteParam params = writer.getDefaultWriteParam();
        FileImageOutputStream toFs = new FileImageOutputStream(to);
        writer.setOutput(toFs);
        IIOImage image = new IIOImage(dest, null, null);
        writer.write(null, image, params);
        toFs.flush();
        toFs.close();

    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}