import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.File; import java.util.Random; import javax.imageio.ImageIO; public class TestCaptchaImage { public static void main(String[] args) throws Exception { String captcha = "09875"; int width = 55; int height = 20; Color fontColor = new Color(36, 85, 92); /*文字顏色*/ Color lineColor = new Color(65, 161, 175); /*線條顏色*/ Color bgColor = new Color(208, 226, 229); /*底色*/ Random random = new Random(); random.setSeed(System.currentTimeMillis()); BufferedImage image = new BufferedImage( width, height, BufferedImage.TYPE_INT_RGB ); Graphics g = image.getGraphics(); /* 底色 */ g.setColor(bgColor); g.fillRect(0, 0, width, height); /* 畫線 */ g.setColor(lineColor); for (int i=0; i < 20; i++) { g.drawLine( random.nextInt(width), 0, random.nextInt(width), height ); } /* 畫出文字 */ g.setFont(new Font("Atlantic Inline", Font.BOLD, 20)); g.setColor(fontColor); g.drawString(captcha, 0, 18); /* 畫線 */ g.setColor(lineColor); for (int i=0; i < 4; i++) { g.drawLine( 0, random.nextInt(height), width, random.nextInt(height) ); } g.dispose(); ImageIO.write(image, "png", new File("captcha_image.png")); } }
參考文件:
Graphics (Java 2 Platform SE 6)
沒有留言:
張貼留言
你好!歡迎你在我的 Blog 上留下你寶貴的意見。