Rev 106 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 106 | espaco | 1 | package br.com.ec.controller.managedbean; |
| 2 | |||
| 3 | import java.io.ByteArrayInputStream; |
||
| 4 | import java.io.File; |
||
| 5 | import java.io.FileNotFoundException; |
||
| 6 | import java.io.FileOutputStream; |
||
| 7 | import java.io.IOException; |
||
| 8 | import java.io.Serializable; |
||
| 9 | import java.util.UUID; |
||
| 10 | |||
| 11 | import javax.faces.FacesException; |
||
| 12 | import javax.faces.context.ExternalContext; |
||
| 13 | import javax.faces.context.FacesContext; |
||
| 14 | import javax.imageio.stream.FileImageOutputStream; |
||
| 15 | import javax.inject.Named; |
||
| 16 | import javax.servlet.ServletContext; |
||
| 17 | |||
| 18 | import org.primefaces.event.CaptureEvent; |
||
| 19 | import org.primefaces.event.FileUploadEvent; |
||
| 20 | import org.primefaces.model.CroppedImage; |
||
| 21 | import org.primefaces.model.DefaultStreamedContent; |
||
| 22 | import org.primefaces.model.StreamedContent; |
||
| 23 | import org.springframework.context.annotation.Scope; |
||
| 24 | |||
| 195 | espaco | 25 | import br.com.ec.core.util.VerificadorUtil; |
| 106 | espaco | 26 | import br.com.ec.domain.util.ConversorUtil; |
| 27 | |||
| 28 | @Named |
||
| 29 | @Scope("view") |
||
| 30 | public class FotoManipularBean implements Serializable { |
||
| 31 | private static final long serialVersionUID = 1L; |
||
| 32 | |||
| 33 | private CroppedImage croppedImage; |
||
| 34 | private String currentImageName; |
||
| 35 | private String newImageName; |
||
| 36 | |||
| 37 | private byte[] bytes; |
||
| 38 | |||
| 39 | private String filename; |
||
| 40 | |||
| 41 | public String getFilename() { |
||
| 42 | return filename; |
||
| 43 | } |
||
| 44 | |||
| 45 | public FotoManipularBean() { |
||
| 46 | setCurrentImageName("sem_foto.jpg"); |
||
| 47 | } |
||
| 48 | |||
| 49 | public void setArquivoFoto(/*ArquivoFoto arquivoFoto*/) { |
||
| 50 | bytes = null; |
||
| 51 | newImageName = null; |
||
| 52 | /* |
||
| 53 | if (VerificadorUtil.naoEstaNulo(arquivoFoto)) { |
||
| 54 | bytes = arquivoFoto.getArquivo(); |
||
| 55 | FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("bytes", bytes); |
||
| 56 | |||
| 57 | setCurrentImageName(getRandomImageName() + ".jpg"); |
||
| 58 | ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext(); |
||
| 59 | String newFileName = servletContext.getRealPath("") + File.separator + "imagens" + File.separator + "temp" + File.separator + getCurrentImageName(); |
||
| 60 | |||
| 61 | FileImageOutputStream imageOutput; |
||
| 62 | try { |
||
| 63 | imageOutput = new FileImageOutputStream(new File(newFileName)); |
||
| 64 | imageOutput.write(bytes, 0, bytes.length); |
||
| 65 | imageOutput.close(); |
||
| 66 | } catch (FileNotFoundException e) { |
||
| 67 | e.printStackTrace(); |
||
| 68 | } catch (IOException e) { |
||
| 69 | e.printStackTrace(); |
||
| 70 | } |
||
| 71 | } else { |
||
| 72 | setCurrentImageName("sem_foto.jpg"); |
||
| 73 | } |
||
| 74 | */ |
||
| 75 | } |
||
| 76 | |||
| 77 | public StreamedContent getImage() throws IOException { |
||
| 78 | if(VerificadorUtil.estaNulo(bytes)) { |
||
| 79 | File file = new File("c://usuario.png"); |
||
| 80 | bytes = ConversorUtil.converterFileParaBytes(file); |
||
| 81 | } |
||
| 82 | |||
| 83 | DefaultStreamedContent defaultStreamedContent = new DefaultStreamedContent(new ByteArrayInputStream(bytes), "image/png", "teste.png"); |
||
| 84 | return defaultStreamedContent; |
||
| 85 | } |
||
| 86 | |||
| 87 | public void fileUploadAction(FileUploadEvent event) { |
||
| 88 | try { |
||
| 89 | |||
| 90 | FacesContext aFacesContext = FacesContext.getCurrentInstance(); |
||
| 91 | ServletContext context = (ServletContext) aFacesContext.getExternalContext().getContext(); |
||
| 92 | |||
| 93 | String realPath = context.getRealPath("/"); |
||
| 94 | |||
| 95 | File file = new File(realPath + "/imagens/temp/"); |
||
| 96 | file.mkdirs(); |
||
| 97 | |||
| 98 | byte[] arquivo = event.getFile().getContents(); |
||
| 99 | String caminho = realPath + "/imagens/temp/" + event.getFile().getFileName(); |
||
| 100 | setCurrentImageName(event.getFile().getFileName()); |
||
| 101 | |||
| 102 | FileOutputStream fos = new FileOutputStream(caminho); |
||
| 103 | fos.write(arquivo); |
||
| 104 | fos.close(); |
||
| 105 | |||
| 106 | } catch (Exception ex) { |
||
| 107 | System.out.println("Erro no upload de imagem" + ex); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | public String crop() { |
||
| 111 | if(croppedImage == null) { |
||
| 112 | return null; |
||
| 113 | } |
||
| 114 | setNewImageName(getRandomImageName()); |
||
| 115 | ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext(); |
||
| 116 | String newFileName = servletContext.getRealPath("") + File.separator + "imagens" + File.separator + "temp" + File.separator + getNewImageName() + ".jpg"; |
||
| 117 | |||
| 118 | FileImageOutputStream imageOutput; |
||
| 119 | try { |
||
| 120 | imageOutput = new FileImageOutputStream(new File(newFileName)); |
||
| 121 | imageOutput.write(croppedImage.getBytes(), 0, croppedImage.getBytes().length); |
||
| 122 | imageOutput.close(); |
||
| 123 | } catch (FileNotFoundException e) { |
||
| 124 | e.printStackTrace(); |
||
| 125 | } catch (IOException e) { |
||
| 126 | e.printStackTrace(); |
||
| 127 | } |
||
| 128 | |||
| 129 | return null; |
||
| 130 | } |
||
| 131 | |||
| 132 | public String getRandomImageName() { |
||
| 133 | int i = (int) (Math.random() * 100000); |
||
| 134 | |||
| 135 | return String.valueOf(i); |
||
| 136 | } |
||
| 137 | |||
| 138 | public CroppedImage getCroppedImage() { |
||
| 139 | return croppedImage; |
||
| 140 | } |
||
| 141 | |||
| 142 | public void setCroppedImage(CroppedImage croppedImage) { |
||
| 143 | this.croppedImage = croppedImage; |
||
| 144 | } |
||
| 145 | |||
| 146 | public String getCurrentImageName() { |
||
| 147 | return currentImageName; |
||
| 148 | } |
||
| 149 | |||
| 150 | public void setCurrentImageName(String currentImageName) { |
||
| 151 | this.currentImageName = currentImageName; |
||
| 152 | } |
||
| 153 | |||
| 154 | public String getNewImageName() { |
||
| 155 | return newImageName; |
||
| 156 | } |
||
| 157 | |||
| 158 | public void setNewImageName(String newImageName) { |
||
| 159 | this.newImageName = newImageName; |
||
| 160 | } |
||
| 161 | |||
| 162 | public void oncapture(CaptureEvent captureEvent) { |
||
| 163 | filename = getRandomImageName(); |
||
| 164 | byte[] data = captureEvent.getData(); |
||
| 165 | |||
| 166 | setNewImageName(filename); |
||
| 167 | ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); |
||
| 168 | String newFileName = externalContext.getRealPath("") + File.separator + "imagens" + File.separator + "temp" + File.separator + getNewImageName() + ".png"; |
||
| 169 | |||
| 170 | setCurrentImageName(getNewImageName() + ".png"); |
||
| 171 | |||
| 172 | FileImageOutputStream imageOutput; |
||
| 173 | try { |
||
| 174 | imageOutput = new FileImageOutputStream(new File(newFileName)); |
||
| 175 | imageOutput.write(data, 0, data.length); |
||
| 176 | imageOutput.close(); |
||
| 177 | } |
||
| 178 | catch(IOException e) { |
||
| 179 | throw new FacesException("Error in writing captured image.", e); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | public static void main(String[] args) { |
||
| 184 | for (int i = 0; i < 20; i++) { |
||
| 185 | System.out.println(UUID.randomUUID().toString()); |
||
| 186 | |||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | } |