⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imgmod02.java

📁 一个生成验证码的测试程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
         int[][][] data,int imgCols,int imgRows){
    //Create the 1D array of type int to be
    // populated with pixel data, one int value
    // per pixel, with four color and alpha bytes
    // per int value.
    int[] oneDPix = new int[
                          imgCols * imgRows * 4];

    //Move the data into the 1D array.  Note the
    // use of the bitwise OR operator and the
    // bitwise left-shift operators to put the
    // four 8-bit bytes into each int.
    for(int row = 0,cnt = 0;row < imgRows;row++){
      for(int col = 0;col < imgCols;col++){
        oneDPix[cnt] = ((data[row][col][0] << 24)
                                   & 0xFF000000)
                     | ((data[row][col][1] << 16)
                                   & 0x00FF0000)
                      | ((data[row][col][2] << 8)
                                   & 0x0000FF00)
                           | ((data[row][col][3])
                                   & 0x000000FF);
        cnt++;
      }//end for loop on col

    }//end for loop on row

    return oneDPix;
  }//end convertToOneDim
}//end ImgMod02.java class
//=============================================//



class ProgramTest extends Frame
                           implements ImgIntfc02{

  double slope;//Controls the slope of the line
  String inputData;//Obtained via the TextField
  TextField inputField;//Reference to TextField

  //Constructor must take no parameters
  ProgramTest(){
    //Create and display the user-input GUI.
    setLayout(new FlowLayout());

    Label instructions = new Label(
               "Type a slope value and Replot.");
    add(instructions);

    inputField = new TextField("1.0",5);
    add(inputField);

    setTitle("Copyright 2004, Baldwin");
    setBounds(400,0,200,100);
    setVisible(true);
  }//end constructor

  //The following method must be defined to
  // implement the ImgIntfc02 interface.
  public int[][][] processImg(
                             int[][][] threeDPix,
                             int imgRows,
                             int imgCols){

    //Display some interesting information
    System.out.println("Program test");
    System.out.println("Width = " + imgCols);
    System.out.println("Height = " + imgRows);

    //Make a working copy of the 3D array to
    // avoid making permanent changes to the
    // image data.
    int[][][] temp3D =
                    new int[imgRows][imgCols][4];
    for(int row = 0;row < imgRows;row++){
      for(int col = 0;col < imgCols;col++){
        temp3D[row][col][0] =
                          threeDPix[row][col][0];
        temp3D[row][col][1] =
                          threeDPix[row][col][1];
        temp3D[row][col][2] =
                          threeDPix[row][col][2];
        temp3D[row][col][3] =
                          threeDPix[row][col][3];
      }//end inner loop
    }//end outer loop

    //Get slope value from the TextField
    slope = Double.parseDouble(
                           inputField.getText());

    //Draw a white diagonal line on the image
    for(int col = 0;col < imgCols;col++){
      int row = (int)(slope*col);
      if(row > imgRows -1)break;
      //Set values for alpha, red, green, and
      // blue colors.
      temp3D[row][col][0] = (byte)0xff;
      temp3D[row][col][1] = (byte)0xff;
      temp3D[row][col][2] = (byte)0xff;
      temp3D[row][col][3] = (byte)0xff;
    }//end for loop
    //Return the modified array of image data.
    return temp3D;
  }//end processImg
}//end class ProgramTest

class WaveIt extends Frame
implements ImgIntfc02{
	
	public WaveIt(){
	    setLayout(new FlowLayout());
	}

	public int[][][] processImg(int[][][] threeDPixSrc, int imgRows, int imgCols) {
		

		
		int[][][] threeDPix =
            new int[imgRows][imgCols][4];
		for(int row = 0;row < imgRows;row++){
			for(int col = 0;col < imgCols;col++){
				//gauss(threeDPixSrc,threeDPix,row,col,imgRows,imgCols);
			}
		}
		
		threeDPix = threeDPixSrc;
		
		int r1 = 8;
		int r2 = 5;
		
		int m1 = 15;
		int m2 = 5;
		double start1 = Math.random()*2*3.14;
	    int[][][] temp3D =
            new int[imgRows][imgCols][4];
		for(int row = 0;row < imgRows;row++){
			for(int col = 0;col < imgCols;col++){

				int col2 = col;
				double ry = 1.0*col/imgCols*r1*3.1415926+start1;
				//System.out.println("WaveIt.processImg() x = "+x);
				int n = (int) ((Math.sin(ry)*m1));//+Math.random()*2);
				//System.out.println("WaveIt.processImg() m = "+m);
				double ry2 = 1.0*col/imgCols*r2*3.1415926;
				//System.out.println("WaveIt.processImg() x = "+x);
				int n2 = (int) ((Math.sin(ry2)*m2));//+Math.random()*2);
				
				int row2 = (n2+n+row+imgRows)%imgRows;
				//row2 = row;
				temp3D[row2][col2][0] =
				                  threeDPix[row][col][0];
				temp3D[row2][col2][1] =
				                  threeDPix[row][col][1];
				temp3D[row2][col2][2] =
				                  threeDPix[row][col][2];
				temp3D[row2][col2][3] =
				                  threeDPix[row][col][3];
			}//end inner loop
		}//end outer loop

		
		int r1_2 = 1;
		int r2_2 = 10;
		
		int m1_1 = 2;
		int m2_2 = 10;	
		
		double start = Math.random()*2*3.14;
		
	    int[][][] temp3D2 =
            new int[imgRows][imgCols][4];
		for(int row = 0;row < imgRows;row++){
			for(int col = 0;col < imgCols;col++){
				double rx = 1.0*row/imgRows*r1_2*3.1415926+start;
				//System.out.println("WaveIt.processImg() x = "+x);
				int j = (int) ((Math.sin(rx)*r2_2));//+Math.random()*2);
				//System.out.println("WaveIt.processImg() m = "+m);
				double rx2 = 1.0*row/imgRows*m1_1*3.1415926;
				//System.out.println("WaveIt.processImg() x = "+x);
				int j2 = (int) ((Math.sin(rx2)*m2_2));//+Math.random()*2);
				//System.out.println("WaveIt.processImg() m = "+m);
				int col2 = (j2+j+col+imgCols)%imgCols;

				int row2 = row;
				
				temp3D2[row2][col2][0] =
					temp3D[row][col][0];
				temp3D2[row2][col2][1] =
					temp3D[row][col][1];
				temp3D2[row2][col2][2] =
					temp3D[row][col][2];
				temp3D2[row2][col2][3] =
					temp3D[row][col][3];
			}//end inner loop
		}//end outer loop
		
		//temp3D2 = threeDPixSrc;
		int[][][] temp3D3 =
            new int[imgRows][imgCols][4];
		gauss(temp3D2,temp3D3,imgRows,imgCols);
		gauss(temp3D3,temp3D2,imgRows,imgCols);
		gauss(temp3D2,temp3D3,imgRows,imgCols);
		gauss(temp3D3,temp3D2,imgRows,imgCols);
		gauss(temp3D2,temp3D3,imgRows,imgCols);
		gauss(temp3D3,temp3D2,imgRows,imgCols);
		gauss(temp3D2,temp3D3,imgRows,imgCols);
		return temp3D3;
	}
	
	public void gauss(int[][][] imgDataSrc,int[][][] imgDataDst, int imgRows, int imgCols){
		for(int x = 0;x < imgRows;x++){
			for(int y = 0;y < imgCols;y++){
				imgDataDst[x][y][0]=255;
		for(int i = 1; i < 4; i++){
			//System.out.println("WaveIt.gauss() imgDataSrc["+x+"]["+y+"]["+i+"]"+imgDataSrc[x][y][i]);
			if(x-2 <= 0 || y-2<=0 || x+2 >= imgRows || y+2>=imgCols){//防止边线
				imgDataDst[x][y][i] =  imgDataSrc[x][y][i];
				continue;
			}
			int c = 0;
			if(imgDataSrc[x-1][y][i] > 100){
				imgDataDst[x][y][i] =  imgDataSrc[x][y][i];
				continue;
			}
			int t =  imgDataSrc[x][y][i]*50;
			if(x-1>=0){
				if(imgDataSrc[x-1][y][i] < 128)c++;
				t +=  imgDataSrc[x-1][y][i]*30;
			}
			
			if(y-1>=0){
				if(imgDataSrc[x][y-1][i] < 128)c++;
				t +=  imgDataSrc[x][y-1][i]*30;
			}
			
			if((x+1)<imgRows){
				if(imgDataSrc[x+1][y][i] < 128)c++;
				t +=  imgDataSrc[x+1][y][i]*30;
			}
			
			if(y+1<imgCols){
				if(imgDataSrc[x][y+1][i] < 128)c++;
				t +=  imgDataSrc[x][y+1][i]*30;
			}
			//-------------------------
//			if(x-1>=0 && y-1>=0){
//				imgDataDst[x][y][i] +=  imgDataSrc[x-1][y-1][i]*1;
//			}
//			
//			if(y-1>=0 && (x+1)<width){
//				imgDataDst[x][y][i] +=  imgDataSrc[x+1][y-1][i]*1;
//			}
//			
//			if((x+1)<width && y+1<height){
//				imgDataDst[x][y][i] +=  imgDataSrc[x+1][y+1][i]*1;
//			}
//			
//			if(x-1>=0 && y+1<height){
//				imgDataDst[x][y][i] +=  imgDataSrc[x-1][y+1][i]*1;
//			}
//
//			//--------------
//			if(x-2>=0){
//				imgDataDst[x][y][i] +=  imgDataSrc[x-2][y][i]*1;
//			}
//			
//			if(y-2>=0){
//				imgDataDst[x][y][i] +=  imgDataSrc[x][y-2][i]*1;
//			}
//			
//			if((x+2)<width){
//				imgDataDst[x][y][i] +=  imgDataSrc[x+2][y][i]*1;
//			}
//			
//			if(y+2<height){
//				imgDataDst[x][y][i] +=  imgDataSrc[x][y+2][i]*1;
//			}
			
			t/=170;
			if(c>2)t=0;
			if(t < 150)imgDataDst[x][y][i]=0;
			else imgDataDst[x][y][i]=t;
			//System.out.println("WaveIt.gauss()imgDataSrc[x][y][i]="+imgDataSrc[x][y][i]);
			//System.out.println("WaveIt.gauss()imgDataDst[x][y][i]="+imgDataDst[x][y][i]);
		
	}
			}}}
	
}

class ImageProducer{
    //给定范围获得随机颜色
    public 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);
       }
	public Image createFontImg(String chars){
		int fontSize = 60;
		String[] fontFamilies = new String[]{
				//"Monotype Corsiva",  
				//"Arial Black"  
				"Batang",
				"Times New Roman"
		};
			//Monotype Corsiva  Arial Black  Times New Roman
	        //int iLength = sCode.length();//得到验证码长度
	        //int width=24*iLength;//图象宽度
	       int charWidth = fontSize-10;
	       int imgWidth = charWidth * chars.length()+fontSize;
	        int charHeight=fontSize;//与高度
	        //int CharWidth = (int)(width-24)/iLength;    //字符距左边宽度
	        //int CharHeight = 16;   //字符距上边高度
	                     // 在内存中创建图象
	        BufferedImage image = new BufferedImage(imgWidth, charHeight*2, BufferedImage.TYPE_INT_RGB);
	        
	        // 获取图形上下文
	        Graphics g = image.getGraphics();
	        ((java.awt.Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
	        //生成随机类
	        Random random = new Random();
	        
	        // 设定背景色
	        g.setColor(getRandColor(254,255));
	        g.fillRect(0, 0, imgWidth, charHeight*2);
	        int x = 20;
	        chars = "foot";
	        //画随机颜色的边框
	        g.drawRect(0,0,imgWidth-1,charHeight*2-1);
	        for (int i = 0; i < chars.length(); i++) {
	        	String fontFamily = fontFamilies[ (int) (Math.random()*10)%fontFamilies.length];
	        	Font CodeFont = new Font(fontFamily,Font.TRUETYPE_FONT,fontSize);
	        	//设定字体
	        	g.setFont(CodeFont);
	        	g.setColor(getRandColor(10,11));
	        	//int x = 10+(charWidth-20)*i+((int)Math.random()*10);
	        	int y = (int) (fontSize);//+Math.random()*20
	        	String c = chars.substring(i,i+1);
	        	g.drawString(c,x,y);
	        	if("fl".indexOf(c) >= 0){
	        		x+=charWidth-37;
	        	}else{
	        		x+=charWidth-23;
	        	}
				
			}
	        x-=40;
	        chars = "apple";
	        
	        for (int i = 0; i < chars.length(); i++) {
	        	String fontFamily = fontFamilies[ (int) (Math.random()*10)%fontFamilies.length];
	        	Font CodeFont = new Font(fontFamily,Font.TRUETYPE_FONT,fontSize);
	        	//设定字体
	        	g.setFont(CodeFont);
	        	g.setColor(getRandColor(10,11));
	        	//int x = 80 + 10+(charWidth-20)*i+((int)Math.random()*10);
	        	int y = 28 + (int) (fontSize);//+Math.random()*20
	        	String c = chars.substring(i,i+1);
	        	g.drawString(c,x,y);
	        	if("fl".indexOf(c) >= 0){
	        		x+=charWidth-37;
	        	}else{
	        		x+=charWidth-23;
	        	}
				
			}
	        g.dispose();
	        System.out.println("ImageProducer.createFontImg()"+g.getClass().getName());
	        BufferedImage tag = new BufferedImage(54*5*2, 50*2,
	                BufferedImage.TYPE_INT_RGB);
	        ((java.awt.Graphics2D)(tag.getGraphics())).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
	        
	  		tag.getGraphics().drawImage(image, 0, 0, 54*5*2, 50*2, null);
//	  		FileOutputStream out;
//	  		try {
//	  			out = new FileOutputStream("d:/testxx.jpg");
////	  			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
////	  			encoder.encode(tag);
////	  			out.close(); //close output stream
//	  			ImageIO.write(image, "jpeg", out);
//
//	  		} catch (FileNotFoundException e) {
//	  			throw new RuntimeException(e.getMessage(),e);
//	  		} catch (ImageFormatException e) {
//	  			throw new RuntimeException(e.getMessage(),e);
//	  		} catch (IOException e) {
//	  			throw new RuntimeException(e.getMessage(),e);
//	  		}
	        return tag;

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -