标题:有个问题请教一下大家,求大神解决
只看楼主
吃货怕做噩梦
Rank: 2
等 级:论坛游民
帖 子:35
专家分:10
注 册:2014-9-12
结帖率:77.78%
已结贴  问题点数:17 回复次数:3 
有个问题请教一下大家,求大神解决
登录页面中的那个验证码怎么编码啊,是个按钮吗?如果不是,那是什么啊?
搜索更多相关主题的帖子: 验证码 
2014-10-29 20:34
Java258
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:6
帖 子:122
专家分:561
注 册:2014-10-15
得分:10 
程序代码:
import java.awt.image.BufferedImage;
import import import import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ImageUtils {
    private static String str = "1234567890QWERTYUIOPASDFGHJKLZXCVBNM";
    private static char[] chars = str.toCharArray();
    private static int WIDTH = 140;
    private static int HEIGHT = 40;
    private static int SIZE = 4;
    private static int LINES = 6;
    private static int FONT_SIZE = 40;
    public static Map<String, BufferedImage> getImage() {
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
                BufferedImage.TYPE_INT_RGB);
        Graphics g = image.createGraphics();
        g.setColor(Color.GRAY);
        g.fillRect(0, 0, WIDTH, HEIGHT);
        StringBuffer bf = new StringBuffer();
        Random r = new Random();
        for (int i = 0; i < SIZE; i++) {
            char c = chars[r.nextInt(chars.length)];
            g.setColor(getColor());
            g.setFont(new Font(null,Font.BOLD,FONT_SIZE));
            g.drawString("" + c, i * WIDTH / SIZE, HEIGHT / 10 * 9);
            bf.append(c);
        }
        for (int i = 0; i < LINES; i++) {
            g.setColor(getColor());
            g.drawLine(r.nextInt(WIDTH), r.nextInt(HEIGHT), r.nextInt(WIDTH), r.nextInt(HEIGHT));
        }
        Map<String, BufferedImage> map = new HashMap<String, BufferedImage>();
        map.put(bf.toString(), image);
        return map;
    }
    private static Color getColor() {
        Random r = new Random();
        return new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));
    }

    public static InputStream imageToStream(BufferedImage image) throws Exception{
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
        encoder.encode(image);
        byte[] bytes = bos.toByteArray();
        InputStream is = new ByteArrayInputStream(bytes);
        return is;
    }
}
这段代码能可以生成验证码,希望可以帮你

虔诚请教,虚心学习……
2014-10-30 11:33
吃货怕做噩梦
Rank: 2
等 级:论坛游民
帖 子:35
专家分:10
注 册:2014-9-12
得分:0 
回复 2 楼 Java258
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
我想问一下上面的两个包是啥意思,谢谢
2014-10-30 18:32
日知己所无
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:38
帖 子:427
专家分:2071
注 册:2014-3-22
得分:7 
回复 3 楼 吃货怕做噩梦
Sun开发的Java版本的JPEG编码、解码类库
2014-11-01 09:26



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-437866-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023179 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved