|
@@ -1,175 +1,175 @@
|
|
|
-package com.onemap.analyse;
|
|
|
-
|
|
|
-import com.drew.imaging.ImageMetadataReader;
|
|
|
-import com.drew.metadata.Metadata;
|
|
|
-import com.drew.metadata.exif.ExifDirectoryBase;
|
|
|
-
|
|
|
-import java.awt.Graphics2D;
|
|
|
-import java.awt.Image;
|
|
|
-import java.awt.geom.AffineTransform;
|
|
|
-import java.awt.image.BufferedImage;
|
|
|
-import java.io.File;
|
|
|
-import javax.imageio.ImageIO;
|
|
|
-
|
|
|
-public class ImageRotationCorrector {
|
|
|
-
|
|
|
- public static BufferedImage correctImageOrientation(BufferedImage image, int orientation) {
|
|
|
- switch (orientation) {
|
|
|
- case 1:
|
|
|
- return image; // 无需旋转
|
|
|
- case 3:
|
|
|
- return rotate180(image);
|
|
|
- case 6:
|
|
|
- return rotate90(image);
|
|
|
- case 8:
|
|
|
- return rotate270(image);
|
|
|
- default:
|
|
|
- return image; // 未知方向,保持原样
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private static BufferedImage rotate90(BufferedImage image) {
|
|
|
- int width = image.getWidth();
|
|
|
- int height = image.getHeight();
|
|
|
- BufferedImage rotated = new BufferedImage(height, width, image.getType());
|
|
|
-
|
|
|
- for (int y = 0; y < height; y++) {
|
|
|
- for (int x = 0; x < width; x++) {
|
|
|
- rotated.setRGB(height - 1 - y, x, image.getRGB(x, y));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return rotated;
|
|
|
- }
|
|
|
-
|
|
|
- private static BufferedImage rotate180(BufferedImage image) {
|
|
|
- int width = image.getWidth();
|
|
|
- int height = image.getHeight();
|
|
|
- BufferedImage rotated = new BufferedImage(width, height, image.getType());
|
|
|
-
|
|
|
- for (int y = 0; y < height; y++) {
|
|
|
- for (int x = 0; x < width; x++) {
|
|
|
- rotated.setRGB(width - 1 - x, height - 1 - y, image.getRGB(x, y));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return rotated;
|
|
|
- }
|
|
|
-
|
|
|
- private static BufferedImage rotate270(BufferedImage image) {
|
|
|
- int width = image.getWidth();
|
|
|
- int height = image.getHeight();
|
|
|
- BufferedImage rotated = new BufferedImage(height, width, image.getType());
|
|
|
-
|
|
|
- for (int y = 0; y < height; y++) {
|
|
|
- for (int x = 0; x < width; x++) {
|
|
|
- rotated.setRGB(y, width - 1 - x, image.getRGB(x, y));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return rotated;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 旋转图像
|
|
|
- *
|
|
|
- * @param image 原始图像
|
|
|
- * @param degrees 旋转角度(0-360)
|
|
|
- * @return 旋转后的图像
|
|
|
- */
|
|
|
- public static BufferedImage rotateImage(BufferedImage image, double degrees) {
|
|
|
- // 将角度转换为弧度
|
|
|
- double radians = Math.toRadians(degrees);
|
|
|
-
|
|
|
- // 计算旋转后的图像尺寸
|
|
|
- double sin = Math.abs(Math.sin(radians));
|
|
|
- double cos = Math.abs(Math.cos(radians));
|
|
|
- int newWidth = (int) Math.round(image.getWidth() * cos + image.getHeight() * sin);
|
|
|
- int newHeight = (int) Math.round(image.getWidth() * sin + image.getHeight() * cos);
|
|
|
-
|
|
|
- // 创建新的BufferedImage
|
|
|
- BufferedImage rotatedImage = new BufferedImage(newWidth, newHeight, image.getType());
|
|
|
- Graphics2D g2d = rotatedImage.createGraphics();
|
|
|
-
|
|
|
- // 设置旋转中心并旋转
|
|
|
- AffineTransform transform = new AffineTransform();
|
|
|
- transform.translate(newWidth / 2, newHeight / 2);
|
|
|
- transform.rotate(radians);
|
|
|
- transform.translate(-image.getWidth() / 2, -image.getHeight() / 2);
|
|
|
-
|
|
|
- // 应用变换
|
|
|
- g2d.setTransform(transform);
|
|
|
- g2d.drawImage(image, 0, 0, null);
|
|
|
- g2d.dispose();
|
|
|
-
|
|
|
- return rotatedImage;
|
|
|
- }
|
|
|
-
|
|
|
- public static int getImageOrientation(File imageFile) throws Exception {
|
|
|
- Metadata metadata = ImageMetadataReader.readMetadata(imageFile);
|
|
|
- for (ExifDirectoryBase directory : metadata.getDirectoriesOfType(ExifDirectoryBase.class)) {
|
|
|
- if (directory.containsTag(ExifDirectoryBase.TAG_ORIENTATION)) {
|
|
|
- return directory.getInt(ExifDirectoryBase.TAG_ORIENTATION);
|
|
|
- }
|
|
|
- }
|
|
|
- return 1; // 默认方向
|
|
|
- }
|
|
|
-
|
|
|
- public static void main(String[] args) {
|
|
|
- try {
|
|
|
- File fileDir = new File("D:\\onemapfile\\gdbh\\flight\\batch\\24001120460\\下");
|
|
|
- File[] files = fileDir.listFiles();
|
|
|
- for (File imageFile : files) {
|
|
|
- String name = imageFile.getName();
|
|
|
-// File imageFile = file;
|
|
|
- BufferedImage image = ImageIO.read(imageFile);
|
|
|
- // 获取EXIF方向信息
|
|
|
- int orientation = getImageOrientation(imageFile);
|
|
|
- if (1 != orientation) {
|
|
|
- // 校正图像方向
|
|
|
- BufferedImage correctedImage = correctImageOrientation(image, orientation);
|
|
|
-
|
|
|
- // 旋转270度(逆时针90度)
|
|
|
- BufferedImage rotatedImage = rotateImage(correctedImage, 270);
|
|
|
- ImageIO.write(rotatedImage, "jpg", new File("D:\\onemapfile\\gdbh\\flight\\batch\\24001120460\\上\\" + name));
|
|
|
- } else {
|
|
|
- ImageIO.write(image, "jpg", new File("D:\\onemapfile\\gdbh\\flight\\batch\\24001120460\\上\\" + name));
|
|
|
- }
|
|
|
- }
|
|
|
- System.out.println("图像方向已校正并保存");
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static void main1(String[] args) {
|
|
|
- try {
|
|
|
- File fileDir = new File("D:\\02DATA\\三亚\\甲方数据\\飞行\\三亚无人机数据\\24001120450\\下");
|
|
|
- File[] files = fileDir.listFiles();
|
|
|
- for (File file : files) {
|
|
|
- File imageFile = new File("D:\\02DATA\\三亚\\甲方数据\\飞行\\三亚无人机数据\\24001120450\\下\\240011204500004.JPG");
|
|
|
- BufferedImage image = ImageIO.read(imageFile);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- File imageFile = new File("D:\\02DATA\\三亚\\甲方数据\\飞行\\三亚无人机数据\\24001120450\\下\\240011204500004.JPG");
|
|
|
- BufferedImage image = ImageIO.read(imageFile);
|
|
|
-
|
|
|
- // 获取EXIF方向信息
|
|
|
- int orientation = getImageOrientation(imageFile);
|
|
|
-
|
|
|
- if (1 != orientation) {
|
|
|
- // 校正图像方向
|
|
|
- BufferedImage correctedImage = correctImageOrientation(image, orientation);
|
|
|
- // 保存校正后的图像
|
|
|
- ImageIO.write(correctedImage, "jpg", new File("D:\\02DATA\\三亚\\甲方数据\\飞行\\三亚无人机数据\\24001120450\\下\\corrected_photo.jpg"));
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- System.out.println("图像方向已校正并保存");
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
+//package com.onemap.analyse;
|
|
|
+//
|
|
|
+//import com.drew.imaging.ImageMetadataReader;
|
|
|
+//import com.drew.metadata.Metadata;
|
|
|
+//import com.drew.metadata.exif.ExifDirectoryBase;
|
|
|
+//
|
|
|
+//import java.awt.Graphics2D;
|
|
|
+//import java.awt.Image;
|
|
|
+//import java.awt.geom.AffineTransform;
|
|
|
+//import java.awt.image.BufferedImage;
|
|
|
+//import java.io.File;
|
|
|
+//import javax.imageio.ImageIO;
|
|
|
+//
|
|
|
+//public class ImageRotationCorrector {
|
|
|
+//
|
|
|
+// public static BufferedImage correctImageOrientation(BufferedImage image, int orientation) {
|
|
|
+// switch (orientation) {
|
|
|
+// case 1:
|
|
|
+// return image; // 无需旋转
|
|
|
+// case 3:
|
|
|
+// return rotate180(image);
|
|
|
+// case 6:
|
|
|
+// return rotate90(image);
|
|
|
+// case 8:
|
|
|
+// return rotate270(image);
|
|
|
+// default:
|
|
|
+// return image; // 未知方向,保持原样
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// private static BufferedImage rotate90(BufferedImage image) {
|
|
|
+// int width = image.getWidth();
|
|
|
+// int height = image.getHeight();
|
|
|
+// BufferedImage rotated = new BufferedImage(height, width, image.getType());
|
|
|
+//
|
|
|
+// for (int y = 0; y < height; y++) {
|
|
|
+// for (int x = 0; x < width; x++) {
|
|
|
+// rotated.setRGB(height - 1 - y, x, image.getRGB(x, y));
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// return rotated;
|
|
|
+// }
|
|
|
+//
|
|
|
+// private static BufferedImage rotate180(BufferedImage image) {
|
|
|
+// int width = image.getWidth();
|
|
|
+// int height = image.getHeight();
|
|
|
+// BufferedImage rotated = new BufferedImage(width, height, image.getType());
|
|
|
+//
|
|
|
+// for (int y = 0; y < height; y++) {
|
|
|
+// for (int x = 0; x < width; x++) {
|
|
|
+// rotated.setRGB(width - 1 - x, height - 1 - y, image.getRGB(x, y));
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// return rotated;
|
|
|
+// }
|
|
|
+//
|
|
|
+// private static BufferedImage rotate270(BufferedImage image) {
|
|
|
+// int width = image.getWidth();
|
|
|
+// int height = image.getHeight();
|
|
|
+// BufferedImage rotated = new BufferedImage(height, width, image.getType());
|
|
|
+//
|
|
|
+// for (int y = 0; y < height; y++) {
|
|
|
+// for (int x = 0; x < width; x++) {
|
|
|
+// rotated.setRGB(y, width - 1 - x, image.getRGB(x, y));
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// return rotated;
|
|
|
+// }
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 旋转图像
|
|
|
+// *
|
|
|
+// * @param image 原始图像
|
|
|
+// * @param degrees 旋转角度(0-360)
|
|
|
+// * @return 旋转后的图像
|
|
|
+// */
|
|
|
+// public static BufferedImage rotateImage(BufferedImage image, double degrees) {
|
|
|
+// // 将角度转换为弧度
|
|
|
+// double radians = Math.toRadians(degrees);
|
|
|
+//
|
|
|
+// // 计算旋转后的图像尺寸
|
|
|
+// double sin = Math.abs(Math.sin(radians));
|
|
|
+// double cos = Math.abs(Math.cos(radians));
|
|
|
+// int newWidth = (int) Math.round(image.getWidth() * cos + image.getHeight() * sin);
|
|
|
+// int newHeight = (int) Math.round(image.getWidth() * sin + image.getHeight() * cos);
|
|
|
+//
|
|
|
+// // 创建新的BufferedImage
|
|
|
+// BufferedImage rotatedImage = new BufferedImage(newWidth, newHeight, image.getType());
|
|
|
+// Graphics2D g2d = rotatedImage.createGraphics();
|
|
|
+//
|
|
|
+// // 设置旋转中心并旋转
|
|
|
+// AffineTransform transform = new AffineTransform();
|
|
|
+// transform.translate(newWidth / 2, newHeight / 2);
|
|
|
+// transform.rotate(radians);
|
|
|
+// transform.translate(-image.getWidth() / 2, -image.getHeight() / 2);
|
|
|
+//
|
|
|
+// // 应用变换
|
|
|
+// g2d.setTransform(transform);
|
|
|
+// g2d.drawImage(image, 0, 0, null);
|
|
|
+// g2d.dispose();
|
|
|
+//
|
|
|
+// return rotatedImage;
|
|
|
+// }
|
|
|
+//
|
|
|
+// public static int getImageOrientation(File imageFile) throws Exception {
|
|
|
+// Metadata metadata = ImageMetadataReader.readMetadata(imageFile);
|
|
|
+// for (ExifDirectoryBase directory : metadata.getDirectoriesOfType(ExifDirectoryBase.class)) {
|
|
|
+// if (directory.containsTag(ExifDirectoryBase.TAG_ORIENTATION)) {
|
|
|
+// return directory.getInt(ExifDirectoryBase.TAG_ORIENTATION);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// return 1; // 默认方向
|
|
|
+// }
|
|
|
+//
|
|
|
+// public static void main(String[] args) {
|
|
|
+// try {
|
|
|
+// File fileDir = new File("D:\\onemapfile\\gdbh\\flight\\batch\\24001120460\\下");
|
|
|
+// File[] files = fileDir.listFiles();
|
|
|
+// for (File imageFile : files) {
|
|
|
+// String name = imageFile.getName();
|
|
|
+//// File imageFile = file;
|
|
|
+// BufferedImage image = ImageIO.read(imageFile);
|
|
|
+// // 获取EXIF方向信息
|
|
|
+// int orientation = getImageOrientation(imageFile);
|
|
|
+// if (1 != orientation) {
|
|
|
+// // 校正图像方向
|
|
|
+// BufferedImage correctedImage = correctImageOrientation(image, orientation);
|
|
|
+//
|
|
|
+// // 旋转270度(逆时针90度)
|
|
|
+// BufferedImage rotatedImage = rotateImage(correctedImage, 270);
|
|
|
+// ImageIO.write(rotatedImage, "jpg", new File("D:\\onemapfile\\gdbh\\flight\\batch\\24001120460\\上\\" + name));
|
|
|
+// } else {
|
|
|
+// ImageIO.write(image, "jpg", new File("D:\\onemapfile\\gdbh\\flight\\batch\\24001120460\\上\\" + name));
|
|
|
+// }
|
|
|
+// }
|
|
|
+// System.out.println("图像方向已校正并保存");
|
|
|
+// } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// public static void main1(String[] args) {
|
|
|
+// try {
|
|
|
+// File fileDir = new File("D:\\02DATA\\三亚\\甲方数据\\飞行\\三亚无人机数据\\24001120450\\下");
|
|
|
+// File[] files = fileDir.listFiles();
|
|
|
+// for (File file : files) {
|
|
|
+// File imageFile = new File("D:\\02DATA\\三亚\\甲方数据\\飞行\\三亚无人机数据\\24001120450\\下\\240011204500004.JPG");
|
|
|
+// BufferedImage image = ImageIO.read(imageFile);
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+// File imageFile = new File("D:\\02DATA\\三亚\\甲方数据\\飞行\\三亚无人机数据\\24001120450\\下\\240011204500004.JPG");
|
|
|
+// BufferedImage image = ImageIO.read(imageFile);
|
|
|
+//
|
|
|
+// // 获取EXIF方向信息
|
|
|
+// int orientation = getImageOrientation(imageFile);
|
|
|
+//
|
|
|
+// if (1 != orientation) {
|
|
|
+// // 校正图像方向
|
|
|
+// BufferedImage correctedImage = correctImageOrientation(image, orientation);
|
|
|
+// // 保存校正后的图像
|
|
|
+// ImageIO.write(correctedImage, "jpg", new File("D:\\02DATA\\三亚\\甲方数据\\飞行\\三亚无人机数据\\24001120450\\下\\corrected_photo.jpg"));
|
|
|
+//
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+// System.out.println("图像方向已校正并保存");
|
|
|
+// } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// }
|
|
|
+//}
|