拆分规整验证码图片中的字符

该博客介绍了一个Java程序,用于分割和去除背景色的验证码图片,将每个字符拆分成单独的子图片。通过调整坐标和尺寸参数,程序可以有效地截取每个字符并保存为独立的图像文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.eduask.luck.validate;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

/**
 * 分割图片
 *
 * @author luck
 *
 */
public class SplitImage {
    public static void main(String[] args) throws Exception {
        String file = "D:\\testimg/test2.jpg";
        BufferedImage img = removeBackgroud(file);
        splitImage(img);
    }

    public static void splitImage(BufferedImage img)
            throws Exception {
        List<BufferedImage> subImgs = new ArrayList<BufferedImage>();
        //subImgs.add(img.getSubimage(10, 10, 60, 20)); 数字对应 图片像素位置x轴,y轴,w宽,h高
        //循环截取图片
        int x = 12;//起始位置x坐标
        int y = 7;//起始位置y坐标
        int w = 83;//截取的小图片之间相隔宽度
        int h = 23;//截取的小图片之间相隔高度
        int xl = 101 ;//横向相隔距离
        int yl = 49;//纵向相隔距离
        for(int i =0 ; i< 10 ;i++){
            for(int j = 0 ; j < 3 ; j ++){
                subImgs.add(img.getSubimage(x+(xl*i), y+(yl*j), w, h));
            }
        }
        int count = 0;
        for (BufferedImage bufferedImage : subImgs) {
            //图片路径   D:\testimg/tes
            File file = new File("D:\\testimg/text/"+count+".jpg");
            ImageIO.write(bufferedImage, "jpg", file);
            count ++;
        }
        //return subImgs;
    }
    public static int isWhite(int colorInt) {  
        Color color = new Color(colorInt);  
        if (color.getRed() + color.getGreen() + color.getBlue() > 100) {  
            return 1;  
        }  
        return 0;  
    }  
 
    public static BufferedImage removeBackgroud(String picFile)  
            throws Exception {  
        BufferedImage img = ImageIO.read(new File(picFile));  
        int width = img.getWidth();  
        int height = img.getHeight();  
        for (int x = 0; x < width; ++x) {  
            for (int y = 0; y < height; ++y) {  
                if (isWhite(img.getRGB(x, y)) == 1) {  
                    img.setRGB(x, y, Color.WHITE.getRGB());  
                } else {  
                    img.setRGB(x, y, Color.BLACK.getRGB());  
                }  
            }  
        }  
        return img;  
    }  
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值