java 处理 jSignature 电子签名后 base64 展示出来图片为黑色背景(将黑色背景改成白色)

/**
 * 保持PNG背景的透明属性(背景不变黑)
 * @param response
 * @param byteArray
 * @throws Exception
 */

public static void convertTransparent(HttpServletResponse response,byte[] byteArray) throws Exception {

    response.setContentType("image/png");
    //浏览器不要缓存
    response.setDateHeader("expries", -1);
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Pragma", "no-cache");
    ByteArrayInputStream inputStream = new ByteArrayInputStream(byteArray);

    BufferedImage image = null;
    image = ImageIO.read(inputStream);
    int w = image.getWidth();
    int h = image.getHeight();
    int minx = image.getMinTileX();
    int miny = image.getMinTileY();
    for (int i = minx; i < w; i++) {
        for (int j = miny; j < h; j++) {
            int rgb = image.getRGB(i, j);
            //以黑色为例
            int RGB = Color.BLACK.getRGB();
            int r = (rgb & 0xff0000) >> 16;
            int g = (rgb & 0xff00) >> 8;
            int b = (rgb & 0xff);
            int R = (RGB & 0xff0000) >> 16;
            int G = (RGB & 0xff00) >> 8;
            int B = (RGB & 0xff);
            if (Math.abs(R - r) < 75 && Math.abs(G - g) < 75 && Math.abs(B - b) < 75) {
                //0xff0000是红色的十六进制代码
                image.setRGB(i, j, 0xff0000);
            }

        }

        ImageIO.write(image, "png", response.getOutputStream());
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值