|
@@ -7,7 +7,10 @@ import java.io.ByteArrayOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
|
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
public class ImageDownloader {
|
|
public class ImageDownloader {
|
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(ImageDownloader.class);
|
|
|
|
|
|
public static byte[] downloadUiAvatar(
|
|
public static byte[] downloadUiAvatar(
|
|
int size,
|
|
int size,
|
|
@@ -43,40 +46,38 @@ public class ImageDownloader {
|
|
}
|
|
}
|
|
|
|
|
|
public static byte[] downloadImage(String urlString) throws IOException {
|
|
public static byte[] downloadImage(String urlString) throws IOException {
|
|
|
|
+ try{
|
|
|
|
+ URL url = new URL(urlString);
|
|
|
|
|
|
- URL url = new URL(urlString);
|
|
|
|
|
|
+ BufferedImage imageIn = ImageIO.read(url);
|
|
|
|
|
|
- BufferedImage imageIn;
|
|
|
|
|
|
+ BufferedImage imageOut = new BufferedImage(
|
|
|
|
+ imageIn.getWidth(),
|
|
|
|
+ imageIn.getHeight(),
|
|
|
|
+ BufferedImage.TYPE_INT_RGB);
|
|
|
|
|
|
- try {
|
|
|
|
- imageIn = ImageIO.read(url);
|
|
|
|
- }
|
|
|
|
- catch (IOException ex) {
|
|
|
|
- ex.printStackTrace();
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (imageIn == null) {
|
|
|
|
- System.out.println("Downloaded UIAvatar image is null");
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- BufferedImage imageOut = new BufferedImage(
|
|
|
|
- imageIn.getWidth(),
|
|
|
|
- imageIn.getHeight(),
|
|
|
|
- BufferedImage.TYPE_INT_RGB);
|
|
|
|
|
|
+ imageOut.createGraphics().drawImage(imageIn, 0, 0, Color.WHITE, null);
|
|
|
|
|
|
- imageOut.createGraphics().drawImage(imageIn, 0, 0, Color.WHITE, null);
|
|
|
|
|
|
+ byte[] imageData;
|
|
|
|
|
|
- byte[] imageData;
|
|
|
|
-
|
|
|
|
- try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
|
|
|
|
|
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
+ ImageIO.write(imageOut, "jpg", baos);
|
|
|
|
+ imageData = baos.toByteArray();
|
|
|
|
+
|
|
|
|
+ return imageData;
|
|
|
|
+ }catch(Exception e){
|
|
|
|
+ log.info( "Can't download "+urlString+". Use empty image.");
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ BufferedImage imageOut = new BufferedImage(
|
|
|
|
+ 64,
|
|
|
|
+ 64,
|
|
|
|
+ BufferedImage.TYPE_INT_RGB);
|
|
|
|
+ byte[] imageData;
|
|
|
|
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
ImageIO.write(imageOut, "jpg", baos);
|
|
ImageIO.write(imageOut, "jpg", baos);
|
|
imageData = baos.toByteArray();
|
|
imageData = baos.toByteArray();
|
|
|
|
+ return imageData;
|
|
}
|
|
}
|
|
-
|
|
|
|
- return imageData;
|
|
|
|
-
|
|
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|