@@ -30,22 +30,22 @@ public class SimpleCharVerifyCodeGenImpl implements IVerifyCodeGen {
3030* @param height
3131*/
3232private static void fillBackground (Graphics graphics , int width , int height ) {
33- // 填充背景
34- graphics .setColor (Color .WHITE );
35- //设置矩形坐标x y 为0
36- graphics .fillRect (0 , 0 , width , height );
33+ // 填充背景
34+ graphics .setColor (Color .WHITE );
35+ //设置矩形坐标x y 为0
36+ graphics .fillRect (0 , 0 , width , height );
3737
38- // 加入干扰线条
39- for (int i = 0 ; i < 8 ; i ++) {
40- //设置随机颜色算法参数
41- graphics .setColor (RandomUtils .randomColor (40 , 150 ));
42- Random random = new Random ();
43- int x = random .nextInt (width );
44- int y = random .nextInt (height );
45- int x1 = random .nextInt (width );
46- int y1 = random .nextInt (height );
47- graphics .drawLine (x , y , x1 , y1 );
48- }
38+ // 加入干扰线条
39+ for (int i = 0 ; i < 8 ; i ++) {
40+ //设置随机颜色算法参数
41+ graphics .setColor (RandomUtils .randomColor (40 , 150 ));
42+ Random random = new Random ();
43+ int x = random .nextInt (width );
44+ int y = random .nextInt (height );
45+ int x1 = random .nextInt (width );
46+ int y1 = random .nextInt (height );
47+ graphics .drawLine (x , y , x1 , y1 );
48+ }
4949}
5050
5151/**
@@ -59,15 +59,15 @@ private static void fillBackground(Graphics graphics, int width, int height) {
5959*/
6060@ Override
6161public String generate (int width , int height , OutputStream os ) throws IOException {
62- BufferedImage image = new BufferedImage (width , height , BufferedImage .TYPE_INT_RGB );
63- Graphics graphics = image .getGraphics ();
64- fillBackground (graphics , width , height );
65- String randomStr = RandomUtils .randomString (VALICATE_CODE_LENGTH );
66- createCharacter (graphics , randomStr );
67- graphics .dispose ();
68- //设置JPEG格式
69- ImageIO .write (image , "JPEG" , os );
70- return randomStr ;
62+ BufferedImage image = new BufferedImage (width , height , BufferedImage .TYPE_INT_RGB );
63+ Graphics graphics = image .getGraphics ();
64+ fillBackground (graphics , width , height );
65+ String randomStr = RandomUtils .randomString (VALICATE_CODE_LENGTH );
66+ createCharacter (graphics , randomStr );
67+ graphics .dispose ();
68+ //设置JPEG格式
69+ ImageIO .write (image , "JPEG" , os );
70+ return randomStr ;
7171}
7272
7373/**
@@ -79,20 +79,20 @@ public String generate(int width, int height, OutputStream os) throws IOExceptio
7979*/
8080@ Override
8181public VerifyCode generate (int width , int height ) {
82- VerifyCode verifyCode = null ;
83- try (
84- //将流的初始化放到这里就不需要手动关闭流
85- ByteArrayOutputStream baos = new ByteArrayOutputStream ();
86- ) {
87- String code = generate (width , height , baos );
88- verifyCode = new VerifyCode ();
89- verifyCode .setCode (code );
90- verifyCode .setImgBytes (baos .toByteArray ());
91- } catch (IOException e ) {
92- logger .error (e .getMessage (), e );
93- verifyCode = null ;
94- }
95- return verifyCode ;
82+ VerifyCode verifyCode = null ;
83+ try (
84+ //将流的初始化放到这里就不需要手动关闭流
85+ ByteArrayOutputStream baos = new ByteArrayOutputStream ();
86+ ) {
87+ String code = generate (width , height , baos );
88+ verifyCode = new VerifyCode ();
89+ verifyCode .setCode (code );
90+ verifyCode .setImgBytes (baos .toByteArray ());
91+ } catch (IOException e ) {
92+ logger .error (e .getMessage (), e );
93+ verifyCode = null ;
94+ }
95+ return verifyCode ;
9696}
9797
9898/**
@@ -102,15 +102,15 @@ public VerifyCode generate(int width, int height) {
102102* @param randomStr
103103*/
104104private void createCharacter (Graphics g , String randomStr ) {
105- char [] charArray = randomStr .toCharArray ();
106- for (int i = 0 ; i < charArray .length ; i ++) {
107- //设置RGB颜色算法参数
108- g .setColor (new Color (50 + RandomUtils .nextInt (100 ),
109- 50 + RandomUtils .nextInt (100 ), 50 + RandomUtils .nextInt (100 )));
110- //设置字体大小,类型
111- g .setFont (new Font (FONT_TYPES [RandomUtils .nextInt (FONT_TYPES .length )], Font .BOLD , 26 ));
112- //设置x y 坐标
113- g .drawString (String .valueOf (charArray [i ]), 15 * i + 5 , 19 + RandomUtils .nextInt (8 ));
114- }
115- }
105+ char [] charArray = randomStr .toCharArray ();
106+ for (int i = 0 ; i < charArray .length ; i ++) {
107+ //设置RGB颜色算法参数
108+ g .setColor (new Color (50 + RandomUtils .nextInt (100 ),
109+ 50 + RandomUtils .nextInt (100 ), 50 + RandomUtils .nextInt (100 )));
110+ //设置字体大小,类型
111+ g .setFont (new Font (FONT_TYPES [RandomUtils .nextInt (FONT_TYPES .length )], Font .BOLD , 26 ));
112+ //设置x y 坐标
113+ g .drawString (String .valueOf (charArray [i ]), 15 * i + 5 , 19 + RandomUtils .nextInt (8 ));
114+ }
115+ }
116116}
0 commit comments