Java 对印章进行抠图

该代码段展示了如何使用Java的Hutool库和AWT/Swing组件将带有白色背景的印章图片转换为带有透明背景的GIF图像。通过读取字节流,处理像素以使白色接近透明,然后重新编码为GIF格式。
摘要由CSDN通过智能技术生成

1.先看效果:

                                 

 2.代码

package cn.com.yuanquanyun.utils;

import cn.hutool.core.io.FileUtil;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class SealTransfer {

    public static byte[] transfer2Byte(byte[] img) {
        ByteArrayOutputStream byteArrayOutputStream = null;
        InputStream is = null;
        byte[] result = null;
        try {
            is = new ByteArrayInputStream(img);
            BufferedImage bi = ImageIO.read(is);
            ImageIcon imageIcon = new ImageIcon(bi);
            BufferedImage bufferedImage = new BufferedImage(imageIcon.getIconWidth(), imageIcon.getIconHeight(),
                    BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();
            g2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon.getImageObserver());
            int alpha = 0;
            for (int j1 = bufferedImage.getMinY(); j1 < bufferedImage.getHeight(); j1++) {
                for (int j2 = bufferedImage.getMinX(); j2 < bufferedImage.getWidth(); j2++) {
                    int rgb = bufferedImage.getRGB(j2, j1);

                    int R = (rgb & 0xff0000) >> 16;
                    int G = (rgb & 0xff00) >> 8;
                    int B = (rgb & 0xff);
                    if (((255 - R) < 120) && ((255 - G) < 120) && ((255 - B) < 120)) {
                        rgb = ((alpha + 1) << 24) | (rgb & 0x00ffffff);
                    }
                    bufferedImage.setRGB(j2, j1, rgb);
                }
            }

            g2D.drawImage(bufferedImage, 0, 0, imageIcon.getImageObserver());
            byteArrayOutputStream = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, "gif", byteArrayOutputStream);//转换成byte数组
            result = byteArrayOutputStream.toByteArray();
        } catch (Exception e) {
            // TODO Auto-generated catch block
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                }
            }
            if (byteArrayOutputStream != null) {
                try {
                    byteArrayOutputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                }
            }
        }
        return result;
    }


    public static void main(String[] args) {
        byte[] bytes = transfer2Byte(FileUtil.readBytes("C:\\Users\\root\\Desktop\\4.png"));
        FileUtil.writeBytes(bytes, "C:\\Users\\root\\Desktop\\ret5.gif");
    }
}

 代码关键点说明:

BufferedImage bi = ImageIO.read(is);//读取图片字节流,并进行解码ImageIO.write(bufferedImage, "gif", byteArrayOutputStream);//将处理后生成的bufferedImage按照gif格式(也可以写其他格式)进行编码存储为byte字节数组

注意: 
       
原始图片最好印章背景是白色的,和印模颜色差别鲜明些。抠出的图会效果好些。

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值