title: How to send a local image instead of URL to Microsoft Computer Vision API using JAVA
body: <p>The question is how to load image file and pass it as object to Microsoft Computer Vision API, all the sample code in Microsoft website is reading image from url.</p>
  
  <pre><code>// This sample uses the Apache HTTP client library(org.apache.httpcomponents:httpclient:4.2.4)
  // and the org.json library (org.json:json:20170516).
  package com.mparnisari.test;
  
  import java.awt.image.BufferedImage;
  import java.io.File;
  import java.net.URI;
  import java.nio.file.Files;
  import java.nio.file.Path;
  import java.nio.file.Paths;
  
  import javax.imageio.ImageIO;
  
  import org.apache.http.HttpEntity;
  import org.apache.http.HttpResponse;
  import org.apache.http.client.HttpClient;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.entity.ContentType;
  import org.apache.http.entity.FileEntity;
  import org.apache.http.entity.StringEntity;
  import org.apache.http.client.utils.URIBuilder;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.apache.http.util.EntityUtils;
  import org.json.JSONObject;
  
  
  public class Main
  {    
      public static final String subscriptionKey = "MY-KEY-HERE";
  
      public static final String uriBase = 
          "https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/analyze";
  
      public static void main(String[] args)
      {
          HttpClient httpclient = new DefaultHttpClient();
  
          try
          {
              URIBuilder builder = new URIBuilder(uriBase);
  
              builder.setParameter("visualFeatures", "Categories,Description,Color");
              builder.setParameter("language", "en");
  
              URI uri = builder.build();
              HttpPost request = new HttpPost(uri);
  
              request.setHeader("Content-Type", "application/json");
              request.setHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
  
              // Request body.
              BufferedImage image = null;
              File f = null;
              f = new File("C:\\Coffee.jpg"); //image file path
              image = ImageIO.read(f);
  
              File file = new File("C:\\Coffee.jpg");
  
              FileEntity reqEntityF = 
                  new FileEntity(file, ContentType.APPLICATION_OCTET_STREAM);
  
              request.setEntity(reqEntityF);
  
              HttpResponse response = httpclient.execute(request);
              HttpEntity entity = response.getEntity();
  
              if (entity != null)
              {
                  // Format and display the JSON response.
                  String jsonString = EntityUtils.toString(entity);
                  JSONObject json = new JSONObject(jsonString);
                  System.out.println("REST Response:\n");
                  System.out.println(json.toString(2));
              }
          }
          catch (Exception e)
          {
              System.out.println(e.getMessage());
          }
      }
  }
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>REST Response:
  
  {
    "code": "BadArgument",
    "requestId": "7ecf2198-1b7f-44d0-9cc2-e05e28791281",
    "message": "JSON format error."
  }
  </code></pre>
  
  <p>As in other post in stackoverflow guide to use <code>FileEntity</code> to upload the image. But it is not working.</p>
  
  <p>i think this part should somehow refactor to read image instead of a URL.</p>
  
  <pre><code>// Execute the REST API call and get the response entity.
  HttpResponse response = httpclient.execute(request);
  HttpEntity entity = response.getEntity();
  </code></pre>
  
  <p>Let me know what is best solution to solve this problem, because if its possible to pass the image from local to the API, it would be great to have a for loop to analyze an image set.</p>