Tif转Jpg使用心得:
如果tif图片需要压缩,或者需要做转换,常用方法:
File file1 = new File("E:\\www\\ffw\\images\\73.jpg");
byte[] bigContent = Files.readAllBytes(file1.toPath());
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bigContent);
Thumbnails.of(byteArrayInputStream)
.scale(1f).outputQuality(0.3f)
.toFile("E:\\www\\ffw\\images\\4.jpg");
File file1 = new File("E:\\www\\ffw\\images\\5_1663186150934798337.jpg");
MultipartFile file = FileUtil.getMultipartFile(file1);
byte[] bytes = FileUtil.imageSet(file.getBytes());
InputStream is = new ByteArrayInputStream(bytes );
BufferedImage bufImg = ImageIO.read(is);
后续网上很多 省略掉..... 主要是 ImageIO.read(is) 会读取为null
解决方案:
引入依赖:
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-tiff</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<!--Thumbnails支持-->
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
让两个工具类支持读取Tif文件
String[] readerList = ImageIO.getReaderFormatNames();
System.out.println("读取支持格式:"+Arrays.asList(readerList));
String[] writerList = ImageIO.getWriterFormatNames();
System.out.println("写入支持格式:"+Arrays.asList(writerList));
引入前:
引入后: