欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java  實現(xiàn)輸出隨機圖片實例代碼

 更新時間:2017年06月08日 17:14:59   作者:Alex_zhuang  
這篇文章主要介紹了java 實現(xiàn)輸出隨機圖片實例代碼的相關資料,需要的朋友可以參考下

java  實現(xiàn)輸出隨機圖片實例代碼

輸出隨機圖片(CAPTCHA圖像):Completely Automated Public Turing Test to Tell Computers and Humans Apart (全自動區(qū)分計算機和人類的測試)

相關主要類(JDK 查看API)

BufferedImage:內(nèi)存圖像
Graphics:畫筆
ImageIO:輸出圖像

放在html頁面上<img src/>

注意:瀏覽器默認會緩存圖片

   public static int WIDTH = 120; 
public static int HEIGHT = 25; 
 
public void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException { 
 
  response.setContentType("text/html"); 
  //創(chuàng)建內(nèi)存圖像 
  BufferedImage image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB); 
  //勾勒圖像 
  Graphics graphics = image.getGraphics(); 
  //設置背景 
  graphics.setColor(Color.WHITE); 
  graphics.fillRect(0, 0, WIDTH, HEIGHT); 
  //設置邊框 
  graphics.setColor(Color.BLUE); 
  graphics.drawRect(1, 1, WIDTH-2, HEIGHT-2); 
  //畫干擾線 
  graphics.setColor(Color.YELLOW); 
  for(int i=0;i<8;i++){ 
    int xStart = new Random().nextInt(WIDTH); 
    int yStart = new Random().nextInt(HEIGHT); 
    int xEnd = new Random().nextInt(WIDTH); 
    int yEnd = new Random().nextInt(HEIGHT); 
    graphics.drawLine(xStart, yStart, xEnd, yEnd); 
  } 
  //寫隨機數(shù) 
  graphics.setColor(Color.RED); 
  int x = 5; 
  for(int i=0;i<4;i++){ 
    graphics.drawString(new Random().nextInt(9)+"", x, 20); 
    x+=30; 
  } 
  response.setContentType("image/jpeg");//設置響應格式 
  ImageIO.write(image, "jpeg", response.getOutputStream()); 
   
} 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關文章

最新評論