Java丨验证码图片去除干扰像素,方便验证码的识别
1、先来看看效果:
原图
除去干扰像素后
2、解析代码:
1)、读取文件夹里面的图片
- String fileName = "picture";
- BufferedImage img = ImageIO.read(new File("img//"+fileName+".jpg"));
2)、获取图片的宽度和高度
- int width = img.getWidth();
- int height = img.getHeight();
3)、循环执行除去干扰像素
- for(int i = 1;i < width;i++){
- Color colorFirst = new Color(img.getRGB(i, 1));
- 3 int numFirstGet = colorFirst.getRed()+colorFirst.getGreen()+colorFirst.getBlue();
- for (int x = 0; x < width; x++)
- {
- for (int y = 0; y < height; y++)
- {
- Color color = new Color(img.getRGB(x, y));
- System.out.println("red:"+color.getRed()+" | green:"+color.getGreen()+" | blue:"+color.getBlue());
- int num = color.getRed()+color.getGreen()+color.getBlue();
- if(num >= numFirstGet){
- img.setRGB(x, y, Color.WHITE.getRGB());
- }
- }
- }
- }
4)、图片背景变黑,验证码变白色
- for(int i = 1;i<width;i++){
- Color color1 = new Color(img.getRGB(i, 1));
- int num1 = color1.getRed()+color1.getGreen()+color1.getBlue();
- for (int x = 0; x < width; x++)
- {
- for (int y = 0; y < height; y++)
- {
- Color color = new Color(img.getRGB(x, y));
- System.out.println("red:"+color.getRed()+" | green:"+color.getGreen()+" | blue:"+color.getBlue());
- int num = color.getRed()+color.getGreen()+color.getBlue();
- if(num==num1){
- img.setRGB(x, y, Color.BLACK.getRGB());
- }else{
- img.setRGB(x, y, Color.WHITE.getRGB());
- }
- }
- }
- }
5)、保存图片
- File file = new File("img\\temp\\"+fileName+".jpg");
- if (!file.exists())
- {
- File dir = file.getParentFile();
- if (!dir.exists())
- {
- dir.mkdirs();
- }
- try
- {
- file.createNewFile();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- ImageIO.write(img, "jpg", file);
3、重要代码:
BufferedImage img = ImageIO.read(new File("img//"+fileName+".jpg"));
- Color color1 = new Color(img.getRGB(i, 1));
- int num1 = color1.getRed()+color1.getGreen()+color1.getBlue();
- getRed()、getGreen()、getBlue()这三个方法分别是获取图片每一个像素的三原色(注释:每一种颜色都是由红、绿、蓝组成的)
4、原理:
- 1)、获取图片的高度和宽度
2)、循环获取图片的每一个像素的值
3)、把每一排的像素值用来作为对比的标准从而替代颜色相同的为白色(横向和纵向都可以循环一次,这里我只循环了横向的像素)
4)、循环获取像素,替代验证码背景为黑色(在这个步骤验证码的背景已经是白色的,数字的颜色还没有替换,所以我们循环一次把白色换为黑色,不是白色的换成白色)
5、所有代码:
- package com.haojieli.main;
- import java.awt.Color;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- public class PictureRemove {
- public static void main(String[] args) throws IOException {
- //读取文件夹里面的图片
- String fileName = "picture";
- BufferedImage img = ImageIO.read(new File("img//"+fileName+".jpg"));
- //获取图片的高宽
- int width = img.getWidth();
- int height = img.getHeight();
- //循环执行除去干扰像素
- for(int i = 1;i < width;i++){
- Color colorFirst = new Color(img.getRGB(i, 1));
- int numFirstGet = colorFirst.getRed()+colorFirst.getGreen()+colorFirst.getBlue();
- for (int x = 0; x < width; x++)
- {
- for (int y = 0; y < height; y++)
- {
- Color color = new Color(img.getRGB(x, y));
- System.out.println("red:"+color.getRed()+" | green:"+color.getGreen()+" | blue:"+color.getBlue());
- int num = color.getRed()+color.getGreen()+color.getBlue();
- if(num >= numFirstGet){
- img.setRGB(x, y, Color.WHITE.getRGB());
- }
- }
- }
- }
- //图片背景变黑色
- for(int i = 1;i<width;i++){
- Color color1 = new Color(img.getRGB(i, 1));
- int num1 = color1.getRed()+color1.getGreen()+color1.getBlue();
- for (int x = 0; x < width; x++)
- {
- for (int y = 0; y < height; y++)
- {
- Color color = new Color(img.getRGB(x, y));
- System.out.println("red:"+color.getRed()+" | green:"+color.getGreen()+" | blue:"+color.getBlue());
- int num = color.getRed()+color.getGreen()+color.getBlue();
- if(num==num1){
- img.setRGB(x, y, Color.BLACK.getRGB());
- }else{
- img.setRGB(x, y, Color.WHITE.getRGB());
- }
- }
- }
- }
- //保存图片
- File file = new File("img\\temp\\"+fileName+".jpg");
- if (!file.exists())
- {
- File dir = file.getParentFile();
- if (!dir.exists())
- {
- dir.mkdirs();
- }
- try
- {
- file.createNewFile();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- ImageIO.write(img, "jpg", file);
- }
- }
- 以上代码只限于这类验证码
的干扰像素的去除 ,其他的验证码类型还待测试!
- 博文到此结束,祝各位读者生活愉快!
Java丨验证码图片去除干扰像素,方便验证码的识别的更多相关文章
- 【Selenium-WebDriver实战篇】Java丨验证码图片去除干扰像素,方便验证码的识别(转)
参考地址:https://www.cnblogs.com/haojieli/p/6212627.html 1.先来看看效果: 原图 除去干扰像素后 2.解析代码: 1).读取文件夹里面的图片 1 St ...
- J2EE如何生成验证码图片和点击刷新验证码
验证码图片生成步骤 创建BufferedImage对象. 获取BufferedImage的画笔,即调用getGraphics()方法获取Graphics对象. 调用Graphics对象的setColo ...
- java 验证码图片处理类,为验证码识别做准备
/* * To change this template, choose Tools | Templates * and open the template in the editor. */pack ...
- Linux 部署java web 项目,验证码图片不显示文字问题
系统上线后,在获取验证码接口时,获取的验证码图片上没有对应的验证码数字,经过验证后,是由于Linux缺少字体造成的. 正常我们也可以将window的字体直接上传到linux服务器上,window的字体 ...
- Struts2 验证码图片实例
本文转载于DongLiYang的博客http://www.cnblogs.com/dongliyang/archive/2012/08/24/2654431.html 其中修改过一部分,针对使用注解而 ...
- 第二百七十节,Tornado框架-生成验证码图片,以及验证码结合Session验证
Tornado框架-生成验证码图片,以及验证码结合Session验证 第一.生成验证码图片 生成验证码图片需要两个必须模块 1.python自带的random(随机模块) 2.Pillow()图像处 ...
- java web学习总结(九) -------------------通过Servlet生成验证码图片
一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:
- 验证码图片生成工具类——Captcha.java
验证码图片生成工具,使用JAVA生成的图片验证码,调用getRandcode方法获取图片验证码,以流的方式传输到前端页面. 源码如下:(点击下载 Captcha.java) import java. ...
- java web 学习九(通过servlet生成验证码图片)
一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:
随机推荐
- 精通 Android Data Binding
转自:https://github.com/LyndonChin/MasteringAndroidDataBinding 官方虽然已经给出了教程 - Data Binding Guide (中文版 - ...
- DM8168 自己主动登录root用户
①指定连接: <DM8168># ln -s /bin/busybox /sbin/getty ②改动/etc/inittab文件: <DM8168># vi /etc/ini ...
- 我为什么选择采用node.js来做新一代的EasyDarwin RTSP开源流媒体服务器
在去年我们还未开始开发基于node.js的新版本EasyDarwin RTSP开源流媒体服务器的时候,我写了一篇博客<对EasyDarwin开源项目后续发展的思考:站在巨人的肩膀上再跳上另一个更 ...
- bash批量去前缀
#!/bin/sh for aFile in *; do oldfile=`basename "$aFile"` newfile=${oldfile::} echo ${oldfi ...
- Chrome Native Messaging 与本地程序之间的通信
最近项目上出现了web打印不稳定的问题,师父决定web调用本地打印程序,在查阅了相关资料和加了几个相关群咨询后得知新版的chrome不支持NNAPI了,最好用Native Messaging来处理,经 ...
- 【python】-- 多进程的基本语法 、进程间数据交互与共享、进程锁和进程池的使用
多进程 进程之间是相互独立的,python是启动进程的时候,是启动的是原生进程.进程是没有GIL锁的,而且不存在锁的概念,进程之间的数据式不能共享的,而线程是可以的. 1.进程的定义 用mulipro ...
- vue介绍和简单使用
Vue是什么? Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue 的核心库只关注视图层,不仅易 ...
- centos6.9下设置nginx服务开机自动启动
首先,在linux系统的/etc/init.d/目录下创建nginx文件,使用如下命令: vi /etc/init.d/nginx 在脚本中添加如下命令: #!/bin/sh # # nginx - ...
- selenium之坑(StaleElementReferenceException: Message: Element not found in the cache...)
有时候循环点击一列链接,只能点到第一个,第二个就失败了 原因是第二个已经是新页面,当然找不到之前页面的元素.就算是后退回来的,页面也是不一样的 页面长的一样不一定是同一张页面,就像两个人长的一样不一定 ...
- maven 手动加载第三方jar、zip包
使用maven搭建工程时,难免要加载大量的第三方的jar包.zip包比较少用,而maven的官网提供的jar往往不能满足需求,这时需要我们手动加载到我们本地或nexus私服的仓库中. 1.加载jar包 ...