The thumbnail generator now creates thumbnails having white background color, instead of black, when the original image has a transparent background

git-svn-id: https://svn.research-infrastructures.eu/d4science/gcube/trunk/portlets/user/share-updates@140001 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-12-14 16:16:06 +00:00
parent d89ac94e50
commit 4ca241f6e6
1 changed files with 35 additions and 4 deletions

View File

@ -1,9 +1,11 @@
package org.gcube.portlets.user.shareupdates.server;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@ -56,7 +58,7 @@ public class FilePreviewer {
private static final String PDF_DEFAULT_IMAGE = "default/pdf.png";
private static final String GENERICFILE_DEFAULT_IMAGE = "default/default_generic.png";
private static final String UPLOAD_LOCATION_LOCAL = System.getProperty("java.io.tmpdir");
/**
* these are the extension for which I have an icon image preview
*/
@ -155,13 +157,20 @@ public class FilePreviewer {
out = new ByteArrayOutputStream();
Thumbnails.of(path2Image)
// get the buffered image
BufferedImage bufferedImage = Thumbnails.of(path2Image).scale(1).asBufferedImage();
bufferedImage = changeBackgroundColorToWhite(bufferedImage);
// write
Thumbnails.fromImages(Arrays.asList(bufferedImage))
.width(80)
.outputFormat("jpg")
.toOutputStream(out);
}
catch (IOException e) {
e.printStackTrace();
_log.warn("Thumbnail extraction failed for this reason: " + e.getMessage());
}
String httpLink = getFTPManager().uploadImageOnFTPServer(new ByteArrayInputStream(out.toByteArray()), ImageType.JPG);
_log.debug("\nFlushed, Image thumbnail available at: " + httpLink);
@ -169,6 +178,28 @@ public class FilePreviewer {
return new LinkPreview(fileName, desc, httpUrl, mimeType, imagesUrl);
}
private static BufferedImage changeBackgroundColorToWhite(BufferedImage bufferedImage){
BufferedImage changedImage = null;
try{
// if the image has a transparent background color, it will be changed to white
if(bufferedImage.getColorModel().getTransparency() != Transparency.OPAQUE) {
_log.debug("Background color is going to be changed to white for the thumbnail");
int w = bufferedImage.getWidth();
int h = bufferedImage.getHeight();
changedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = changedImage.createGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,w,h);
g.drawRenderedImage(bufferedImage, null);
g.dispose();
}
}catch(Exception e){
_log.warn("Failed to change the transparent background to white for image");
}
return changedImage == null ? bufferedImage : changedImage;
}
/**
*
* @param fileNameLabel thename of the file
@ -261,7 +292,7 @@ public class FilePreviewer {
MediaType mediaType = detector.detect(stream, metadata);
return mediaType.getBaseType().toString();
}
/**
* Check if a resource at a given url exists or not. If it exists, create a file in the temp dir
* of the tomcat