需要的jar依賴:
????????????<dependency>
?? ??? ??? ? ? ?<groupId>com.drewnoakes</groupId>
?? ??? ??? ? ? ?<artifactId>metadata-extractor</artifactId>
?? ??? ??? ? ? ?<version>2.16.0</version> <!-- 請檢查最新版本 -->
?? ??? ??? ?</dependency>
String file = "C:\\Users\\52659\\Desktop\\temp2\\ee5cf73916bb4149b17b854901641fe7.jpg";
?? ??? ?ImgUtil.scale(
?? ??? ??? ??? ?FileUtil.file(file),
?? ??? ??? ??? ?FileUtil.file(file+"_s.jpg"),
?? ??? ??? ??? ?0.5f//縮放比例
?? ??? ??? ?);
?? ??? ?
?? ??? ?// 獲取偏轉角度
? ? ? ? int angle = getAngle(new File(file));
? ? ? ? System.out.println(angle);
? ? ? ??
? ? ? ? ImgUtil.rotate(FileUtil.file(file+"_s.jpg"),angle,FileUtil.file(file+"_s.jpg"));
private static int getAngle(File file) throws Exception {
?? ? ? // Metadata metadata = ImageMetadataReader.readMetadata(file.getInputStream());
?? ? ? ?Metadata metadata = ImageMetadataReader.readMetadata(file);
?? ? ? ?for (Directory directory : metadata.getDirectories()) {
?? ? ? ? ? ?for (Tag tag : directory.getTags()) {
?? ? ? ? ? ? ? ?if ("Orientation".equals(tag.getTagName())) {
?? ? ? ? ? ? ? ? ? ?String orientation = tag.getDescription();
?? ? ? ? ? ? ? ? ? ?if (orientation.contains("90")) {
?? ? ? ? ? ? ? ? ? ? ? ?return 90;
?? ? ? ? ? ? ? ? ? ?} else if (orientation.contains("180")) {
?? ? ? ? ? ? ? ? ? ? ? ?return 180;
?? ? ? ? ? ? ? ? ? ?} else if (orientation.contains("270")) {
?? ? ? ? ? ? ? ? ? ? ? ?return 270;
?? ? ? ? ? ? ? ? ? ?}
?? ? ? ? ? ? ? ?}
?? ? ? ? ? ?}
?? ? ? ?}
?? ? ? ?return 0;?
?? ?}