Java integration

Overview

Cloudinary is a cloud-based service that provides an end-to-end image and video management solution including uploads, storage, manipulations, optimizations and delivery.

With Cloudinary you can easily upload your media assets to the cloud and automatically perform smart image and video manipulations without installing any complex software. All your media resources are then seamlessly delivered through a fast CDN, optimized and using industry best practices. Cloudinary offers comprehensive APIs and administration capabilities and is easy to integrate with new and existing web and mobile applications.

Quick example

Take a look at the following Cloudinary URL that generates the image below:

Simply accessing the above URL told Cloudinary to transform an uploaded image, create a 150x150px thumbnail using face detection based cropping, round the image's corners, add a sepia effect, convert it to a transparent PNG format, add a watermark layer, rotate the image by 10 degrees and ultimately delivered the resulting image through a fast CDN using smart caching techniques.

Java Library Features

Cloudinary provides an open source Java library for further simplifying the integration:

  • Build URLs for transformation & manipulation.
  • API wrappers: upload, administration, sprite generation and more.
  • JSP tag library to ease and facilitate the inclusion, transformation, upload, and storage in a Java EE web application.
  • Direct uploading from the browser using a jQuery plugin.

The library is built for Java 6 / JSP 2.0 and will work with higher versions. The following are resources which serve as a good starting point to better familiarize yourself with the library:

Choosing the right Maven package

The Maven repository includes several packages ("artifacts") to choose from:

  • cloudinary-http - for general Java applications. It utilizes the Apache HTTP libraries.
    • cloudinary-http44 - Cloudinary Apache HTTP 4.4 Library
    • cloudinary-http43 - Cloudinary Apache HTTP 4.3 Library
    • cloudinary-http42 - Cloudinary Apache HTTP 4.2 Library
  • cloudinary-taglib - provides a Java Tag Library for J2EE applications
  • cloudinary-android - provides support for android applications

Java capitalization and data type guidelines

When using the Java SDK, keep these guidelines in mind:

  • Parameter names: snake_case. For example: public_id
  • Classes: PascalCase. For example: CloudinaryImageTag
  • Methods: camelCase. For example: imageUploadTag
  • Pass parameter data as: Map

Java - Getting started guide

1Installation

The easiest way to start using Cloudinary's Java library is to use Maven.

  • Download and install Maven. Follow http://maven.apache.org/download.cgi for reference.
  • Create a maven project. See example here.
  • Add the Cloudinary dependency to the list of dependencies in the pom.xml :

    <dependencies>
      ...
      <dependency>
        <groupId>com.cloudinary</groupId>
        <artifactId>cloudinary-http44</artifactId>
        <version>[Cloudinary API version, e.g. 1.1.3]</version>
      </dependency>
    </dependencies>
  • If you are building a Java EE web application you should consider using the tag library by adding:

    <dependencies>
      ...
      <dependency>
        <groupId>com.cloudinary</groupId>
        <artifactId>cloudinary-taglib</artifactId>
        <version>[Cloudinary API version, e.g. 1.1.3]</version>
      </dependency>
    </dependencies>

When using in Java code import the appropriate package:

import com.cloudinary.*;

When using in a JSP view import the tag library:

<%@taglib uri="https://cloudinary.com/jsp/taglib" prefix="cl" %>

2Configuration

Your Cloud Name account parameter is required to create instance of Cloudinary class that is main entry point for using the library. API Key and API Secret are further needed to perform secure API calls to Cloudinary (e.g., image and video uploads). See Account and API setup for more details.

Setting the configuration parameters can be done either programmatically using appropriate constructor of Cloudinary class or globally using an environment variable.

You can find your configuration parameters in the dashboard of our Management Console.

Here's an example of setting configuration parameters in your Java application:

import com.cloudinary.*;
...
Cloudinary cloudinary = new Cloudinary(ObjectUtils.asMap(
  "cloud_name", "my_cloud_name",
  "api_key", "my_api_key",
  "api_secret", "my_api_secret"));

In a Java EE environment you can set an environment variable available to your Java EE container:

CLOUDINARY_URL=cloudinary://{api_key}:{api_secret}@{cloud_name}

This will enable you to receive a Cloudinary instance:

Cloudinary cloudinary = Singleton.getCloudinary();

Or you can directly register a Cloudinary instance in your initializer code:

SingletonManager manager = new SingletonManager();
manager.setCloudinary(someCloudinaryInstance);
manager.init();

Note: The tags in the tag library require an instance to be available in the Singleton to function correctly.

See Configuration Options for more details and additional configuration methods.

3Upload media assets

You can upload images, videos or any other files from your server. Uploading is done over HTTPS using a secure protocol based on the api_key and api_secret parameters you provide.

The following command uploads a local file to Cloudinary:

import java.io.File;
import java.util.Map;
...
File toUpload = new File("daisy.png");
Map uploadResult = cloudinary.uploader().upload(toUpload, ObjectUtils.emptyMap());

Alternatively, you can a specify a local path, a public HTTP URL, an S3 URL or an actual media file's data. For example:

Map uploadResult = cloudinary.uploader().upload("http://www.example.com/image.jpg", ObjectUtils.emptyMap());

Each media asset uploaded to Cloudinary is assigned an unique Public ID and is available for immediate delivery and transformation. The upload method returns a Map of the uploaded image's properties including image URL and Public ID. In case of an HTTP error or an application server error a RuntimeException is thrown. The returned Map is a representation of the underlying JSON object returned from the with content similar to that shown in the following example:

{
 "public_id":"tquyfignx5bxcbsupr6a",
 "version":1375302801,
 "signature":"52ecf23eeb987b3b5a72fa4ade51b1c7a1426a97",
 "width":1920,
 "height":1200,
 "format":"jpg",
 "resource_type":"image",
 "created_at":"2017-07-31T20:33:21Z",
 "bytes":737633,
 "type":"upload",
 "url":
   "https://res.cloudinary.com/demo/image/upload/v1375302801/tquyfignx5bxcbsupr6a.jpg",
 "secure_url":
   "https://res.cloudinary.com/demo/image/upload/v1375302801/tquyfignx5bxcbsupr6a.jpg"
}

As you can see in the following example, with a single call you can define your own Public ID, apply an incoming transformation before storing the resource in the cloud, generate derived resources eagerly and assign tags to uploaded resources:

import java.util.Arrays;
...
Map result = cloudinary.uploader().upload(new File("daisy.png"), ObjectUtils.asMap(
  "public_id", "sample_id",
  "transformation", new Transformation().crop("limit").width(40).height(40),
  "eager", Arrays.asList(
    new Transformation().width(200).height(200)
      .crop("thumb").gravity("face").radius(20)
      .effect("sepia"),
    new Transformation().width(100).height(150)
      .crop("fit").fetchFormat("png")
  ),
  "tags", "special, for_homepage"));

Many more upload options as well as direct uploading from the browser are detailed here: Java image upload and Java video upload.

4Display and manipulate images

You can access uploaded resources using the URL contained in the upload result immediately after uploading or build a URL using various helper methods of the Url class which could be accessed via an instance of Cloudinary.

String url = cloudinary.url().format("jpg")
  .transformation(new Transformation().width(250).height(168).crop("fit"))
  .generate("sample");
//https://res.cloudinary.com/demo/image/upload/c_fit,h_168,w_250/sample.jpg

Or in a Java EE JSP view:

<link rel="shortcut icon"
      href="<cl:url src="sample" width="16" height="16" crop="fit" format="jpg"/>"/>

For example, you can use that url and feed it to an HTML image tag or you can use a Cloudinary helper function:

String imageHtml = cloudinary.url().format("jpg")
  .transformation(new Transformation().width(100).height(150).crop("fill"))
  .imageTag("sample", Cludinary.asMap("alt","Sample Image"));
100x150 JPG

Or in a JSP view:

<cl:image src="sample" format="jpg" width="100" height="150" crop="fill" effect="sepia"/>
100x150 JPG

This is equivalent to:

<img src='https://res.cloudinary.com/demo/image/upload/c_fill,e_sepia,h_150,w_100/sample.jpg' 
     width='100' height='150' />

Using simple parameters you can perform powerful manipulations. The Java library builds Cloudinary URLs that you can embed in your web and mobile views for dynamically transforming your uploaded resources in the cloud and delivering the results through a fast CDN with advanced caching.

You can easily convert formats, resize, perform face detection based cropping, apply effects and filters, append textual layers or watermarks and more.

The following command, for example, embeds a JPG thumbnail of a profile photo fetched from Facebook in real-time, crops it to a circle, applies a sepia effect and delivers it optimized through a CDN:

//Java:
cloudinary.url().type("facebook").transformation(
  new Transformation()
    .width(90)
    .height(98)
    .crop("fill")
    .gravity("face")
    .radius("max")
    .effect("sepia"))
  .imageTag("billclinton.jpg"));

//JSP view
<cl:image src="billclinton" type="facebook" format="jpg" width="90" height="98" 
  crop="fill" gravity="face" radius="max" effect="sepia"/>
90x98 JPG

For a full list of supported transformations and their usage, refer to Image transformations. For more details about Cloudinary's transformation and manipulation in Java, see Java image manipulation and Java video manipulation.

5Sample projects

To find additional useful code samples and learn how to integrate Cloudinary with your Java applications, take a look at our Sample Projects. These projects are based on the Spring MVC v3.2.

Photo Album: A fully working web application that allows you to uploads photos, maintain a database with references to them, list them with their metadata, and display them using various cloud-based transformations. Image uploading is performed both from the server side and directly from the browser using a jQuery plugin.

6What's next

Sign up for a free account if you haven't done so already. Follow the steps above and try Cloudinary out. Finished all steps? That's just an example of what Cloudinary can offer. Here's some additional reading material to help you get the best out of Cloudinary:

Learn more about Django image upload and Django video upload .

Explore powerful Django image manipulation and Django video manipulation features and see our Image transformations and Video manipulation and delivery docs.

Browse additional Java integration topics: configuration, Admin API, etc.

Stay tuned for updates, tips and tutorials: Blog, Twitter, Facebook.