KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > util > RegisterCode


1 /**
2  * Copyright 2003-2006 the original author or authors.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6
7        http://www.apache.org/licenses/LICENSE-2.0
8
9   * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */

15
16 package com.jdon.util;
17
18 import java.awt.*;
19 import java.awt.image.*;
20 import java.util.*;
21 import java.io.*;
22 import javax.imageio.*;
23
24 public class RegisterCode {
25
26   public Color getRandColor(int fc, int bc) { //给定范围获得éš?机颜色
27
Random random = new Random();
28     if (fc > 255)
29       fc = 255;
30     if (bc > 255)
31       bc = 255;
32     int r = fc + random.nextInt(bc - fc);
33     int g = fc + random.nextInt(bc - fc);
34     int b = fc + random.nextInt(bc - fc);
35     return new Color(r, g, b);
36   }
37
38   public BufferedImage getBufferedImage(int width, int height) {
39     return new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
40   }
41
42   public BufferedImage getBufferedImage(InputStream in) throws
43       Exception JavaDoc {
44     return ImageIO.read(in);
45   }
46
47   public void create(int imageWidth, int imageHeight, String JavaDoc randNumber,
48                      String JavaDoc fontType, int fontSize, int x, int y,
49                      OutputStream out) {
50     BufferedImage image = getBufferedImage(imageWidth, imageHeight);
51     generate(image, randNumber, fontType, fontSize, x, y, out);
52   }
53
54   public void generate(BufferedImage image, String JavaDoc randNumber,
55                        String JavaDoc fontType, int fontSize, int x, int y,
56                        OutputStream out) {
57
58     try {
59
60       int width = image.getWidth();
61       int height = image.getHeight();
62
63       // 获å?–图形上下文
64
Graphics g = image.getGraphics();
65
66       // 设定背景色
67
g.setColor(getRandColor(200, 250));
68       g.fillRect(0, 0, width, height);
69
70       //设定字体
71
g.setFont(new Font(fontType, Font.PLAIN, fontSize));
72
73       //画边框
74
//g.setColor(new Color());
75
//g.drawRect(0,0,width-1,height-1);
76

77       // éš?机产生155æ?¡å¹²æ‰°çº¿ï¼Œä½¿å›¾è±¡ä¸­çš„认è¯?ç ?ä¸?易被其它程åº?探测到
78
g.setColor(getRandColor(160, 200));
79       //生æˆ?éš?机类
80
Random random = new Random();
81       for (int i = 0; i < 155; i++) {
82         int x2 = random.nextInt(width);
83         int y2 = random.nextInt(height);
84         int x3 = random.nextInt(12);
85         int y3 = random.nextInt(12);
86         g.drawLine(x2, y2, x2 + x3, y2 + y3);
87       }
88
89       // 将认è¯?ç ?显示到图象中
90
g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110),
91                            20 + random.nextInt(110)));
92
93       g.drawString(randNumber, x, y);
94
95       // 图象生效
96
g.dispose();
97
98       // 输出图象到页é?¢
99
ImageIO.write( (BufferedImage) image, "JPEG", out);
100     } catch (Exception JavaDoc ex) {
101       System.err.println("generate image error: " + ex);
102     }
103
104   }
105
106 }
107
Popular Tags