/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.crunii.com</a>
 */
package com.crunii.micro.common.captcha;

import com.github.bingoohuang.patchca.background.BackgroundFactory;
import com.github.bingoohuang.patchca.custom.ConfigurableCaptchaService;
import com.github.bingoohuang.patchca.filter.ConfigurableFilterFactory;
import com.github.bingoohuang.patchca.filter.library.AbstractImageOp;
import com.github.bingoohuang.patchca.filter.library.WobbleImageOp;
import com.github.bingoohuang.patchca.font.FontFactory;
import com.github.bingoohuang.patchca.service.Captcha;
import com.github.bingoohuang.patchca.text.renderer.BestFitTextRenderer;
import com.github.bingoohuang.patchca.word.RandomWordFactory;
import com.github.bingoohuang.patchca.word.WordFactory;
import org.springframework.beans.factory.InitializingBean;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author 田平 create 2018年10月30日下午2:44:28
 */
public class MicroCaptcha extends ConfigurableCaptchaService implements InitializingBean {

	/**
	 * 取消0 o i 1
	 */
	private String words = "abcdefghkmnpqstwxyzABCDEFGHKMNPQSTWXYZ23456789";
	private int wordLength = 4;

	/**
	 * @return the wordLength
	 */
	public int getWordLength() {
		return wordLength;
	}

	/**
	 * @param wordLength the wordLength to set
	 */
	public void setWordLength(int wordLength) {
		this.wordLength = wordLength;
	}

	/**
	 * @return the words
	 */
	public String getWords() {
		return words;
	}

	/**
	 * @param words the words to set
	 */
	public void setWords(String words) {
		this.words = words;
	}

	public MicroCaptcha() {

	}

	@Override
	public void afterPropertiesSet() throws Exception {
		RandomWordFactory wordFactory = new RandomWordFactory();
		wordFactory.setCharacters(words);
		wordFactory.setMaxLength(wordLength);
		wordFactory.setMinLength(wordLength);
		setWordFactory(wordFactory);
		// 兼容linux的字体
		FontFactory fontFactory = new FontFactory() {
			@Override
			public Font getFont(int arg0) {
				return new Font("STIX", Font.PLAIN, 36);
			}

			@Override
			public void setWord(String word) {
			}

			@Override
			public void setWordFactory(WordFactory wordFactory) {
			}
		};
		setFontFactory(fontFactory);

		CustomBackgroundFactory backgroundFactory = new CustomBackgroundFactory();
		setBackgroundFactory(backgroundFactory);

		// 图片滤镜设置
		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);
		setFilterFactory(filterFactory);

		// 文字渲染器设置
		BestFitTextRenderer textRenderer = new BestFitTextRenderer();
		textRenderer.setBottomMargin(3);
		textRenderer.setTopMargin(3);
		setTextRenderer(textRenderer);

	}

	private class CustomBackgroundFactory 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);
				}
			}
		}
	}

	public static void main(String[] args) throws Exception {
		MicroCaptcha microCaptcha = new MicroCaptcha();
		microCaptcha.afterPropertiesSet();
		microCaptcha.setWidth(250);
		microCaptcha.setHeight(60);
		Captcha captcha = microCaptcha.getCaptcha();
		System.out.println(captcha.getChallenge());
		File file = new File("D:\\captcha.png");
		try (FileOutputStream out = new FileOutputStream(file);) {
			ImageIO.write(captcha.getImage(), "png", out);
		}
	}
}
