Skip to content
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
Cannot retrieve contributors at this time

Quickstart: Analyze a remote image using the Computer Vision REST API and Java

In this quickstart, you'll analyze a remotely stored image to extract visual features by using Java and the Computer Vision REST API. With the Analyze Image method, you can extract visual features based on image content.


  • An Azure subscription - Create one for free
  • Java™ Platform, Standard Edition Development Kit 7 or 8 (JDK 7 or 8)
  • Once you have your Azure subscription, create a Computer Vision resource in the Azure portal to get your key and endpoint. After it deploys, click Go to resource.
    • You will need the key and endpoint from the resource you create to connect your application to the Computer Vision service. You'll paste your key and endpoint into the code below later in the quickstart.
    • You can use the free pricing tier (F0) to try the service, and upgrade later to a paid tier for production.

Create and run the sample application

To create and run the sample, do the following steps:

  1. Create a new Java project in your favorite IDE or editor. If the option is available, create the Java project from a command line application template.

  2. Import the following libraries into your Java project. If you're using Maven, the Maven coordinates are provided for each library.

  3. Add the following import statements to the file that contains the Main public class for your project.

    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.client.utils.URIBuilder;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.util.EntityUtils;
    import org.json.JSONObject;
  4. Replace the AnalyzeImage public class with the following code.

  5. Replace the values of key and endpoint with your Computer Vision key and endpoint.

  6. Optionally, replace the value of imageToAnalyze with the URL of a different image that you want to analyze.

public class AnalyzeImage {
    // **********************************************
    // *** Update or verify the following values. ***
    // **********************************************

    private static String key = "PASTE_YOUR_COMPUTER_VISION_KEY_HERE";
    private static String endpoint = "PASTE_YOUR_COMPUTER_VISION_ENDPOINT_HERE";

    private static final String uriBase = endpoint + "vision/v3.1/analyze";
    private static final String imageToAnalyze =
            "" +

    public static void main(String[] args) {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        try {
            URIBuilder builder = new URIBuilder(uriBase);

            // Request parameters. All of them are optional.
            builder.setParameter("visualFeatures", "Categories,Description,Color");

            // Prepare the URI for the REST API method.
            URI uri =;
            HttpPost request = new HttpPost(uri);

            // Request headers.
            request.setHeader("Content-Type", "application/json");
            request.setHeader("Ocp-Apim-Subscription-Key", key);

            // Request body.
            StringEntity requestEntity =
                    new StringEntity("{\"url\":\"" + imageToAnalyze + "\"}");

            // Call the REST API method and get the response entity.
            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");
        } catch (Exception e) {
            // Display error message.

Compile and run the program

  1. Save, then build the Java project.
  2. If you're using an IDE, run Main.

Alternately, if you're running the program from a command line window, run the following commands. These commands presume your libraries are in a folder named libs that is in the same folder as; if not, you will need to replace libs with the path to your libraries.

  1. Compile the file

    javac -cp ".;libs/*"
  2. Run the program. It will send the request to the QnA Maker API to create the KB, then it will poll for the results every 30 seconds. Each response is printed to the command line window.

    java -cp ".;libs/*" Main

Examine the response

A successful response is returned in JSON. The sample application parses and displays a successful response in the console window, similar to the following example:

REST Response:

  "metadata": {
    "width": 1826,
    "format": "Jpeg",
    "height": 2436
  "color": {
    "dominantColorForeground": "Brown",
    "isBWImg": false,
    "accentColor": "B74314",
    "dominantColorBackground": "Brown",
    "dominantColors": ["Brown"]
  "requestId": "bbffe1a1-4fa3-4a6b-a4d5-a4964c58a811",
  "description": {
    "captions": [{
      "confidence": 0.8241405091548035,
      "text": "a group of people on a city street filled with traffic at night"
    "tags": [
  "categories": [{
    "score": 0.625,
    "name": "outdoor_street"

Next steps

Explore a Java Swing application that uses Computer Vision to perform optical character recognition (OCR); create smart-cropped thumbnails; plus detect, categorize, tag, and describe visual features, including faces, in an image. To rapidly experiment with the Computer Vision API, try the Open API testing console.