Previous portrait segmentation Posts written in Baidu AI community. Recently, some developers will encounter problems with the base64 stored pictures of transparent images returned. They also want to know how to change the background color of the stored transparent pictures and how to quickly make an application of certificate photos.
What about this article? Start with transparent images returned from the interface. Returning
Foreground - Portrait foreground matting, transparent background
Pictures saved in png format. The background color was modified. Document size changes will not be demonstrated. After all, we still need to give you some opportunities for self-development.
Calling Baidu AI Portrait Segmentation Interface
Register Baidu account, create applications will not be stated.
import com.baidu.aip.bodyanalysis.AipBodyAnalysis; import org.json.JSONObject; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.Base64; /** * Calling Baidu AI Portrait Segmentation Interface Example * @author Xiao handsome * @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"; //Calling interface JSONObject object = client.bodySeg(filePath, null); String foreground = object.get("foreground").toString(); //Save foreground as transparent pictures in png format GenerateImage(foreground, "F:\\testimg\\101103.png"); } /** * base64 Converting Strings to Pictures * @param imgStr Picture base64 data returned by interface * @param imgFilePath The local path of the image to be saved contains the file name and format, such as F:/generateimage.jpg * @return */ public static boolean GenerateImage(String imgStr, String imgFilePath) { // Base64 decoding of byte array strings and generating pictures if (imgStr == null) // Image data is empty return false; Base64.Decoder decoder = Base64.getDecoder(); try { // Base64 decoding byte[] b = decoder.decode(imgStr); for (int i = 0; i < b.length; ++i) { if (b[i] < 0) { // Adjust abnormal data b[i] += 256; } } // Generating jpeg images OutputStream out = new FileOutputStream(imgFilePath); // Newly generated pictures out.write(b); out.flush(); out.close(); System.out.println("Save successfully" + imgFilePath); return true; } catch(Exception e) { System.out.println("Error" + e.getMessage()); return false; } } }
Take a look at the effect of the transformation of the example diagram
Original graph
Foreground - Portrait foreground matting, transparent background
scoremap - gray image of human foreground
Add background color to transparent background images
Need to use
BufferedImage.TYPE_INT_RGB
Source comments are explained as follows
Represents an image with 8-bit RGB color components packed in ointeger pixels is an 8-bit RGB image for processing.
Look at the rendering first.
Java code implementation
https://gitee.com/xshuai/imagetool/blob/master/src/main/java/cn/xsshome/imagetool/util/PngColoringUtil.java
Here is the code address. imagetool There are many other image processing tools in the project. Best use JDK1.8+
If not, please replace it. bytes turn base64 Method
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 Transparent Background Coloring * @author Xiao handsome * @className PngColoringUtil * @Date 2019/10/11-17:03 **/ public class PngColoringUtil { /** * @Description Add background color to PNG image * @Author Xiao handsome * @param sourceImage The original picture should be PNG transparent. * @param targetImage Modified picture * @param backgroudColor Background color **/ 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("There are problems." + e.getMessage()); } } /** * @Description Add background color to PNG image and return to base64 * @Author Xiao handsome * @param sourceImage The original picture should be PNG transparent. * @param backgroudColor Background color * @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("There are problems." + e.getMessage()); return null; } } /** * @Description Add background color to PNG image and return Buffered Image * @Author Xiao handsome * @param sourceImage The original picture should be PNG transparent. * @param backgroudColor Background color * @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("There are problems." + e.getMessage()); return null; } } }
Call sample code
public static void main(String[] args) throws Exception{ String image = "F:\\testimg\\1011.png";//Original Picture Path String resultImage = "F:\\testimg\\10111700.jpg";//Picture path after changing background color changePNGBackgroudColor(image,resultImage, Color.pink);//Background color of original target image }