Java實(shí)現(xiàn)摳圖片文字或簽名的完整代碼
java摳圖片文字或簽名
運(yùn)行原理
第一步 遍歷像素點(diǎn)
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++) { // 內(nèi)層遍歷是X軸的像素 for (int x = image.getMinX(); x < image.getWidth(); x++) { int rgb = image.getRGB(x, y); // 對(duì)當(dāng)前顏色判斷是否在指定區(qū)間內(nèi) if (!colorInRange(rgb)) { minX = minX > x ? x : minX; minY = minY > y ? y : minY; maxX = maxX < x ? x : maxX; maxY = maxY < y ? y : maxY; } } }
第二步 判斷像素是否是黑色或者指定顏色
// 判斷是背景還是內(nèi)容 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三分量來判斷當(dāng)前顏色是否在指定的顏色區(qū)間內(nèi) if (red >= color_range && green >= color_range && blue >= color_range) { return true; } return false; }
第三步 統(tǒng)計(jì) 選取圖像的像素點(diǎn)最小坐標(biāo)或最大坐標(biāo)
minX = minX > x ? x : minX; minY = minY > y ? y : minY; maxX = maxX < x ? x : maxX; maxY = maxY < y ? y : maxY;
第四步 新建畫布(長(zhǎng)度和高度=最大像素點(diǎn)-最小像素點(diǎn))
BufferedImage bufferedImage = new BufferedImage(maxX - minX, maxY - minY, BufferedImage.TYPE_4BYTE_ABGR);
第五步 畫圖
for (int x = bufferedImage.getMinX(); x < bufferedImage.getWidth(); x++) { // 內(nèi)層遍歷是X軸的像素 for (int y = bufferedImage.getMinX(); y < bufferedImage.getHeight(); y++) { int rgb = image.getRGB(x + minX, y + minY); if (!colorInRange(rgb)) { // 設(shè)置為不透明 alpha = 255; // #AARRGGBB 最前兩位為透明度 rgb = (alpha << 24) | (0x000000);//黑色構(gòu)圖 bufferedImage.setRGB(x, y, rgb); } } } // 生成圖片為PNG ImageIO.write(bufferedImage, "png", new File(output)); // 輸出圖片坐標(biāo) 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 { //識(shí)別顏色度數(shù) 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++) { // 內(nèi)層遍歷是X軸的像素 for (int x = image.getMinX(); x < image.getWidth(); x++) { int rgb = image.getRGB(x, y); // 對(duì)當(dāng)前顏色判斷是否在指定區(qū)間內(nèi) 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++) { // 內(nèi)層遍歷是X軸的像素 for (int y = bufferedImage.getMinX(); y < bufferedImage.getHeight(); y++) { int rgb = image.getRGB(x + minX, y + minY); if (!colorInRange(rgb)) { // 設(shè)置為不透明 alpha = 255; // #AARRGGBB 最前兩位為透明度 rgb = (alpha << 24) | (0x000000);//黑色構(gòu)圖 bufferedImage.setRGB(x, y, rgb); } } } // 生成圖片為PNG ImageIO.write(bufferedImage, "png", new File(output)); // 輸出圖片坐標(biāo) System.out.println(minX + " " + minY + " " + maxX + " " + maxY); } // 判斷是背景還是內(nèi)容 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三分量來判斷當(dāng)前顏色是否在指定的顏色區(qū)間內(nèi) 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"); } }
到此這篇關(guān)于java摳圖片文字或簽名的文章就介紹到這了,更多相關(guān)java摳圖簽名內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
完美解決因數(shù)據(jù)庫(kù)一次查詢數(shù)據(jù)量過大導(dǎo)致的內(nèi)存溢出問題
今天小編就為大家分享一篇完美解決因數(shù)據(jù)庫(kù)一次查詢數(shù)據(jù)量過大導(dǎo)致的內(nèi)存溢出問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06如果淘寶的七天自動(dòng)確認(rèn)收貨讓你設(shè)計(jì)你用Java怎么實(shí)現(xiàn)
在面試的時(shí)候如果面試官問淘寶的七天自動(dòng)確認(rèn)收貨讓你設(shè)計(jì),你會(huì)怎么具體實(shí)現(xiàn)呢?跟著小編看一下下邊的實(shí)現(xiàn)過程,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值2021-09-09使用java打印心型、圓形圖案的實(shí)現(xiàn)代碼
這篇文章主要介紹了使用java打印心型、圓形圖案的實(shí)現(xiàn)代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12spring boot 不連接數(shù)據(jù)庫(kù)啟動(dòng)的解決
這篇文章主要介紹了spring boot 不連接數(shù)據(jù)庫(kù)啟動(dòng)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08springboot+vue實(shí)現(xiàn)Minio文件存儲(chǔ)的示例代碼
本文主要介紹了springboot+vue實(shí)現(xiàn)Minio文件存儲(chǔ)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-02-02