|
5 | 5 |
|
6 | 6 | import java.io.File;
|
7 | 7 |
|
| 8 | +/** |
| 9 | + * 基础图片处理使用Demo https://cloud.tencent.com/document/product/460/36540 |
| 10 | + * Demo均为下载时处理,如需上传时处理或云上处理则使用对应方法,图片处理规则相同。 |
| 11 | + * 此处为演示参数使用,完整处理规则请参考API文档 |
| 12 | + */ |
8 | 13 | public class BasicImageProcessing {
|
9 |
| - public static void imageZoomDemo(COSClient cosClient) { |
| 14 | + |
| 15 | + public static void main(String[] args) throws Exception { |
| 16 | + COSClient cosClient = ClientUtils.getTestClient(); |
| 17 | + imageRotateDemo(cosClient); |
| 18 | + cosClient.shutdown(); |
| 19 | + } |
| 20 | + |
| 21 | + public static void imageProcessing(COSClient cosClient, String rule) { |
| 22 | + //图片所在bucket名称 |
10 | 23 | String bucketName = "examplebucket-1250000000";
|
11 |
| - String key = "qrcode.png"; |
| 24 | + //图片在bucket中的相对位置,比如根目录下file文件夹中的demo.png路径为file/demo.png |
| 25 | + String key = "image.png"; |
12 | 26 | GetObjectRequest getObj = new GetObjectRequest(bucketName, key);
|
13 |
| - // 宽高缩放50% |
14 |
| - String rule = "imageMogr2/thumbnail/!50p"; |
15 | 27 | getObj.putCustomQueryParameter(rule, null);
|
16 |
| - cosClient.getObject(getObj, new File("qrcode-50p.png")); |
| 28 | + cosClient.getObject(getObj, new File("demo.png")); |
17 | 29 | }
|
18 | 30 |
|
| 31 | + /** |
| 32 | + * 缩放图片宽高为原图50% |
| 33 | + * https://cloud.tencent.com/document/product/460/36540 |
| 34 | + */ |
| 35 | + public static void imageZoomDemo(COSClient cosClient) { |
| 36 | + String rule = "imageMogr2/thumbnail/!50p"; |
| 37 | + imageProcessing(cosClient, rule); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * 内切圆裁剪功能,radius是内切圆的半径150 |
| 42 | + * https://cloud.tencent.com/document/product/460/36541 |
| 43 | + */ |
19 | 44 | public static void imageCroppingDemo(COSClient cosClient) {
|
20 |
| - String bucketName = "examplebucket-1250000000"; |
21 |
| - String key = "qrcode.png"; |
22 |
| - GetObjectRequest getObj = new GetObjectRequest(bucketName, key); |
23 |
| - // 宽高缩放50% |
24 | 45 | String rule = "imageMogr2/iradius/150";
|
25 |
| - getObj.putCustomQueryParameter(rule, null); |
26 |
| - cosClient.getObject(getObj, new File("qrcode-cropping.png")); |
| 46 | + imageProcessing(cosClient, rule); |
27 | 47 | }
|
28 | 48 |
|
| 49 | + /** |
| 50 | + * 图片顺时针旋转角度90 取值范围0 - 360 |
| 51 | + * https://cloud.tencent.com/document/product/460/36542 |
| 52 | + */ |
29 | 53 | public static void imageRotateDemo(COSClient cosClient) {
|
30 |
| - String bucketName = "examplebucket-1250000000"; |
31 |
| - String key = "qrcode.png"; |
32 |
| - GetObjectRequest getObj = new GetObjectRequest(bucketName, key); |
33 |
| - // 宽高缩放50% |
34 | 54 | String rule = "imageMogr2/rotate/90";
|
35 |
| - getObj.putCustomQueryParameter(rule, null); |
36 |
| - cosClient.getObject(getObj, new File("qrcode-rotate.png")); |
| 55 | + imageProcessing(cosClient, rule); |
37 | 56 | }
|
38 |
| - public static void main(String[] args) throws Exception { |
39 |
| - COSClient cosClient = ClientUtils.getTestClient(); |
40 |
| - imageRotateDemo(cosClient); |
41 |
| - cosClient.shutdown(); |
| 57 | + |
| 58 | + /** |
| 59 | + * 将图片转换为 png 格式 |
| 60 | + * https://cloud.tencent.com/document/product/460/36543 |
| 61 | + */ |
| 62 | + public static void imageConvertingFormatDemo(COSClient cosClient) { |
| 63 | + String rule = "imageMogr2/format/png"; |
| 64 | + imageProcessing(cosClient, rule); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * 设置图片的绝对质量为60 |
| 69 | + * https://cloud.tencent.com/document/product/460/36544 |
| 70 | + */ |
| 71 | + public static void imageQualityChangeDemo(COSClient cosClient) { |
| 72 | + String rule = "imageMogr2/quality/60"; |
| 73 | + imageProcessing(cosClient, rule); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * 模糊半径取8,sigma 值取5,进行高斯模糊处理 |
| 78 | + * https://cloud.tencent.com/document/product/460/36545 |
| 79 | + */ |
| 80 | + public static void imageGaussianBlurringDemo(COSClient cosClient) { |
| 81 | + String rule = "imageMogr2/blur/8x5"; |
| 82 | + imageProcessing(cosClient, rule); |
42 | 83 | }
|
| 84 | + |
| 85 | + /** |
| 86 | + * 将图片亮度提高70 |
| 87 | + * https://cloud.tencent.com/document/product/460/51808 |
| 88 | + */ |
| 89 | + public static void imageAdjustingBrightnessDemo(COSClient cosClient) { |
| 90 | + String rule = "imageMogr2/bright/70"; |
| 91 | + imageProcessing(cosClient, rule); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * 将图片对比度降低50 |
| 96 | + * https://cloud.tencent.com/document/product/460/51809 |
| 97 | + */ |
| 98 | + public static void imageAdjustingContrastDemo(COSClient cosClient) { |
| 99 | + String rule = "imageMogr2/contrast/-50"; |
| 100 | + imageProcessing(cosClient, rule); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * 设置锐化参数为70 |
| 105 | + * https://cloud.tencent.com/document/product/460/51809 |
| 106 | + */ |
| 107 | + public static void imageSharpeningDemo(COSClient cosClient) { |
| 108 | + String rule = "imageMogr2/sharpen/70"; |
| 109 | + imageProcessing(cosClient, rule); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * 设图片变为灰度图 |
| 114 | + * https://cloud.tencent.com/document/product/460/66519 |
| 115 | + */ |
| 116 | + public static void grayscaleImageDemo(COSClient cosClient) { |
| 117 | + String rule = "imageMogr2/grayscale/1"; |
| 118 | + imageProcessing(cosClient, rule); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * 设置水印图片,并指定水印位置 |
| 123 | + * https://cloud.tencent.com/document/product/460/51809 |
| 124 | + */ |
| 125 | + public static void imageWatermarkingDemo(COSClient cosClient) { |
| 126 | + String rule = "watermark/1/image/aHR0cDovL2V4YW1wbGVzLTEyNTEwMDAwMDQucGljc2gubXlxY2xvdWQuY29tL3NodWl5aW4uanBn/gravity/southeast"; |
| 127 | + imageProcessing(cosClient, rule); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * 设置水印文字,并指定水印位置 |
| 132 | + * https://cloud.tencent.com/document/product/460/6951 |
| 133 | + */ |
| 134 | + public static void textWatermarkingDemo(COSClient cosClient) { |
| 135 | + String rule = "watermark/2/text/6IW-6K6v5LqRwrfkuIfosaHkvJjlm74/fill/IzNEM0QzRA/fontsize/20/dissolve/50/gravity/northeast/dx/20/dy/20/batch/1/degree/45"; |
| 136 | + imageProcessing(cosClient, rule); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * 去除图片元信息 |
| 141 | + * https://cloud.tencent.com/document/product/460/36547 |
| 142 | + */ |
| 143 | + public static void removingImageMetadata(COSClient cosClient) { |
| 144 | + String rule = "imageMogr2/strip"; |
| 145 | + imageProcessing(cosClient, rule); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * 选用样式1,并限定缩略图的宽高最小值为400 × 600 绝对质量为85 |
| 150 | + * https://cloud.tencent.com/document/product/460/6929 |
| 151 | + */ |
| 152 | + public static void quickThumbnailTemplate(COSClient cosClient) { |
| 153 | + String rule = "imageView2/1/w/400/h/600/q/85"; |
| 154 | + imageProcessing(cosClient, rule); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * 将 JPG 图片转换为 PNG 格式,并限制图片大小为15KB |
| 159 | + * https://cloud.tencent.com/document/product/460/56732 |
| 160 | + */ |
| 161 | + public static void limitingOutputImageSize(COSClient cosClient) { |
| 162 | + String rule = "imageMogr2/strip/format/png/size-limit/15k!"; |
| 163 | + imageProcessing(cosClient, rule); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * 管道操作符|能够实现对图片按顺序进行多种处理。 |
| 168 | + * 用户可以通过管道操作符将多个处理参数分隔开,从而实现在一次访问中按顺序对图片进行不同处理 |
| 169 | + * https://cloud.tencent.com/document/product/460/15293 |
| 170 | + */ |
| 171 | + public static void pipelineOperatorsDemo(COSClient cosClient) { |
| 172 | + //对缩放后的图片进行文字水印操作 |
| 173 | + String rule = "imageMogr2/thumbnail/!50p|watermark/2/text/5pWw5o2u5LiH6LGh/fill/I0ZGRkZGRg==/fontsize/30/dx/20/dy/20"; |
| 174 | + imageProcessing(cosClient, rule); |
| 175 | + } |
| 176 | + |
| 177 | + |
| 178 | + public static void obtainingImageAverageHueDemo(COSClient cosClient) { |
| 179 | + String rule = "imageMogr2/contrast/-50"; |
| 180 | + imageProcessing(cosClient, rule); |
| 181 | + } |
| 182 | + |
| 183 | + public static void obtainingBasicImageInformationDemo(COSClient cosClient) { |
| 184 | + String rule = "imageMogr2/contrast/-50"; |
| 185 | + imageProcessing(cosClient, rule); |
| 186 | + } |
| 187 | + |
| 188 | + public static void obtainingImageEXIFDemo(COSClient cosClient) { |
| 189 | + String rule = "imageMogr2/contrast/-50"; |
| 190 | + imageProcessing(cosClient, rule); |
| 191 | + } |
| 192 | + |
43 | 193 | }
|
0 commit comments