Java quick start

This quickstart is designed to give you a quick taste of a few common Cloudinary features. It does not necessarily employ coding best practices, nor is the code you create here intended for production.
You can perform this quickstart in a code sandbox or a clean project in the development environment of your choice.
You can also view the full code for this quickstart in the GitHub repo.


Tip: If you're new to Cloudinary, you might want to take a look at the Getting Started Developer Guide first for a high-level overview of integrating Cloudinari into your code, as well as an introduction to the main concepts.
You may also find our glossary helpful in understanding Cloudinary-specific terms.


Prerequisites
To perform the steps in this quickstart, you need:

  • Cloudinary Programmable Media Account. If you haven't already, you can sign up quickly and for free.
  • your account credentials. You can find your credentials in the Dashboard page of the Cloudinary console.
  • A working Java environment that supports the Java version.
  • A clean java project using Maven.

1. Set up and configure the library
Add cloudinary and dotenv dependencies to the list of dependencies in pom.xml:

<dependency>
    <groupId>com.cloudinary</groupId>
    <artifactId>cloudinary-http44</artifactId>
    <version>1.32.2</version>
</dependency>
<dependency>
    <groupId>com.cloudinary</groupId>
    <artifactId>cloudinary-taglib</artifactId>
    <version>1.32.2</version>
</dependency>
<dependency>
    <groupId>io.github.cdimascio</groupId>
    <artifactId>dotenv-java</artifactId>
    <version>2.2.4</version>
</dependency>

Then, in your project, create a file called .env and add the following line to Cloudinary environment variables (replace Cloudinary://<API_KEY> below with your own environment variable value: <API_SECRET>@< CLOUD_NAME>):

// Copy and paste your API environment variable

CLOUDINARY_URL=cloudinary://<API_KEY>:<API_SECRET>@<CLOUD_NAME>

MORE INFORMATION
IMPORTANT: When writing your own applications, follow your organization's storage confidentiality policy and don't reveal API secrets.

In the code, include the following Cloudinary library and dotenv library in the project. Copy and paste the following code into the Main.java file:

// Import the required packages

import com.cloudinary.*;
import com.cloudinary.utils.ObjectUtils;
import io.github.cdimascio.dotenv.Dotenv;

import java.util.Map;

You can now load Cloudinary credentials from the .env file as shown below. Copy and paste the following code into the Main class:

// Set your Cloudinary credentials

Dotenv dotenv = Dotenv.load();
Cloudinary cloudinary = new Cloudinary(dotenv.get("CLOUDINARY_URL"));
cloudinary.config.secure = true;
System.out.println(cloudinary.config.cloudName);

2. Upload the image
Copy and paste the following code into the try block in the Main class:

// Upload the image
Map params1 = ObjectUtils.asMap(
    "use_filename", true,
    "unique_filename", false,
    "overwrite", true
);

System.out.println(
        cloudinary.uploader().upload("https://cloudinary-devs.github.io/cld-docs-assets/assets/images/coffee_cup.jpg", params1));

3. Get the detailed information of the image

// Get the asset details
Map params2 = ObjectUtils.asMap(
        "quality_analysis", true
);

System.out.println(
        cloudinary.api().resource("coffee_cup", params2));

4. Convert uploaded images

// Create the image tag with the transformed image and log it to the console
System.out.println(
        cloudinary.url().transformation(new Transformation()
        .crop("pad")
        .width(300)
        .height(400)
        .background("auto:predominant"))
        .imageTag("coffee_cup"));

// The code above generates an HTML image tag similar to the following:
//  <img src='https://res.cloudinary.com/demo/image/upload/b_auto:predominant,c_pad,h_400,w_300/coffee_cup' height='400' width='300'/>

5. Run the code
Run the code by clicking the Run button on the main class.
You can display images on your website using the returned image tag. Now, copy and paste the URL to view the converted image in your browser:

https://res.cloudinary.com/demo/image/upload/b_auto:predominant,c_pad,h_400,w_300/coffee_cup

Check out the completed code
You can find a complete code example on GitHub.

Guess you like

Origin blog.csdn.net/std7879/article/details/127699782