Heim  >  Artikel  >  Java  >  So schneiden Sie Bildtext oder Signatur in Java aus

So schneiden Sie Bildtext oder Signatur in Java aus

PHPz
PHPznach vorne
2023-05-12 16:40:061555Durchsuche

JavaBildtext oder Signatur

Funktionsprinzip

Der erste Schritt besteht darin, die Pixel zu durchlaufen

BufferedImage image = ImageIO.read(new File(input));
// 图片透明度
int alpha = 0;
// 最小
int maxX = 0, maxY = 0;
// 最大
int minX = image.getWidth(), minY = image.getHeight();

for (int y = image.getMinY(); y < image.getHeight(); y++) {
// 内层遍历是X轴的像素
for (int x = image.getMinX(); x < image.getWidth(); x++) {
int rgb = image.getRGB(x, y);
// 对当前颜色判断是否在指定区间内
if (!colorInRange(rgb)) {
minX = minX > x ? x : minX;
minY = minY > y ? y : minY;
maxX = maxX < x ? x : maxX;
maxY = maxY < y ? y : maxY;
}
}
}

Der zweite Schritt besteht darin, festzustellen, ob Das Pixel ist Schwarz oder hat eine bestimmte Farbe neue Leinwand (Länge und Höhe = maximaler Pixelpunkt – minimale Pixel)

// 判断是背景还是内容
public static boolean colorInRange(int color) {
// 获取color(RGB)中R位
int red = (color & 0xff0000) >> 16;
// 获取color(RGB)中G位
int green = (color & 0x00ff00) >> 8;
// 获取color(RGB)中B位
int blue = (color & 0x0000ff);
// 通过RGB三分量来判断当前颜色是否在指定的颜色区间内
if (red >= color_range && green >= color_range && blue >= color_range) {
return true;
}
return false;
}

Schritt fünf Zeichnung

minX = minX > x ? x : minX;
minY = minY > y ? y : minY;
maxX = maxX < x ? x : maxX;
maxY = maxY < y ? y : maxY;

Vollständiger Code

BufferedImage bufferedImage = new BufferedImage(maxX - minX, maxY - minY, BufferedImage.TYPE_4BYTE_ABGR);

Das obige ist der detaillierte Inhalt vonSo schneiden Sie Bildtext oder Signatur in Java aus. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:yisu.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen