c语言拼图验证码编写,java实现拼图验证码
c语言拼图验证码编写,java实现拼图验证码
最近项目需要将原有的图形验证码更换为拼图验证,开始去网上搜了一些相关的DEMO,发现做这个的DEMO很少。不得已,只能自己慢慢摸索啦。 经过几天的坑,算是基本完成了需求。现分享下实现的过程
DEMO效果图如下
demo效果
1:首先需要一些模板图, 固定尺寸,下边demo项目中固定为260*116的尺寸。
原图
2:通过原图,在随机位置生成小方格,并根据小方格生成底图,底图由小方格的灰度化之后,绘制到原图上。
private Map createImg(File file, String filename) throws IOException {
BufferedImage sourceBuff = ImageIO.read(file);
int width = sourceBuff.getWidth();
int height = sourceBuff.getHeight();
//生成随机x,y
Random random = new Random();
//X轴距离右端tailoring_w 以上) Y轴距离底部tailoring_y以上
this.location_x = (width - tailoring_w * 2) tailoring_w;
this.location_y = (height - tailoring_h);
//裁剪小图
BufferedImage sourceSmall = cutImg(file, location_x, location_y, tailoring_w, tailoring_h);
//创建shape区域
List shapes = createSmallShape();
Shape area = shapes.get(0);
Shape bigarea = shapes.get(1);
//创建图层用于处理小图的阴影
BufferedImage bfm1 = new BufferedImage(tailoring_w, tailoring_h, BufferedImage.TYPE_IT_ARGB);
//创建图层用于处理大图的凹槽
BufferedImage bfm2 = new BufferedImage(tailoring_w, tailoring_h, BufferedImage.TYPE_IT_ARGB);
for (int i = 0; i < tailoring_w; i) {
for (int j = 0; j < tailoring_h; j) {
if ((i, j)) {
bfm1.setRGB(i, j, sourceSmall.getRGB(i, j));
}
if (big(i, j)) {
bfm2.setRGB(i, j, Color.black.getRGB());
}
}
}
//处理图片的边缘高亮及其阴影效果
BufferedImage resultImgBuff = dealLightAndShadow(bfm1, area);
//生成随机名称
String smallFileame = randomImgame(small_ filename.replaceAll(.png, ) _);
File smallfile = new File(imgPath File.separator smallFileame);
if (!()) { //因为根据坐标点生成小图,如果已经存在,那么就不需要重复创建,直接使用
ImageIO.write(resultImgBuff, png, smallfile);
}
Map result = new HashMap();
result.put(smallImgame, smallFileame);
//将灰图当做水印印到原图上
String bigImgame = createBigImg(bfm2, new File(sourceImgPath File.separator filename), filename);
result.put(bigImgame, bigImgame);
result.put(location_y, String.valueOf(location_y));
result.put(sourceImgame, filename);
return result;
}
: 生成Shape区域,即小方格区域,注意:此处需要生成2个Shape区域, 一个为小方格的Shape,一个为凹槽(拼接小方格的区域)Shape。为什么需要生成2个,因为生成的小方格需要做阴影效果,会生成在Shape之外,因此如果直接使用Shape当做凹槽的区域,那么生成效果图,方格区域会比凹槽略大,出现叠影。
private java.util.List createSmallShape() {
//处理小图,在4个方向上 随机到2个方向添加凸出
int face1 = (); //凸出1
int face2; //凸出2
//使凸出1 与 凸出2不在同一个方向
while (true) {
face2 = ();
if (face1 != face2) {
break;
}
}
//生成随机区域值, (10-20)之间
int position1 = ((tailoring_h - arc * 2) / 2) (tailoring_h - arc * 2)/2;
Shape shape1 = createShape(face1, 0, position1);
Shape bigshape1 = createShape(face1, 2, position1);
//生成中间正方体Shape, (具体边界弧半径 = x坐标位)
Shape centre = new Rectangle2D.Float(arc, arc, tailoring_w - 2 * 10, tailoring_h - 2 * 10);
int position2 = ((tailoring_h - arc * 2) / 2) (tailoring_h - arc * 2)/2;
Shape shape2 = createShape(face2, 0, position2);
//因为后边图形需要生成阴影, 所以生成的小图shape 阴影宽度 = 灰度化的背景小图shape(即大图上的凹槽)
Shape bigshape2 = createShape(face2, shadowWidth / 2, position2);
Shape bigcentre = new Rectangle2D.Float(10 - shadowWidth / 2, 10 - shadowWidth / 2, 0 shadowWidth, 0 shadowWidth);
//合并Shape
Area area = new Area(centre);
area.add(new Area(shape1));
area.add(new Area(shape2));
//合并大Shape
Area bigarea = new Area(bigcentre);
bigarea.add(new Area(bigshape1));
bigarea.add(new Area(bigshape2));
List list = new ArrayList();
list.add(area);
list.add(bigarea);
return list;
}
4:生成Shape区域之后,根据小方格Shape区域,根据原图创建出方格,根据凹槽Shape创建一个灰化的凹槽区域。
5:对小方格进行边缘亮化、阴影处理。 在处理阴影时,需要注意:因为阴影生成在Shape外部,我们还需要使用一个图层用来绘制阴影。然后将小方格绘制到这个图层上
//处理小图的边缘灯光及其阴影效果
private BufferedImage dealLightAndShadow(BufferedImage bfm, Shape shape) throws IOException {
//创建新的透明图层,该图层用于边缘化阴影, 将生成的小图合并到该图上
BufferedImage buffimg = ((Graphics2D) bfm.getGraphics()).getDeviceConfiguration().createCompatibleImage(50, 50, Transparency.TRASLUCET);
Graphics2D graphics2D = ();
Graphics2D g2 = (Graphics2D) bfm.getGraphics();
//原有小图,边缘亮处理
paintBorderGlow(g2, lightHeightWidth, shape);
//新图层添加阴影
paintBorderShadow(graphics2D, shadowWidth, shape);
graphics2D.drawImage(bfm, 0, 0, null);
return buffimg;
}
/**
* 处理阴影
* @param g2
* @param shadowWidth
* @param clipShape
*/
private void paintBorderShadow(Graphics2D g2, int shadowWidth, Shape clipShape) {
g2.setRenderingHint(RenderingHints.KEY_ATIALIASIG, RenderingHints.VALUE_ATIALIAS_O);
int sw = shadowWidth * 2;
for (int i = sw; i >= 2; i -= 2) {
float pct = (float) (sw - i) / (sw - 1);
//pct<0. 用于去掉阴影边缘白边, pct>0.8用于去掉过深的彩, 如果使用Color.lightGray. 可去掉pct>0.8
if (pct < 0. || pct > 0.8) {
continue;
}
g2.setColor(getMixedColor(new Color(54, 54, 54), pct, Color.WHITE, 1.0f - pct));
g2.setStroke(new BasicStroke(i));
g2.draw(clipShape);
}
}
/**
* 处理边缘亮
* @param g2
* @param glowWidth
* @param clipShape
*/
public void paintBorderGlow(Graphics2D g2, int glowWidth, Shape clipShape) {
int gw = glowWidth * 2;
for (int i = gw; i >= 2; i -= 2) {
float pct = (float) (gw - i) / (gw - 1);
Color mixHi = getMixedColor(clrGlowInnerHi, pct, clrGlowOuterHi, 1.0f - pct);
Color mixLo = getMixedColor(clrGlowInnerLo, pct, clrGlowOuterLo, 1.0f - pct);
g2.setPaint(new GradientPaint(0.0f, 5 * 0.25f, mixHi, 0.0f, 5, mixLo));
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, pct));
g2.setStroke(new BasicStroke(i));
g2.draw(clipShape);
}
}
6:最后,将凹槽图层绘制到原图上,生成底部。
至此,生成图形的代码已经完成。 下面就是处理页面展示的问题啦。已将DEMO分享github上。
#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
推荐阅读
留言与评论(共有 20 条评论) |
本站网友 引咎 | 5分钟前 发表 |
int glowWidth | |
本站网友 移动硬盘参数错误 | 23分钟前 发表 |
阴影处理 | |
本站网友 郑州的旅行社 | 4分钟前 发表 |
clrGlowOuterHi | |
本站网友 广州k歌王 | 28分钟前 发表 |
0 | |
本站网友 微信财富宝 | 10分钟前 发表 |
Shape clipShape) { g2.setRenderingHint(RenderingHints.KEY_ATIALIASIG | |
本站网友 黄秋生电影伊波拉病毒 | 0秒前 发表 |
Shape clipShape) { g2.setRenderingHint(RenderingHints.KEY_ATIALIASIG | |
本站网友 rundll32 | 8分钟前 发表 |
String filename) throws IOException { BufferedImage sourceBuff = ImageIO.read(file); int width = sourceBuff.getWidth(); int height = sourceBuff.getHeight(); //生成随机x,y Random random = new Random(); //X轴距离右端tailoring_w 以上) Y轴距离底部tailoring_y以上 this.location_x = (width - tailoring_w * 2) tailoring_w; this.location_y = (height - tailoring_h); //裁剪小图 BufferedImage sourceSmall = cutImg(file | |
本站网友 文怡 | 8分钟前 发表 |
下面就是处理页面展示的问题啦 | |
本站网友 手机rom | 15分钟前 发表 |
为什么需要生成2个,因为生成的小方格需要做阴影效果,会生成在Shape之外,因此如果直接使用Shape当做凹槽的区域,那么生成效果图,方格区域会比凹槽略大,出现叠影 | |
本站网友 网络投资理财平台 | 20分钟前 发表 |
position1); //生成中间正方体Shape | |
本站网友 中山婚纱影楼 | 15分钟前 发表 |
j)) { bfm2.setRGB(i | |
本站网友 大众书局 | 28分钟前 发表 |
5 | |
本站网友 国家药监局审评中心 | 5分钟前 发表 |
shadowWidth | |
本站网友 预期结果 | 10分钟前 发表 |
filename); return result; } | |
本站网友 康普利特 | 20分钟前 发表 |
Shape clipShape) { int gw = glowWidth * 2; for (int i = gw; i >= 2; i -= 2) { float pct = (float) (gw - i) / (gw - 1); Color mixHi = getMixedColor(clrGlowInnerHi | |
本站网友 面部护理 | 14分钟前 发表 |
5 * 0.25f | |
本站网友 adtran | 11分钟前 发表 |
0 | |
本站网友 放水钱 | 12分钟前 发表 |
int glowWidth | |
本站网友 戈薇 | 15分钟前 发表 |
smallFileame); //将灰图当做水印印到原图上 String bigImgame = createBigImg(bfm2 |