added code to generate font fun captcha(type_1)

added code to generate gif format captcha(type_2)
added code to generate shadow text captcha(type_3)
minor changes in Main.scala file to fix errors
This commit is contained in:
Prajwal Goudar
2019-03-06 02:53:09 +05:30
parent d3db566901
commit 994c5751ca
58 changed files with 4150 additions and 4 deletions

View File

@@ -19,8 +19,6 @@
* For additional info see http://www.freeutils.net/source/jlhttp/
*/
package httpserver;
import java.io.*;
import java.lang.annotation.*;
import java.lang.reflect.*;

View File

@@ -0,0 +1,117 @@
package lc;
import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageOutputStream;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.font.TextLayout;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.IOException;
import java.io.File;
import java.util.Random;
import java.io.ByteArrayOutputStream;
public class CaptchaTypes{
private String secret = "";
private void setRenderingHints(Graphics2D g2d){
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
}
private BufferedImage charToImg(String text){
BufferedImage img = new BufferedImage(250, 100, BufferedImage.TYPE_INT_RGB);
Font font = new Font("Bradley Hand", Font.ROMAN_BASELINE, 48);
Graphics2D graphics2D = img.createGraphics();
graphics2D.setFont(font);
graphics2D.setColor(new Color((int)(Math.random() * 0x1000000)));
graphics2D.drawString( text , 45, 45);
graphics2D.dispose();
return img;
}
public byte[] type_1(String captchaText){
String[] fonts = {"Captcha Code","Mom'sTypewriter","Annifont","SF Intoxicated Blues",
"BeachType","Batmos","Barbecue","Bad Seed","Aswell","Alien Marksman"};
String[] colors = {"#f68787","#f8a978","#f1eb9a","#a4f6a5"};
BufferedImage img = new BufferedImage(350, 100, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = img.createGraphics();
Random rand = new Random();
for(int i=0; i< captchaText.length(); i++) {
Font font = new Font(fonts[rand.nextInt(10)], Font.ROMAN_BASELINE, 48);
graphics2D.setFont(font);
FontMetrics fontMetrics = graphics2D.getFontMetrics();
setRenderingHints(graphics2D);
graphics2D.setColor(Color.decode(colors[rand.nextInt(4)]));
if(rand.nextBoolean()) {
graphics2D.drawString(String.valueOf(captchaText.toLowerCase().charAt(i)), (i * 48), fontMetrics.getAscent());
}
else {
graphics2D.drawString(String.valueOf(captchaText.toUpperCase().charAt(i)), (i * 48), fontMetrics.getAscent());
}
}
graphics2D.dispose();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try{
ImageIO.write(img, "png",new File("Text.png"));
ImageIO.write(img,"png",baos);
}
catch (IOException ex){
ex.printStackTrace();
}
return baos.toByteArray();
}
public void type_2(String text){
try {
ImageOutputStream output = new FileImageOutputStream(new File("captchaTest.gif"));
GifSequenceWriter writer = new GifSequenceWriter( output, 1,1000, true );
for(int i=0; i< text.length(); i++){
BufferedImage nextImage = charToImg(String.valueOf(text.charAt(i)));
writer.writeToSequence(nextImage);
}
writer.close();
output.close();
} catch (IOException e){
e.printStackTrace();
}
}
public void type_3(String text){
BufferedImage img = new BufferedImage(350, 100, BufferedImage.TYPE_INT_RGB);
Font font = new Font("Arial",Font.ROMAN_BASELINE ,48);
Graphics2D graphics2D = img.createGraphics();
TextLayout textLayout = new TextLayout(text, font, graphics2D.getFontRenderContext());
setRenderingHints(graphics2D);
graphics2D.setPaint(Color.WHITE);
graphics2D.fillRect(0, 0, 350, 100);
graphics2D.setPaint(Color.BLACK);
textLayout.draw(graphics2D, 15, 50);
graphics2D.dispose();
float[] kernel = {
1f / 9f, 1f / 9f, 1f / 9f,
1f / 9f, 1f / 9f, 1f / 9f,
1f / 9f, 1f / 9f, 1f / 9f
};
ConvolveOp op = new ConvolveOp(new Kernel(3, 3, kernel),
ConvolveOp.EDGE_NO_OP, null);
BufferedImage img2 = op.filter(img, null);
Graphics2D g2d = img2.createGraphics();
setRenderingHints(g2d);
g2d.setPaint(Color.WHITE);
textLayout.draw(g2d, 13, 50);
g2d.dispose();
try{
ImageIO.write(img2, "png",new File("Te.png"));
}
catch (IOException ex){
ex.printStackTrace();
}
}
}

View File

@@ -7,4 +7,3 @@ interface ChallengeProvider {
//TODO: def configure(): Unit
}

View File

@@ -0,0 +1,151 @@
package lc;
import javax.imageio.*;
import javax.imageio.metadata.*;
import javax.imageio.stream.*;
import java.awt.image.*;
import java.io.*;
import java.util.Iterator;
public class GifSequenceWriter {
protected ImageWriter gifWriter;
protected ImageWriteParam imageWriteParam;
protected IIOMetadata imageMetaData;
/**
* Creates a new GifSequenceWriter
*
* @param outputStream the ImageOutputStream to be written to
* @param imageType one of the imageTypes specified in BufferedImage
* @param timeBetweenFramesMS the time between frames in miliseconds
* @param loopContinuously wether the gif should loop repeatedly
* @throws IIOException if no gif ImageWriters are found
*
* @author Elliot Kroo (elliot[at]kroo[dot]net)
*/
public GifSequenceWriter(
ImageOutputStream outputStream,
int imageType,
int timeBetweenFramesMS,
boolean loopContinuously) throws IIOException, IOException {
// my method to create a writer
gifWriter = getWriter();
imageWriteParam = gifWriter.getDefaultWriteParam();
ImageTypeSpecifier imageTypeSpecifier =
ImageTypeSpecifier.createFromBufferedImageType(imageType);
imageMetaData =
gifWriter.getDefaultImageMetadata(imageTypeSpecifier,
imageWriteParam);
String metaFormatName = imageMetaData.getNativeMetadataFormatName();
IIOMetadataNode root = (IIOMetadataNode)
imageMetaData.getAsTree(metaFormatName);
IIOMetadataNode graphicsControlExtensionNode = getNode(
root,
"GraphicControlExtension");
graphicsControlExtensionNode.setAttribute("disposalMethod", "none");
graphicsControlExtensionNode.setAttribute("userInputFlag", "FALSE");
graphicsControlExtensionNode.setAttribute(
"transparentColorFlag",
"FALSE");
graphicsControlExtensionNode.setAttribute(
"delayTime",
Integer.toString(timeBetweenFramesMS / 10));
graphicsControlExtensionNode.setAttribute(
"transparentColorIndex",
"0");
IIOMetadataNode commentsNode = getNode(root, "CommentExtensions");
commentsNode.setAttribute("CommentExtension", "Created by MAH");
IIOMetadataNode appEntensionsNode = getNode(
root,
"ApplicationExtensions");
IIOMetadataNode child = new IIOMetadataNode("ApplicationExtension");
child.setAttribute("applicationID", "NETSCAPE");
child.setAttribute("authenticationCode", "2.0");
int loop = loopContinuously ? 0 : 1;
child.setUserObject(new byte[]{ 0x1, (byte) (loop & 0xFF), (byte)
((loop >> 8) & 0xFF)});
appEntensionsNode.appendChild(child);
imageMetaData.setFromTree(metaFormatName, root);
gifWriter.setOutput(outputStream);
gifWriter.prepareWriteSequence(null);
}
public void writeToSequence(RenderedImage img) throws IOException {
gifWriter.writeToSequence(
new IIOImage(
img,
null,
imageMetaData),
imageWriteParam);
}
/**
* Close this GifSequenceWriter object. This does not close the underlying
* stream, just finishes off the GIF.
*/
public void close() throws IOException {
gifWriter.endWriteSequence();
}
/**
* Returns the first available GIF ImageWriter using
* ImageIO.getImageWritersBySuffix("gif").
*
* @return a GIF ImageWriter object
* @throws IIOException if no GIF image writers are returned
*/
private static ImageWriter getWriter() throws IIOException {
Iterator<ImageWriter> iter = ImageIO.getImageWritersBySuffix("gif");
if(!iter.hasNext()) {
throw new IIOException("No GIF Image Writers Exist");
} else {
return iter.next();
}
}
/**
* Returns an existing child node, or creates and returns a new child node (if
* the requested node does not exist).
*
* @param rootNode the <tt>IIOMetadataNode</tt> to search for the child node.
* @param nodeName the name of the child node.
*
* @return the child node, if found or a new node created with the given name.
*/
private static IIOMetadataNode getNode(
IIOMetadataNode rootNode,
String nodeName) {
int nNodes = rootNode.getLength();
for (int i = 0; i < nNodes; i++) {
if (rootNode.item(i).getNodeName().compareToIgnoreCase(nodeName)
== 0) {
return((IIOMetadataNode) rootNode.item(i));
}
}
IIOMetadataNode node = new IIOMetadataNode(nodeName);
rootNode.appendChild(node);
return(node);
}
/**
public GifSequenceWriter(
BufferedOutputStream outputStream,
int imageType,
int timeBetweenFramesMS,
boolean loopContinuously) {
*/
}

View File

@@ -0,0 +1,23 @@
package lc;
public class MainCaptcha implements ChallengeProvider{
public String getId(){
return "SomeText";
}
public Challenge returnChallenge(){
CaptchaTypes captchaTypes = new CaptchaTypes();
return new Challenge(captchaTypes.type_1("Hello"),"png","qwert");
}
public boolean checkAnswer(String secret, String answer){
return true;
}
public static void main(String[] args){
MainCaptcha mainCaptcha = new MainCaptcha();
Challenge challenge = mainCaptcha.returnChallenge();
System.out.println("Content type: " + challenge.contentType);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -3,7 +3,7 @@ package lc
import com.sksamuel.scrimage._
import java.sql._
import java.io._
import httpserver._
import HTTPServer._
import javax.imageio._
import java.awt.image._
import org.json4s._