Java实现抠图片文字或签名的完整代码
作者:卑微小钟 发布时间:2023-04-18 00:04:44
标签: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.jpg","E:/tmp/demo1_1.jpg");
}
}
来源:https://blog.csdn.net/zhongjianboy/article/details/125423018
0
投稿
猜你喜欢
- CXF简介CXF是一个开源的WebService框架。Apache CXF = Celtix + XFire,开始叫 Apache Celt
- 背景java程序员一般写的是后端服务是JavaWeb类型的项目,主要包括Http接口和dubbo接口,Http接口一般采用的rest风格,那
- 1、获取Class对象在 Java API 中,提供了获取 Class 类对象的三种方法:第一种,使用 Class.forName 静态方法
- 在J2EE项目的开发中,不管是对底层的数据库操作过程,还是业务层的处理过程,还是控制层的处理过程,都不可避免会遇到各种可预知的、不可预知的异
- 使用mybatis-plus自动生成了5个模块(xml/bean/mapper/service/controller)的代码,这里练习一下m
- 根据中国的国情,宽带共享遭受dns污染和HTTP拦截非常严重,造成网络请求的不稳定.但是ip/tcp协议一般不受影响。因此可以把域名先解析成
- 由于工作中 使用 MultipartFile 与现有的一些上传文件组件冲突所以使用其他的接收上传文件的方法.首先我把 M
- SessionSession对象用于获取与数据库的物理连接。 Session对象是重量轻,设计了一个互动是需要与数据库每次被实例化。持久化对
- 一.工程文件二.Main.java主函数,实现类package ui;//主函数实现public class Main { &
- 今天来说一个Java多机部署下定时任务的处理方案。需求: 有两台服务器同时部署了同一套代码, 代码中写有spring自带的定时任务,但是每次
- LRU简介LRU是Least Recently Used 近期最少使用算法,它就可以将长时间没有被利用的数据进行删除。实现最近面了阿里的外包
- 一、算法描述波雷费密码是一种对称式密码,是首种双字母取代的加密法。下面描述算法步骤:1、从1号二维码M05,提取明文信息和密文,M05格式:
- 1、说明isInterrupted()可以判断当前线程是否被中断,仅仅是对interrupt()标识的一个判断,并不会影响标识发生任何改变(
- 一、Java后端使用MultipartFile@PostMapping(value = "/upload")  
- 这篇文章主要介绍了springboot日期转换器实现实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需
- 从这章开始,会介绍几个常用的函数式接口工具,首先先来看下这个大家族:首先从Function接口开始介绍一. 概述该接口顾名思义,函数的意思,
- 开窗函数能在每行的最后一行都显示聚合函数的结果,所以聚合函数可以用作开窗函数聚合函数和开窗函数聚合函数是将多行变成一行,如果要显示其他列,必
- 一个简单的红包生成算法,代码如下:/** * 红包 * @param n * @param money 单位:分 * @return **/
- 本文实例讲述了C#利用System.Uri转URL为绝对地址的方法。分享给大家供大家参考。具体分析如下:在使用ASPOSE.Word生成Wo
- IntInt是Java八种基本数据类型之一,一般大小为4字节32位,取值范围为2-31—231。两个Int类型变量用“==”比较的是值的大小