You need to enable JavaScript to run this app.
导航

基础图片处理(Java SDK)

最近更新时间2024.02.04 18:31:08

首次发布时间2023.10.12 11:15:42

TOS 支持对存储的图片进行处理,包括图片缩放、图片裁剪、图片水印、格式转换等图片处理操作。本文介绍如何通过 TOS Java SDK 进行基础图片处理。

注意事项

  • 原图格式仅支持 JPG、PNG、BMP、GIF、WEBP 和 TIFF。
  • 原图大小不能超过 20MB。
  • 原图宽、高不能超过 30000 px,总像素不能超过 2.5 亿 px(旋转操作的原图宽、高不能超过 4096 px)。
  • 缩放后的图片宽、高不能超过 16384 px,总像素不能超过 16777216 px。

示例代码

以下代码展示如何将图片高度固定为 100px,图片格式转换为 JPG 格式,然后将图片下载到本地。

import com.volcengine.tos.TOSV2;
import com.volcengine.tos.TOSV2ClientBuilder;
import com.volcengine.tos.TosClientException;
import com.volcengine.tos.TosServerException;
import com.volcengine.tos.model.object.GetObjectV2Input;
import com.volcengine.tos.model.object.GetObjectV2Output;

import java.io.FileOutputStream;
import java.io.IOException;

public class ImageProcessExample {
    public static void main(String[] args) {
        String endpoint = "your endpoint";
        String region = "your region";
        String accessKey = System.getenv("TOS_ACCESS_KEY");
        String secretKey = System.getenv("TOS_SECRET_KEY"); 

        String bucketName = "bucket-example";
        // 需要确保下载的数据已存在
        String objectKey = "image.png";
        String filePath = "example_dir/image.jpg";
        String style = "image/resize,h_100/format,jpg"; //将图片高度固定为 100px,并转为 JPG 格式

        TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);

        try {
            GetObjectV2Input input = new GetObjectV2Input().setBucket(bucketName).setKey(objectKey).setProcess(style);
            // 以下代码展示如何将转码后的图片数据下载到本地。
            try (FileOutputStream fileOutputStream = new FileOutputStream(filePath);
                 GetObjectV2Output output = tos.getObject(input)) {
                byte[] buffer = new byte[1024];
                int length;
                while ((length = output.getContent().read(buffer)) != -1) {
                    fileOutputStream.write(buffer, 0, length);
                }
                System.out.println("image process succeed, object's metadata is " + output.getGetObjectBasicOutput());
            } catch (IOException e) {
                System.out.println("write data to file failed");
                e.printStackTrace();
            }
        } catch (TosClientException e) {
            // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送。
            System.out.println("image process failed");
            System.out.println("Message: " + e.getMessage());
            if (e.getCause() != null) {
                e.getCause().printStackTrace();
            }
        } catch (TosServerException e) {
            // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息。
            System.out.println("image process failed");
            System.out.println("StatusCode: " + e.getStatusCode());
            System.out.println("Code: " + e.getCode());
            System.out.println("Message: " + e.getMessage());
            System.out.println("RequestID: " + e.getRequestID());
        } catch (Throwable t) {
            // 作为兜底捕获其他异常,一般不会执行到这里。
            System.out.println("image process failed");
            System.out.println("unexpected exception, message: " + t.getMessage());
        }
    }
}

相关文档

关于图片处理的详细介绍,请参见图片处理概述