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丨验证码图片去除干扰像素,方便验证码的识别的更多相关文章

  1. 【Selenium-WebDriver实战篇】Java丨验证码图片去除干扰像素,方便验证码的识别(转)

    参考地址:https://www.cnblogs.com/haojieli/p/6212627.html 1.先来看看效果: 原图 除去干扰像素后 2.解析代码: 1).读取文件夹里面的图片 1 St ...

  2. J2EE如何生成验证码图片和点击刷新验证码

    验证码图片生成步骤 创建BufferedImage对象. 获取BufferedImage的画笔,即调用getGraphics()方法获取Graphics对象. 调用Graphics对象的setColo ...

  3. java 验证码图片处理类,为验证码识别做准备

    /* * To change this template, choose Tools | Templates * and open the template in the editor. */pack ...

  4. Linux 部署java web 项目,验证码图片不显示文字问题

    系统上线后,在获取验证码接口时,获取的验证码图片上没有对应的验证码数字,经过验证后,是由于Linux缺少字体造成的. 正常我们也可以将window的字体直接上传到linux服务器上,window的字体 ...

  5. Struts2 验证码图片实例

    本文转载于DongLiYang的博客http://www.cnblogs.com/dongliyang/archive/2012/08/24/2654431.html 其中修改过一部分,针对使用注解而 ...

  6. 第二百七十节,Tornado框架-生成验证码图片,以及验证码结合Session验证

    Tornado框架-生成验证码图片,以及验证码结合Session验证 第一.生成验证码图片  生成验证码图片需要两个必须模块 1.python自带的random(随机模块) 2.Pillow()图像处 ...

  7. java web学习总结(九) -------------------通过Servlet生成验证码图片

    一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:

  8. 验证码图片生成工具类——Captcha.java

    验证码图片生成工具,使用JAVA生成的图片验证码,调用getRandcode方法获取图片验证码,以流的方式传输到前端页面. 源码如下:(点击下载  Captcha.java) import java. ...

  9. java web 学习九(通过servlet生成验证码图片)

    一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:

随机推荐

  1. sql with 递归查询

    用with实现递归查询 1.数据准备 假定有一个表DiGui,有两个字段Id int ParentId int Id ParentId 4 0 5 0 7 0 2 1 8 5 15 5 9 7 14 ...

  2. RocketMQ 4.3正式发布,支持分布式事务

      冯嘉 作者 | 冯嘉   近日,Apache RocketMQ 4.3 版本宣布发布,此次发布不仅包括提升性能,减少内存使用等原有特性增强,还修复了部分社区提出的若干问题,更重要的是该版本开源了社 ...

  3. JSP 表单处理向服务器提交信息

    JSP 表单处理 我们在浏览网页的时候,经常需要向服务器提交信息,并让后台程序处理.浏览器中使用 GET 和 POST 方法向服务器提交数据. GET 方法 GET方法将请求的编码信息添加在网址后面, ...

  4. yum lock 解决方法

    方法一: # ps aux | grep yum # kill -9 pid 方法二:可以通过执行rm -rf /var/run/yum.pid 来强行解除锁定,然后你的yum就可以运行了 解释: [ ...

  5. 函数柯里化常见应用---add(1,2) add(1)(2) add(1)(2)(3) add(1,2,3)(4)

    这是一道经典的题目,先上代码: 解法1: function add () { var args = Array.prototype.slice.call(arguments); var fn = fu ...

  6. VC++MFC对话框程序中给对话添加背景图片

    VC对话框怎么显示背景图片呢.在MFC中实现背景图片,不像C#应用程序那么简单.今天就和朋友们说说如何在VC界面中设置背景图片 ^_^   工具/原料 Visual C++ 2010 方法一:用Pic ...

  7. Entity Framework 4.1:复杂类型

    这篇文章将讨论复杂类型. 默认情况下,EF4.1 将类映射到表,这是约定,但是有时候,我们需要模型比表的粒度更细一些. 地址是一个典型的例子,看一下下面的客户类. )] publicstring St ...

  8. vue router-link子级返回父级页面

    vue-router嵌套路由,从二级路由跳转到一级路由时,间歇性导致一级路由重叠 解决方法: 1.使用this.$router.push跳转

  9. netstat命令简单使用

    1.适用范围 该命令用于打印网络连接.路由表.接口统计.伪装连接.多播成员等信息. (netstat已经过时,现在使用ss命令,所以本文不会作过多翻译,只着重一些重要部分) 2.语法概览 netsta ...

  10. css3 transition效果

    <meta charset="UTF-8"> <style> .btn { display: inline-block; font-size: 12px; ...