本文实例阐述了java图像幻灯片验证(登录验证)的原理和实现方法。 分享仅供参考。 具体如下。
这是我简单制作的效果图,处理300X150px的检查图,将图像发送到前端。 虽然需要50毫秒左右,但是速度非常快。
原理:
利用java从大图中随机抠出一张小图,在大图中抠出小图的位置加阴影,将这两张图像返回前端;
2 .在前端获取图片,用户滑动缩略图到阴影的位置,获取缩略图滑动的距离,返回java后台进行检查;
3 .返回检查路径、检查路径编号
4 .在前端登录界面,将账号、密码、校验码传递到Java后台登录。
实现:
1 .计算所需的小图的轮廓用二维数组表示。 二维数组有两个值,0和1。 其中,0表示没有颜色,1表示有颜色。 如下图所示,抠图的轮廓。
左边和下面有半圆。 这根据圆的公式就可以了。 代码示例:
静态目标长度=55; //小图长
静态目标宽度=45; //小照片宽度
静态int circler=6; //半径
静态int R1=3; //距离点
//*
*
* @Createdate: 2019年1月24日上午10:52:42
* @Title: getBlockData
* @Description:生成小图的轮廓
* @author mzl
* @return int[][]
* @throws
*/
私有状态[ ] [ ] getblockdata (
int [ ] [ ] data=new int [目标长度] [目标宽度];
双精度x2=target length-circler;
//随机生成圆的位置
doubleH1=Circlermath.random(* ) * (targetWidth-3*circleR-r1 );
双po=circler * circler;
双精度begin=target length-circler-R1;
双面begin=target width-circler-R1;
for(intI=0; I目标长度; I ) {
for(intj=0; j targetWidth; j ) {
doubled3=math.pow (I-x2,2 ) math.pow (j-h1,2 );
doubled2=math.pow (j-2,2 ) math.pow (I-h1,2 );
if () j=ybeginD2=po )|(I=xbeginD3=po ) )。
data[i][j]=0;
} else {
data[i][j]=1;
}
}
}
返回数据;
}
2 .根据经过计算处理的小图的轮廓,向大图中抠出
//*
*
* @Createdate: 2019年1月24日上午10:51:30
* @Title: cutByTemplate
* @Description:生成小图像,为大图像添加阴影
* @author mzl
* @param oriImage
* @param targetImage
* @param templateImage
* @param x
* @param y void
* @throws
*/
私有图像目标图像,int [ ] [ ]模板图像,int x,int y :缓冲区域映像
for(intI=0; I目标长度; I ) {
for(intj=0; j targetWidth; j ) {
int rgb=templateImage[i][j];
//原图中对应位置的变色处理
intRGB_ori=oriimage.getRGB(xI,y j );
if(RGB==1) {
//从地图中复制对应颜色的值
targetimage.setRGB(I,j,rgb_ori );
//原画对应于位置的颜色的变化
oriimage.setRGB(xI,y j,rgb_ori0x363636 );
}else{
//在此使背景透明
targetimage.setRGB(I,j,rgb_ori0x00ffffff );
}
}
}
}
3 .将大图小图转至base64码,方便返回前端
//*
*
* @Createdate: 2019年1月24日上午11:49:42
* @ title :创建图像
* @Description:获取大图、小图Base64代码
* @author mzl
* @param url
* @返回图
* @throws
*/
publicstaticmapcreateimage (字符串URL、int L、int W、Map resultMap ) {
try {
bufferedimagebufferedimage=imageio.read (新文件输入流(URL ) );
bufferedimagetarget=newbufferedimage (target length,targetWidth,BufferedImage.TYPE_4BYTE_ABGR );
cutbytemplate (缓冲图像,target,getBlockData ),l,w );
resultmap.put('b ',getimagebase64 ) bufferedimage ); //大画
resultmap.put(s )、getimagebase64 ) target ); //小照片
}catch(ioexceptione ) {
e .打印堆栈跟踪(;
}finally{
返回结果映射;
}
}
//*
*
* @Createdate: 2019年1月24日上午11:14:19
* @Title: getImageStr
* @将* @Description:图像传输到BASE64
* @author mzl
* @param image
* @return
* @throws IOException String
* @throws
*/
publicstaticstringgetimagebase 64 (bufferedimageimage ) throws IOException {
bytearrayoutputstreamout=newbytearrayoutputstream (;
imageio.write(image,' png ',out );
byte[] b=out.toByteArray (; 转换为字节数组
base64编码器编码器=new base64编码器(;
返回编码程序.编码程序(b ); 生成base64编码
}
此图像已完成键码验证。
希望本文的描述对大家的java编程有帮助。