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.能量晶体解释及其做什么(黄色晶体)
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前By尊渡假赌尊渡假赌尊渡假赌
刺客信条阴影:贝壳谜语解决方案
2 周前ByDDD
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它们
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

Dreamweaver CS6
视觉化网页开发工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

Dreamweaver Mac版
视觉化网页开发工具