/*solution
target/classes/下面并没有照片 图片路径缺失
*/ package com.imooc.o2o.util;
import java.io.File; import java.io.IOException; //import java.util.ArrayList; import java.util.List; import javax.imageio.ImageIO; import net.coobird.thumbnailator.Thumbnails; import net.coobird.thumbnailator.geometry.Positions; //import org.springframework.web.multipart.commons.CommonsMultipartFile; public class ImageUtil { public static void main(String[] args) throws IOException { //从线程逆推到线加载器,获取绝对值路径 String basePath= Thread.currentThread().getContextClassLoader().getResource("").getPath(); //basePath=URLDecoder.decode(basePath,"utf-8"); //of可以直接传入文件目标是在test01上加上src/main/resource的watermark //ImageIO.read获取图片流 //0.5f指的是透明度 //outputQuality是压缩图片0.8f压缩成80% //toFile指的是输出的位置 Thumbnails.of(new File("/Users/lenovo/Desktop/backgroud/test01.jpg")) .size(100, 100).watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(basePath+"/watermark01.png")),0.25f).outputQuality(0.8f) .toFile("/Users/lenovo/Desktop/backgroud/test01new.jpg");
} } /* import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import net.coobird.thumbnailator.Thumbnails; import org.springframework.web.multipart.commons.CommonsMultipartFile; public class ImageUtil { public static String generateThumbnail(CommonsMultipartFile thumbnail, String targetAddr) { String realFileName = FileUtil.getRandomFileName(); String extension = getFileExtension(thumbnail); makeDirPath(targetAddr); String relativeAddr = targetAddr + realFileName + extension; File dest = new File(FileUtil.getImgBasePath() + relativeAddr); try { Thumbnails.of(thumbnail.getInputStream()).size(200, 200).outputQuality(0.25f).toFile(dest); } catch (IOException e) { throw new RuntimeException("创建缩略图失败:" + e.toString()); } return relativeAddr; } public static String generateNormalImg(CommonsMultipartFile thumbnail, String targetAddr) { String realFileName = FileUtil.getRandomFileName(); String extension = getFileExtension(thumbnail); makeDirPath(targetAddr); String relativeAddr = targetAddr + realFileName + extension; File dest = new File(FileUtil.getImgBasePath() + relativeAddr); try { Thumbnails.of(thumbnail.getInputStream()).size(337, 640).outputQuality(0.5f).toFile(dest); } catch (IOException e) { throw new RuntimeException("创建缩略图失败:" + e.toString()); } return relativeAddr; } public static List<String> generateNormalImgs(List<CommonsMultipartFile> imgs, String targetAddr) { int count = 0; List<String> relativeAddrList = new ArrayList<String>(); if (imgs != null && imgs.size() > 0) { makeDirPath(targetAddr); for (CommonsMultipartFile img : imgs) { String realFileName = FileUtil.getRandomFileName(); String extension = getFileExtension(img); String relativeAddr = targetAddr + realFileName + count + extension; File dest = new File(FileUtil.getImgBasePath() + relativeAddr); count++; try { Thumbnails.of(img.getInputStream()).size(600, 300).outputQuality(0.5f).toFile(dest); } catch (IOException e) { throw new RuntimeException("创建图片失败:" + e.toString()); } relativeAddrList.add(relativeAddr); } } return relativeAddrList; } private static void makeDirPath(String targetAddr) { String realFileParentPath = FileUtil.getImgBasePath() + targetAddr; File dirPath = new File(realFileParentPath); if (!dirPath.exists()) { dirPath.mkdirs(); } } private static String getFileExtension(CommonsMultipartFile cFile) { String originalFileName = cFile.getOriginalFilename(); return originalFileName.substring(originalFileName.lastIndexOf(".")); } }
} }*/
|