java生成图片

来源:互联网 发布:图形矢量化软件 编辑:程序博客网 时间:2024/06/07 19:39
 StringBuffer sb = new StringBuffer();        BufferedImage image = new BufferedImage(WIDTH, HEIGHT,                BufferedImage.TYPE_INT_RGB);        Graphics graphic = image.getGraphics();        graphic.setColor(Color.LIGHT_GRAY);        graphic.fillRect(0, 0, WIDTH, HEIGHT);        Random ran = new Random();        // 画随机字符        for (int i = 1; i <= SIZE; i++) {            int r = ran.nextInt(chars.length);            graphic.setColor(getRandomColor());            graphic.setFont(new Font(null, Font.BOLD + Font.ITALIC, FONT_SIZE));            graphic.drawString(chars[r] + "", (i - 1) * WIDTH / SIZE,                    HEIGHT / 2);            sb.append(chars[r]);// 将字符保存,存入Session        }        // 画干扰线        for (int i = 1; i <= LINES; i++) {            graphic.setColor(getRandomColor());            graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT),                    ran.nextInt(WIDTH), ran.nextInt(HEIGHT));        }        Map<String, BufferedImage> map = new HashMap<String, BufferedImage>();        map.put(sb.toString(), image);        return map;

 ByteArrayOutputStream bos = new ByteArrayOutputStream();        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);        encoder.encode(image);        byte[] imageBts = bos.toByteArray();        InputStream in = new ByteArrayInputStream(imageBts);

0 0
原创粉丝点击