Example usage for javax.imageio.stream FileImageOutputStream close

List of usage examples for javax.imageio.stream FileImageOutputStream close

Introduction

In this page you can find the example usage for javax.imageio.stream FileImageOutputStream close.

Prototype

public void close() throws IOException 

Source Link

Usage

From source file:cz.cas.lib.proarc.common.imports.TiffImporter.java

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 {/*from  w w  w .ja v  a  2s  . com*/
        ImageSupport.writeImageToStream(image, imageType.getDefaultFileExtension(), fos, 1.0f);
        return imgFile;
    } finally {
        fos.close();
    }
}

From source file:editeurpanovisu.ReadWriteImage.java

/**
 *
 * @param img//w ww.  j  a v a 2 s  . c  o m
 * @param destFile
 * @param sharpen
 * @param sharpenLevel
 * @throws IOException
 */
public static void writePng(Image img, String destFile, boolean sharpen, float sharpenLevel)
        throws IOException {
    sharpenMatrix = calculeSharpenMatrix(sharpenLevel);
    BufferedImage imageRGBSharpen = null;
    IIOImage iioImage = null;
    BufferedImage image = SwingFXUtils.fromFXImage(img, null); // Get buffered image.
    BufferedImage imageRGB = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.BITMASK);
    Graphics2D graphics = imageRGB.createGraphics();
    graphics.drawImage(image, 0, 0, null);
    if (sharpen) {
        imageRGBSharpen = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
        Kernel kernel = new Kernel(3, 3, sharpenMatrix);
        ConvolveOp cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
        cop.filter(imageRGB, imageRGBSharpen);
    }
    ImageWriter writer = null;
    FileImageOutputStream output = null;
    try {
        writer = ImageIO.getImageWritersByFormatName("png").next();
        ImageWriteParam param = writer.getDefaultWriteParam();
        output = new FileImageOutputStream(new File(destFile));
        writer.setOutput(output);
        if (sharpen) {
            iioImage = new IIOImage(imageRGBSharpen, null, null);
        } else {
            iioImage = new IIOImage(imageRGB, null, null);
        }
        writer.write(null, iioImage, param);
    } catch (IOException ex) {
        throw ex;
    } finally {
        if (writer != null) {
            writer.dispose();
        }
        if (output != null) {
            output.close();
        }
    }
    graphics.dispose();
}

From source file:editeurpanovisu.ReadWriteImage.java

/**
 *
 * @param img// w  w  w  .  ja v a  2s  .c o m
 * @param destFile
 * @param sharpen
 * @param sharpenLevel
 * @throws IOException
 */
public static void writeBMP(Image img, String destFile, boolean sharpen, float sharpenLevel)
        throws IOException {
    sharpenMatrix = calculeSharpenMatrix(sharpenLevel);
    BufferedImage imageRGBSharpen = null;
    IIOImage iioImage = null;

    BufferedImage image = SwingFXUtils.fromFXImage(img, null); // Get buffered image.
    BufferedImage imageRGB = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.OPAQUE); // Remove alpha-channel from buffered image.

    Graphics2D graphics = imageRGB.createGraphics();
    graphics.drawImage(image, 0, 0, null);
    if (sharpen) {

        imageRGBSharpen = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
        Kernel kernel = new Kernel(3, 3, sharpenMatrix);
        ConvolveOp cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
        cop.filter(imageRGB, imageRGBSharpen);
    }
    ImageWriter writer = null;
    FileImageOutputStream output = null;
    try {
        writer = ImageIO.getImageWritersByFormatName("bmp").next();
        ImageWriteParam param = writer.getDefaultWriteParam();
        output = new FileImageOutputStream(new File(destFile));
        writer.setOutput(output);
        if (sharpen) {
            iioImage = new IIOImage(imageRGBSharpen, null, null);
        } else {
            iioImage = new IIOImage(imageRGB, null, null);
        }
        writer.write(null, iioImage, param);
    } catch (IOException ex) {
        throw ex;
    } finally {
        if (writer != null) {
            writer.dispose();
        }
        if (output != null) {
            output.close();
        }
    }
    graphics.dispose();
}

From source file:com.bc.util.io.FileUtils.java

public static void copy(File source, File target) throws IOException {
    final byte[] buffer = new byte[ONE_KB * ONE_KB];
    int bytesRead;

    FileImageInputStream sourceStream = null;
    FileImageOutputStream targetStream = null;

    try {// ww  w. ja  va 2s  .c o m
        final File targetDir = target.getParentFile();
        if (!targetDir.isDirectory()) {
            if (!targetDir.mkdirs()) {
                throw new IOException("failed to create target directory: " + targetDir.getAbsolutePath());
            }
        }
        target.createNewFile();

        sourceStream = new FileImageInputStream(source);
        targetStream = new FileImageOutputStream(target);
        while ((bytesRead = sourceStream.read(buffer)) >= 0) {
            targetStream.write(buffer, 0, bytesRead);
        }
    } finally {
        if (sourceStream != null) {
            sourceStream.close();
        }
        if (targetStream != null) {
            targetStream.flush();
            targetStream.close();
        }
    }
}

From source file:editeurpanovisu.ReadWriteImage.java

/**
 *
 * @param img/*  w  ww . ja  v a  2  s  .  c o m*/
 * @param destFile
 * @param quality
 * @param sharpen
 * @param sharpenLevel
 * @throws IOException
 */
public static void writeJpeg(Image img, String destFile, float quality, boolean sharpen, float sharpenLevel)
        throws IOException {
    sharpenMatrix = calculeSharpenMatrix(sharpenLevel);
    BufferedImage imageRGBSharpen = null;
    IIOImage iioImage = null;
    BufferedImage image = SwingFXUtils.fromFXImage(img, null); // Get buffered image.

    BufferedImage imageRGB = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.OPAQUE); // Remove alpha-channel from buffered image.

    Graphics2D graphics = imageRGB.createGraphics();
    graphics.drawImage(image, 0, 0, null);
    if (sharpen) {
        imageRGBSharpen = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
        Kernel kernel = new Kernel(3, 3, sharpenMatrix);
        ConvolveOp cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
        cop.filter(imageRGB, imageRGBSharpen);
    }
    ImageWriter writer = null;
    FileImageOutputStream output = null;
    try {
        writer = ImageIO.getImageWritersByFormatName("jpeg").next();
        ImageWriteParam param = writer.getDefaultWriteParam();
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionQuality(quality);
        output = new FileImageOutputStream(new File(destFile));
        writer.setOutput(output);
        if (sharpen) {
            iioImage = new IIOImage(imageRGBSharpen, null, null);
        } else {
            iioImage = new IIOImage(imageRGB, null, null);
        }
        writer.write(null, iioImage, param);
    } catch (IOException ex) {
        throw ex;
    } finally {
        if (writer != null) {
            writer.dispose();
        }
        if (output != null) {
            output.close();
        }
    }
    graphics.dispose();
}

From source file:edu.ku.brc.specify.extras.FishBaseInfoGetter.java

/**
 * Performs a "generic" HTTP request and fill member variable with results use
 * "getDigirResultsetStr" to get the results as a String
 *
 *///  w w  w.j  a v  a  2s.co  m
public Image getImage(final String fileName, final String url) {
    imageURL = url;

    String fullPath = tmpDir + File.separator + fileName;
    ////System.out.println(fullPath);
    File file = new File(fullPath);
    if (file.exists()) {
        try {
            return new ImageIcon(fullPath).getImage();

        } catch (Exception ex) {
            edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(FishBaseInfoGetter.class, ex);
            ex.printStackTrace();
            status = ErrorCode.Error;
        }

    } else {
        byte[] bytes = super.doHTTPRequest(url);

        try {
            FileImageOutputStream fos = new FileImageOutputStream(file);
            fos.write(bytes);
            fos.flush();
            fos.close();

            return new ImageIcon(bytes).getImage();

        } catch (Exception ex) {
            edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(FishBaseInfoGetter.class, ex);
            ex.printStackTrace();
            status = ErrorCode.Error;
        }
    }
    return null;
}

From source file:beans.FotoBean.java

public void tirarFoto(CaptureEvent captureEvent) throws IOException {
    arquivo = gerarNome();/*from www.  j av  a2 s.c o m*/
    byte[] data = captureEvent.getData();

    ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext()
            .getContext();
    String pasta = servletContext.getRealPath("") + File.separator + "resources" + File.separator
            + "fotossalvas";
    File vpasta = new File(pasta);
    if (!vpasta.exists()) {
        vpasta.setWritable(true);
        vpasta.mkdirs();
    }
    String novoarquivo = pasta + File.separator + arquivo + ".jpeg";
    System.out.println(novoarquivo);
    fotosalva = new File(novoarquivo);
    fotosalva.createNewFile();
    FileImageOutputStream imageOutput;
    try {
        imageOutput = new FileImageOutputStream(fotosalva);
        imageOutput.write(data, 0, data.length);
        imageOutput.close();

    } catch (IOException e) {
        Util.criarMensagemErro(e.toString());

    }
}

From source file:de.hpi.fgis.hdrs.node.Index.java

private void writeMeta() throws IOException {
    File metaFile = new File(indexRoot.getAbsolutePath() + File.separator + METAFILE);
    if (metaFile.isFile()) {
        if (!metaFile.delete()) {
            throw new IOException("Cannot delete old meta file for index " + order);
        }/*from w w w  .  j  a v a 2  s  .co  m*/
    }
    FileImageOutputStream out = new FileImageOutputStream(metaFile);
    out.writeInt(segments.size());
    for (SegmentInfo info : segments) {
        info.write(out);
    }
    out.close();
}

From source file:com.kahlon.guard.controller.PersonImageManager.java

/**
 * Capture Image//  ww w. ja  va2  s  . c  o  m
 * 
 * @param captureEvent
 */
public void onCapture(CaptureEvent captureEvent) {

    photo = getRandomImageName();
    byte[] data = captureEvent.getData();

    ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext()
            .getContext();
    String newFileName = servletContext.getRealPath("") + File.separator + "resources" + File.separator
            + "image" + File.separator + "temp" + File.separator + photo + ".png";
    if (fileNames == null) {
        fileNames = new ArrayList<>();
    }
    fileNames.add(newFileName);

    FileImageOutputStream imageOutput;
    try {
        imageOutput = new FileImageOutputStream(new File(newFileName));
        imageOutput.write(data, 0, data.length);
        imageOutput.close();
        imageUpload = false;
    } catch (Exception e) {
        throw new FacesException("Error in writing captured image.");
    }
}

From source file:com.kahlon.guard.controller.PersonImageManager.java

/**
 * Upload person image file//from  w ww .ja v a 2s  .  co  m
 *
 * @param event
 * @throws java.io.IOException
 */
public void onUpload(FileUploadEvent event) throws IOException {
    photo = getRandomImageName();
    byte[] data = IOUtils.toByteArray(event.getFile().getInputstream());

    ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext()
            .getContext();
    String newFileName = servletContext.getRealPath("") + File.separator + "resources" + File.separator
            + "image" + File.separator + "temp" + File.separator + photo + ".png";
    if (fileNames == null) {
        fileNames = new ArrayList<>();
    }
    fileNames.add(newFileName);

    FileImageOutputStream imageOutput;
    try {
        imageOutput = new FileImageOutputStream(new File(newFileName));
        imageOutput.write(data, 0, data.length);
        imageOutput.close();

        BufferedImage originalImage = ImageIO.read(new File(newFileName));
        int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();

        BufferedImage resizeImagePng = resizeImageWithHint(originalImage, type);
        ImageIO.write(resizeImagePng, "png", new File(newFileName));

    } catch (Exception e) {
        throw new FacesException("Error in writing captured image.");
    }
    FacesMessage msg = new FacesMessage("Success! ", event.getFile().getFileName() + " is uploaded.");
    FacesContext.getCurrentInstance().addMessage(null, msg);
    imageUpload = false;
}