1、先来看看效果:

原图

除去干扰像素后

2、解析代码:

1)、读取文件夹里面的图片

  1. String fileName = "picture";
  2. BufferedImage img = ImageIO.read(new File("img//"+fileName+".jpg"));

2)、获取图片的宽度和高度

  1. int width = img.getWidth();
  2. int height = img.getHeight();

3)、循环执行除去干扰像素

  1. for(int i = 1;i < width;i++){
  2.   Color colorFirst = new Color(img.getRGB(i, 1));
  3. 3   int numFirstGet = colorFirst.getRed()+colorFirst.getGreen()+colorFirst.getBlue();
  4.   for (int x = 0; x < width; x++)
  5. {
  6.   for (int y = 0; y < height; y++)
  7.   {
  8.     Color color = new Color(img.getRGB(x, y));
  9.   System.out.println("red:"+color.getRed()+" | green:"+color.getGreen()+" | blue:"+color.getBlue());
  10. int num = color.getRed()+color.getGreen()+color.getBlue();
  11. if(num >= numFirstGet){
  12.   img.setRGB(x, y, Color.WHITE.getRGB());
  13. }
  14. }
  15. }
  16. }

4)、图片背景变黑,验证码变白色

  1.      for(int i = 1;i<width;i++){
  2. Color color1 = new Color(img.getRGB(i, 1));
  3. int num1 = color1.getRed()+color1.getGreen()+color1.getBlue();
  4. for (int x = 0; x < width; x++)
  5. {
  6. for (int y = 0; y < height; y++)
  7. {
  8. Color color = new Color(img.getRGB(x, y));
  9. System.out.println("red:"+color.getRed()+" | green:"+color.getGreen()+" | blue:"+color.getBlue());
  10. int num = color.getRed()+color.getGreen()+color.getBlue();
  11. if(num==num1){
  12. img.setRGB(x, y, Color.BLACK.getRGB());
  13. }else{
  14. img.setRGB(x, y, Color.WHITE.getRGB());
  15. }
  16. }
  17. }
  18. }

5)、保存图片

  1.      File file = new File("img\\temp\\"+fileName+".jpg");
  2. if (!file.exists())
  3. {
  4. File dir = file.getParentFile();
  5. if (!dir.exists())
  6. {
  7. dir.mkdirs();
  8. }
  9. try
  10. {
  11. file.createNewFile();
  12. }
  13. catch (IOException e)
  14. {
  15. e.printStackTrace();
  16. }
  17. }
  18. ImageIO.write(img, "jpg", file);

3、重要代码:

BufferedImage img = ImageIO.read(new File("img//"+fileName+".jpg"));

  1. Color color1 = new Color(img.getRGB(i, 1));
  2. int num1 = color1.getRed()+color1.getGreen()+color1.getBlue();
  1. getRed()、getGreen()、getBlue()这三个方法分别是获取图片每一个像素的三原色(注释:每一种颜色都是由红、绿、蓝组成的)

4、原理:

  1. 1)、获取图片的高度和宽度
    2)、循环获取图片的每一个像素的值
    3)、把每一排的像素值用来作为对比的标准从而替代颜色相同的为白色(横向和纵向都可以循环一次,这里我只循环了横向的像素)
    4)、循环获取像素,替代验证码背景为黑色(在这个步骤验证码的背景已经是白色的,数字的颜色还没有替换,所以我们循环一次把白色换为黑色,不是白色的换成白色)

5、所有代码:

  1. package com.haojieli.main;
  2.  
  3. import java.awt.Color;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.IOException;
  7.  
  8. import javax.imageio.ImageIO;
  9.  
  10. public class PictureRemove {
  11.  
  12. public static void main(String[] args) throws IOException {
  13. //读取文件夹里面的图片
  14. String fileName = "picture";
  15. BufferedImage img = ImageIO.read(new File("img//"+fileName+".jpg"));
  16. //获取图片的高宽
  17. int width = img.getWidth();
  18. int height = img.getHeight();
  19.  
  20. //循环执行除去干扰像素
  21. for(int i = 1;i < width;i++){
  22. Color colorFirst = new Color(img.getRGB(i, 1));
  23. int numFirstGet = colorFirst.getRed()+colorFirst.getGreen()+colorFirst.getBlue();
  24. for (int x = 0; x < width; x++)
  25. {
  26. for (int y = 0; y < height; y++)
  27. {
  28. Color color = new Color(img.getRGB(x, y));
  29. System.out.println("red:"+color.getRed()+" | green:"+color.getGreen()+" | blue:"+color.getBlue());
  30. int num = color.getRed()+color.getGreen()+color.getBlue();
  31. if(num >= numFirstGet){
  32. img.setRGB(x, y, Color.WHITE.getRGB());
  33. }
  34. }
  35. }
  36. }
  37.  
  38. //图片背景变黑色
  39. for(int i = 1;i<width;i++){
  40. Color color1 = new Color(img.getRGB(i, 1));
  41. int num1 = color1.getRed()+color1.getGreen()+color1.getBlue();
  42. for (int x = 0; x < width; x++)
  43. {
  44. for (int y = 0; y < height; y++)
  45. {
  46. Color color = new Color(img.getRGB(x, y));
  47. System.out.println("red:"+color.getRed()+" | green:"+color.getGreen()+" | blue:"+color.getBlue());
  48. int num = color.getRed()+color.getGreen()+color.getBlue();
  49. if(num==num1){
  50. img.setRGB(x, y, Color.BLACK.getRGB());
  51. }else{
  52. img.setRGB(x, y, Color.WHITE.getRGB());
  53. }
  54. }
  55. }
  56. }
  57. //保存图片
  58. File file = new File("img\\temp\\"+fileName+".jpg");
  59. if (!file.exists())
  60. {
  61. File dir = file.getParentFile();
  62. if (!dir.exists())
  63. {
  64. dir.mkdirs();
  65. }
  66. try
  67. {
  68. file.createNewFile();
  69. }
  70. catch (IOException e)
  71. {
  72. e.printStackTrace();
  73. }
  74. }
  75. ImageIO.write(img, "jpg", file);
  76. }
  77. }
  1. 以上代码只限于这类验证码 的干扰像素的去除 ,其他的验证码类型还待测试!
  2. 博文到此结束,祝各位读者生活愉快!

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. 精通 Android Data Binding

    转自:https://github.com/LyndonChin/MasteringAndroidDataBinding 官方虽然已经给出了教程 - Data Binding Guide (中文版 - ...

  2. DM8168 自己主动登录root用户

    ①指定连接: <DM8168># ln -s /bin/busybox /sbin/getty ②改动/etc/inittab文件: <DM8168># vi /etc/ini ...

  3. 我为什么选择采用node.js来做新一代的EasyDarwin RTSP开源流媒体服务器

    在去年我们还未开始开发基于node.js的新版本EasyDarwin RTSP开源流媒体服务器的时候,我写了一篇博客<对EasyDarwin开源项目后续发展的思考:站在巨人的肩膀上再跳上另一个更 ...

  4. bash批量去前缀

    #!/bin/sh for aFile in *; do oldfile=`basename "$aFile"` newfile=${oldfile::} echo ${oldfi ...

  5. Chrome Native Messaging 与本地程序之间的通信

    最近项目上出现了web打印不稳定的问题,师父决定web调用本地打印程序,在查阅了相关资料和加了几个相关群咨询后得知新版的chrome不支持NNAPI了,最好用Native Messaging来处理,经 ...

  6. 【python】-- 多进程的基本语法 、进程间数据交互与共享、进程锁和进程池的使用

    多进程 进程之间是相互独立的,python是启动进程的时候,是启动的是原生进程.进程是没有GIL锁的,而且不存在锁的概念,进程之间的数据式不能共享的,而线程是可以的. 1.进程的定义 用mulipro ...

  7. vue介绍和简单使用

    Vue是什么? Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue 的核心库只关注视图层,不仅易 ...

  8. centos6.9下设置nginx服务开机自动启动

    首先,在linux系统的/etc/init.d/目录下创建nginx文件,使用如下命令: vi /etc/init.d/nginx 在脚本中添加如下命令: #!/bin/sh # # nginx - ...

  9. selenium之坑(StaleElementReferenceException: Message: Element not found in the cache...)

    有时候循环点击一列链接,只能点到第一个,第二个就失败了 原因是第二个已经是新页面,当然找不到之前页面的元素.就算是后退回来的,页面也是不一样的 页面长的一样不一定是同一张页面,就像两个人长的一样不一定 ...

  10. maven 手动加载第三方jar、zip包

    使用maven搭建工程时,难免要加载大量的第三方的jar包.zip包比较少用,而maven的官网提供的jar往往不能满足需求,这时需要我们手动加载到我们本地或nexus私服的仓库中. 1.加载jar包 ...