Java实现抠图片文字或签名的完整代码
java抠图片文字或签名
运行原理
完整代码
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.webp","E:/tmp/demo1_1.webp");
}
}
到此这篇关于java抠图片文字或签名的文章就介绍到这了,更多相关java抠图签名内容请搜索易知道(ezd.cc)以前的文章或继续浏览下面的相关文章希望大家以后多多支持易知道(ezd.cc)!
相关内容
-
电脑系统文字怎么修改|电脑系统文字怎么修改颜
电脑系统文字怎么修改|电脑系统文字怎么修改颜色,,1. 电脑系统...
-
苹果8图片怎么编辑文字|苹果8怎么编辑照片写字
苹果8图片怎么编辑文字|苹果8怎么编辑照片写字,,苹果8图片怎么...
-
wps提取中的文字内容|正版WPS使用图片提取文字
wps提取中的文字内容|正版WPS使用图片提取文字,提取,文字,内...
-
win7怎么看电脑运行时间|win7怎么看软件运行时
win7怎么看电脑运行时间|win7怎么看软件运行时间,,1. win7怎么...
-
手机版wps里如何给字加拼音|wps在文字上加拼音
手机版wps里如何给字加拼音|wps在文字上加拼音的方法wps怎么...
-
wps编辑公式编辑三列|WPS文字里编辑公式
wps编辑公式编辑三列|WPS文字里编辑公式,编辑,公式,wps,1.WPS...
-
wps竖向文字如何居中
wps竖向文字如何居中,WPS教程,1.WPS office 的word,一定是在表...
-
cad单行字快捷键|cad2007单行文字快捷键
cad单行字快捷键|cad2007单行文字快捷键,,1. cad2007单行文字...
-
wps中调整文字左对齐|在wps中使文字左对齐
wps中调整文字左对齐|在wps中使文字左对齐,对齐,调整,文字,wp...
-
最新版Win7系统运行速度非常缓慢怎么解决
最新版Win7系统运行速度非常缓慢怎么解决,最新版,运行速度, ...
-
wps文档内容杂乱规范|在wps文档固定文字和图片
wps文档内容杂乱规范|在wps文档固定文字和图片防止排版混乱,...
-
文档排版怎么设置|文档文字排版怎么设置
文档排版怎么设置|文档文字排版怎么设置,,1. 文档文字排版怎么...
-
计算机正常运行中死机故障的解决
计算机正常运行中死机故障的解决,,通常有三个问题,如黑屏、花屏...
-
电脑版美图秀秀怎么抠图|电脑版美图秀秀怎么抠
电脑版美图秀秀怎么抠图|电脑版美图秀秀怎么抠图教程,,1. 电脑...
-
如何在电脑上抠图|电脑上怎么抠图
如何在电脑上抠图|电脑上怎么抠图,,电脑上怎么抠图1.打开ps软...