Skip to content

Instantly share code, notes, and snippets.

@chenqiyue
Created September 2, 2017 16:05
Show Gist options
  • Save chenqiyue/d6f6f5aeaadde82c71a4644368ff7389 to your computer and use it in GitHub Desktop.
Save chenqiyue/d6f6f5aeaadde82c71a4644368ff7389 to your computer and use it in GitHub Desktop.
adapter for patchca-captcha
import org.patchca.color.ColorFactory;
import org.patchca.filter.FilterFactory;
import org.patchca.filter.predefined.CurvesRippleFilterFactory;
import org.patchca.filter.predefined.DiffuseRippleFilterFactory;
import org.patchca.filter.predefined.DoubleRippleFilterFactory;
import org.patchca.filter.predefined.MarbleRippleFilterFactory;
import org.patchca.filter.predefined.WobbleRippleFilterFactory;
import org.patchca.service.Captcha;
import org.patchca.service.ConfigurableCaptchaService;
import org.patchca.word.RandomWordFactory;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
/**
* adapter for patchca-captcha
* @author cqy
* @since 2017/8/24.
*/
@Slf4j
public class CaptchaAdapter {
ConfigurableCaptchaService cs = new ConfigurableCaptchaService(){
@Override
public Captcha getCaptcha() {
Captcha captcha = super.getCaptcha();
try {
captcha.setImage(convertCMYK2RGB(captcha.getImage()));
} catch (IOException ignored) {
ignored.printStackTrace();
}
return captcha;
}
//fix ImageIO bug: https://stackoverflow.com/questions/2408613/unable-to-read-jpeg-image-using-imageio-readfile-file
private BufferedImage convertCMYK2RGB(BufferedImage image) throws IOException{
// log.info("Converting a CYMK image to RGB");
//Create a new RGB image
BufferedImage rgbImage = new BufferedImage(image.getWidth(), image.getHeight(),
BufferedImage.TYPE_3BYTE_BGR);
// then do a funky color convert
ColorConvertOp op = new ColorConvertOp(null);
op.filter(image, rgbImage);
return rgbImage;
}
};
public CaptchaAdapter() {
Random r = new Random();
ColorFactory colorFactory = new ColorFactory() {
private Color min = Color.black;
private Color max = Color.white;
@Override
public Color getColor(int index) {
return new Color( min.getRed() + r.nextInt((max.getRed() - min.getRed())),
min.getGreen() + r.nextInt((max.getGreen() - min.getGreen())),
min.getBlue() + r.nextInt((max.getBlue() - min.getBlue())));
}
};
cs.setColorFactory(colorFactory);
RandomWordFactory wordFactory = new RandomWordFactory();
wordFactory.setCharacters("23456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM");
wordFactory.setMaxLength(4);
wordFactory.setMinLength(4);
cs.setWordFactory(wordFactory);
// SingleColorBackgroundFactory factory = new SingleColorBackgroundFactory();
// factory.setColorFactory(colorFactory);
// cs.setBackgroundFactory(factory);
cs.setFilterFactory(new FilterFactory() {
final List<FilterFactory> origins = Arrays.asList(
new CurvesRippleFilterFactory(colorFactory),
new MarbleRippleFilterFactory(),
new DoubleRippleFilterFactory(),
new WobbleRippleFilterFactory(),
new DiffuseRippleFilterFactory()
);
@Override
public BufferedImage applyFilters(BufferedImage source) {
int index = r.nextInt(origins.size());
System.out.println(index);
return origins.get(index).applyFilters(source);
}
});
}
// @RequestMapping("captcha")
public Captcha captcha(HttpServletResponse response) throws IOException {
response.setContentType("image/jpeg");
// response.setHeader("Pragma", "no-cache");
long time = System.currentTimeMillis();
// response.setDateHeader("Last-Modified", time);
// response.setDateHeader("Date", time);
// response.setDateHeader("Expires", time);
Captcha captcha = cs.getCaptcha();
ServletOutputStream out = response.getOutputStream();
// ImageIO.write(captcha.getImage(), "jpg", out);
return captcha;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment