|
| 1 | +package com.study.module.util.image; |
| 2 | + |
| 3 | +import net.coobird.thumbnailator.Thumbnails; |
| 4 | +import net.coobird.thumbnailator.geometry.Position; |
| 5 | +import net.coobird.thumbnailator.geometry.Positions; |
| 6 | + |
| 7 | +import javax.imageio.ImageIO; |
| 8 | +import java.awt.image.BufferedImage; |
| 9 | +import java.io.File; |
| 10 | +import java.io.FileOutputStream; |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.OutputStream; |
| 13 | +import java.util.UUID; |
| 14 | + |
| 15 | +/** |
| 16 | + * 一个非常好的图片处理类工具:thumbnailator |
| 17 | + * <p>Thumbnailator是一个非常好的图片开源工具,可以很好的完成图片处理。 |
| 18 | + * 从API提供现有的图像文件和图像对象的缩略图中简化了缩略过程,两三行代码就能够从现有图片生成缩略图, |
| 19 | + * 且允许微调缩略图生成,同时保持了需要写入到最低限度的代码量。同时还支持根据一个目录批量生成缩略图。 |
| 20 | + * </p> |
| 21 | + * |
| 22 | + * @author drew |
| 23 | + * @date 2021/3/2 9:36 |
| 24 | + **/ |
| 25 | +public class ThumbnailatorUtil { |
| 26 | + |
| 27 | + public static void main(String[] args) { |
| 28 | + String output = "D:/" + UUID.randomUUID().toString().substring(0, 8).concat(".png"); |
| 29 | + // imgThumb("D:/logo.png", output, 20, 20); |
| 30 | + ImgWatermark("D:/source_0.png", output, 300, 300, Positions.BOTTOM_RIGHT, "D:/logo.png",0.5f, 0.8f); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * 指定大小进行缩放 |
| 35 | + * 若图片横比width小,高比height小,不变 若图片横比width小,高比height大,高缩小到height,图片比例不变 |
| 36 | + * 若图片横比width大,高比height小,横缩小到width,图片比例不变 |
| 37 | + * 若图片横比width大,高比height大,图片按比例缩小,横为width或高为height |
| 38 | + * |
| 39 | + * @param source 输入源 |
| 40 | + * @param output 输出源 |
| 41 | + * @param width 宽 |
| 42 | + * @param height 高 |
| 43 | + */ |
| 44 | + public static void imgThumb(String source, String output, int width, int height) { |
| 45 | + try { |
| 46 | + Thumbnails.of(source).size(width, height).toFile(output); |
| 47 | + } catch (IOException e) { |
| 48 | + e.printStackTrace(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * 指定大小进行缩放 |
| 54 | + * |
| 55 | + * @param source 输入源 |
| 56 | + * @param output 输出源 |
| 57 | + * @param width 宽 |
| 58 | + * @param height 高 |
| 59 | + */ |
| 60 | + public static void imgThumb(File source, String output, int width, int height) { |
| 61 | + try { |
| 62 | + Thumbnails.of(source).size(width, height).toFile(output); |
| 63 | + } catch (IOException e) { |
| 64 | + e.printStackTrace(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * 按照比例进行缩放 |
| 70 | + * |
| 71 | + * @param source 输入源 |
| 72 | + * @param output 输出源 |
| 73 | + * @param scale 缩放比例 |
| 74 | + */ |
| 75 | + public static void imgScale(String source, String output, double scale) { |
| 76 | + try { |
| 77 | + Thumbnails.of(source).scale(scale).toFile(output); |
| 78 | + } catch (IOException e) { |
| 79 | + e.printStackTrace(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public static void imgScale(File source, String output, double scale) { |
| 84 | + try { |
| 85 | + Thumbnails.of(source).scale(scale).toFile(output); |
| 86 | + } catch (IOException e) { |
| 87 | + e.printStackTrace(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * 不按照比例,指定大小进行缩放 |
| 93 | + * |
| 94 | + * @param source 输入源(文件路径,例如:D:/demo.jpg) |
| 95 | + * @param output 输出源(输出文件所在的目录: D:/demo.jpg) |
| 96 | + * @param width 宽 |
| 97 | + * @param height 高 |
| 98 | + * @param keepAspectRatio 默认是按照比例缩放的,值为false 时不按比例缩放 |
| 99 | + */ |
| 100 | + public static void imgNoScale(String source, String output, int width, int height, boolean keepAspectRatio) { |
| 101 | + try { |
| 102 | + Thumbnails.of(source).size(width, height).keepAspectRatio(keepAspectRatio).toFile(output); |
| 103 | + } catch (IOException e) { |
| 104 | + e.printStackTrace(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + public static void imgNoScale(File source, String output, int width, int height, boolean keepAspectRatio) { |
| 109 | + try { |
| 110 | + Thumbnails.of(source).size(width, height).keepAspectRatio(keepAspectRatio).toFile(output); |
| 111 | + } catch (IOException e) { |
| 112 | + e.printStackTrace(); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * 旋转 ,正数:顺时针 负数:逆时针 |
| 118 | + * |
| 119 | + * @param source 输入源 |
| 120 | + * @param output 输出源 |
| 121 | + * @param width 宽 |
| 122 | + * @param height 高 |
| 123 | + * @param rotate 角度 |
| 124 | + */ |
| 125 | + public static void imgRotate(String source, String output, int width, int height, double rotate) { |
| 126 | + try { |
| 127 | + Thumbnails.of(source).size(width, height).rotate(rotate).toFile(output); |
| 128 | + } catch (IOException e) { |
| 129 | + e.printStackTrace(); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + public static void imgRotate(File source, String output, int width, int height, double rotate) { |
| 134 | + try { |
| 135 | + Thumbnails.of(source).size(width, height).rotate(rotate).toFile(output); |
| 136 | + } catch (IOException e) { |
| 137 | + e.printStackTrace(); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * 水印(对图片源文件进行图片水印) |
| 143 | + * |
| 144 | + * @param source 输入源 |
| 145 | + * @param output 输入源 |
| 146 | + * @param width 宽 |
| 147 | + * @param height 高 |
| 148 | + * @param position 水印位置 Positions.BOTTOM_RIGHT o.5f |
| 149 | + * @param watermark 水印图片地址(例如:D:/output.png) |
| 150 | + * @param transparency 透明度 0.5f |
| 151 | + * @param quality 图片质量 0.8f |
| 152 | + */ |
| 153 | + public static void ImgWatermark(String source, String output, int width, int height, Position position, String watermark, float transparency, float quality) { |
| 154 | + try { |
| 155 | + Thumbnails.of(source).size(width, height).watermark(position, ImageIO.read(new File(watermark)), transparency).outputQuality(0.8f).toFile(output); |
| 156 | + } catch (IOException e) { |
| 157 | + e.printStackTrace(); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + public static void ImgWatermark(File source, String output, int width, int height, Position position, String watermark, float transparency, float quality) { |
| 162 | + try { |
| 163 | + Thumbnails.of(source).size(width, height).watermark(position, ImageIO.read(new File(watermark)), transparency).outputQuality(0.8f).toFile(output); |
| 164 | + } catch (IOException e) { |
| 165 | + e.printStackTrace(); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * 裁剪图片 |
| 171 | + * |
| 172 | + * @param source 输入源 |
| 173 | + * @param output 输出源 |
| 174 | + * @param position 裁剪位置 |
| 175 | + * @param x 裁剪区域x |
| 176 | + * @param y 裁剪区域y |
| 177 | + * @param width 宽 |
| 178 | + * @param height 高 |
| 179 | + * @param keepAspectRatio 默认是按照比例缩放的,值为false 时不按比例缩放 |
| 180 | + */ |
| 181 | + public static void imgSourceRegion(String source, String output, Position position, int x, int y, int width, int height, boolean keepAspectRatio) { |
| 182 | + try { |
| 183 | + Thumbnails.of(source).sourceRegion(position, x, y).size(width, height).keepAspectRatio(keepAspectRatio).toFile(output); |
| 184 | + } catch (IOException e) { |
| 185 | + e.printStackTrace(); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + public static void imgSourceRegion(File source, String output, Position position, int x, int y, int width, int height, boolean keepAspectRatio) { |
| 190 | + try { |
| 191 | + Thumbnails.of(source).sourceRegion(position, x, y).size(width, height).keepAspectRatio(keepAspectRatio).toFile(output); |
| 192 | + } catch (IOException e) { |
| 193 | + e.printStackTrace(); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * 按坐标裁剪 |
| 199 | + * |
| 200 | + * @param source 输入源 |
| 201 | + * @param output 输出源 |
| 202 | + * @param x 起始x坐标 |
| 203 | + * @param y 起始y坐标 |
| 204 | + * @param x1 结束x坐标 |
| 205 | + * @param y1 结束y坐标 |
| 206 | + * @param width 宽 |
| 207 | + * @param height 高 |
| 208 | + * @param keepAspectRatio 默认是按照比例缩放的,值为false 时不按比例缩放 |
| 209 | + */ |
| 210 | + public static void imgSourceRegion(String source, String output, int x, int y, int x1, int y1, int width, int height, boolean keepAspectRatio) { |
| 211 | + try { |
| 212 | + Thumbnails.of(source).sourceRegion(x, y, x1, y1).size(width, height).keepAspectRatio(keepAspectRatio).toFile(output); |
| 213 | + } catch (IOException e) { |
| 214 | + e.printStackTrace(); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + public static void imgSourceRegion(File source, String output, int x, int y, int x1, int y1, int width, int height, boolean keepAspectRatio) { |
| 219 | + try { |
| 220 | + Thumbnails.of(source).sourceRegion(x, y, x1, y1).size(width, height).keepAspectRatio(keepAspectRatio).toFile(output); |
| 221 | + } catch (IOException e) { |
| 222 | + e.printStackTrace(); |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + /** |
| 227 | + * 转化图像格式 |
| 228 | + * |
| 229 | + * @param source 输入源 |
| 230 | + * @param output 输出源 |
| 231 | + * @param width 宽 |
| 232 | + * @param height 高 |
| 233 | + * @param format 图片类型,gif、png、jpg |
| 234 | + */ |
| 235 | + public static void imgFormat(String source, String output, int width, int height, String format) { |
| 236 | + try { |
| 237 | + Thumbnails.of(source).size(width, height).outputFormat(format).toFile(output); |
| 238 | + } catch (IOException e) { |
| 239 | + e.printStackTrace(); |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + public static void imgFormat(File source, String output, int width, int height, String format) { |
| 244 | + try { |
| 245 | + Thumbnails.of(source).size(width, height).outputFormat(format).toFile(output); |
| 246 | + } catch (IOException e) { |
| 247 | + e.printStackTrace(); |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + /** |
| 252 | + * 输出到OutputStream |
| 253 | + * |
| 254 | + * @param source 输入源 |
| 255 | + * @param output 输出源 |
| 256 | + * @param width 宽 |
| 257 | + * @param height 高 |
| 258 | + * @return toOutputStream(流对象) |
| 259 | + */ |
| 260 | + public static OutputStream imgOutputStream(String source, String output, int width, int height) { |
| 261 | + OutputStream os = null; |
| 262 | + try { |
| 263 | + os = new FileOutputStream(output); |
| 264 | + Thumbnails.of(source).size(width, height).toOutputStream(os); |
| 265 | + } catch (IOException e) { |
| 266 | + e.printStackTrace(); |
| 267 | + } |
| 268 | + return os; |
| 269 | + } |
| 270 | + |
| 271 | + public static OutputStream imgOutputStream(File source, String output, int width, int height) { |
| 272 | + OutputStream os = null; |
| 273 | + try { |
| 274 | + os = new FileOutputStream(output); |
| 275 | + Thumbnails.of(source).size(width, height).toOutputStream(os); |
| 276 | + } catch (IOException e) { |
| 277 | + e.printStackTrace(); |
| 278 | + } |
| 279 | + return os; |
| 280 | + } |
| 281 | + |
| 282 | + /** |
| 283 | + * 输出到BufferedImage |
| 284 | + * |
| 285 | + * @param source 输入源 |
| 286 | + * @param output 输出源 |
| 287 | + * @param width 宽 |
| 288 | + * @param height 高 |
| 289 | + * @param format 图片类型,gif、png、jpg |
| 290 | + * @return BufferedImage |
| 291 | + */ |
| 292 | + public static BufferedImage imgBufferedImage(String source, String output, int width, int height, String format) { |
| 293 | + BufferedImage buf = null; |
| 294 | + try { |
| 295 | + buf = Thumbnails.of(source).size(width, height).asBufferedImage(); |
| 296 | + ImageIO.write(buf, format, new File(output)); |
| 297 | + } catch (IOException e) { |
| 298 | + e.printStackTrace(); |
| 299 | + } |
| 300 | + return buf; |
| 301 | + } |
| 302 | + |
| 303 | + public static BufferedImage imgBufferedImage(File source, String output, int width, int height, String format) { |
| 304 | + BufferedImage buf = null; |
| 305 | + try { |
| 306 | + buf = Thumbnails.of(source).size(width, height).asBufferedImage(); |
| 307 | + ImageIO.write(buf, format, new File(output)); |
| 308 | + } catch (IOException e) { |
| 309 | + e.printStackTrace(); |
| 310 | + } |
| 311 | + return buf; |
| 312 | + } |
| 313 | + |
| 314 | +} |
0 commit comments