/** * 保持上传照片的图片方向不发生调整 * * @param filePath 图片的文件路径 */ public static void MaintainOrientation(String filePath) { try { File file = new File(filePath); if (!file.exists()) { logger.fatal(new FileNotFoundException()); return; } Metadata metadata = ImageMetadataReader.readMetadata(file); Directory directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class); int orientation = 0; if (null != directory && directory.containsTag(ExifSubIFDDirectory.TAG_ORIENTATION)) { orientation = directory.getInt(ExifSubIFDDirectory.TAG_ORIENTATION); logger.debug(filePath + " 's orientation is :" + orientation); } String format = FileUtil.getFileSuffix(filePath); switch (orientation) { // case 1: // return "Top, left side (Horizontal / normal)"; // case 2: // return "Top, right side (Mirror horizontal)"; case 3: // return "Bottom, right side (Rotate 180)"; BufferedImage old_img3 = (BufferedImage) ImageIO.read(file); int w3 = old_img3.getWidth(); int h3 = old_img3.getHeight(); BufferedImage new_img3 = new BufferedImage(w3, h3, BufferedImage.TYPE_INT_BGR); Graphics2D g2d3 = new_img3.createGraphics(); AffineTransform origXform3 = g2d3.getTransform(); AffineTransform newXform3 = (AffineTransform) (origXform3.clone()); // center of rotation is center of the panel // double xRot3 = w3/2.0; newXform3.rotate(Math.toRadians(180.0), w3 / 2.0, h3 / 2.0); // 旋转180度 g2d3.setTransform(newXform3); // draw image centered in panel g2d3.drawImage(old_img3, 0, 0, null); // Reset to Original g2d3.setTransform(origXform3); // 写到新的文件 FileOutputStream out3 = new FileOutputStream(file); try { ImageIO.write(new_img3, format, out3); } finally { out3.close(); } break; // case 4: // return "Bottom, left side (Mirror vertical)"; // case 5: // return "Left side, top (Mirror horizontal and rotate 270 // CW)"; case 6: // return "Right side, top (Rotate 90 CW)"; BufferedImage old_img6 = (BufferedImage) ImageIO.read(file); int w6 = old_img6.getWidth(); int h6 = old_img6.getHeight(); BufferedImage new_img6 = new BufferedImage(h6, w6, BufferedImage.TYPE_INT_BGR); Graphics2D g2d6 = new_img6.createGraphics(); AffineTransform origXform6 = g2d6.getTransform(); AffineTransform newXform6 = (AffineTransform) (origXform6.clone()); // center of rotation is center of the panel double xRot6 = h6 / 2.0; newXform6.rotate(Math.toRadians(90.0), xRot6, xRot6); // 旋转90度 g2d6.setTransform(newXform6); // draw image centered in panel g2d6.drawImage(old_img6, 0, 0, null); // Reset to Original g2d6.setTransform(origXform6); // 写到新的文件 FileOutputStream out6 = new FileOutputStream(file); try { ImageIO.write(new_img6, format, out6); } finally { out6.close(); } break; // case 7: // return "Right side, bottom (Mirror horizontal and rotate 90 // CW)"; case 8: // return "Left side, bottom (Rotate 270 CW)"; BufferedImage old_img8 = (BufferedImage) ImageIO.read(file); int w8 = old_img8.getWidth(); int h8 = old_img8.getHeight(); BufferedImage new_img8 = new BufferedImage(h8, w8, BufferedImage.TYPE_INT_BGR); Graphics2D g2d8 = new_img8.createGraphics(); AffineTransform origXform8 = g2d8.getTransform(); AffineTransform newXform8 = (AffineTransform) (origXform8.clone()); // center of rotation is center of the panel double xRot8 = w8 / 2.0; newXform8.rotate(Math.toRadians(270.0), xRot8, xRot8); // 旋转90度 g2d8.setTransform(newXform8); // draw image centered in panel g2d8.drawImage(old_img8, 0, 0, null); // Reset to Original g2d8.setTransform(origXform8); // 写到新的文件 FileOutputStream out8 = new FileOutputStream(file); try { ImageIO.write(new_img8, format, out8); } finally { out8.close(); } break; // default: // return String.valueOf(orientation); } } catch (Exception e) { e.printStackTrace(); logger.fatal(e); } }
/** * An application entry point. Takes the name of one or more files as arguments and prints the * contents of all metadata directories to <code>System.out</code>. * * <p>If <code>-thumb</code> is passed, then any thumbnail data will be written to a file with * name of the input file having <code>.thumb.jpg</code> appended. * * <p>If <code>-markdown</code> is passed, then output will be in markdown format. * * <p>If <code>-hex</code> is passed, then the ID of each tag will be displayed in hexadecimal. * * @param args the command line arguments */ public static void main(@NotNull String[] args) throws MetadataException, IOException { Collection<String> argList = new ArrayList<String>(Arrays.asList(args)); boolean thumbRequested = argList.remove("-thumb"); boolean markdownFormat = argList.remove("-markdown"); boolean showHex = argList.remove("-hex"); if (argList.size() < 1) { String version = ImageMetadataReader.class.getPackage().getImplementationVersion(); System.out.println("metadata-extractor version " + version); System.out.println(); System.out.println( String.format( "Usage: java -jar metadata-extractor-%s.jar <filename> [<filename>] [-thumb] [-markdown] [-hex]", version == null ? "a.b.c" : version)); System.exit(1); } for (String filePath : argList) { long startTime = System.nanoTime(); File file = new File(filePath); if (!markdownFormat && argList.size() > 1) System.out.printf("\n***** PROCESSING: %s\n%n", filePath); Metadata metadata = null; try { metadata = ImageMetadataReader.readMetadata(file); } catch (Exception e) { e.printStackTrace(System.err); System.exit(1); } long took = System.nanoTime() - startTime; if (!markdownFormat) System.out.printf( "Processed %.3f MB file in %.2f ms%n%n", file.length() / (1024d * 1024), took / 1000000d); if (markdownFormat) { String fileName = file.getName(); String urlName = StringUtil.urlEncode(filePath); ExifIFD0Directory exifIFD0Directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class); String make = exifIFD0Directory == null ? "" : exifIFD0Directory.getString(ExifIFD0Directory.TAG_MAKE); String model = exifIFD0Directory == null ? "" : exifIFD0Directory.getString(ExifIFD0Directory.TAG_MODEL); System.out.println(); System.out.println("---"); System.out.println(); System.out.printf("# %s - %s%n", make, model); System.out.println(); System.out.printf( "<a href=\"https://raw.githubusercontent.com/drewnoakes/metadata-extractor-images/master/%s\">%n", urlName); System.out.printf( "<img src=\"https://raw.githubusercontent.com/drewnoakes/metadata-extractor-images/master/%s\" width=\"300\"/><br/>%n", urlName); System.out.println(fileName); System.out.println("</a>"); System.out.println(); System.out.println("Directory | Tag Id | Tag Name | Extracted Value"); System.out.println(":--------:|-------:|----------|----------------"); } // iterate over the metadata and print to System.out for (Directory directory : metadata.getDirectories()) { String directoryName = directory.getName(); for (Tag tag : directory.getTags()) { String tagName = tag.getTagName(); String description = tag.getDescription(); // truncate the description if it's too long if (description != null && description.length() > 1024) { description = description.substring(0, 1024) + "..."; } if (markdownFormat) { System.out.printf( "%s|0x%s|%s|%s%n", directoryName, Integer.toHexString(tag.getTagType()), tagName, description); } else { // simple formatting if (showHex) { System.out.printf( "[%s - %s] %s = %s%n", directoryName, tag.getTagTypeHex(), tagName, description); } else { System.out.printf("[%s] %s = %s%n", directoryName, tagName, description); } } } // print out any errors for (String error : directory.getErrors()) System.err.println("ERROR: " + error); } if (args.length > 1 && thumbRequested) { ExifThumbnailDirectory directory = metadata.getFirstDirectoryOfType(ExifThumbnailDirectory.class); if (directory != null && directory.hasThumbnailData()) { System.out.println("Writing thumbnail..."); directory.writeThumbnail(args[0].trim() + ".thumb.jpg"); } else { System.out.println("No thumbnail data exists in this image"); } } } }