java去除图片白边

    /**
     * 去除白边
     * @param address 图片路径
     * @param to 生成的图片路径
     * @throws Exception
     */
    private void removeWhiteEdge(String address, String to) throws Exception {
        BufferedImage source = ImageIO.read(new File(address));
        int top = 0;
        int left = 0;
        int bottom = source.getHeight();
        int right = source.getWidth();

        //寻找最上面的标线,从左(0)到右,从上(0)到下
        for (int i = 0; i < source.getHeight(); i++)//行
        {
            boolean find = false;
            for (int j = 0; j < source.getWidth(); j++)//列
            {
                int pixel = source.getRGB(j, i);
                if (!IsWhite(pixel)) {
                    top = i;
                    find = true;
                    break;
                }

            }
            if (find)
                break;
        }
        //寻找最左边的标线,从上(top位)到下,从左到右
        for (int i = 0; i < source.getWidth(); i++)//列
        {
            boolean find = false;
            for (int j = top; j < source.getHeight(); j++)//行
            {
                int pixel = source.getRGB(i, j);
                if (!IsWhite(pixel)) {
                    left = i;
                    find = true;
                    break;
                }
            }
            if (find)
                break;
        }
        //寻找最下边标线,从下到上,从左到右
        for (int i = source.getHeight() - 1; i >= 0; i--)//行
        {
            boolean find = false;
            for (int j = left; j < source.getWidth(); j++)//列
            {
                int pixel = source.getRGB(j, i);
                if (!IsWhite(pixel)) {
                    bottom = i;
                    find = true;
                    break;
                }
            }
            if (find)
                break;
        }
        //寻找最右边的标线,从上到下,从右往左
        for (int i = source.getWidth() - 1; i >= 0; i--)//列
        {
            boolean find = false;
            for (int j = 0; j <= bottom; j++)//行
            {
                int pixel = source.getRGB(i, j);
                if (!IsWhite(pixel)) {
                    right = i;
                    find = true;
                    break;
                }
            }
            if (find)
                break;
        }
        BufferedImage sdf = source.getSubimage(left, top, right - left, bottom - top);
        ImageIO.write(sdf, "png", new File(to));

    }
    /**
     * 判断是否白色(10点的容差)
     * @param pixel
     * @return
     */
    public boolean IsWhite(int pixel) {
        int[] rgb = new int[3];
        rgb[0] = (pixel & 0xff0000) >> 16; //r
        rgb[1] = (pixel & 0xff00) >> 8; //g
        rgb[2] = (pixel & 0xff); //b
        //RGB都为255为纯白
        if (rgb[0] > 245 && rgb[1] > 245 && rgb[2] > 245)
            return true;
        return false;
    }

 

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值