Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.tocsi.images; import com.tocsi.beanModel.GlobalsBean; import com.tocsi.utilities.Utilities; import java.awt.image.BufferedImage; import java.awt.image.ImagingOpException; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.Serializable; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.util.ArrayList; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import javax.faces.context.FacesContext; import javax.faces.event.PhaseId; import javax.imageio.ImageIO; import org.apache.commons.io.FilenameUtils; import org.imgscalr.Scalr; import org.primefaces.context.RequestContext; import org.primefaces.event.FileUploadEvent; import org.primefaces.model.DefaultStreamedContent; import org.primefaces.model.StreamedContent; import org.primefaces.model.UploadedFile; /** * * @author Melvin */ @ManagedBean @SessionScoped public class ImageListBean implements Serializable { private ArrayList<ImageBean> imageList = new ArrayList<>(); private ArrayList<ImageBean> imageListDelete = new ArrayList<>(); private ArrayList<ImageBean> imageListNew = new ArrayList<>(); private ArrayList<ImageBean> receiveImageList = new ArrayList<>(); private ArrayList<ImageBean> receiveImageListDelete = new ArrayList<>(); private ArrayList<ImageBean> receiveImageListNew = new ArrayList<>(); private StreamedContent imageThumbnail; private StreamedContent imageFullSize; private String imageFileName; private String imageCaption; private int imageIndex; public ArrayList<ImageBean> getReceiveImageList() { return receiveImageList; } public void setReceiveImageList(ArrayList<ImageBean> receiveImageList) { this.receiveImageList = receiveImageList; } public ArrayList<ImageBean> getReceiveImageListDelete() { return receiveImageListDelete; } public void setReceiveImageListDelete(ArrayList<ImageBean> receiveImageListDelete) { this.receiveImageListDelete = receiveImageListDelete; } public ArrayList<ImageBean> getReceiveImageListNew() { return receiveImageListNew; } public void setReceiveImageListNew(ArrayList<ImageBean> receiveImageListNew) { this.receiveImageListNew = receiveImageListNew; } public ArrayList<ImageBean> getImageList() { return imageList; } public void setImageList(ArrayList<ImageBean> imageList) { this.imageList = imageList; } public ArrayList<ImageBean> getImageListNew() { return imageListNew; } public ArrayList<ImageBean> getImageListDelete() { return imageListDelete; } public void setImageListDelete(ArrayList<ImageBean> imageListDelete) { this.imageListDelete = imageListDelete; } public void setImageListNew(ArrayList<ImageBean> imageListNew) { this.imageListNew = imageListNew; } public String getImageCaption() { return imageCaption; } public void setImageCaption(String imageCaption) { this.imageCaption = imageCaption; } public void setImageFullSize(StreamedContent imageFullSize) { this.imageFullSize = imageFullSize; } public String getImageFileName() { return imageFileName; } public void setImageFileName(String imageFileName) { this.imageFileName = imageFileName; } public void setImageThumbnail(StreamedContent imageThumbnail) { this.imageThumbnail = imageThumbnail; } public void handleFileUpload(FileUploadEvent event) { if (event.getFile() != null) { try { UploadedFile uf = (UploadedFile) event.getFile(); File folder = new File(GlobalsBean.destOriginal); //File folderThumb = new File(destThumbnail); String filename = FilenameUtils.getBaseName(uf.getFileName()); String extension = FilenameUtils.getExtension(uf.getFileName()); File file = File.createTempFile(filename + "-", "." + extension, folder); //File fileThumb = File.createTempFile(filename + "-", "." + extension, folderThumb); //resize image here BufferedImage originalImage = ImageIO.read(uf.getInputstream()); InputStream is = uf.getInputstream(); if (originalImage.getWidth() > 700) { //resize image if image's width excess 700px BufferedImage origImage = Scalr.resize(originalImage, Scalr.Method.QUALITY, Scalr.Mode.FIT_TO_WIDTH, 640, (int) ((originalImage.getHeight() / ((double) (originalImage.getWidth() / 700.0)))), Scalr.OP_ANTIALIAS); ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageIO.write(origImage, extension, os); is = new ByteArrayInputStream(os.toByteArray()); } //create thumbnail BufferedImage thumbnail = Scalr.resize(ImageIO.read(uf.getInputstream()), 150); ByteArrayOutputStream osThumb = new ByteArrayOutputStream(); ImageIO.write(thumbnail, extension, osThumb); InputStream isThumbnail = new ByteArrayInputStream(osThumb.toByteArray()); try (InputStream input = is) { Files.copy(input, file.toPath(), StandardCopyOption.REPLACE_EXISTING); } try (InputStream input = isThumbnail) { Files.copy(input, Paths.get(GlobalsBean.destThumbnail + GlobalsBean.separator + file.getName()), StandardCopyOption.REPLACE_EXISTING); } //File chartFile = new File(file.getAbsolutePath()); //chart = new DefaultStreamedContent(new FileInputStream(chartFile), "image/jpg"); ImageBean ib = new ImageBean(); ib.setFilename(file.getName()); ib.setThumbFile(file.getName()); ib.setImageFileLocation(GlobalsBean.destOriginal + GlobalsBean.separator + file.getName()); imageList.add(ib); } catch (IOException | IllegalArgumentException | ImagingOpException ex) { Utilities.displayError(ex.getMessage()); } } } public StreamedContent getImageThumbnail() throws IOException { FacesContext context = FacesContext.getCurrentInstance(); if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) { // So, we're rendering the HTML. Return a stub StreamedContent so that it will generate right URL. return new DefaultStreamedContent(); } else { String path = GlobalsBean.destThumbnail + GlobalsBean.separator + context.getExternalContext().getRequestParameterMap().get("imageID"); Path filePath = Paths.get(path); byte[] data = Files.readAllBytes(filePath); return new DefaultStreamedContent(new ByteArrayInputStream(data)); } } public StreamedContent getImageFullSize() throws IOException { FacesContext context = FacesContext.getCurrentInstance(); if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) { // So, we're rendering the HTML. Return a stub StreamedContent so that it will generate right URL. return new DefaultStreamedContent(); //return null; } else { String path = GlobalsBean.destOriginal + GlobalsBean.separator + imageFileName; Path filePath = Paths.get(path); byte[] data = Files.readAllBytes(filePath); return new DefaultStreamedContent(new ByteArrayInputStream(data)); } } public void viewPhoto(String filename, String caption) { setImageFileName(filename); setImageCaption(caption); RequestContext.getCurrentInstance().execute("PF('dlgImageViewer').show()"); } public void viewCaption(String caption, ImageBean image) { setImageCaption(caption); imageIndex = this.imageList.indexOf(image); RequestContext.getCurrentInstance().execute("PF('dlgCaption').show()"); } public void setPhotoCaption() { imageList.get(imageIndex).setCaption(imageCaption); } public void removeImage(ImageBean image) { try { if (image.getImageID() > 0) { imageListDelete.add(image); } else { File imageFile = new File(GlobalsBean.destOriginal + GlobalsBean.separator + image.getFilename()); File thumbnailFile = new File( GlobalsBean.destThumbnail + GlobalsBean.separator + image.getFilename()); imageFile.delete(); thumbnailFile.delete(); } this.imageList.remove(image); Utilities.displayInfo("Image successfully removed."); } catch (Exception ex) { Utilities.displayError("Error: " + ex.getMessage()); } } }