Example usage for javax.imageio.stream FileImageOutputStream write

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

Introduction

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

Prototype

public void write(byte[] b, int off, int len) throws IOException 

Source Link

Usage

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 va2  s . co 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:beans.FotoBean.java

public void tirarFoto(CaptureEvent captureEvent) throws IOException {
    arquivo = gerarNome();/*w  w  w  .  jav a  2s . co 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:com.kahlon.guard.controller.PersonImageManager.java

/**
 * Capture Image/*from   ww w.j  a  v  a2s  . c  om*/
 * 
 * @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

/**
 * Crop Image//  www.java2s .c om
 * @return
 */
public String crop() {
    if (croppedImage == null) {
        return null;
    }

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

    FileImageOutputStream imageOutput;
    try {
        imageOutput = new FileImageOutputStream(new File(newFileName));
        imageOutput.write(croppedImage.getBytes(), 0, croppedImage.getBytes().length);
        imageOutput.close();
    } catch (FileNotFoundException e) {
        throw new FacesException("File not found.", e.getCause());
    } catch (IOException e) {
        throw new FacesException("IO Exception found.", e.getCause());
    }

    return null;
}

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

/**
 * Upload person image file/*  www. ja  va 2 s. c  om*/
 *
 * @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;
}

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  .ja  va  2s .c  om
    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:org.angcms.util.ResourceUtils.java

public static String createImage_(String folder, String imageFileName, byte[] data) {
    try {//from   www. ja  v  a  2 s.co  m
        String actualFileName = getUniqueName(getRealPath(folder), imageFileName);
        FileImageOutputStream imageOutput;
        imageOutput = new FileImageOutputStream(new File(actualFileName));
        imageOutput.write(data, 0, data.length);
        imageOutput.close();
        return actualFileName.substring(actualFileName.lastIndexOf(File.separator) + 1);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:org.giavacms.base.common.util.ResourceUtils.java

public static String createImage_(String folder, String imageFileName, byte[] data) {
    try {/*  ww w  .  ja v a 2s. com*/
        String actualFileName = getUniqueName(getRealPath() + folder, imageFileName);
        FileImageOutputStream imageOutput;
        imageOutput = new FileImageOutputStream(new File(actualFileName));
        imageOutput.write(data, 0, data.length);
        imageOutput.close();
        return actualFileName.substring(actualFileName.lastIndexOf(File.separator) + 1);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:org.meveo.admin.util.ModuleUtil.java

public static void cropPicture(String filename, CroppedImage croppedImage) throws Exception {
    FileImageOutputStream imageOutput = new FileImageOutputStream(new File(filename));
    imageOutput.write(croppedImage.getBytes(), 0, croppedImage.getBytes().length);
    imageOutput.flush();//from  w w w. ja v a 2 s.  com
    imageOutput.close();
}