Captcha 같이 영문(대+소)+숫자 를 섞어서 출력하려고 하다가 보니 몇가지를 만들었네.

2009. 11. 24. 15:17내꺼

"Completely Automated Public Turing test to tell Computers and Humans Apart"



Code1 


근데 말이지… 좀더 줄이고 다양하게 할 수 있을꺼 같은데…


--------------------------------------------------------------------------------


<textarea name="code" class="brush:java;" style="margin: 0px; width: 561px; height: 195px;"> 

public static String getCode(int len) { 

byte[] wd = new byte[]{0x41,0x61,0x30,0x30}; 

int[] lwd = new int[]{26,26,10,10}; 

Random r = new Random(); 

String str = ""; 

for(int i = len; i-- &gt; 0;) 

str += (char)(wd[i&amp;0x03] + r.nextInt(lwd[i&amp;0x03])); 

return str; 

}

</textarea>


Code2


좀더 개선된 코드당.


--------------------------------------------------------------------------------

<textarea name="code" class="brush:java;" style="margin: 0px; height: 186px; width: 913px;"> 

public static String getCode(int len) {

int[] RND_VAL = new int[]{48, 57, 65, 90, 97, 122};

Random r = new Random();

String str = "";

int k = 0;


while(len-- &gt; 0) {

k = r.nextInt(0xff);

k = (k + (~k &amp; 0x01)) % RND_VAL.length;

str += (char)(RND_VAL[k-1] + r.nextInt((RND_VAL[k]-RND_VAL[k-1]+1)));

}

return str;


}

</textarea>



Sample

위의 로직을 추가해서 이미지를 생성하는 샘플 임댜.

실행한 위치에 

out_{내용}.jpg 라는 파일들이 생성되요.

--------------------------------------------------------------------------------

<textarea name="code" class="brush:java;"> 

public class GenImage {

public static void main(String[] args) throws IOException {

int ptn[][] = new      int[][]{CaptchaText.RND_VAL_1,CaptchaText.RND_VAL_2,CaptchaText.RND_VAL_3,CaptchaText.RND_VAL_4};

String[] tit = new String[]{"문자(대+소)+숫자", "특수문자 + 영문자(대+소)+숫자", "영문자(대+소)", "숫자 "};

for(int i = 0; i &lt; 4; i++) {

createImage(200, 100,ptn[i], "out_" + tit[i] + ".jpg";

}

}


public static void createImage(int width, int height, int[] ptn, String fileName) {

CaptchaText ct = new CaptchaText(); 

BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);

Graphics g = img.getGraphics();

g.setColor(new Color(0xFFFFFF));

g.setFont(new Font("Serif", Font.BOLD, 20));

g.drawString(ct.getCode(10, ptn), 40, 50);

try {

ImageIO.write(img, "jpg", new File(fileName));

} catch (IOException e) {

e.printStackTrace();

}

}

}


-----------------------------------------------------------------------------------

public class CaptchaText {

/** 범위 설정 - 영문자(대+소)+숫자 */

public final static int[] RND_VAL_1 = new int[]{48, 57, 65, 90, 97, 122};

/** 범위 설정 - ascii 특수문자 + 영문자(대+소)+숫자 */

public final static int[] RND_VAL_2 = new int[]{33, 33, 35, 38, 40, 43, 45, 62, 64, 90, 97, 126};

/** 범위 설정 - 영문자(대+소) */

public final static int[] RND_VAL_3 = new int[]{65, 90, 97, 122};

/** 범위 설정 - 숫자 */

public final static int[] RND_VAL_4 = new int[]{48, 57};

/**

* &lt;pre&gt;

* 기본 형

* - RND_VAL_1

* &lt;/pre&gt;

* @param len

* @return

*/

public String getCode(int len) {

return getCode(len, RND_VAL_1);

}

/**

* &lt;pre&gt;

* 확장 형

* - RND_VAL_1 ~ RND_VAL_4

* &lt;/pre&gt;

* @param len

* @param RND_VAL

* @return

*/

public String getCode(int len, int[] RND_VAL) {

Random r = new Random();

String str = "";

int k = 0;

while(len-- &gt; 0) {

k = r.nextInt(0xff);

k = (k + (~k &amp; 0x01)) % RND_VAL.length;

str += (char)(RND_VAL[k-1] + r.nextInt((RND_VAL[k]-RND_VAL[k-1]+1)));

}

return str;

}

}

</textarea>

'내꺼' 카테고리의 다른 글

스마트 입찰예가 자동계산 앱 (iOS, Android)  (0) 2020.05.28
css 디버깅 툴  (0) 2012.07.09
Host 정보 변경 파이썬 코드  (0) 2009.07.21
host 정보를 바꿔봐~  (2) 2009.06.25
귀찮을때 쓰는 hosts 바꿔치기 batch...  (0) 2009.06.16