1. Project Clover database Mon Dec 22 2025 14:59:38 CST
  2. Package org.europa.together.application

File ImgSclrProcessor.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

26
122
19
1
281
248
42
0.34
6.42
19
2.21

Classes

Class Line # Actions
ImgSclrProcessor 25 122 0% 42 0
1.0100%
 

Contributing tests

This file is covered by 28 tests. .

Source view

1    package org.europa.together.application;
2   
3    import java.awt.Graphics;
4    import java.awt.image.BufferedImage;
5    import java.awt.image.DataBuffer;
6    import java.io.File;
7    import java.util.ArrayList;
8    import java.util.List;
9    import javax.imageio.ImageIO;
10    import org.apache.commons.imaging.ImageFormat;
11    import org.apache.commons.imaging.Imaging;
12    import org.apache.commons.imaging.common.ImageMetadata;
13    import org.apache.commons.imaging.common.ImageMetadata.ImageMetadataItem;
14    import org.europa.together.business.ImageProcessor;
15    import org.europa.together.business.Logger;
16    import org.europa.together.domain.LogLevel;
17    import org.europa.together.exceptions.MisconfigurationException;
18    import org.imgscalr.Scalr;
19    import org.springframework.stereotype.Repository;
20   
21    /**
22    * Implementation of a simple image processor.
23    */
24    @Repository
 
25    public class ImgSclrProcessor implements ImageProcessor {
26   
27    private static final long serialVersionUID = 12L;
28    private static final Logger LOGGER = new LogbackLogger(ImgSclrProcessor.class);
29   
30    private static final long MULTIPLIER = 4L;
31    private static final int DIVISOR = 100;
32    private BufferedImage image = null;
33    private ImageMetadata metadata = null;
34    private String fileName = null;
35    private String fileExtension = null;
36    private int height = 0;
37    private int width = 0;
38   
39    /**
40    * Constructor.
41    */
 
42  1 toggle public ImgSclrProcessor() {
43  1 LOGGER.log("instance class", LogLevel.INFO);
44    }
45   
 
46  2 toggle @Override
47    public boolean loadImage(final BufferedImage image) {
48  2 boolean success = false;
49  2 if (isImageSet()) {
50  1 this.clearImage();
51    }
52  2 if (image != null) {
53  1 this.image = copyImage(image);
54  1 this.height = image.getHeight();
55  1 this.width = image.getWidth();
56  1 success = true;
57    }
58  2 return success;
59    }
60   
 
61  28 toggle @Override
62    public boolean loadImage(final File imageFile) {
63  28 boolean success = false;
64  28 if (isImageSet()) {
65  1 this.clearImage();
66    }
67  28 try {
68  28 this.image = Imaging.getBufferedImage(imageFile);
69  27 this.metadata = Imaging.getMetadata(imageFile);
70  27 this.fileName = imageFile.getName();
71  27 this.height = image.getHeight();
72  27 this.width = image.getWidth();
73  27 LOGGER.log("Load Image: " + this.fileName, LogLevel.DEBUG);
74  27 ImageFormat format = Imaging.guessFormat(imageFile);
75  27 this.fileExtension = format.getDefaultExtension();
76  27 LOGGER.log("Extension: " + this.fileExtension, LogLevel.DEBUG);
77  27 success = true;
78    } catch (Exception ex) {
79  1 LOGGER.catchException(ex);
80    }
81  28 return success;
82    }
83   
 
84  13 toggle @Override
85    public boolean saveImage(final BufferedImage renderedImage, final File file,
86    final String format) throws MisconfigurationException {
87  13 boolean success = false;
88  13 if (!isFormatAccepted(format)) {
89  1 throw new MisconfigurationException(format + " is not supported.");
90    }
91  12 try {
92  12 if (format.equalsIgnoreCase("png")) {
93  8 ImageIO.write(renderedImage, "PNG", file);
94    }
95  12 if (format.equalsIgnoreCase("gif")) {
96  1 ImageIO.write(renderedImage, "GIF", file);
97    }
98  12 if (format.equalsIgnoreCase("jpg")) {
99  2 int w = renderedImage.getWidth();
100  1 int h = renderedImage.getHeight();
101  1 BufferedImage newImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
102  1 int[] rgb = renderedImage.getRGB(0, 0, w, h, null, 0, w);
103  1 newImage.setRGB(0, 0, w, h, rgb, 0, w);
104  1 ImageIO.write(newImage, "JPG", file);
105    }
106  11 success = true;
107  11 String msg = "Image " + this.fileName + " successful to: "
108    + file.getPath() + " saved.";
109  11 LOGGER.log(msg, LogLevel.DEBUG);
110    } catch (Exception ex) {
111  1 LOGGER.catchException(ex);
112    }
113  12 return success;
114    }
115   
 
116  2 toggle @Override
117    public long getImageSize(final BufferedImage image) {
118  2 long size = 0;
119  2 if (image != null) {
120  1 DataBuffer dataBuffer = image.getData().getDataBuffer();
121  1 size = ((long) dataBuffer.getSize()) * MULTIPLIER;
122    }
123  2 return size;
124    }
125   
 
126  6 toggle @Override
127    public int getHeight() {
128  6 return this.height;
129    }
130   
 
131  6 toggle @Override
132    public int getWidth() {
133  6 return this.width;
134    }
135   
 
136  37 toggle @Override
137    public void clearImage() {
138  37 this.image = null;
139  37 this.metadata = null;
140  37 this.fileName = null;
141  37 this.fileExtension = null;
142  37 this.height = 0;
143  37 this.width = 0;
144  37 LOGGER.log("Loaded image reset.", LogLevel.TRACE);
145    }
146   
 
147  2 toggle @Override
148    public BufferedImage crop(final int x, final int y, final int height, final int width)
149    throws MisconfigurationException {
150  2 BufferedImage renderedImg;
151  2 try {
152  2 renderedImg = Scalr.crop(this.image, x, y, width, height);
153   
154    } catch (Exception ex) {
155  1 LOGGER.catchException(ex);
156  1 throw new MisconfigurationException(ex.getMessage());
157    }
158  1 return renderedImg;
159    }
160   
 
161  2 toggle @Override
162    public BufferedImage flipHorizontal() throws MisconfigurationException {
163  2 BufferedImage renderdImg;
164  2 try {
165  2 renderdImg = Scalr.rotate(this.image, Scalr.Rotation.FLIP_HORZ);
166    } catch (Exception ex) {
167  1 LOGGER.catchException(ex);
168  1 throw new MisconfigurationException(ex.getMessage());
169    }
170  1 return renderdImg;
171    }
172   
 
173  2 toggle @Override
174    public BufferedImage flipVertical() throws MisconfigurationException {
175  2 BufferedImage renderdImg;
176  2 try {
177  2 renderdImg = Scalr.rotate(this.image, Scalr.Rotation.FLIP_VERT);
178    } catch (Exception ex) {
179  1 LOGGER.catchException(ex);
180  1 throw new MisconfigurationException(ex.getMessage());
181    }
182  1 return renderdImg;
183    }
184   
 
185  7 toggle @Override
186    public BufferedImage getImage() {
187  7 return copyImage(this.image);
188    }
189   
 
190  5 toggle @Override
191    public BufferedImage resize(final int percentage) throws MisconfigurationException {
192  5 BufferedImage renderdImg;
193  5 if (percentage < 1) {
194  1 throw new MisconfigurationException(
195    "The size of the new Image have to have minimum 1%.");
196    } else {
197  4 try {
198  4 int newHeight = (this.height * percentage) / DIVISOR;
199  4 int newWidth = (this.width * percentage) / DIVISOR;
200  4 String msg = "Image " + this.fileName + " resize (" + percentage + "%)"
201    + " to height:" + newHeight
202    + " width:" + newWidth;
203  4 LOGGER.log(msg, LogLevel.DEBUG);
204   
205  4 renderdImg = Scalr.resize(this.image, newWidth, newHeight);
206   
207    } catch (Exception ex) {
208  1 LOGGER.catchException(ex);
209  1 throw new MisconfigurationException(ex.getMessage());
210    }
211    }
212  3 return renderdImg;
213    }
214   
 
215  2 toggle @Override
216    public BufferedImage rotateRight() throws MisconfigurationException {
217  2 BufferedImage renderedImg;
218  2 try {
219  2 renderedImg = Scalr.rotate(this.image, Scalr.Rotation.CW_90);
220    } catch (Exception ex) {
221  1 LOGGER.catchException(ex);
222  1 throw new MisconfigurationException(ex.getMessage());
223    }
224  1 return renderedImg;
225    }
226   
 
227  4 toggle @Override
228    public List<ImageMetadataItem> getMetaData() {
229  4 List<ImageMetadataItem> collection = new ArrayList<>();
230  4 if (this.metadata != null) {
231  2 collection.addAll(metadata.getItems());
232    }
233  4 return collection;
234    }
235   
 
236  4 toggle @Override
237    public String toString() {
238  4 StringBuilder out = new StringBuilder();
239  4 List<ImageMetadataItem> metaList = new ArrayList<>();
240  4 metaList.addAll(getMetaData());
241   
242  4 if (!metaList.isEmpty()) {
243  2 for (final ImageMetadataItem item : metaList) {
244  110 out.append("\n\t" + item);
245    }
246    } else {
247  2 out.append("No meta data from " + fileName + " extracted.");
248    }
249  4 return out.toString();
250    }
251   
 
252  41 toggle @Override //make private
253    public boolean isImageSet() {
254  41 boolean success = false;
255  41 if (this.image != null) {
256  11 success = true;
257    }
258  41 return success;
259    }
260   
 
261  8 toggle @Override
262    public BufferedImage copyImage(final BufferedImage source) {
263  8 BufferedImage grapic = new BufferedImage(source.getWidth(),
264    source.getHeight(), source.getType());
265  8 Graphics graphics = grapic.getGraphics();
266  8 graphics.drawImage(source, 0, 0, null);
267  8 graphics.dispose();
268  8 return grapic;
269    }
270   
 
271  13 toggle private boolean isFormatAccepted(final String imageFormat) {
272  13 boolean accept = false;
273  13 if (imageFormat.equalsIgnoreCase("jpg")
274    || imageFormat.equalsIgnoreCase("jpeg")
275    || imageFormat.equalsIgnoreCase("png")
276    || imageFormat.equalsIgnoreCase("gif")) {
277  12 accept = true;
278    }
279  13 return accept;
280    }
281    }