package br.com.ec.controller.managedbean;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.UUID;
import javax.faces.FacesException;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.imageio.stream.FileImageOutputStream;
import javax.inject.Named;
import javax.servlet.ServletContext;
import org.primefaces.event.CaptureEvent;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.CroppedImage;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.springframework.context.annotation.Scope;
import br.com.ec.core.util.VerificadorUtil;
import br.com.ec.domain.util.ConversorUtil;
@Named
@Scope
("view")
public class FotoManipularBean
implements Serializable {
private static final long serialVersionUID = 1L
;
private CroppedImage croppedImage
;
private String currentImageName
;
private String newImageName
;
private byte[] bytes
;
private String filename
;
public String getFilename
() {
return filename
;
}
public FotoManipularBean
() {
setCurrentImageName
("sem_foto.jpg");
}
public void setArquivoFoto
(/*ArquivoFoto arquivoFoto*/) {
bytes =
null;
newImageName =
null;
/*
if (VerificadorUtil.naoEstaNulo(arquivoFoto)) {
bytes = arquivoFoto.getArquivo();
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("bytes", bytes);
setCurrentImageName(getRandomImageName() + ".jpg");
ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
String newFileName = servletContext.getRealPath("") + File.separator + "imagens" + File.separator + "temp" + File.separator + getCurrentImageName();
FileImageOutputStream imageOutput;
try {
imageOutput = new FileImageOutputStream(new File(newFileName));
imageOutput.write(bytes, 0, bytes.length);
imageOutput.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
setCurrentImageName("sem_foto.jpg");
}
*/
}
public StreamedContent getImage
() throws IOException {
if(VerificadorUtil.
estaNulo(bytes
)) {
File file =
new File("c://usuario.png");
bytes = ConversorUtil.
converterFileParaBytes(file
);
}
DefaultStreamedContent defaultStreamedContent =
new DefaultStreamedContent
(new ByteArrayInputStream(bytes
),
"image/png",
"teste.png");
return defaultStreamedContent
;
}
public void fileUploadAction
(FileUploadEvent event
) {
try {
FacesContext aFacesContext = FacesContext.
getCurrentInstance();
ServletContext context =
(ServletContext
) aFacesContext.
getExternalContext().
getContext();
String realPath = context.
getRealPath("/");
File file =
new File(realPath +
"/imagens/temp/");
file.
mkdirs();
byte[] arquivo = event.
getFile().
getContents();
String caminho = realPath +
"/imagens/temp/" + event.
getFile().
getFileName();
setCurrentImageName
(event.
getFile().
getFileName());
FileOutputStream fos =
new FileOutputStream(caminho
);
fos.
write(arquivo
);
fos.
close();
} catch (Exception ex
) {
System.
out.
println("Erro no upload de imagem" + ex
);
}
}
public String crop
() {
if(croppedImage ==
null) {
return null;
}
setNewImageName
(getRandomImageName
());
ServletContext servletContext =
(ServletContext
) FacesContext.
getCurrentInstance().
getExternalContext().
getContext();
String newFileName = servletContext.
getRealPath("") +
File.
separator +
"imagens" +
File.
separator +
"temp" +
File.
separator + getNewImageName
() +
".jpg";
FileImageOutputStream imageOutput
;
try {
imageOutput =
new FileImageOutputStream(new File(newFileName
));
imageOutput.
write(croppedImage.
getBytes(),
0, croppedImage.
getBytes().
length);
imageOutput.
close();
} catch (FileNotFoundException e
) {
e.
printStackTrace();
} catch (IOException e
) {
e.
printStackTrace();
}
return null;
}
public String getRandomImageName
() {
int i =
(int) (Math.
random() * 100000);
return String.
valueOf(i
);
}
public CroppedImage getCroppedImage
() {
return croppedImage
;
}
public void setCroppedImage
(CroppedImage croppedImage
) {
this.
croppedImage = croppedImage
;
}
public String getCurrentImageName
() {
return currentImageName
;
}
public void setCurrentImageName
(String currentImageName
) {
this.
currentImageName = currentImageName
;
}
public String getNewImageName
() {
return newImageName
;
}
public void setNewImageName
(String newImageName
) {
this.
newImageName = newImageName
;
}
public void oncapture
(CaptureEvent captureEvent
) {
filename = getRandomImageName
();
byte[] data = captureEvent.
getData();
setNewImageName
(filename
);
ExternalContext externalContext = FacesContext.
getCurrentInstance().
getExternalContext();
String newFileName = externalContext.
getRealPath("") +
File.
separator +
"imagens" +
File.
separator +
"temp" +
File.
separator + getNewImageName
() +
".png";
setCurrentImageName
(getNewImageName
() +
".png");
FileImageOutputStream imageOutput
;
try {
imageOutput =
new FileImageOutputStream(new File(newFileName
));
imageOutput.
write(data,
0, data.
length);
imageOutput.
close();
}
catch(IOException e
) {
throw new FacesException
("Error in writing captured image.", e
);
}
}
public static void main
(String[] args
) {
for (int i =
0; i
< 20; i++
) {
System.
out.
println(UUID.
randomUUID().
toString());
}
}
}