Transforming Media With Java Code
Tags: cloudinary, java, transformations
Published: 2021-05-07 16:45:38

---
title: Transforming Media With Java Code 
published: true
description: How to transform media on Cloudinary with Java code, e.g., crop images, apply a boomerang effect on videos, or concatenate or add a progress bar to videos.
tags: Cloudinary, Java, transformations
cover_image: https://res.cloudinary.com/cloudinary-training/image/upload/f_auto,q_auto/blog/java/java-cover.png
---

# Transforming Media With Java

Transforming media on Cloudinary in Java is fun because of the fluent interface. Cloudinary transformations are used to produce optimized media as well as aesthetic effects.



## Fluent Interface

In 2005, Martin Fowler created an API design pattern called the [fluent interface, which makes use of ](https://martinfowler.com/bliki/FluentInterface.html)method chaining and a domain-specific language and[ which has been adopted ](https://martinfowler.com/bliki/FluentInterface.html)in many popular APIs, such as .NET’s SQL API [Linq. ](https://docs.microsoft.com/en-us/dotnet/standard/linq/)With Linq, .NET programmers can write code that generates SQL.

Since then, Fowler has introduced many [design patterns](https://martinfowler.com/eaaCatalog/) for object-oriented programming that have been embraced by OO developers for building APIs. 


### Method Chaining

Method chaining refers to a method that returns an instance of the object that encapsulates it,  as in this example:


```
class Hello {
	private string name;
	Hello()
	{
		System.out.println("Calling The Constructor");
	}
	public Hello setName(String name)
	{
		this.name = name;
		return this;
	}
	void say()
	{
		System.out.println("Hello " + name);
	}
}

public class Hello {
	public static void main(String[] args)
	{
		// "method chaining".
		new Hello().setName("Martin").say();
	}
}
```


**Fun fact:**  The popular jQuery library, which leverages method chaining, was developed around the time Martin Fowler launched the fluent interface.  


### Domain-Specific Language

Since the fluent interface comprises both object chaining and a domain-specific language, the code flows and is easy to read.  

### Fluent Interface for Transformations

We're going to see here how to use the Cloudinary Java SDK's fluent interface to code fun and useful transformations.  


## Cropping of Media

### Scale 

Cloudinary performs simple cropping transformations with a fluent interface. An example is to apply a single dimension to an image or video and then scale the media item to maintain the aspect ratio, like this:


```
new Transformation().width(300).crop("scale")
```


Here’s an example of single-dimension scaling:

[https://res.cloudinary.com/cloudinary-training/image/upload/w_300,c_scale/dog.jpg](https://res.cloudinary.com/cloudinary-training/image/upload/w_300,c_scale/dog.jpg)


<table>
  <tr>
   <td>Original: 2519 x 2501
   </td>
   <td>Scaled: 300 x 298
   </td>
  </tr>
  <tr>
   <td>




<img src="https://res.cloudinary.com/cloudinary-training/image/upload/f_auto,q_auto/blog/java/dog1.jpg" alt="dog original photo" title="dog original photo">

   </td>
   <td>




<img src="https://res.cloudinary.com/cloudinary-training/image/upload/f_auto,q_auto/blog/java/dog2.jpg"  alt="dog scaled photo" title="dog scaled photo">

   </td>
  </tr>
</table>


The transformed image adjusts the unspecified dimension (in this case, the height) so that the aspect ratio stays the same.

### Fit

To create a media item, say, an image, with a certain width and height, specify them and maintain the aspect ratio with the ``crop`` type ``fit``. Cloudinary then changes the dimensions and maintains aspect ration, but the image remains within the bounding box based on the two specified dimensions. See the example below.
dim

```
new Transformation().width(300).height(200).crop("fit")
```

<table>
  <tr>
   <td>

Fit: 201 x 200
   </td>
  </tr>
  <tr>
   <td>



<img src="https://res.cloudinary.com/cloudinary-training/image/upload/f_auto,q_auto/blog/java/dog3.jpg"  alt="resize with fit " title="resize with fit">

   </td>
  </tr>
</table>


The transformed image might not have the exact dimensions as specified but will not exceed them.

### Pad

You can set exact dimensions with the ``crop`` type ``pad``. To maintain the aspect ratio, ``pad`` scales the image to fit within the specified dimensions and creates a padding for any dimension that must be made smaller than the value specified. The color of the pad will default to white. For example:


```
new Transformation().width(300).height(200).crop("pad")
```

<table>
  <tr>
   <td>

Pad: 300 x 300
   </td>
  </tr>
  <tr>
   <td>



<img src="https://res.cloudinary.com/cloudinary-training/image/upload/f_auto,q_auto/blog/java/dog4.jpg"  alt="resize with pad" title="resize with pad">

   </td>
  </tr>
</table>


To change the color of the padding, just chain the ``background`` parameter to the code:


```
new Transformation().width(300).height(200).crop("pad").background("red")
```

<table>
  <tr>
   <td>
Red Padding: 300 x 200
   </td>
  </tr>
  <tr>
   <td>



<img src="https://res.cloudinary.com/cloudinary-training/image/upload/f_auto,q_auto/blog/java/dog5.jpg"  alt="resize with red padding" title="resize with red padding">

   </td>
  </tr>
</table>


To have Cloudinary determine the most used color in the media item and then apply that color to the padding, set ``background`` to ``auto``:


```
new Transformation().width(300).height(200).crop("pad").background("auto")
```


<table>
  <tr>
   <td>

Auto Padding: 300 x 200
   </td>
  </tr>
  <tr>
   <td>




<img src="https://res.cloudinary.com/cloudinary-training/image/upload/f_auto,q_auto/blog/java/dog6.jpg"  alt="resize with auto pad" title="resize with auto pad">

   </td>
  </tr>
</table>

There are many more cropping transformations and you can learn about them [online](https://cloudinary.com/documentation/transformation_reference).


## Fun With Transformations

Once you have uploaded your media to Cloudinary, you can write Java code to create URLs, image tags, and video tags that contain transformation parameters. Below are a few fun video examples in which we create URLs with transformations.


### Boomerang Effect With Video

Consider a time-lapse video of an hourglass. How would the hourglass run if time moves backwards? To find out, apply the **boomerang** effect on the video as a transformation, like this:


```
cloudinary.url().transformation(new Transformation().effect("boomerang"))
.resourceType("video").generate("purple-hourglass.mp4")
```


Click this link to play the video in your browser:

[https://res.cloudinary.com/cloudinary-training/video/upload/e_boomerang/purple-hourglass.mp4](https://res.cloudinary.com/cloudinary-training/video/upload/e_boomerang/purple-hourglass.mp4)




![boomerang effect](https://res.cloudinary.com/cloudinary-training/image/upload/f_auto,q_auto/blog/java/hourglass.jpg "boomerang effect")



### Concatenation of Videos

To play two videos in sequence, try this concatenation technique:


```
cloudinary.url().resourceType("video")
.transformation(new Transformation().width(300).height(200).crop("filt").chain()
.overlay(
new Layer().publicId("video:purple-hourglass"))
.flags("splice").width(300).height(200).crop("fit"))
.generate("3-o-clock.mp4")
```


Click this link to play the concatenated video in your browser:

[https://res.cloudinary.com/cloudinary-training/video/upload/c_fit,h_200,w_300/c_fit,fl_splice,h_200,l_video:purple-hourglass,w_300/3-o-clock.mp4](https://res.cloudinary.com/cloudinary-training/video/upload/c_fit,h_200,w_300/c_fit,fl_splice,h_200,l_video:purple-hourglass,w_300/3-o-clock.mp4)






![video concatenation](https://res.cloudinary.com/cloudinary-training/image/upload/f_auto,q_auto/blog/java/clock.jpg "video concatenation")



### Progress Indicator

How about creating a visual indicator that shows how much time is left in your video? Simply add a progress bar, like this:


```
cloudinary.url().transformation(new Transformation().effect("progressbar:frame:FF0000:12"))						.resourceType("video").generate("purple-hourglass.mp4")
```


Click this link to play the video with a progress bar:

[https://res.cloudinary.com/cloudinary-training/video/upload/e_progressbar:frame:FF0000:12/purple-hourglass.mp4](https://res.cloudinary.com/cloudinary-training/video/upload/e_progressbar:frame:FF0000:12/purple-hourglass.mp4)





![progress bar](https://res.cloudinary.com/cloudinary-training/image/upload/f_auto,q_auto/blog/java/progress-bar-video.jpg "progress bar")



## Summary

The design of the Cloudinary Java SDK makes it easy to code in Java. As you learn more about the many ways in which you can transform media with Cloudinary, you’ll become more creative and fluent with the process. Have fun!


## Credits



*   Hourglass-timer video by [Samer Daboul](https://www.pexels.com/@samerdaboul?utm_content=attributionCopyText&utm_medium=referral&utm_source=pexels) from [Pexels](https://www.pexels.com/photo/close-up-view-of-a-sand-timer-1196530/?utm_content=attributionCopyText&utm_medium=referral&utm_source=pexels)
*   Clock video by [Jason H. Austin](https://www.pexels.com/@jason-h-austin-544053?utm_content=attributionCopyText&utm_medium=referral&utm_source=pexels) from [Pexels](https://www.pexels.com/photo/round-wall-clock-1509518/?utm_content=attributionCopyText&utm_medium=referral&utm_source=pexels)
*   [Cloudinary Media Inspector Extension](https://chrome.google.com/webstore/detail/cloudinary-media-inspecto/ehnkhkglbafecknplfmjklnnjimokpkg) for insight on images