以前在百度AI社区写的人像分割帖子,最近有一些开发者会遇到返回的透明图的base64存图片有问题,还想知道存起来的透明图片如何更改背景色,想快速作个证件照的应用。java
此文呢。就从接口返回的透明图片搞起。把返回的git
foreground - 人像前景抠图,透明背景json
保存成png格式的图片。并进行背景色修改。证件照尺寸修改就不演示了。毕竟仍是要给你们一些自我发挥的机会的呢。数组
注册百度帐号、建立应用就不陈述了。 ssh
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; } } }
须要用到
BufferedImage.TYPE_INT_RGB
源码注释解释以下
Represents an image with 8-bit RGB color components packed intointeger pixels 就是8位RGB的图像进行处理xss
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);//原图 目标图 背景色 }