All Products
Search
Document Center

Watermark

Last Updated: Jul 11, 2019

Initialize a client

Initialize a client before using the SDK. For more information, see initVodClient.

Add a watermark

For more information about the request and response parameters, see AddWatermark.

  • For more information about how to obtain the upload URL and credential for a file, see CreateUploadAttachedMedia.
  • For more information about how to upload a watermark file to Object Storage Service (OSS), see OSS upload.
import com.aliyuncs.vod.model.v20170321.AddWatermarkRequest;
import com.aliyuncs.vod.model.v20170321.AddWatermarkResponse;


/**
 * Method for adding the watermark configuration
 */
public static AddWatermarkResponse addWatermark(DefaultAcsClient client) throws Exception {
    AddWatermarkRequest request = new AddWatermarkRequest();
    // Sets the watermark name.
    request.setName("addwatermark");
    // Obtains the OSS URL of the watermark file.
    String fileUrl = "http://test-bucket.oss-cn-shanghai.aliyuncs.com/watermark/test.png";
    // The OSS URL must be specified for an image watermark file. This watermark file must be stored in the same region as the relevant video. For example, if a video is stored in China (Shanghai), its watermark file must also be stored in China (Shanghai).
    request.setFileUrl(fileUrl);
    // Specifies the watermark configuration.
    JSONObject watermarkConfig = null;
    // Specifies the display position configuration of an image watermark.
    watermarkConfig = buildImageWatermarkConfig();

    // Specifies the display position configuration of a text watermark.
    //watermarkConfig = buildTextWatermarkConfig();
    request.setWatermarkConfig(watermarkConfig.toJSONString());

    // Sets the watermark type. Enter Text for a text watermark or Image for an image watermark.
    request.setType("Image");
    return client.getAcsResponse(request);
}


/**
 * Call example
 * @param args
 * @throws ClientException
 */
public static void main(String[] args) throws ClientException {
    DefaultAcsClient client = initVodClient("<Your AccessKey ID>", "<Your AccessKey Secret>");
    AddWatermarkResponse response = new AddWatermarkResponse();
    try {
        // Adds watermark information and obtains a response.
        response = addWatermark(client);
        // The watermark ID.
        System.out.println("WatermarkId = " + response.getWatermarkInfo().getWatermarkId());
        // The display position and effect configuration of the watermark.
        System.out.println("WatermarkConfig = " + response.getWatermarkInfo().getWatermarkConfig());
        // The URL of the watermark file. A text watermark has no file URL.
        System.out.println("FileUrl = " + response.getWatermarkInfo().getFileUrl());
    } catch (Exception e) {
        System.out.println("ErrorMessage = " + e.getLocalizedMessage());
    }
    System.out.println("RequestId = " + response.getRequestId());
}

/**
 * Method for constructing the configuration of an image watermark. Modify parameters as required.
 * @return
 */
public static JSONObject buildImageWatermarkConfig() {
    JSONObject watermarkConfig = new JSONObject();
    // The lateral offset of the watermark.
    watermarkConfig.put("Dx", "8");
    // The longitudinal offset of the watermark.
    watermarkConfig.put("Dy", "8");
    // The width of the watermark.
    watermarkConfig.put("Width", "55");
    // The height of the watermark.
    watermarkConfig.put("Height", "55");
    // The relative display position of the watermark, such as the top-left, top-right, bottom-left, or bottom-right corner.
    watermarkConfig.put("ReferPos", "BottomRight");
    // The timeline of watermark display, including the start time and end time.
    JSONObject timeline = new JSONObject();
    // The start time for watermark display.
    timeline.put("Start", "2");
    // The end time for watermark display.
    timeline.put("Duration", "ToEND");
    watermarkConfig.put("Timeline", timeline);
    return watermarkConfig;
}

/**
 * Method for constructing the configuration of a text watermark. Modify parameters as required.
 * @return
 */
public static JSONObject buildTextWatermarkConfig() {
    JSONObject watermarkConfig = new JSONObject();
    // The content of the text watermark.
    watermarkConfig.put("Content", "testwatermark");
    // The font of the text watermark.
    watermarkConfig.put("FontName", "SimSun");
    // The font size of the text watermark.
    watermarkConfig.put("FontSize", 25);
    // The color or RGB value (such as #000000) of the text watermark.
    watermarkConfig.put("FontColor", "Black");
    // The transparency of the text watermark.
    watermarkConfig.put("FontAlpha", "0.2");
    // The color or RGB value (such as #ffffff) of the font outline for the text watermark.
    watermarkConfig.put("BorderColor", "White");
    // The outline width of the text watermark.
    watermarkConfig.put("BorderWidth", 1);
    // The offset of the text watermark to the top of the video image.
    watermarkConfig.put("Top", 20);
    // The offset of the text watermark to the left of the video image.
    watermarkConfig.put("Left", 15);
    return watermarkConfig;
}

Modify a watermark

For more information about the request and response parameters, see UpdateWatermark.

import com.aliyuncs.vod.model.v20170321.UpdateWatermarkRequest;
import com.aliyuncs.vod.model.v20170321.UpdateWatermarkResponse;

/**
 * Method for modifying the watermark configuration
 * Note: The URL of an image watermark file cannot be directly modified. If you want to modify the URL, you need to create watermark information again and set the new URL in the information.
 */
public static UpdateWatermarkResponse updateWatermark(DefaultAcsClient client) throws Exception {
    UpdateWatermarkRequest request = new UpdateWatermarkRequest();
    request.setName("updatewatermark");
    // Specifies the ID of the watermark whose configuration needs to be updated.
    request.setWatermarkId("421ddddd4f6e734a526fd2e442");
    // Specifies the watermark configuration.
    JSONObject watermarkConfig = null;
    // Specifies the display position configuration of an image watermark.
    //watermarkConfig = buildImageWatermarkConfig();
    // Specifies the display position configuration of a text watermark.
    watermarkConfig = buildTextWatermarkConfig();
    request.setWatermarkConfig(watermarkConfig.toJSONString());
    return client.getAcsResponse(request);
}

/**
 * Call example
 */
public static void main(String[] args) {
    DefaultAcsClient client = initVodClient("<Your AccessKey ID>", "<Your AccessKey Secret>");
    UpdateWatermarkResponse response = new UpdateWatermarkResponse();
    try {
        response = updateWatermark(client);
        // The watermark ID.
        System.out.println("WatermarkId = " + response.getWatermarkInfo().getWatermarkId());
        // The display position and effect configuration of the watermark.
        System.out.println("WatermarkConfig = " + response.getWatermarkInfo().getWatermarkConfig());
        // The URL of the watermark file. A text watermark has no file URL.
        System.out.println("FileUrl = " + response.getWatermarkInfo().getFileUrl());
    } catch (Exception e) {
        System.out.println("ErrorMessage = " + e.getLocalizedMessage());
    }
    System.out.println("RequestId = " + response.getRequestId());
}

/**
 * Method for constructing the configuration of an image watermark. Modify parameters as required.
 * @return
 */
public static JSONObject buildImageWatermarkConfig() {
    JSONObject watermarkConfig = new JSONObject();
    // The lateral offset of the watermark.
    watermarkConfig.put("Dx", "10");
    // The longitudinal offset of the watermark.
    watermarkConfig.put("Dy", "10");
    // The width of the watermark.
    watermarkConfig.put("Width", "66");
    // The height of the watermark.
    watermarkConfig.put("Height", "66");
    // The relative display position of the watermark, such as the top-left, top-right, bottom-left, or bottom-right corner.
    watermarkConfig.put("ReferPos", "BottomLeft");
    // The timeline of watermark display, including the start time and end time.
    JSONObject timeline = new JSONObject();
    // The start time for watermark display.
    timeline.put("Start", "2");
    // The end time for watermark display.
    timeline.put("Duration", "ToEND");
    watermarkConfig.put("Timeline", timeline);
    return watermarkConfig;
}

/**
 * Method for constructing the configuration of a text watermark. Modify parameters as required.
 * @return
 */
public static JSONObject buildTextWatermarkConfig() {
    JSONObject watermarkConfig = new JSONObject();
    // The content of the text watermark.
    watermarkConfig.put("Content", "testwatermark");
    // The font of the text watermark.
    watermarkConfig.put("FontName", "SimSun");
    // The font size of the text watermark.
    watermarkConfig.put("FontSize", 40);
    // The color or RGB value (such as #000000) of the text watermark.
    watermarkConfig.put("FontColor", "Black");
    // The transparency of the text watermark.
    watermarkConfig.put("FontAlpha", "0.2");
    // The color or RGB value (such as #ffffff) of the font outline for the text watermark.
    watermarkConfig.put("BorderColor", "White");
    // The outline width of the text watermark.
    watermarkConfig.put("BorderWidth", 2);
    // The offset of the text watermark to the top of the video image.
    watermarkConfig.put("Top", 20);
    // The offset of the text watermark to the left of the video image.
    watermarkConfig.put("Left", 15);
    return watermarkConfig;
}

Delete a watermark

For more information about the request and response parameters, see DeleteWatermark.

import com.aliyuncs.vod.model.v20170321.DeleteWatermarkRequest;
import com.aliyuncs.vod.model.v20170321.DeleteWatermarkResponse;

/**
 * Method for deleting the watermark configuration
 */
public static DeleteWatermarkResponse deleteWatermark(DefaultAcsClient client) throws Exception {
    DeleteWatermarkRequest request = new DeleteWatermarkRequest();
    request.setWatermarkId("53f9d796fad9d7b862b2e5e5b");
    return client.getAcsResponse(request);
}

/**
 * Call example
 */
public static void main(String[] args) {
    DefaultAcsClient client = initVodClient("<Your AccessKey ID>", "<Your AccessKey Secret>");
    DeleteWatermarkResponse response = new DeleteWatermarkResponse();
    try {
        response = deleteWatermark(client);
    } catch (Exception e) {
        System.out.println("ErrorMessage = " + e.getLocalizedMessage());
    }
    System.out.println("RequestId = " + response.getRequestId());
}

Query a list of watermarks

For more information about the request and response parameters, see ListWatermark.

import com.aliyuncs.vod.model.v20170321.ListWatermarkRequest;
import com.aliyuncs.vod.model.v20170321.ListWatermarkResponse;

/**
 * Method for querying the configuration of watermarks
 */
public static ListWatermarkResponse listWatermark(DefaultAcsClient client) throws Exception {
    ListWatermarkRequest request = new ListWatermarkRequest();
    return client.getAcsResponse(request);
}

/**
 * Call example
 */
public static void main(String[] args) {
    DefaultAcsClient client = initVodClient("<Your AccessKey ID>", "<Your AccessKey Secret>");
    ListWatermarkResponse response = new ListWatermarkResponse();
    try {
        response = listWatermark(client);
        for (ListWatermarkResponse.WatermarkInfo watermarkInfo : response.getWatermarkInfos()) {
            // The watermark ID.
            System.out.println("WatermarkId = " + watermarkInfo.getWatermarkId());
            // The display position and effect configuration of the watermark.
            System.out.println("WatermarkConfig = " + watermarkInfo.getWatermarkConfig());
            // The URL of the watermark file. A text watermark has no file URL.
            System.out.println("FileUrl = " + watermarkInfo.getFileUrl());
        }
    } catch (Exception e) {
        System.out.println("ErrorMessage = " + e.getLocalizedMessage());
    }
    System.out.println("RequestId = " + response.getRequestId());
}

Query a watermark

For more information about the request and response parameters, see GetWatermark.

import com.aliyuncs.vod.model.v20170321.GetWatermarkRequest;
import com.aliyuncs.vod.model.v20170321.GetWatermarkResponse;

/**
 * Method for querying the configuration of a watermark
 */
public static GetWatermarkResponse getWatermark(DefaultAcsClient client) throws Exception {
    GetWatermarkRequest request = new GetWatermarkRequest();
    // Specifies the ID of the watermark to be queried.
    request.setWatermarkId("96d4f6e734a526fd2e442");
    return client.getAcsResponse(request);
}

/**
 * Call example
 */
public static void main(String[] args) {
    DefaultAcsClient client = initVodClient("<Your AccessKey ID>", "<Your AccessKey Secret>");
    GetWatermarkResponse response = new GetWatermarkResponse();
    try {
        response = getWatermark(client);
        // The watermark ID.
        System.out.println("WatermarkId = " + response.getWatermarkInfo().getWatermarkId());
        // The display position and effect configuration of the watermark.
        System.out.println("WatermarkConfig = " + response.getWatermarkInfo().getWatermarkConfig());
        // The URL of the watermark file. A text watermark has no file URL.
        System.out.println("FileUrl = " + response.getWatermarkInfo().getFileUrl());
    } catch (Exception e) {
        System.out.println("ErrorMessage = " + e.getLocalizedMessage());
    }
    System.out.println("RequestId = " + response.getRequestId());
}

Set the default watermark

For more information about the request and response parameters, see SetDefaultWatermark.

import com.aliyuncs.vod.model.v20170321.SetDefaultWatermarkRequest;
import com.aliyuncs.vod.model.v20170321.SetDefaultWatermarkResponse;

/**
 * Method for setting the default watermark configuration
 */
public static SetDefaultWatermarkResponse setDefaultWatermark(DefaultAcsClient client) throws Exception {
    SetDefaultWatermarkRequest request = new SetDefaultWatermarkRequest();
    // Sets the ID of the default watermark.
    request.setWatermarkId("82105a29c6e96d4f6e734a");
    return client.getAcsResponse(request);
}

/**
 * Call example
 */
public static void main(String[] args) {
    DefaultAcsClient client = initVodClient("<Your AccessKey ID>", "<Your AccessKey Secret>");
    SetDefaultWatermarkResponse response = new SetDefaultWatermarkResponse();
    try {
        response = setDefaultWatermark(client);
    } catch (Exception e) {
        System.out.println("ErrorMessage = " + e.getLocalizedMessage());
    }
    System.out.println("RequestId = " + response.getRequestId());
}