//原图 1056 2272 &Am neil tu //原图宽高都小于3000 不设置keepAspectRatio(false) 那么只有高会改为指定高,宽等比例改变,反之设置keepAspectRatio(false)那么宽高都会按照指定大小改变 Thumbnails.of("E:\\img3.jpg").size(3000,3000).keepAspectRatio(false).toFile("E:\\3.jpg"); Thumbnails.of("E:\\img3.jpg").size(3000,3000).toFile("E:\\3.jpg"); //原图宽都小于3000 ,高大于2000 不设置keepAspectRatio(false)那么高会减小到指定大小,宽会等比例改变,反之设置keepAspectRatio(false)那么宽高都会按照指定大小改变 Thumbnails.of("E:\\img3.jpg").size(3000,2000).toFile("E:\\3.jpg"); Thumbnails.of("E:\\img3.jpg").size(3000,2000).keepAspectRatio(false).toFile("E:\\3.jpg"); //原图宽都大于500,高小于3000 不设置keepAspectRatio(false)那么宽会减小到指定大小,高会等比例改变,反之设置keepAspectRatio(false)那么宽高都会按照指定大小改变 Thumbnails.of("E:\\img3.jpg").size(500,3000).toFile("E:\\3.jpg"); Thumbnails.of("E:\\img3.jpg").size(500,3000).keepAspectRatio(false).toFile("E:\\3.jpg"); //原图 宽高都大于500 不设置keepAspectRatio(false)那么只有高会改为指定高,宽等比例改变,反之设置keepAspectRatio(false)那么宽高都会按照指定大小改变 Thumbnails.of("E:\\img3.jpg").size(500,500).toFile("E:\\3.jpg"); //只指定高 高度变为指定高,宽等比例改变 Thumbnails.of("E:\\img3.jpg").height(500).toFile("E:\\3.jpg"); //只指定宽,宽变为指定宽,高等比例改变 Thumbnails.of("E:\\img3.jpg").width(500).toFile("E\\3.jpg"); //按照比例因子进行缩放,不指定高宽的具体大小 scale 比例 scale取值越大,比例越高,1f代表原比例,0.5f代表原比例的一半,以此类推 Thumbnails.of("E:\\img3.jpg").scale(0.5f).toFile("E:\\3.jpg"); //图片旋转 rotate 旋转 正数:顺时针, 负数:逆时针 必须有比例大小的设置,不能单独用rotate Thumbnails.of("E:\\img3.jpg").scale(1f).rotate(90).toFile("E:\\3.jpg"); Thumbnails.of("E:\\img3.jpg").size(1000,2000).rotate(90).toFile("E:\\3.jpg"); //水印 watermark(位置(指取水印图放置的位置),水印图,透明度) Thumbnails.of("E:\\img3.jpg").scale(1f).watermark(Positions.BOTTOM_LEFT,ImageIO.read(new File("E:\\img6.jpg")),0.5f).toFile("E:\\3.jpg"); //转化图像格式 如jpg 转 png Thumbnails.of("E:\\img3.jpg").scale(1f).outputFormat("png").toFile("E:\\3.png"); //裁剪 图片中心400*400的区域 Thumbnails.of("E:\\img3.jpg").sourceRegion(Positions.CENTER,400,400).size(200,200) .keepAspectRatio(false).toFile("E:\\3.png"); //裁剪 图片右下400*400的区域 Thumbnails.of("E:\\img3.jpg").sourceRegion(Positions.BOTTOM_RIGHT,400,400).size(200,200) .keepAspectRatio(false).toFile("E:\\3.png"); //裁剪 指定坐标 Thumbnails.of("E:\\img3.jpg") .sourceRegion(600,500,400,400).size(200,200) .keepAspectRatio(false).toFile("E:\\3.png"); //转成ByteArrayOutputStream 或者二进制数组 MultipartFile file = new MultipartFile() { @Override public String getName() { return null; } @Override public String getOriginalFilename() { return null; } @Override public String getContentType() { return null; } @Override public boolean isEmpty() { return false; } @Override public long getSize() { return 0; } @Override public byte[] getBytes() throws IOException { return new byte[0]; } @Override public InputStream getInputStream() throws IOException { return null; } @Override public void transferTo(File dest) throws IOException, IllegalStateException { } }; byte [] buf = null; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Thumbnails.of(file.getInputStream()).scale(0.1f).toOutputStream(outputStream); //或者 File file2 = new File("E:\\img3.jpg"); FileInputStream fileInputStream = new FileInputStream(file2); Thumbnails.of(fileInputStream).scale(0.1f).toOutputStream(outputStream); //或者 ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); Thumbnails.of(file.getInputStream()).height(720).outputQuality(1f).toOutputStream(byteOutputStream); ByteArrayInputStream inputStream = new ByteArrayInputStream(byteOutputStream.toByteArray()); byteOutputStream.flush(); InputStream inputStreamParent = inputStream; buf = outputStream.toByteArray(); //图片的压缩质量 outputQuality是图片的质量,值是在0到1,越接近于1质量越好,越接近于0质量越差。 Thumbnails.of("原图文件的路径").scale(1f) .outputQuality(0.5f).toFile("压缩后文件的路径");
注意:scale、width|height、size三者不能同时共存,但必须要有一个
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/221057.html原文链接:https://javaforall.net
