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()); }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
springboot利用aop實現(xiàn)接口異步(進度條)的全過程
我們在開發(fā)中,調(diào)用第三方接口時,往往是提交數(shù)據(jù),要異步去獲取數(shù)據(jù),下面這篇文章主要給大家介紹了關于springboot利用aop實現(xiàn)接口異步(進度條)的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-01-01springboot使用redis對單個對象進行自動緩存更新刪除的實現(xiàn)
本文主要介紹了springboot使用redis對單個對象進行自動緩存更新刪除的實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08解決@RequestBody使用不能class類型匹配的問題
這篇文章主要介紹了解決@RequestBody使用不能class類型匹配的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07java實現(xiàn)酷狗音樂臨時緩存文件轉換為MP3文件的方法
這篇文章主要介紹了java實現(xiàn)酷狗音樂臨時緩存文件轉換為MP3文件的方法,涉及java針對文件操作的相關技巧,需要的朋友可以參考下2016-08-08關于elcipse 安裝lombok插件解決 @Slf4j 等找不到log變量問題
這篇文章主要介紹了關于elcipse 安裝lombok插件解決 @Slf4j 等找不到log變量問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01