JAVA 后端實現簽名圖片裁剪多余空白區(qū)域

一、前言

針對白底黑字的用戶簽名圖片,裁減掉四周多余空白區(qū)域,生成新的圖片,示例如下。

test.png

二、代碼實現

1、調用ImageIO.read獲取圖片以及原圖的寬度、高度
2、通過雙層for循環(huán)獲取黑色字所在矩陣的上下左右四個極值(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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容