- private static final long serialVersionUID = -4156533490858298387L;
-
- /**
- * Picture dimensions.
- */
- private Dimension dimensions;
-
- public Picture(final File parentDirectory, final String fileName)
- throws IOException, ImageFormatError {
- super(parentDirectory, fileName);
- }
-
- private String computeThumbnailHash(final Dimension targetDimension) {
-
- // compute new thumbnails hash number
- final String forMagicHash = fileName + " "
- + String.valueOf(targetDimension.width) + " "
- + String.valueOf(targetDimension.height) + " "
- + String.valueOf(getFileLength()) + " "
- + Constants.THUMBNAIL_VERSION;
-
- // System.out.println("Computing hash from: " + forMagicHash);
- return Utils.getStringCrcAsHex(forMagicHash);
- }
-
- public Dimension getDimensions() {
- return dimensions;
- };
-
- /**
- * Get thumbnail file name for this image and desired thumbnail dimensions
- * relative to designated thumbnails directory within parent directory.
- */
- public String getRelativeThumbnailFileName(final Dimension targetDimension) {
- return FilePathParser.getFileNameWithoutExtension(fileName) + " ("
- + computeThumbnailHash(targetDimension) + ").jpeg";
- }
-
- @Override
- public void updateFileMetainfo(final File parentDirectory)
- throws IOException {
- BufferedImage image;
- try {
- image = Utils.getBufferedImage(getFile(parentDirectory));
- } catch (final ImageFormatError e) {
- throw new RuntimeException(e);
- }
- dimensions = new Dimension(image.getWidth(), image.getHeight());
- }
-
- /**
- * Actual returned target thumbnail size will be adjusted from desired one
- * by attempting to generate as large as possible thumbnail, while not
- * exceeding provided thumbnail width and height, and preserving aspect
- * ratio of original image.
- */
- public static java.awt.Dimension getTargetThumbnailDimension(
- final java.awt.Dimension originalImageDimension,
- final java.awt.Dimension desiredTargetDimension) {
-
- final double thumbWidthToHeightRatio = (double) desiredTargetDimension.width
- / (double) desiredTargetDimension.height;
- final double inputImageWidthToHeightRatio = (double) originalImageDimension.width
- / (double) originalImageDimension.height;
-
- if (thumbWidthToHeightRatio < inputImageWidthToHeightRatio) {
-
- final java.awt.Dimension result = new java.awt.Dimension(
- desiredTargetDimension.width,
- (int) (desiredTargetDimension.width / inputImageWidthToHeightRatio));
-
- return result;
- } else {
-
- final java.awt.Dimension result = new java.awt.Dimension(
- (int) (desiredTargetDimension.height * inputImageWidthToHeightRatio),
- desiredTargetDimension.height);
-
- return result;
- }
- }
-
- /**
- * Propose list of thumbnail dimensions.
- */
- public static List<Dimension> getThumbnailDimensions(Dimension current) {
- final ArrayList<Dimension> result = new ArrayList<Dimension>();
- result.add(current);
-
- while (current.getArea() > 100000) {
- current = current.getScaled(0.5d);
- result.add(current);
- }
-
- return result;
-
- }
-
- public static void makeThumbnail(final File inputFile,
- final File outputFile,
- final java.awt.Dimension preferredTargetDimensions) {
-
- try {
-
- final BufferedImage inputImage = Utils.getBufferedImage(inputFile);
-
- final java.awt.Dimension sourceImageDimension = new java.awt.Dimension(
- inputImage.getWidth(), inputImage.getHeight());
-
- final java.awt.Dimension targetDimensions = getTargetThumbnailDimension(
- sourceImageDimension, preferredTargetDimensions);
-
- final OutputStream out = new FileOutputStream(outputFile);
-
- final Image scaledImage = scaleImage(inputImage,
- targetDimensions.width, targetDimensions.height);
-
- final BufferedImage bufferedImage = new BufferedImage(
- scaledImage.getWidth(null), scaledImage.getHeight(null),
- BufferedImage.TYPE_INT_RGB);
-
- final Graphics2D g = bufferedImage.createGraphics();
- g.drawImage(scaledImage, 0, 0, null);
- g.dispose();
-
- ImageIO.write(bufferedImage, "jpg", out);
- out.close();
-
- } catch (final Exception exception) {
- System.out.println(exception.toString());
- exception.printStackTrace();
- }
- }
-
- /**
- * @return new image scaled to desired dimensions
- */
- public static Image scaleImage(final Image srcImage, final int width,
- final int height) {
-
- ImageFilter filter;
- filter = new java.awt.image.AreaAveragingScaleFilter(width, height);