天天看点

自动换证件照背景图的一个小玩意

每次想换个证件照背景总得找公司UI,实在是太费劲的,就网上找能免费换背景的程序,结果都不行。无意之间看到一个网站可以免费抠图,替换背景图,于是就试试,发现可以换,而且效果不错的。

1、访问https://www.remove.bg/官网,用邮箱注册,注册了以后一个月可以免费调用他们的api50次,如果想跟多的服务可以充钱或者换个邮箱。

2、程序代码如下:

import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
import org.apache.hc.client5.http.fluent.Request;
import org.apache.hc.client5.http.fluent.Response;

import java.io.File;

public class Tools {
    public static void main(String[] args) {

        try {
            Response response = Request.post("https://api.remove.bg/v1.0/removebg")
                    .addHeader("X-Api-Key", "个人私钥")
                    .body(
                            MultipartEntityBuilder.create()
                                    .addBinaryBody("image_file", new File("D:/222.png"))
                                    .addTextBody("size", "auto")
                                    .addTextBody("bg_color", "white")
                                    .build()
                    ).execute();
            response.saveContent(new File("666.png"));
        }catch (Exception e){
            System.out.println(e);
        }
    }
}
           

3、启动以后就会在你项目的根目录生成已经换好背景的照片了。

当然这是个简单版本的,有兴趣的可以基于这个自己做个更完善的功能。。。

------------------------------------------------------分割线----------------------------------------------------

4、忽然发现有个小米证件照的小程序免费可以实现以上功能,主需要上传照片就可以,强烈推荐。。。。