Java丨驗證碼圖片去除干擾像素,方便驗證碼的識別


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   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"));

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、所有代碼:

 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 }

 

以上代碼只限於這類驗證碼  的干擾像素的去除 ,其他的驗證碼類型還待測試!

博文到此結束,祝各位讀者生活愉快!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM