Subversion Repositories Integrator Subversion

Rev

Rev 200 | Details | Compare with Previous | Last modification | View Log | RSS feed

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