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. textarea中的内容的获取

    今天他们说为啥获取不到textarea的数值 这个问题让我很纳闷  为什么会获取不到呢? 按照逻辑来说 同样都是表单元素  怎么可能出现呢? 我就看了一眼代码 alert($("#texta ...

  2. CSS改变字体下划线颜色

    下图是网页中一个非常普通的列表. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQXVndXMzMzQ0/font/5a6L5L2T/fontsize/40 ...

  3. Jmeter查看QPS和响应时间随着时间的变化曲线

    以下两个插件提供测试结果,扩展图表显示 --- Response Times Over Time --- Transactions per Second 1.打开 https://jmeter-plu ...

  4. hive常规配置及常用命令使用

    hive 常用的几种shell交互方式 查看hive命令帮助:bin/hive -help [hd@hadoop-senior hive]$ bin/hive -help usage: hive -d ...

  5. 记录-springMVC访问web-inf下文件问题+在jsp页面导入jquery插件路径不对问题

    环境:spring + springMvc + mybatis + maven 关于在springMVC环境访问web-inf目录下文件,其一有在springMVC xml文件下加 <!-- 对 ...

  6. Mac下nginx安装和配置

    nginx安装 brew search nginx brew install nginx 安装完以后,可以在终端输出的信息里看到一些配置路径: /usr/local/etc/nginx/nginx.c ...

  7. centos7 Authentication failure

    root@localhost ~]#su bash-4.2$ su Password: su: Authentication failure //这里切换的是系统用户,现在还不清楚为什么postgre ...

  8. 正则表达式 匹配符合A表达式切不符合B表达式的字符串

    有一道这样的面试题 写一个Java方法,利用正则表达式判断输入str中包含字符串”ios“或”apple“(大小写不敏感),但不包括”mediaplayer“.如果满足条件,返回所包含的字符串”ios ...

  9. RLearning第1弹:初识R语言

    R作为一种统计分析软件,是集统计分析与图形显示于一体的.体积小.开源.很强的互动性.自从学了R本人就很少再用matlab了... 一.R语言由函数和赋值构成. R使用<-(最好养成使用习惯),而 ...

  10. awk 字符串函数

    awk 提供了许多强大的字符串函数,见下表: awk 内置字符串函数 gsub(r,s) 在整个 $0 中用 s 替代 r gsub(r,s,t) 在整个 t 中用 s 替代 r index(s,t) ...