ImageUtils { @Nullable public static Image createJpegThumbnail(@Nullable final byte[] imageBytes, final int lengthOfLongestSideInPixels) throws IOException { if (imageBytes != null && imageBytes.length > 0 && lengthOfLongestSideInPixels > 0) { ImageOrientation orientation = ImageOrientation.getOrientation(new ByteArrayInputStream(imageBytes)); if (orientation == null) { orientation = ImageOrientation.ORIENTATION_1; } try { BufferedImage image = convertToBufferedImage(imageBytes); if (image != null) { if (image.getColorModel().hasAlpha()) { image = dropAlphaChannel(image); } return JpegImage.create(orientation.transform(Scalr.resize(image, Scalr.Method.AUTOMATIC, Scalr.Mode.AUTOMATIC, lengthOfLongestSideInPixels))); } } catch (Exception e) { final String message = "Exception while trying to create a thumbnail"; LOG.error(message, e); throw new IOException(e); } } return null; } private  ImageUtils(); static boolean isSupportedImage(@Nullable final byte[] imageBytes); @Nullable static Image createJpegThumbnail(@Nullable final byte[] imageBytes, final int lengthOfLongestSideInPixels); @Nullable static Geolocation getGeolocation(@Nullable final byte[] imageBytes); @Nullable static ImageOrientation getOrientation(@Nullable final byte[] imageBytes); @Nullable static BufferedImage convertToBufferedImage(@Nullable final byte[] imageBytes); @Nullable static byte[] convertToJpegByteArray(@Nullable final BufferedImage image); @Nullable static ImageType getImageType(@Nullable final byte[] imageBytes);  }
@Test public void testCreateThumbnail() throws Exception { Assert.assertNull(ImageUtils.createJpegThumbnail(null, -1)); Assert.assertNull(ImageUtils.createJpegThumbnail(null, 0)); Assert.assertNull(ImageUtils.createJpegThumbnail(null, 100)); Assert.assertNull(ImageUtils.createJpegThumbnail(new byte[]{}, -1)); Assert.assertNull(ImageUtils.createJpegThumbnail(new byte[]{}, 0)); Assert.assertNull(ImageUtils.createJpegThumbnail(new byte[]{}, 100)); Assert.assertNull(ImageUtils.createJpegThumbnail(IMAGE_1, -1)); Assert.assertNull(ImageUtils.createJpegThumbnail(IMAGE_1, 0)); Assert.assertNull(ImageUtils.createJpegThumbnail(NOT_AN_IMAGE, 100)); testCreateThumbnailHelper(IMAGE_1, 50, new Dimension(47, 50)); testCreateThumbnailHelper(IMAGE_1, 100, new Dimension(93, 100)); testCreateThumbnailHelper(IMAGE_1, (int)Math.max(IMAGE_1_EXPECTED_SIZE.getHeight(), IMAGE_1_EXPECTED_SIZE.getWidth()), IMAGE_1_EXPECTED_SIZE); testCreateThumbnailHelper(IMAGE_2, 50, new Dimension(38, 50)); testCreateThumbnailHelper(IMAGE_2, 100, new Dimension(75, 100)); testCreateThumbnailHelper(IMAGE_2, (int)Math.max(IMAGE_2_EXPECTED_SIZE.getHeight(), IMAGE_2_EXPECTED_SIZE.getWidth()), IMAGE_2_EXPECTED_SIZE); testCreateThumbnailHelper(IMAGE_3, 50, new Dimension(38, 50)); testCreateThumbnailHelper(IMAGE_3, 100, new Dimension(75, 100)); testCreateThumbnailHelper(IMAGE_3, (int)Math.max(IMAGE_3_EXPECTED_SIZE.getHeight(), IMAGE_3_EXPECTED_SIZE.getWidth()), IMAGE_3_EXPECTED_SIZE); testCreateThumbnailHelper(IMAGE_4, 50, new Dimension(47, 50)); testCreateThumbnailHelper(IMAGE_4, 100, new Dimension(93, 100)); testCreateThumbnailHelper(IMAGE_4, (int)Math.max(IMAGE_4_EXPECTED_SIZE.getHeight(), IMAGE_4_EXPECTED_SIZE.getWidth()), IMAGE_4_EXPECTED_SIZE); testCreateThumbnailHelper(IMAGE_6, 50, new Dimension(43, 50)); testCreateThumbnailHelper(IMAGE_6, 100, new Dimension(85, 100)); testCreateThumbnailHelper(IMAGE_6, (int)Math.max(IMAGE_6_EXPECTED_SIZE.getHeight(), IMAGE_6_EXPECTED_SIZE.getWidth()), IMAGE_6_EXPECTED_SIZE); }