JAVA后端实现签名图片裁剪多余空白区域

2021-04-27  本文已影响0人  CNSTT

一、前言

针对白底黑字的用户签名图片,裁减掉大量空白区域,生成新的图片,示例如下。

test.png

二、代码实现

1、调用ImageIO.read获取图片以及原图的宽度、高度
2、通过双层for循环获取黑色字所在矩阵的上下左右四个极值(rgb(0, 0, 0)黑色对应的getRGB为-16777216)
3、调用BufferedImage 的getSubimage方法裁剪指定矩阵
4、调用ImageIO.write方法生成裁剪后的图片

public BufferedImage getSubimage (int x, int y, int w, int h) {}
x 左上角x坐标
y 左上角y坐标
w 新图片宽度
y 新图片高度

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.regex.Pattern;

public class Trim {

    private static int WHITE = new Color(255, 255, 255).getRGB();
    private static int BLACK = new Color(0, 0, 0).getRGB();

    public static int[] bufferedImageToIntArray(BufferedImage image, int width, int height) {
        try {
            int rgb = 0;
            int x1 = width;
            int y1 = height;
            int x2 = 0;
            int y2 = 0;
            int temp1 = 0;
            int temp2 = 0;
            // 方式一:通过getRGB()方式获得像素数组
            for (int i = 0; i < width; i++) {
                for (int j = 0; j < height; j++) {
                    rgb = image.getRGB(i, j);
                    if (rgb == -16777216) {
                        temp1 = i;
                        temp2 = j;
                        // 计算最左侧
                        if (x1 >= temp1) {
                            x1 = temp1;
                        }
                        // 计算最右侧
                        if (x2 <= temp1) {
                            x2 = temp1;
                        }
                        // 计算最下方
                        if (y2 <= temp2) {
                            y2 = temp2;
                        }
                        // 计算最上方
                        if (y1 >= temp2) {
                            y1 = temp2;
                        }
                    }
                }
            }
            System.out.println("BLACK: " + BLACK);
            System.out.println("x1: " + x1);
            System.out.println("x2: " + x2);
            System.out.println("y1: " + y1);
            System.out.println("y2: " + y2);
            System.out.println("宽度: " + String.valueOf(x2 - x1));
            System.out.println("高度: " + String.valueOf(y2 - y1));
            return new int[] {x1, y1, x2 - x1, y2 - y1};
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws IOException {
        BufferedImage bufferedImage = ImageIO.read(new File("E:/log/test.png"));
        int width = bufferedImage.getWidth();
        int height = bufferedImage.getHeight();
        System.out.println("原图片宽度" + width);
        System.out.println("原图片高度" + height);
        int[] arr = bufferedImageToIntArray(bufferedImage, width, height);
        // blank是作为四周边距留白        
        int blank = 20;
        BufferedImage newBufferedImage = bufferedImage.getSubimage(arr[0] - blank, arr[1] - blank, arr[2] + blank * 2, arr[3] + blank * 2);
        ImageIO.write(newBufferedImage, "png", new File("E:/log/test1.png"));
    }

}

// 输出
原图片宽度998
原图片高度507
BLACK: -16777216
x1: 153
x2: 762
y1: 93
y2: 437
宽度: 609
高度: 344

三、生成文件

test1.png
微信截图_20210427185950.png
上一篇 下一篇

猜你喜欢

热点阅读