Example usage for javax.imageio.stream FileImageOutputStream FileImageOutputStream

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

Introduction

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

Prototype

public FileImageOutputStream(RandomAccessFile raf) 

Source Link

Document

Constructs a FileImageOutputStream that will write to a given RandomAccessFile .

Usage

From source file:org.yamj.core.service.file.FileStorageService.java

public void storeImage(String filename, StorageType type, BufferedImage bi, ImageFormat imageFormat,
        int quality) throws Exception {
    LOG.debug("Store {} {} image: {}", type, imageFormat, filename);
    String storageFileName = getStorageName(type, filename);
    File outputFile = new File(storageFileName);

    ImageWriter writer = null;//www  . j av a2 s  .  c o  m
    FileImageOutputStream output = null;
    try {
        if (ImageFormat.PNG == imageFormat) {
            ImageIO.write(bi, "png", outputFile);
        } else {
            float jpegQuality = (float) quality / 100;
            BufferedImage bufImage = new BufferedImage(bi.getWidth(), bi.getHeight(),
                    BufferedImage.TYPE_INT_RGB);
            bufImage.createGraphics().drawImage(bi, 0, 0, null, null);

            @SuppressWarnings("rawtypes")
            Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
            writer = (ImageWriter) iter.next();

            ImageWriteParam iwp = writer.getDefaultWriteParam();
            iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            iwp.setCompressionQuality(jpegQuality);

            output = new FileImageOutputStream(outputFile);
            writer.setOutput(output);
            IIOImage image = new IIOImage(bufImage, null, null);
            writer.write(null, image, iwp);
        }
    } finally {
        if (writer != null) {
            writer.dispose();
        }
        if (output != null) {
            try {
                output.close();
            } catch (IOException ex) {
                LOG.trace("Failed to close stream: {}", ex.getMessage(), ex);
            }
        }
    }
}

From source file:editeurpanovisu.ReadWriteImage.java

/**
 *
 * @param img/* w ww .  ja  v  a  2s . c om*/
 * @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.sire.web.CajFacturaEnviadaBean.java

private void savePicture() {
    if (file != null) {
        try {/*  w  w  w. j  a v a  2s.  c o  m*/
            BufferedImage originalImage = ImageIO.read(file.getInputstream());

            BufferedImage outputImage = new BufferedImage((int) (originalImage.getWidth() * 0.25),
                    (int) (originalImage.getHeight() * 0.25), originalImage.getType());

            Graphics2D g2d = outputImage.createGraphics();
            g2d.drawImage(originalImage, 0, 0, (int) (originalImage.getWidth() * 0.25),
                    (int) (originalImage.getHeight() * 0.25), null);
            g2d.dispose();

            String imagesFolder = System.getProperty("imagesFolder");

            if (imagesFolder == null) {
                String currentUsersHomeDir = System.getProperty("user.home");
                imagesFolder = currentUsersHomeDir + File.separator + "photos";
            }

            Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
            ImageWriter writer = (ImageWriter) iter.next();
            ImageWriteParam iwp = writer.getDefaultWriteParam();

            iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            float quality = 1.0f; // reduce quality by 0%  
            iwp.setCompressionQuality(quality);

            File f = new File(imagesFolder + File.separator + fileName);
            try (FileImageOutputStream output = new FileImageOutputStream(f)) {
                writer.setOutput(output);

                IIOImage image = new IIOImage(outputImage, null, null);
                writer.write(null, image, iwp);
                writer.dispose();
            }
        } catch (IOException ex) {
            LOGGER.severe(ex.getMessage());
        }
    }
}

From source file:editeurpanovisu.ReadWriteImage.java

/**
 *
 * @param img/*from  w ww. ja  v  a  2  s .  c  om*/
 * @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:com.mycompany.CRMFly.ManagedBeans.EmployeeBean.java

public void handleFileUpload(FileUploadEvent event) {
    // UploadedFile file = event.getFile();
    setUploaded_image(event.getFile());//from   ww  w. j  a  va2 s  .  c  o  m
    ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext()
            .getContext();
    String v_file_ext = getUploaded_image().getFileName()
            .split("\\.")[(getUploaded_image().getFileName().split("\\.").length) - 1];
    setUpload_location(servletContext.getRealPath("") + File.separator + "temp-images" + File.separator + "3"
            + "." + v_file_ext);
    FileImageOutputStream imageOutput;
    try {
        imageOutput = new FileImageOutputStream(new File(getUpload_location()));
        imageOutput.write(getUploaded_image().getContents(), 0, getUploaded_image().getContents().length);
        imageOutput.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    byte[] file = new byte[uploaded_image.getContents().length];
    System.arraycopy(uploaded_image.getContents(), 0, file, 0, uploaded_image.getContents().length);
    employee.setPhoto(file);

    FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
    FacesContext.getCurrentInstance().addMessage(null, msg);
}

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  ww. j  a  va 2  s  .  c o  m*/
    }
    FileImageOutputStream out = new FileImageOutputStream(metaFile);
    out.writeInt(segments.size());
    for (SegmentInfo info : segments) {
        info.write(out);
    }
    out.close();
}

From source file:net.intelliant.util.UtilCommon.java

private static String generateCompressedImageForInputFile(String locationInFileSystem, String srcFileName)
        throws NoSuchAlgorithmException, IOException {
    String outputFileName = srcFileName;
    File srcFile = new File(locationInFileSystem, srcFileName);
    File compressedFile = null;// ww  w . ja  v  a2s.c  o m
    FileImageOutputStream outputStream = null;
    try {
        if (srcFile.exists()) {
            String md5sum = getMD5SumForFile(srcFile.getAbsolutePath());
            int extentionIndex = srcFileName.lastIndexOf("."); // find index of extension.
            if (extentionIndex != -1) {
                outputFileName = outputFileName.replaceFirst("\\.", "_" + md5sum + ".");
            }
            compressedFile = new File(locationInFileSystem, outputFileName);
            if (Debug.infoOn()) {
                Debug.logInfo("[generateCompressedImageFor] sourceFile >> " + srcFile.getAbsolutePath(),
                        module);
                Debug.logInfo("[generateCompressedImageFor] md5sum >> " + md5sum, module);
                Debug.logInfo(
                        "[generateCompressedImageFor] compressedFile >> " + compressedFile.getAbsolutePath(),
                        module);
            }
            if (!compressedFile.exists()) {
                if (Debug.infoOn()) {
                    Debug.logInfo("[generateCompressedImageFor] compressed file does NOT exist..", module);
                }
                Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");
                ImageWriter imageWriter = (ImageWriter) iter.next();
                ImageWriteParam iwp = imageWriter.getDefaultWriteParam();
                iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                iwp.setCompressionQuality(Float.parseFloat(imageCompressionQuality)); // an integer between 0 and 1, 1 specifies minimum compression and maximum quality
                BufferedImage bufferedImage = ImageIO.read(srcFile);
                outputStream = new FileImageOutputStream(compressedFile);
                imageWriter.setOutput(outputStream);
                IIOImage image = new IIOImage(bufferedImage, null, null);
                imageWriter.write(null, image, iwp);
            } else {
                if (Debug.infoOn()) {
                    Debug.logInfo(
                            "[generateCompressedImageFor] compressed file exists, not compressing again..",
                            module);
                }
            }
        } else {
            Debug.logWarning(String.format("Source image file does NOT exist..", srcFile), module);
        }
    } finally {
        if (outputStream != null) {
            outputStream.close();
            outputStream = null;
        }
    }
    return outputFileName;
}

From source file:de.inren.service.picture.PictureModificationServiceImpl.java

private void writeJpeg(BufferedImage image, File file, float quality) throws IOException {
    ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
    ImageWriteParam iwp = writer.getDefaultWriteParam();
    iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    iwp.setCompressionQuality(quality);/*  ww  w. j  a v  a 2s . c om*/

    FileImageOutputStream output = new FileImageOutputStream(file);
    writer.setOutput(output);
    writer.write(null, new IIOImage(image, null, null), iwp);
}

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   www.j a v  a2 s. c  om
        ImageSupport.writeImageToStream(image, imageType.getDefaultFileExtension(), fos, 1.0f);
        return imgFile;
    } finally {
        fos.close();
    }
}

From source file:org.nekorp.workflow.desktop.servicio.reporte.orden.servicio.OrdenServicioDataFactory.java

private void saveJPG(BufferedImage img, File file) {
    ImageWriter writer = null;//from  w w  w. j av  a 2 s .  c o  m
    FileImageOutputStream output = null;
    try {
        writer = ImageIO.getImageWritersByFormatName("jpeg").next();
        ImageWriteParam param = writer.getDefaultWriteParam();
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionQuality(1);
        output = new FileImageOutputStream(file);
        writer.setOutput(output);
        IIOImage iioImage = new IIOImage(img, null, null);
        writer.write(null, iioImage, param);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } finally {
        try {
            if (writer != null) {
                writer.dispose();
            }
            if (output != null) {
                output.close();
            }
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }
}