Various characteristics of images are modified to enhance their appearance. Contrast, brightness, and gamma are among the most commonly used attributes to improve the quality of the images. In various cases, you have to control these attributes programmatically from within your Java applications. To achieve that, in this article, you will learn how to adjust the contrast, brightness, and gamma of an image in Java.
- Java Image Editing API
- Adjust Contrast of an Image in Java
- Adjust Brightness of an Image in Java
- Modify Gamma of an Image in Java
Java API to Adjust Image Contrast, Brightness, and Gamma
To control the contrast, brightness, and gamma of images, we will use Aspose.Imaging for Java. It is a powerful image processing API that supports a wide range of image formats. Using the API, you can edit images seamlessly without writing complex code. You can download the API’s JAR and add its reference to your project. Also, you can install it into your Maven-based applications by adding the following configurations in pom.xml.
Repository:
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>http://repository.aspose.com/repo/</url>
</repository>
Dependency:
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-imaging-java</artifactId>
<version>21.12</version>
<classifier>jdk16</classifier>
</dependency>
Adjust Contrast of an Image in Java
The difference in pixel intensity of an image is known as contrast. The contrast value is increased or decreased to control the visibility of objects in an image. Increasing the contrast at a certain level makes the image look more clear. Whereas, decreasing the contrast value makes the objects less distinguishable.
The following are the steps to adjust the contrast of an image in Java.
- First, use RasterImage class to load the image.
- Then, use RasterImage.cacheData() method to cache image if it isn’t already.
- Use RasterImage.adjustContrast(float) method to set contrast value in range [-100, 100].
- Finally, save the image using RasterImage.save(string) method.
The following code sample shows how to adjust the contrast of an image in Java.
The following is the comparison of input and output images after increasing the contrast value.
Adjust Brightness of an Image in Java
The brightness is adjusted to increase or decrease the darkness or lightness of an image. Let’s have a look at how to modify the brightness of an image in Java.
- First, use RasterImage class to load the image.
- Then, use RasterImage.cacheData() method to cache image if it isn’t already.
- Adjust brightness of the image using RasterImage.adjustBrightness(int) method.
- Finally, save the image using RasterImage.save(string) method.
The following code sample shows how to adjust the brightness of an image in Java.
The following is the comparison of input and output images after modifying brightness.
Adjust Gamma of an Image in Java
The gamma attribute controls the ratio of red, green, and blue colors in an image. Also, it affects the brightness of the image. The following are the steps to adjust the gamma of an image in Java.
- First, use RasterImage class to load the image.
- Then, use RasterImage.cacheData() method to cache image if it isn’t already.
- To change gamma of the image, use RasterImage.adjustGamma(float, float, float) method.
- Finally, save the image using RasterImage.save(string) method.
The following code sample shows how to adjust the gamma of an image in Java.
The following is the comparison of input and output images after modifying the gamma value.
Get a Free API License
You can get a free temporary license to use Aspose.Imaging for Java without evaluation limitations.
Conclusion
In this article, you have learned how to adjust the contrast, brightness, and gamma of images in Java. Furthermore, the code samples have demonstrated how modifying the values of contrast, brightness, and gamma affects an image. To explore more about Java image processing API, visit documentation. Also, you can download the source code samples of the API from GitHub. In case of any queries, contact us at our forum.