You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Thumbnailer.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit;
  17. import java.awt.Dimension;
  18. import java.awt.Image;
  19. import java.awt.image.BufferedImage;
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.io.FilenameFilter;
  23. import java.io.IOException;
  24. import java.util.Iterator;
  25. import javax.imageio.ImageIO;
  26. import javax.imageio.ImageReader;
  27. import javax.imageio.stream.ImageInputStream;
  28. import com.beust.jcommander.JCommander;
  29. import com.beust.jcommander.Parameter;
  30. import com.beust.jcommander.ParameterException;
  31. import com.beust.jcommander.Parameters;
  32. public class Thumbnailer {
  33. public static void main(String[] args) {
  34. Params params = new Params();
  35. JCommander jc = new JCommander(params);
  36. try {
  37. jc.parse(args);
  38. } catch (ParameterException t) {
  39. System.err.println(t.getMessage());
  40. jc.usage();
  41. }
  42. createImageThumbnail(params.sourceFolder, params.destinationFolder, params.maximumDimension);
  43. }
  44. public static void createImageThumbnail(String sourceFolder, String destinationFolder,
  45. int maxDimension) {
  46. if (maxDimension <= 0)
  47. return;
  48. File source = new File(sourceFolder);
  49. File destination = new File(destinationFolder);
  50. destination.mkdirs();
  51. File[] sourceFiles = source.listFiles(new FilenameFilter() {
  52. @Override
  53. public boolean accept(File dir, String name) {
  54. return name.toLowerCase().endsWith(".png");
  55. }
  56. });
  57. for (File sourceFile : sourceFiles) {
  58. File destinationFile = new File(destination, sourceFile.getName());
  59. try {
  60. Dimension sz = getImageDimensions(sourceFile);
  61. int w = 0;
  62. int h = 0;
  63. if (sz.width > maxDimension) {
  64. // Scale to Width
  65. w = maxDimension;
  66. float f = maxDimension;
  67. h = (int) ((f / sz.width) * sz.height); // normalize height
  68. } else if (sz.height > maxDimension) {
  69. // Scale to Height
  70. h = maxDimension;
  71. float f = maxDimension;
  72. w = (int) ((f / sz.height) * sz.width); // normalize width
  73. } else {
  74. // No thumbnail
  75. return;
  76. }
  77. System.out.println("Generating thumbnail for " + sourceFile.getName() + " as (" + w
  78. + "," + h + ")");
  79. BufferedImage image = ImageIO.read(sourceFile);
  80. Image scaledImage = image.getScaledInstance(w, h, BufferedImage.SCALE_SMOOTH);
  81. BufferedImage destImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
  82. destImage.createGraphics().drawImage(scaledImage, 0, 0, null);
  83. FileOutputStream fos = new FileOutputStream(destinationFile);
  84. ImageIO.write(destImage, "png", fos);
  85. fos.flush();
  86. fos.getFD().sync();
  87. fos.close();
  88. } catch (Throwable t) {
  89. t.printStackTrace();
  90. }
  91. }
  92. }
  93. public static Dimension getImageDimensions(File file) throws IOException {
  94. ImageInputStream in = ImageIO.createImageInputStream(file);
  95. try {
  96. final Iterator<ImageReader> readers = ImageIO.getImageReaders(in);
  97. if (readers.hasNext()) {
  98. ImageReader reader = readers.next();
  99. try {
  100. reader.setInput(in);
  101. return new Dimension(reader.getWidth(0), reader.getHeight(0));
  102. } finally {
  103. reader.dispose();
  104. }
  105. }
  106. } finally {
  107. if (in != null)
  108. in.close();
  109. }
  110. return null;
  111. }
  112. @Parameters(separators = " ")
  113. private static class Params {
  114. @Parameter(names = { "--sourceFolder" }, description = "Source folder for raw images", required = true)
  115. public String sourceFolder;
  116. @Parameter(names = { "--destinationFolder" }, description = "Destination folder for thumbnails", required = true)
  117. public String destinationFolder;
  118. @Parameter(names = { "--maxDimension" }, description = "Maximum width or height for thumbnail", required = true)
  119. public int maximumDimension;
  120. }
  121. }