[Java] java编写的破解验证码的代码片段 →→→→→进入此内容的聊天室

来自 , 2020-12-05, 写在 Java, 查看 113 次.
URL http://www.code666.cn/view/d5ff1353
  1. package com.coolinsoft.miaosha.util.ocr;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics2D;
  5. import java.awt.color.ColorSpace;
  6. import java.awt.geom.AffineTransform;
  7. import java.awt.image.AffineTransformOp;
  8. import java.awt.image.BufferedImage;
  9. import java.awt.image.ColorConvertOp;
  10. import java.awt.image.ColorModel;
  11. import java.awt.image.MemoryImageSource;
  12. import java.awt.image.PixelGrabber;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15.  
  16. public class ImageFilter {
  17.         private BufferedImage image;
  18.         private int iw, ih;   //图片宽度、高度
  19.         private int[] pixels; //像素
  20.  
  21.         public ImageFilter(BufferedImage image) {
  22.                 this.image = image;
  23.                 iw = image.getWidth();
  24.                 ih = image.getHeight();
  25.                 pixels = new int[iw * ih];
  26.         }
  27.  
  28.         /** 图像二值化 */
  29.         public BufferedImage changeGrey() {
  30.                 PixelGrabber pg = new PixelGrabber(image.getSource(), 0, 0, iw, ih,pixels, 0, iw);
  31.                 try {
  32.                         pg.grabPixels();
  33.                 } catch (InterruptedException e) {
  34.                         e.printStackTrace();
  35.                 }
  36.                 // 设定二值化的域值,默认值为100
  37.                 int grey = 150;
  38.                 // 对图像进行二值化处理,Alpha值保持不变
  39.                 ColorModel cm = ColorModel.getRGBdefault();
  40.                 for (int i = 0; i < iw * ih; i++) {
  41.                         int red, green, blue;
  42.                         int alpha = cm.getAlpha(pixels[i]);
  43.                         if (cm.getRed(pixels[i]) > grey) {
  44.                                 red = 255;
  45.                         } else {
  46.                                 red = 0;
  47.                         }
  48.  
  49.                         if (cm.getGreen(pixels[i]) > grey) {
  50.                                 green = 255;
  51.                         } else {
  52.                                 green = 0;
  53.                         }
  54.  
  55.                         if (cm.getBlue(pixels[i]) > grey) {
  56.                                 blue = 255;
  57.                         } else {
  58.                                 blue = 0;
  59.                         }
  60.  
  61.                         pixels[i] = alpha << 24 | red << 16 | green << 8 | blue;
  62.                 }
  63.                 // 将数组中的象素产生一个图像
  64.                 return ImageIOHelper.imageProducerToBufferedImage(new MemoryImageSource(iw, ih,pixels, 0, iw));
  65.         }
  66.  
  67.         /** 提升清晰度,进行锐化 */
  68.         public BufferedImage sharp() {
  69.                 PixelGrabber pg = new PixelGrabber(image.getSource(), 0, 0, iw, ih,
  70.                                 pixels, 0, iw);
  71.                 try {
  72.                         pg.grabPixels();
  73.                 } catch (InterruptedException e) {
  74.                         e.printStackTrace();
  75.                 }
  76.  
  77.                 // 象素的中间变量
  78.                 int tempPixels[] = new int[iw * ih];
  79.                 for (int i = 0; i < iw * ih; i++) {
  80.                         tempPixels[i] = pixels[i];
  81.                 }
  82.                 // 对图像进行尖锐化处理,Alpha值保持不变
  83.                 ColorModel cm = ColorModel.getRGBdefault();
  84.                 for (int i = 1; i < ih - 1; i++) {
  85.                         for (int j = 1; j < iw - 1; j++) {
  86.                                 int alpha = cm.getAlpha(pixels[i * iw + j]);
  87.  
  88.                                 // 对图像进行尖锐化
  89.                                 int red6 = cm.getRed(pixels[i * iw + j + 1]);
  90.                                 int red5 = cm.getRed(pixels[i * iw + j]);
  91.                                 int red8 = cm.getRed(pixels[(i + 1) * iw + j]);
  92.                                 int sharpRed = Math.abs(red6 - red5) + Math.abs(red8 - red5);
  93.  
  94.                                 int green5 = cm.getGreen(pixels[i * iw + j]);
  95.                                 int green6 = cm.getGreen(pixels[i * iw + j + 1]);
  96.                                 int green8 = cm.getGreen(pixels[(i + 1) * iw + j]);
  97.                                 int sharpGreen = Math.abs(green6 - green5)
  98.                                                 + Math.abs(green8 - green5);
  99.  
  100.                                 int blue5 = cm.getBlue(pixels[i * iw + j]);
  101.                                 int blue6 = cm.getBlue(pixels[i * iw + j + 1]);
  102.                                 int blue8 = cm.getBlue(pixels[(i + 1) * iw + j]);
  103.                                 int sharpBlue = Math.abs(blue6 - blue5)
  104.                                                 + Math.abs(blue8 - blue5);
  105.  
  106.                                 if (sharpRed > 255) {
  107.                                         sharpRed = 255;
  108.                                 }
  109.                                 if (sharpGreen > 255) {
  110.                                         sharpGreen = 255;
  111.                                 }
  112.                                 if (sharpBlue > 255) {
  113.                                         sharpBlue = 255;
  114.                                 }
  115.  
  116.                                 tempPixels[i * iw + j] = alpha << 24 | sharpRed << 16
  117.                                                 | sharpGreen << 8 | sharpBlue;
  118.                         }
  119.                 }
  120.  
  121.                 // 将数组中的象素产生一个图像
  122.                 return ImageIOHelper
  123.                                 .imageProducerToBufferedImage(new MemoryImageSource(iw, ih,
  124.                                                 tempPixels, 0, iw));
  125.         }
  126.        
  127.        
  128.        
  129.        
  130.        
  131.        
  132.  
  133.  
  134.         public static int isWhite(int colorInt) {
  135.                 Color color = new Color(colorInt);
  136.                 if (color.getRed() + color.getGreen() + color.getBlue() > 600) {
  137.                         return 1;
  138.                 }
  139.                 return 0;
  140.         }
  141.        
  142.         public  BufferedImage removeBackgroud(){
  143.         BufferedImage img = this.image;
  144.         img = img.getSubimage(1, 1, img.getWidth() - 2, img.getHeight() - 2);
  145.         int width = img.getWidth();
  146.         int height = img.getHeight();
  147.         double subWidth = (double) width / 5.0;
  148.         for (int i = 0; i < 5; i++) {
  149.                 Map<Integer, Integer> map = new HashMap<Integer, Integer>();
  150.                for (int x = (int) (1 + i * subWidth); x < (i + 1) * subWidth && x < width - 1; ++x) {
  151.                       for (int y = 0; y < height; ++y) {
  152.                               if (isWhite(img.getRGB(x, y)) == 1)
  153.                                       continue;
  154.                               if (map.containsKey(img.getRGB(x, y))) {
  155.                                       map.put(img.getRGB(x, y), map.get(img.getRGB(x, y)) + 1);
  156.                               } else {
  157.                                       map.put(img.getRGB(x, y), 1);
  158.                               }
  159.                       }
  160.                }
  161.                int max = 0;
  162.                int colorMax = 0;
  163.                for (Integer color : map.keySet()) {
  164.                         if (max < map.get(color)) {
  165.                              max = map.get(color);
  166.                              colorMax = color;
  167.                    }
  168.             }
  169.             for (int x = (int) (1 + i * subWidth); x < (i + 1) * subWidth && x < width - 1; ++x) {
  170.                     for (int y = 0; y < height; ++y) {
  171.                             if (img.getRGB(x, y) != colorMax) {
  172.                                     img.setRGB(x, y, Color.WHITE.getRGB());
  173.                             } else {
  174.                                      img.setRGB(x, y, Color.BLACK.getRGB());
  175.                             }
  176.                     }
  177.             }
  178.       }
  179.        return img;
  180.    }
  181.        
  182.  
  183.         /** 中值滤波 */
  184.         public BufferedImage median() {
  185.                 PixelGrabber pg = new PixelGrabber(image.getSource(), 0, 0, iw, ih,
  186.                                 pixels, 0, iw);
  187.                 try {
  188.                         pg.grabPixels();
  189.                 } catch (InterruptedException e) {
  190.                         e.printStackTrace();
  191.                 }
  192.                 // 对图像进行中值滤波,Alpha值保持不变
  193.                 ColorModel cm = ColorModel.getRGBdefault();
  194.                 for (int i = 1; i < ih - 1; i++) {
  195.                         for (int j = 1; j < iw - 1; j++) {
  196.                                 int red, green, blue;
  197.                                 int alpha = cm.getAlpha(pixels[i * iw + j]);
  198.  
  199.                                 //int red2 = cm.getRed(pixels[(i - 1) * iw + j]);
  200.                                 int red4 = cm.getRed(pixels[i * iw + j - 1]);
  201.                                 int red5 = cm.getRed(pixels[i * iw + j]);
  202.                                 int red6 = cm.getRed(pixels[i * iw + j + 1]);
  203.                                 //int red8 = cm.getRed(pixels[(i + 1) * iw + j]);
  204.  
  205.                                 // 水平方向进行中值滤波
  206.                                 if (red4 >= red5) {
  207.                                         if (red5 >= red6) {
  208.                                                 red = red5;
  209.                                         } else {
  210.                                                 if (red4 >= red6) {
  211.                                                         red = red6;
  212.                                                 } else {
  213.                                                         red = red4;
  214.                                                 }
  215.                                         }
  216.                                 } else {
  217.                                         if (red4 > red6) {
  218.                                                 red = red4;
  219.                                         } else {
  220.                                                 if (red5 > red6) {
  221.                                                         red = red6;
  222.                                                 } else {
  223.                                                         red = red5;
  224.                                                 }
  225.                                         }
  226.                                 }
  227.  
  228.                                 // int green2 = cm.getGreen(pixels[(i - 1) * iw + j]);
  229.                                 int green4 = cm.getGreen(pixels[i * iw + j - 1]);
  230.                                 int green5 = cm.getGreen(pixels[i * iw + j]);
  231.                                 int green6 = cm.getGreen(pixels[i * iw + j + 1]);
  232.                                 // int green8 = cm.getGreen(pixels[(i + 1) * iw + j]);
  233.  
  234.                                 // 水平方向进行中值滤波
  235.                                 if (green4 >= green5) {
  236.                                         if (green5 >= green6) {
  237.                                                 green = green5;
  238.                                         } else {
  239.                                                 if (green4 >= green6) {
  240.                                                         green = green6;
  241.                                                 } else {
  242.                                                         green = green4;
  243.                                                 }
  244.                                         }
  245.                                 } else {
  246.                                         if (green4 > green6) {
  247.                                                 green = green4;
  248.                                         } else {
  249.                                                 if (green5 > green6) {
  250.                                                         green = green6;
  251.                                                 } else {
  252.                                                         green = green5;
  253.                                                 }
  254.                                         }
  255.                                 }
  256.  
  257.                                 // int blue2 = cm.getBlue(pixels[(i - 1) * iw + j]);
  258.                                 int blue4 = cm.getBlue(pixels[i * iw + j - 1]);
  259.                                 int blue5 = cm.getBlue(pixels[i * iw + j]);
  260.                                 int blue6 = cm.getBlue(pixels[i * iw + j + 1]);
  261.                                 // int blue8 = cm.getBlue(pixels[(i + 1) * iw + j]);
  262.  
  263.                                 // 水平方向进行中值滤波
  264.                                 if (blue4 >= blue5) {
  265.                                         if (blue5 >= blue6) {
  266.                                                 blue = blue5;
  267.                                         } else {
  268.                                                 if (blue4 >= blue6) {
  269.                                                         blue = blue6;
  270.                                                 } else {
  271.                                                         blue = blue4;
  272.                                                 }
  273.                                         }
  274.                                 } else {
  275.                                         if (blue4 > blue6) {
  276.                                                 blue = blue4;
  277.                                         } else {
  278.                                                 if (blue5 > blue6) {
  279.                                                         blue = blue6;
  280.                                                 } else {
  281.                                                         blue = blue5;
  282.                                                 }
  283.                                         }
  284.                                 }
  285.                                 pixels[i * iw + j] = alpha << 24 | red << 16 | green << 8
  286.                                                 | blue;
  287.                         }
  288.                 }
  289.  
  290.                 // 将数组中的象素产生一个图像
  291.                 return ImageIOHelper
  292.                                 .imageProducerToBufferedImage(new MemoryImageSource(iw, ih,
  293.                                                 pixels, 0, iw));
  294.         }
  295.  
  296.         /** 线性灰度变换 */
  297.         public BufferedImage lineGrey() {
  298.                 PixelGrabber pg = new PixelGrabber(image.getSource(), 0, 0, iw, ih,
  299.                                 pixels, 0, iw);
  300.                 try {
  301.                         pg.grabPixels();
  302.                 } catch (InterruptedException e) {
  303.                         e.printStackTrace();
  304.                 }
  305.                 // 对图像进行进行线性拉伸,Alpha值保持不变
  306.                 ColorModel cm = ColorModel.getRGBdefault();
  307.                 for (int i = 0; i < iw * ih; i++) {
  308.                         int alpha = cm.getAlpha(pixels[i]);
  309.                         int red = cm.getRed(pixels[i]);
  310.                         int green = cm.getGreen(pixels[i]);
  311.                         int blue = cm.getBlue(pixels[i]);
  312.  
  313.                         // 增加了图像的亮度
  314.                         red = (int) (1.1 * red + 30);
  315.                         green = (int) (1.1 * green + 30);
  316.                         blue = (int) (1.1 * blue + 30);
  317.                         if (red >= 255) {
  318.                                 red = 255;
  319.                         }
  320.                         if (green >= 255) {
  321.                                 green = 255;
  322.                         }
  323.                         if (blue >= 255) {
  324.                                 blue = 255;
  325.                         }
  326.                         pixels[i] = alpha << 24 | red << 16 | green << 8 | blue;
  327.                 }
  328.  
  329.                 // 将数组中的象素产生一个图像
  330.  
  331.                 return ImageIOHelper
  332.                                 .imageProducerToBufferedImage(new MemoryImageSource(iw, ih,
  333.                                                 pixels, 0, iw));
  334.         }
  335.  
  336.         /** 转换为黑白灰度图 */
  337.         public BufferedImage grayFilter() {
  338.                 ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
  339.                 ColorConvertOp op = new ColorConvertOp(cs, null);
  340.                 return op.filter(image, null);
  341.         }
  342.  
  343.         /** 平滑缩放 */
  344.         public BufferedImage scaling(double s) {
  345.                 AffineTransform tx = new AffineTransform();
  346.                 tx.scale(s, s);
  347.                 AffineTransformOp op = new AffineTransformOp(tx,
  348.                                 AffineTransformOp.TYPE_BILINEAR);
  349.                 return op.filter(image, null);
  350.         }
  351.  
  352.         public BufferedImage scale(Float s) {
  353.                 int srcW = image.getWidth();
  354.                 int srcH = image.getHeight();
  355.                 int newW = Math.round(srcW * s);
  356.                 int newH = Math.round(srcH * s);
  357.                 // 先做水平方向上的伸缩变换
  358.                 BufferedImage tmp = new BufferedImage(newW, newH, image.getType());
  359.                 Graphics2D g = tmp.createGraphics();
  360.                 for (int x = 0; x < newW; x++) {
  361.                         g.setClip(x, 0, 1, srcH);
  362.                         // 按比例放缩
  363.                         g.drawImage(image, x - x * srcW / newW, 0, null);
  364.                 }
  365.  
  366.                 // 再做垂直方向上的伸缩变换
  367.                 BufferedImage dst = new BufferedImage(newW, newH, image.getType());
  368.                 g = dst.createGraphics();
  369.                 for (int y = 0; y < newH; y++) {
  370.                         g.setClip(0, y, newW, 1);
  371.                         // 按比例放缩
  372.                         g.drawImage(tmp, 0, y - y * srcH / newH, null);
  373.                 }
  374.                 return dst;
  375.         }
  376.  
  377. }
  378.  
  379. //java/5690

回复 "java编写的破解验证码的代码片段"

这儿你可以回复上面这条便签

captcha