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. Memcached 常用的方法

    Memcache常用方法 Memcache::add — 添加一个值,如果已经存在,则返回false Memcache::addServer — 添加一个可供使用的服务器地址 Memcache::cl ...

  2. python reduce & map 习题

    基于廖雪峰教程作业 http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014317 ...

  3. Spring Cloud 微服务五:Spring cloud gateway限流

    前言:在互联网应用中,特别是电商,高并发的场景非常多,比如:秒杀.抢购.双11等,在开始时间点会使流量爆发式地涌入,如果对网络流量不加控制很有可能造成后台实例资源耗尽.限流是指通过指定的策略削减流量, ...

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

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

  5. Leetcode - CopyWithRandomList

    Algorithm: Iterate and copy the original list first. For the random pointer, just copy the value fro ...

  6. HTML使用post方式提交中文内容出现乱码的错误解决方式

    今天在做一个例子的时候,使用post方式提交表单,如果有中文的话,在另一个页面显示出来的时候,总是会出现乱码: 但是将提交方式改为get的时候,就不会出现这种错误. 详细错误见下面图片和代码. HTM ...

  7. A vectorized example

    http://cs231n.stanford.edu/slides/2017/cs231n_2017_lecture4.pdf

  8. Android笔记之OnLongClickListener

    OnLongClickListener中的回调函数boolean onLongClick(View v),其返回值的官方释义如下 如果这个回调消耗了长点击,则返回true,否则返回false. 即使翻 ...

  9. Django 之restframework1

    Restframework 这里先简单的介绍一下restful协议 ----一切皆是资源,操作只是请求方式 基于restful协议的框架有很多Django下的restframework只是其中的一种 ...

  10. Opennms -安装

    参考官方网站:https://docs.opennms.org/opennms/releases/latest/guide-install/guide-install.html#gi-install- ...