Java Program to Remove Background From Image Using Slazzer API Full Project For Beginners

  • Post author:
  • Post category:Java
  • Post comments:0 Comments

 

 

removebg.java

 

 

//use org.apache.httpcomponents

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.HttpClientBuilder;


public class App {
    public static void main(String[] args) throws ClientProtocolException, IOException {
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpEntity entity = MultipartEntityBuilder.create()
        .addBinaryBody("source_image_file", new File("YOUR_IMAGE_PAH"))
        .build();

        HttpPost httpPost = new HttpPost("https://api.slazzer.com/v2.0/remove_image_background");
        httpPost.setHeader("API-KEY", "YOUR_SLAZZER_API_KEY");
        httpPost.setEntity(entity);
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity result = response.getEntity();
        InputStream inputStream = result.getContent();
        @SuppressWarnings("resource")
        OutputStream outputStream = new FileOutputStream("output.png");
        byte[] buffer = new byte[1024];
        int bytesRead;
        while((bytesRead = inputStream.read(buffer)) !=-1){
        outputStream.write(buffer, 0, bytesRead);
    }
    inputStream.close();
    outputStream.flush();
    }
}

Leave a Reply