首页 > 编程知识 正文

验证码图片生成,手机图片验证码

时间:2023-05-03 13:10:04 阅读:11470 作者:4231

/*

*根据源图像剪切两张图

*/

package com.chain way.dragimgvalidate;

import java.awt.Rectangle;

import Java.awt.image.buffered image;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.util.Iterator;

import java.util.Random;

import javax.imageio.ImageIO;

import javax.imageio.image read param;

import javax.imageio.imagereader;

import javax.imageio.stream.image inputstream;

公共类图像实用工具{

publicstaticvoidcut(intx,int y,int width,int height,String srcpath,String subpath ) throws IOException {//裁剪方法

文件输入流is=null;

ImageInputStream iis=null;

try{

is=新文件输入(srcpath ); //读取原始图像

iterator it=imageio.getimagereadersbyformatname (' jpg ); //ImageReader主张可以解码指定的格式

ImageReader reader=it.next (;

is=imageio.createimageinputstream (is; //获取图像流

reader.setinput(IIS,true; 将//IIS标记为true ()仅向前搜索)意味着只能依次读取输入源中包含的图像

imagereadparamparam=reader.getdefaultreadparam (; //指定输入时如何从Java Image I/O框架上下文中的流转换图像或图像组

rectanglerect=newrectangle(x,y,width,height ); //定义空间中的区域

param.setsourceregion(rect ); 提供缓冲图像,并将其用作像素数据解码的目标。

bufferedimagebi=reader.read(0,param ); //索引导入imageIndex指定的对象

imageio.write(bi,' jpg ',newfile ) SubPath ); //保存新图像

}finally{

if(is!=空)

is.close (;

if(IIS!=空)

iis.close (;

}

}

私有image newsrc,缓冲image news RC2,int x,int y,int width,int width

//固定圆的半径为5

int c_r=20;

doubleRR=math.pow(c_r,2 ); //r平方

//圆中心的位置

Random rand=new Random (;

intc _ a=rand.nextint (width-2 * c _ r ) ) x c_r ); //x c_r 10; //圆中心的x坐标必须在(x r,x with-r )范围内

//system.out.println(c_a );

int c_b=y;

//第二个圆(排除圆内的点) ) ) ) ) ) )。

Random rand2=new Random (;

int c2_a=x;

int C2 _ b=rand2. nextint (height-2 * c _ r ) ) y c_r ); //y c_r 50; //圆心的y坐标必须在(y r,y height-r )范围内

//system.out.println (ori image.getwidth (' ori image.getheight ) );

for(intI=0; I

for(intj=0; Jj

//data[I][j]=oriimage.getRGB(I,j );

//(x-a () y-b )=r具有中心坐标为(a,b )和半径为r的三个参数a,b,r。

doublef=math.pow(I-c_a ),2 ) math.pow ) j-c_b ),2 );

doubleF2=math.pow(I-C2_a ),2 ) math.pow ) j-C2_b ),2 );

intRGB=oriimage.getRGB(I,j );

if(I=Xi=yj=RR )//矩形内

//块范围内的值

in(newsrc,newSrc2,I,j,rgb );

}elseif(f=RR ) ) ) )。

//在圆内

in(newsrc,newSrc2,I,j,rgb );

}else{

//使剩下的位置透明

out(newsrc,newSrc2,I,j,rgb );

}

}

}

}

私有映像(缓冲映像newsrc,缓冲映像news RC2,int i,int j,int rgb ) {

newsrc.setrgb(I,j,RGB );

//原图变灰

intr=(0xffRGB;

intg=(0xff ) RGB8);

intb=(0xff ) RGB16 );

RGB=r(G8 ) (b 16 ) ) 100 24;

//RGB=r(g8 ) ) b 16;

NEWSRC2.setrgb(I,j,RGB );

}

私有imagenewsrc,缓冲图像news RC2,int i,int j,int rgb ) {

NEWSRC.setRGB(I,j,0x00ffffff );

NEWSRC2.setrgb(I,j,RGB );

}

publicstaticvoidmain (字符串[ ] args ) throws Exception { )。

ImageUtil tt=new ImageUtil (;

//图像必须是png格式,因为它需要创建透明的背景

//原图

bufferedimagesrc=imageio.read (新文件(e :/temp/1.png ) );

//移动图

bufferedimagenewsrc=newbufferedimage (src.getwidth ),src.getHeight ),BufferedImage.TYPE_4BYTE_ABGR ); 创建新类型的支持//透明的缓冲图像

//对比图

bufferedimagenewsrc2=newbufferedimage (src.getwidth ),src.getHeight ),BufferedImage.TYPE_4BYTE_ABGR ); 创建新类型的支持//透明的缓冲图像

//抠块的大小

int blockWidth=200;

int blockHeight=200;

Random rand1=new Random (;

intx=rand1. nextint (src.getwidth (-block width-20 ) 20; //10,width-200

Random rand2=new Random (;

inty=rand2. nextint (src.getheight (-block height-20 ) 20; //

TT.cutbytemplate2(src,newSrc,newSrc2,x,y,blockWidth,blockHeight ); //图像尺寸固定,位置随机

//生成移动地图

imageio.write(newsrc,' png ',newfile ) ) e:/temp/3.png );

//生成比较图表

imageio.write(newsRC2,' png ',newfile ) ) e:/temp/4.png );

}

}

效果图表如下。

大小: 822.2 KB

共享到:

2018-05-15 21:01

浏览一八三八

评论

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。