Last active
December 16, 2019 17:06
-
-
Save deividsoncs/9ee1e389119d2b31a2b613436fd43bf5 to your computer and use it in GitHub Desktop.
Nosso BackBean onde aguardaremos o request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dint.util.ManageBean; | |
import java.awt.Color; | |
import java.awt.Font; | |
import java.awt.GradientPaint; | |
import java.awt.Graphics2D; | |
import java.awt.RenderingHints; | |
import java.awt.image.BufferedImage; | |
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.Serializable; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.Random; | |
import javax.faces.bean.ManagedBean; | |
import javax.faces.bean.RequestScoped; | |
import javax.imageio.ImageIO; | |
import org.primefaces.model.DefaultStreamedContent; | |
import org.primefaces.model.StreamedContent; | |
/** | |
* @author calixto | |
* Referências: http://zetcode.com/tutorials/jeetutorials/captcha/ | |
* http://www.kianworknotes.com/2016/01/simple-captcha-by-primefaces.html | |
* http://blog.marcoreis.net/como-usar-graphicimage-no-primefaces/ | |
*/ | |
@ManagedBean | |
//Atenção no escopo deste Bean é um Request!!! | |
@RequestScoped | |
public class CaptchaBean extends ManageBean implements Serializable { | |
public StreamedContent getCaptcha() { | |
StreamedContent dText = null; | |
try { | |
//tamanho da imagem gerada | |
int width = 150; | |
int height = 50; | |
List arrayList = new ArrayList(); | |
//caracteres possível no captcha | |
String capcode = "abcdefghijklmnopqurstuvwxyzABCDEFGHIJKLMONOPQURSTUVWXYZ0123456789!@#$%&*"; | |
for (int i = 1; i < capcode.length() - 1; i++) { | |
arrayList.add(capcode.charAt(i)); | |
} | |
//embaralhando cadeia de caracteres | |
Collections.shuffle(arrayList); | |
Iterator itr = arrayList.iterator(); | |
String s = ""; | |
String s2 = ""; | |
Object obj; | |
while (itr.hasNext()) { | |
obj = itr.next(); | |
s = obj.toString(); | |
s2 = s2 + s; | |
} | |
String s1 = s2.substring(0, 6); | |
char[] s3 = s1.toCharArray(); | |
//iniciando a manipulação de imagens | |
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); | |
Graphics2D g2d = bufferedImage.createGraphics(); | |
//definindo a fonte a ser utilizada na imagem | |
Font font = new Font("Georgia", Font.BOLD, 18); | |
g2d.setFont(font); | |
RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); | |
rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); | |
g2d.setRenderingHints(rh); | |
//setando o gradiente do fundo da imagem do captcha | |
GradientPaint gp = new GradientPaint(0, 0, Color.red, 0, height / 2, Color.black, true); | |
g2d.setPaint(gp); | |
g2d.fillRect(0, 0, width, height); | |
g2d.setColor(new Color(255, 153, 0)); | |
Random r = new Random(); | |
int index = Math.abs(r.nextInt()) % 5; | |
String captcha = String.copyValueOf(s3); | |
//passo o captcha para a sessão, aqui fica a cargo do escopo da sua aplicação | |
super.setObjetcSession("captcha", captcha); | |
int x = 0; | |
int y = 0; | |
for (int i = 0; i < s3.length; i++) { | |
x += 10 + (Math.abs(r.nextInt()) % 15); | |
y = 20 + Math.abs(r.nextInt()) % 20; | |
//desenhando a variável s3 | |
g2d.drawChars(s3, i, 1, x, y); | |
} | |
g2d.dispose(); | |
//gerando o stream da imagem | |
ByteArrayOutputStream os = new ByteArrayOutputStream(); | |
ImageIO.write(bufferedImage, "png", os); | |
os.flush(); | |
os.close(); | |
dText = new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()), "image/png"); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
//StreamedContent sera recebido pelo componente <p:graphicImage> | |
return dText; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment