AdaptiveImageHelper.java
3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.designer.Style
* com.day.image.Layer
* javax.jcr.RepositoryException
*/
package com.day.cq.wcm.foundation;
import com.day.cq.wcm.api.designer.Style;
import com.day.cq.wcm.foundation.Image;
import com.day.image.Layer;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import javax.jcr.RepositoryException;
public class AdaptiveImageHelper {
public static Quality getQualityFromString(String imageQualityString) {
if (imageQualityString == null) {
return null;
}
return Quality.valueOf(imageQualityString.toUpperCase());
}
public Layer scaleThisImage(Image image, int newWidth, int newHeight, Style style) throws RepositoryException, IOException {
int potentialScaledHeight;
Layer layer = this.applyStyleDataToImage(image, style);
int currentWidth = layer.getWidth();
int currentHeight = layer.getHeight();
double widthRatio = (double)newWidth / (double)currentWidth;
double heightRatio = (double)newHeight / (double)currentHeight;
if (newHeight == 0) {
newHeight = (int)((double)currentHeight * widthRatio);
}
Dimension newSize = (potentialScaledHeight = (int)((double)currentHeight * widthRatio)) >= newHeight ? new Dimension(newWidth, potentialScaledHeight) : new Dimension((int)((double)currentWidth * heightRatio), newHeight);
return this.renderScaledImageOnLayer(layer, newSize, newWidth, newHeight);
}
public Layer applyStyleDataToImage(Image image, Style style) throws RepositoryException, IOException {
Layer layer = image.getLayer(false, false, false);
image.loadStyleData(style);
image.crop(layer);
image.rotate(layer);
return layer;
}
public static Layer renderScaledPlaceholderImage(int width, int height) {
Layer background = new Layer(width, height, (Paint)Color.white);
return background;
}
private Layer renderScaledImageOnLayer(Layer layer, Dimension scaledSize, int newWidth, int newHeight) {
layer.resize(scaledSize.width, scaledSize.height);
int shiftX = 0;
int shiftY = 0;
if (scaledSize.width != newWidth) {
shiftX = Math.abs(scaledSize.width - newWidth) / 2;
} else {
shiftY = Math.abs(scaledSize.height - newHeight) / 2;
}
Rectangle newDimensions = new Rectangle();
newDimensions.setBounds(shiftX, shiftY, newWidth, newHeight);
layer.crop((Rectangle2D)newDimensions);
return layer;
}
public static enum Quality {
LOW(0.4),
MEDIUM(0.82),
HIGH(1.0);
private double quality;
private Quality(double quality) {
this.quality = quality;
}
public double getQualityValue() {
return this.quality;
}
}
}