java摳圖片文字或簽名
運作原理
第一步遍歷像素點
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; } } }
第二步判斷像素是否為黑色或指定顏色
// 判断是背景还是内容 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; }
第三步驟統計選取影像的像素點最小座標或最大座標
minX = minX > x ? x : minX; minY = minY > y ? y : minY; maxX = maxX < x ? x : maxX; maxY = maxY < y ? y : maxY;
第四步驟新畫布(長度與高度=最大像素點-最小像素點)
BufferedImage bufferedImage = new BufferedImage(maxX - minX, maxY - minY, BufferedImage.TYPE_4BYTE_ABGR);
第五步畫圖
for (int x = bufferedImage.getMinX(); x < bufferedImage.getWidth(); x++) { // 内层遍历是X轴的像素 for (int y = bufferedImage.getMinX(); y < bufferedImage.getHeight(); y++) { int rgb = image.getRGB(x + minX, y + minY); if (!colorInRange(rgb)) { // 设置为不透明 alpha = 255; // #AARRGGBB 最前两位为透明度 rgb = (alpha << 24) | (0x000000);//黑色构图 bufferedImage.setRGB(x, y, rgb); } } } // 生成图片为PNG ImageIO.write(bufferedImage, "png", new File(output)); // 输出图片坐标 System.out.println(minX + " " + minY + " " + maxX + " " + maxY);
完整程式碼
import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class Main { //识别颜色度数 public static int color_range = 100; public static void recognize(String input, String output) throws IOException { 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; } } } BufferedImage bufferedImage = new BufferedImage(maxX - minX, maxY - minY, BufferedImage.TYPE_4BYTE_ABGR); for (int x = bufferedImage.getMinX(); x < bufferedImage.getWidth(); x++) { // 内层遍历是X轴的像素 for (int y = bufferedImage.getMinX(); y < bufferedImage.getHeight(); y++) { int rgb = image.getRGB(x + minX, y + minY); if (!colorInRange(rgb)) { // 设置为不透明 alpha = 255; // #AARRGGBB 最前两位为透明度 rgb = (alpha << 24) | (0x000000);//黑色构图 bufferedImage.setRGB(x, y, rgb); } } } // 生成图片为PNG ImageIO.write(bufferedImage, "png", new File(output)); // 输出图片坐标 System.out.println(minX + " " + minY + " " + maxX + " " + maxY); } // 判断是背景还是内容 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; } public static void main(String[] args) throws IOException { recognize("E:/tmp/demo1.jpg","E:/tmp/demo1_1.jpg"); } }
以上是Java怎麼實作摳圖片文字或簽名的詳細內容。更多資訊請關注PHP中文網其他相關文章!
陳述
本文轉載於:亿速云。如有侵權,請聯絡admin@php.cn刪除

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
刺客信條陰影:貝殼謎語解決方案
2 週前ByDDD
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能