Java怎么实现抠图片文字或签名

发布时间:2022-06-23 13:49:51 作者:iii
来源:亿速云 阅读:202

本篇内容介绍了“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");
    }
}

“Java怎么实现抠图片文字或签名”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. python实现图片插入文字
  2. Python如何实现图片添加文字

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:基于JS怎么实现二维码名片生成

下一篇:Python time模块之时间戳与结构化时间怎么使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》