這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)java中項目實現(xiàn)一個隨機(jī)輸出圖片功能,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

java 實現(xiàn)輸出隨機(jī)圖片實例代碼
輸出隨機(jī)圖片(CAPTCHA圖像):Completely Automated Public Turing Test to Tell Computers and Humans Apart (全自動區(qū)分計算機(jī)和人類的測試)
相關(guān)主要類(JDK 查看API)
BufferedImage:內(nèi)存圖像
Graphics:畫筆
ImageIO:輸出圖像
放在html頁面上<img src/>
注意:瀏覽器默認(rèn)會緩存圖片
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();
//設(shè)置背景
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, WIDTH, HEIGHT);
//設(shè)置邊框
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);
}
//寫隨機(jī)數(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");//設(shè)置響應(yīng)格式
ImageIO.write(image, "jpeg", response.getOutputStream());
}
網(wǎng)站標(biāo)題:java中項目實現(xiàn)一個隨機(jī)輸出圖片功能-創(chuàng)新互聯(lián)
文章地址:http://www.chinadenli.net/article18/dhedgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、網(wǎng)站設(shè)計公司、網(wǎng)站維護(hù)、品牌網(wǎng)站制作、品牌網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容