Java tutorial
/* * Copyright (c) 2015 xuerdongcom@126.com All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.jmu.service.Patchca; import org.apache.commons.lang3.StringUtils; import org.patchca.background.BackgroundFactory; import org.patchca.color.ColorFactory; import org.patchca.filter.ConfigurableFilterFactory; import org.patchca.filter.library.AbstractImageOp; import org.patchca.filter.library.WobbleImageOp; import org.patchca.filter.predefined.CurvesRippleFilterFactory; import org.patchca.font.RandomFontFactory; import org.patchca.service.ConfigurableCaptchaService; import org.patchca.text.renderer.BestFitTextRenderer; import org.patchca.utils.encoder.EncoderHelper; import org.patchca.word.RandomWordFactory; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import javax.servlet.http.HttpSession; import java.awt.*; import java.awt.image.BufferedImage; import java.awt.image.BufferedImageOp; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import java.util.Random; /** * ??? */ @Service public class PatchcaService { private static ConfigurableCaptchaService cs = new ConfigurableCaptchaService(); private static Random random = new Random(); @PostConstruct public void init() { cs.setColorFactory(new ColorFactory() { @Override public Color getColor(int x) { int[] c = new int[3]; int i = random.nextInt(c.length); for (int fi = 0; fi < c.length; fi++) { if (fi == i) { c[fi] = random.nextInt(71); } else { c[fi] = random.nextInt(256); } } return new Color(c[0], c[1], c[2]); } }); cs.setFilterFactory(new CurvesRippleFilterFactory()); // ?? MyCustomBackgroundFactory backgroundFactory = new MyCustomBackgroundFactory(); cs.setBackgroundFactory(backgroundFactory); // ??,?,o0 RandomWordFactory wf = new RandomWordFactory(); wf.setCharacters("abcdefghkmnpqstwxyz23456789ABCDEFGHGKLMNPQRSTUVWXYZ"); wf.setMaxLength(4); wf.setMinLength(4); cs.setWordFactory(wf); // ?? RandomFontFactory ff = new RandomFontFactory(); ff.setMinSize(32); ff.setMaxSize(28); cs.setFontFactory(ff); // ConfigurableFilterFactory filterFactory = new ConfigurableFilterFactory(); List<BufferedImageOp> filters = new ArrayList<BufferedImageOp>(); WobbleImageOp wobbleImageOp = new WobbleImageOp(); wobbleImageOp.setEdgeMode(AbstractImageOp.EDGE_MIRROR); wobbleImageOp.setxAmplitude(2.0); wobbleImageOp.setyAmplitude(1.0); filters.add(wobbleImageOp); filterFactory.setFilters(filters); cs.setFilterFactory(filterFactory); // BestFitTextRenderer textRenderer = new BestFitTextRenderer(); textRenderer.setBottomMargin(3); textRenderer.setTopMargin(3); cs.setTextRenderer(textRenderer); } public void resetcs(int width, int height) { cs.setWidth(width); cs.setHeight(height); } /** * ??? * @param width * @param height * @param os * @param session ?session * @throws IOException */ public void getPatchca(int width, int height, OutputStream os, HttpSession session) throws IOException { resetcs(width, height); String token = EncoderHelper.getChallangeAndWriteImage(cs, "png", os); session.setAttribute("patchca", token); } /** * ajax??? ??? * @param session * @param value * @return */ public Boolean ajaxValidatePatchca(HttpSession session, String value) { boolean b = false; String patchca = (String) session.getAttribute("patchca"); if (StringUtils.isNotEmpty(patchca)) { b = value.equalsIgnoreCase(patchca); } return b; } /** * ???? ????? * @param session * @param value * @return */ public Boolean validatePatchca(HttpSession session, String value) { boolean b = false; String patchca = (String) session.getAttribute("patchca"); if (StringUtils.isNotEmpty(patchca)) { b = value.equalsIgnoreCase(patchca); } session.removeAttribute("patchca"); return b; } /** * * ??,? */ private class MyCustomBackgroundFactory implements BackgroundFactory { private Random random = new Random(); public void fillBackground(BufferedImage image) { Graphics graphics = image.getGraphics(); // ?? int imgWidth = image.getWidth(); int imgHeight = image.getHeight(); // graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, imgWidth, imgHeight); // 100(???) for (int i = 0; i < 100; i++) { // ? int rInt = random.nextInt(255); int gInt = random.nextInt(255); int bInt = random.nextInt(255); graphics.setColor(new Color(rInt, gInt, bInt)); // ?? int xInt = random.nextInt(imgWidth - 3); int yInt = random.nextInt(imgHeight - 2); // ? int sAngleInt = random.nextInt(360); int eAngleInt = random.nextInt(360); // ?? int wInt = random.nextInt(6); int hInt = random.nextInt(6); graphics.fillArc(xInt, yInt, wInt, hInt, sAngleInt, eAngleInt); // 5? if (i % 20 == 0) { int xInt2 = random.nextInt(imgWidth); int yInt2 = random.nextInt(imgHeight); graphics.drawLine(xInt, yInt, xInt2, yInt2); } } } } }