1. Project Clover database Wed Jan 17 2024 23:40:18 CST
  2. Package org.europa.together.application

File ImgSclrProcessor.java

 

Coverage histogram

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

Code metrics

26
117
18
1
270
238
41
0.35
6.5
18
2.28

Classes

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