java加载透明图片_【人像分割】Java给透明图片加背景色

be306abffccabeffa86188248a8dfade.png

之前在百度AI社区写的人像分割帖子,最近有一些开发者会遇到返回的透明图的base64存图片有问题,还想知道存起来的透明图片如何更改背景色,想快速做个证件照的应用。

此文呢。就从接口返回的透明图片搞起。把返回的

foreground - 人像前景抠图,透明背景

保存成png格式的图片。并进行背景色修改。证件照尺寸修改就不演示了。毕竟还是要给大家一些自我发挥的机会的呢。

调用百度AI人像分割接口

注册百度账号、创建应用就不陈述了。

import com.baidu.aip.bodyanalysis.AipBodyAnalysis;

import org.json.JSONObject;

import java.io.FileOutputStream;

import java.io.OutputStream;

import java.util.Base64;

/**

* 调用百度AI 人像分割接口示例

* @author 小帅丶

* @Date 2019/10/11-15:56

**/

public class TestPersonSeg {

public static void main(String[] args) {

String APPID = "";

String APIKEY = "";

String SECRETKEY = "";

AipBodyAnalysis client = new AipBodyAnalysis(APPID, APIKEY, SECRETKEY);

String filePath = "F:\\testimg\\20151218164804_8kY2a.jpeg";

//调用接口

JSONObject object = client.bodySeg(filePath, null);

String foreground = object.get("foreground").toString();

//把foreground 保存成png格式的透明图片

GenerateImage(foreground, "F:\\testimg\\101103.png");

}

/**

* base64字符串转化成图片

* @param imgStr 接口返回的图片base64数据

* @param imgFilePath 即将要保存的图片的本地路径包含文件名称和格式 例如:F:/generateimage.jpg

* @return

*/

public static boolean GenerateImage(String imgStr, String imgFilePath) { // 对字节数组字符串进行Base64解码并生成图片

if (imgStr == null) // 图像数据为空

return false;

Base64.Decoder decoder = Base64.getDecoder();

try {

// Base64解码

byte[] b = decoder.decode(imgStr);

for (int i = 0; i < b.length; ++i) {

if (b[i] < 0) { // 调整异常数据

b[i] += 256;

}

}

// 生成jpeg图片

OutputStream out = new FileOutputStream(imgFilePath); // 新生成的图片

out.write(b);

out.flush();

out.close();

System.out.println("保存成功" + imgFilePath);

return true;

} catch(Exception e) {

System.out.println("出错了" + e.getMessage());

return false;

}

}

}

看一下示例图转换后的效果

原图

e3a4a825583e251fb87c2a1095048e9d.png

foreground - 人像前景抠图,透明背景

e8491c38cc04420f864ed5698abf7c13.png

scoremap - 人像前景灰度图

0c2c774be0601df85e90b2076e338357.png

给透明背景的图片增加背景色

需要用到

BufferedImage.TYPE_INT_RGB

源码注释解释如下

Represents an image with 8-bit RGB color components packed intointeger pixels 就是8位RGB的图像进行处理

先看个效果图

191f74e81758e528ec679923c22498ac.png

Java代码实现

https://gitee.com/xshuai/imagetool/blob/master/src/main/java/cn/xsshome/imagetool/util/PngColoringUtil.java

这里是代码地址。imagetool项目里面还有很多其他的图片处理工具类哦 最好使用JDK1.8+

如果不是请替换bytes转base64方法

import javax.imageio.ImageIO;

import java.awt.*;

import java.awt.image.BufferedImage;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.IOException;

import java.util.Base64;

/**

* @Description 透明背景上色

* @author 小帅丶

* @className PngColoringUtil

* @Date 2019/10/11-17:03

**/

public class PngColoringUtil {

/**

* @Description 给PNG图片增加背景色

* @Author 小帅丶

* @param sourceImage 原始图片 最好是PNG透明的

* @param targetImage 修改后的图片

* @param backgroudColor 背景色

**/

public static void changePNGBackgroudColor(String sourceImage, String targetImage, Color backgroudColor) {

try {

BufferedImage result = changePNGBackgroudColor(sourceImage, backgroudColor);

File output = new File(targetImage);

ImageIO.write(result, "jpg", output);

} catch (IOException e) {

System.out.println("有问题了" + e.getMessage());

}

}

/**

* @Description 给PNG图片增加背景色 返回base64

* @Author 小帅丶

* @param sourceImage 原始图片 最好是PNG透明的

* @param backgroudColor 背景色

* @return java.lang.String

**/

public static String changePNGBackgroudColorByBase64(String sourceImage, Color backgroudColor){

try {

String base64 = "";

BufferedImage result = changePNGBackgroudColor(sourceImage, backgroudColor);

ByteArrayOutputStream baos = new ByteArrayOutputStream();

ImageIO.write(result, "jpg", baos );

baos.flush();

byte[] imageInByte = baos.toByteArray();

baos.close();

final Base64.Encoder encoder = Base64.getEncoder();

base64 = encoder.encodeToString(imageInByte);

return base64;

}catch (Exception e){

System.out.println("有问题了" + e.getMessage());

return null;

}

}

/**

* @Description 给PNG图片增加背景色 返回BufferedImage

* @Author 小帅丶

* @param sourceImage 原始图片 最好是PNG透明的

* @param backgroudColor 背景色

* @return BufferedImage

**/

public static BufferedImage changePNGBackgroudColor(String sourceImage, Color backgroudColor) {

try {

File input = new File(sourceImage);

BufferedImage image = ImageIO.read(input);

BufferedImage result = new BufferedImage(

image.getWidth(),

image.getHeight(),

BufferedImage.TYPE_INT_RGB);

Graphics2D graphic = result.createGraphics();

graphic.drawImage(image, 0, 0, backgroudColor, null);

graphic.dispose();

return result;

} catch (IOException e) {

System.out.println("有问题了" + e.getMessage());

return null;

}

}

}

调用示例代码

public static void main(String[] args) throws Exception{

String image = "F:\\testimg\\1011.png";//原始图片路径

String resultImage = "F:\\testimg\\10111700.jpg";//更改背景色后的图片路径

changePNGBackgroudColor(image,resultImage, Color.pink);//原图 目标图 背景色

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值