如何用Java实现图片中文字的识别和检测?
作者:WarmSmile
时间:2023-05-08
来源:互联网
浏览:0
java抠图片文字或签名运行原理第一步遍历像素点BufferedImageimage=ImageIO.read(newFile(input));//图片透明度intalpha=0;//最小intmaxX=0,maxY=0;//最大intminX=image.getWidth(),minY=image.getHeight();for(inty=image.getMinY();yy?y:minY;maxX=maxX16;//获取color(RGB)中G位intgreen=(color&0x00ff00)>>8
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");
}
}
作者最新文章
同程旅行:世界杯开赛首日影音房预订增长47%,重庆成都热度领跑
2026-08-25 14:11
三星T9防摔移动固态:3米防摔+高速传输,数据安全之选
2026-08-25 11:05
vivo X5pro D忘了手机密码怎么办?
2026-08-25 09:07
Redmi K30 Pro变焦版(8GB/128GB/全网通/5G版)手机忘了密码怎么办?一键轻松刷机
2026-08-25 07:51
联想 A3580手机越来越卡怎么办_安卓手机卡顿的处理方法
2026-08-24 18:05
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
Windows 10
Windows
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式
Windows/macOS/Linux
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















