導航:首頁 > 編程語言 > java干擾線

java干擾線

發布時間:2023-03-17 22:27:18

㈠ 在java的image圖片類中畫線怎麼畫

importjava.awt.*;
classMyCanvasextendsCanvas{
publicvoidpaint(Graphicsg){
g.setFont(newFont("華文行楷",Font.PLAIN,20));
g.setColor(Color.RED);
g.drawString("繪圖測試!",20,20);
g.drawLine(30,60,100,60);
g.setColor(Color.BLUE);
g.draw3DRect(60,50,70,30,false);
g.setColor(Color.BLUE);
g.fillArc(0,60,65,65,40,80);
g.setFont(newFont("華文彩雲",Font.PLAIN,30));
g.setColor(Color.PINK);
g.drawString("彩雲之南",20,120);
}
}

publicclassAWTDrawing{
publicstaticvoidmain(Stringargs[]){
Framef=newFrame("AWT繪圖");
MyCanvasp=newMyCanvas();
f.add(p);
f.setSize(170,170);
f.setBackground(newColor(160,220,220));
f.setVisible(true);
}
}


效果圖(該網站bug315上面還有很多java相關的知識,你可以去學習一下呵呵):

㈡ JAVAWEB項目怎麼實現驗證碼

importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.image.BufferedImage;
importjava.io.IOException;
importjava.io.OutputStream;
importjava.util.Random;
importjavax.imageio.ImageIO;

publicclassCode{

//圖片的寬度。
privateintwidth=160;
//圖片的高度。
privateintheight=38;
//驗證碼字元個數
privateintcodeCount=4;
//驗證碼干擾線數
privateintlineCount=20;
//驗證碼
privateStringcode=null;
//驗證碼圖片Buffer
privateBufferedImagebuffImg=null;
Randomrandom=newRandom();

privatebooleantype=false;

publicCode(){

}

publicCode(intwidth,intheight){
this.width=width;
this.height=height;
}

publicCode(intwidth,intheight,intcodeCount){
this.width=width;
this.height=height;
this.codeCount=codeCount;
}

publicCode(intwidth,intheight,intcodeCount,intlineCount){
this.width=width;
this.height=height;
this.codeCount=codeCount;
this.lineCount=lineCount;
}

publicvoidinit(booleantype){
this.type=type;
}

//生成圖片
privatevoidcreatImage(booleantype){
intfontWidth=width/codeCount;//字體的寬度
intfontHeight=height-5;//字體的高度
intcodeY=height-8;

//圖像buffer
buffImg=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphicsg=buffImg.getGraphics();
//Graphics2Dg=buffImg.createGraphics();
//設置背景色
g.setColor(getRandColor(200,250));
g.fillRect(0,0,width,height);//設置字體
Fontfont=null;
if(!type)font=newFont("Fixedsys",Font.BOLD,fontHeight);
elsefont=getFont(fontHeight);
g.setFont(font);

//設置干擾線
for(inti=0;i<lineCount/2;i++){
intxs=random.nextInt(width);
intys=random.nextInt(height);
intxe=xs+random.nextInt(width);
intye=ys+random.nextInt(height);
g.setColor(getRandColor(1,255));
if(!type)g.drawLine(xs,ys,xe,ye);
elseshear(g,width,height,getRandColor(1,255));
}

//添加噪點
floatyawpRate=0.01f;//雜訊率
intarea=(int)(yawpRate*width*height);
for(inti=0;i<area;i++){
intx=random.nextInt(width);
inty=random.nextInt(height);

buffImg.setRGB(x,y,random.nextInt(255));
}


Stringstr1=randomStr(codeCount);//得到隨機字元
this.code=str1;
for(inti=0;i<codeCount;i++){
StringstrRand=str1.substring(i,i+1);
g.setColor(getRandColor(1,255));
//g.drawString(a,x,y);
//a為要畫出來的東西,x和y表示要畫的東西最左側字元的基線位於此圖形上下文坐標系的(x,y)位置處

g.drawString(strRand,i*fontWidth+3,codeY);
}


}

//得到隨機字元
privateStringrandomStr(intn){
Stringstr1="";//I和l不要
Stringstr2="";
intlen=str1.length()-1;
doubler;
for(inti=0;i<n;i++){
r=(Math.random())*len;
str2=str2+str1.charAt((int)r);
}
returnstr2;
}

//得到隨機顏色
privateColorgetRandColor(intfc,intbc){//給定范圍獲得隨機顏色
if(fc>255)
fc=255;
if(bc>255)
bc=255;
intr=fc+random.nextInt(bc-fc);
intg=fc+random.nextInt(bc-fc);
intb=fc+random.nextInt(bc-fc);
returnnewColor(r,g,b);
}

/**
*產生隨機字體
*/
privateFontgetFont(intsize){
Randomrandom=newRandom();
Fontfont[]=newFont[5];
font[0]=newFont("Ravie",Font.PLAIN,size);
font[1]=newFont("AntiqueOliveCompact",Font.PLAIN,size);
font[2]=newFont("Fixedsys",Font.PLAIN,size);
font[3]=newFont("WideLatin",Font.PLAIN,size);
font[4]=newFont("GillSansUltraBold",Font.PLAIN,size);
returnfont[random.nextInt(5)];
}

//扭曲方法
privatevoidshear(Graphicsg,intw1,inth1,Colorcolor){
shearX(g,w1,h1,color);
shearY(g,w1,h1,color);
}

privatevoidshearX(Graphicsg,intw1,inth1,Colorcolor){

intperiod=random.nextInt(2);

booleanborderGap=true;
intframes=1;
intphase=random.nextInt(2);

for(inti=0;i<h1;i++){
doubled=(double)(period>>1)
*Math.sin((double)i/(double)period
+(6.2831853071795862D*(double)phase)
/(double)frames);
g.Area(0,i,w1,1,(int)d,0);
if(borderGap){
g.setColor(color);
g.drawLine((int)d,i,0,i);
g.drawLine((int)d+w1,i,w1,i);
}
}

}

privatevoidshearY(Graphicsg,intw1,inth1,Colorcolor){

intperiod=random.nextInt(40)+10;//50;

booleanborderGap=true;
intframes=20;
intphase=7;
for(inti=0;i<w1;i++){
doubled=(double)(period>>1)
*Math.sin((double)i/(double)period
+(6.2831853071795862D*(double)phase)
/(double)frames);
g.Area(i,0,1,h1,0,(int)d);
if(borderGap){
g.setColor(color);
g.drawLine(i,(int)d,i,0);
g.drawLine(i,(int)d+h1,i,h1);
}

}

}publicvoidwrite(OutputStreamsos)throwsIOException{
if(buffImg==null)creatImage(type);
ImageIO.write(buffImg,"png",sos);
//JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(sos);
//encoder.encode(buffImg);
sos.close();
}

publicBufferedImagegetBuffImg(){
if(buffImg==null)creatImage(type);
returnbuffImg;
}

publicStringgetCode(){
returncode.toLowerCase();
}

//使用方法
/*publicvoidgetCode3(HttpServletRequestreq,HttpServletResponseresponse,HttpSessionsession)throwsIOException{
//設置響應的類型格式為圖片格式
response.setContentType("image/jpeg");
//禁止圖像緩存。
response.setHeader("Pragma","no-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires",0);


CreateImageCodevCode=newCreateImageCode(100,30,5,10);
session.setAttribute("code",vCode.getCode());
vCode.write(response.getOutputStream());
response.flushBuffer();
}*/

}

㈢ Java如何實現驗證碼驗證功能

package util; import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.util.Random;import javax.imageio.ImageIO; public final class ImageUtil { // 驗證碼字元集 private static final char[] chars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; // 字元數量 private static final int SIZE = 4; // 干擾線數量 private static final int LINES = 5; // 寬度 private static final int WIDTH = 80; // 高度 private static final int HEIGHT = 40; // 字體大小 private static final int FONT_SIZE = 30; /** * 生成隨機驗證碼及圖片 * 返回的數組中,第1個值是驗證碼,第2個值是圖片 */ public static Object[] createImage() { StringBuffer sb = new StringBuffer(); // 1.創建空白圖片 BufferedImage image = new BufferedImage( WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); // 2.獲取圖片畫筆 Graphics graphic = image.getGraphics(); // 3.設置畫筆顏色 graphic.setColor(Color.LIGHT_GRAY); // 4.繪制矩形背景 graphic.fillRect(0, 0, WIDTH, HEIGHT); // 5.畫隨機字元 Random ran = new Random(); for (int i = 0; i <SIZE; i++) { // 取隨機字元索引 int n = ran.nextInt(chars.length); // 設置隨機顏色 graphic.setColor(getRandomColor()); // 設置字體大小 graphic.setFont(new Font( null, Font.BOLD + Font.ITALIC, FONT_SIZE)); // 畫字元 graphic.drawString( chars[n] + "", i * WIDTH / SIZE, HEIGHT / 2); // 記錄字元 sb.append(chars[n]); } // 6.畫干擾線 for (int i = 0; i < LINES; i++) { // 設置隨機顏色 graphic.setColor(getRandomColor()); // 隨機畫線 graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT), ran.nextInt(WIDTH), ran.nextInt(HEIGHT)); } // 7.返回驗證碼和圖片 return new Object[]{sb.toString(), image}; } /** * 隨機取色 */ public static Color getRandomColor() { Random ran = new Random(); Color color = new Color(ran.nextInt(256), ran.nextInt(256), ran.nextInt(256)); return color; } public static void main(String[] args) throws IOException { Object[] objs = createImage(); BufferedImage image = (BufferedImage) objs[1]; OutputStream os = new FileOutputStream("d:/1.png"); ImageIO.write(image, "jpeg", os); os.close(); } }

㈣ 用java寫的驗證碼識別技術原理

<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>
<%!
Color getRandColor(int fc,int bc){//給定范圍獲得隨機顏色
Random random = new Random();
if(fc>255) fc=255;
if(bc>255) bc=255;
int r=fc+random.nextInt(bc-fc);
int g=fc+random.nextInt(bc-fc);
int b=fc+random.nextInt(bc-fc);
return new Color(r,g,b);
}
%>
<%
//設置頁面不緩存
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);// 在內存中創建圖象
int width=60, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);// 獲取圖形上下文
Graphics g = image.getGraphics();//生成隨機類
Random random = new Random();// 設定背景色
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);//設定字體
g.setFont(new Font("Times New Roman",Font.PLAIN,18));//畫邊框
//g.setColor(new Color());
//g.drawRect(0,0,width-1,height-1);
// 隨機產生155條干擾線,使圖象中的認證碼不易被其它程序探測到
g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
}// 取隨機產生的認證碼(4位數字)
//String rand = request.getParameter("rand");
//rand = rand.substring(0,rand.indexOf("."));
String sRand="";
for (int i=0;i<4;i++){
String rand=String.valueOf(random.nextInt(10));
sRand+=rand;
// 將認證碼顯示到圖象中
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));//調用函數出來的顏色相同,可能是因為種子太接近,所以只能直接生成
g.drawString(rand,13*i+6,16);
}// 將認證碼存入SESSION
session.setAttribute("rand",sRand);
// 圖象生效
g.dispose();// 輸出圖象到頁面
ImageIO.write(image, "JPEG", response.getOutputStream());
out.clear();
out = pageContext.pushBody();
%>

閱讀全文

與java干擾線相關的資料

熱點內容
android公網ip 瀏覽:611
要塞1地圖放哪個文件夾 瀏覽:848
凡科建站怎麼弄伺服器 瀏覽:939
蘋果手機怎麼設置app播放 瀏覽:202
下載網站源碼用什麼瀏覽器 瀏覽:241
六線譜pdf 瀏覽:156
linuxmysqlsock 瀏覽:239
人教版數學pdf下載 瀏覽:460
文檔安全加密系統 瀏覽:491
數控銑床編程簡單數字 瀏覽:788
編程電纜如何重啟 瀏覽:121
myqq命令行發消息 瀏覽:365
日產逍客怎麼使用app升窗 瀏覽:503
安卓系統怎麼快速刪除微信內容 瀏覽:653
csharppython 瀏覽:409
程序員脖子按摩儀 瀏覽:562
小米桌面文件夾亂碼怎麼回事 瀏覽:858
點歌台app怎麼連接 瀏覽:318
大學電腦編程學什麼好 瀏覽:348
上哪裡取消應用加密 瀏覽:172