private <T> T scaleImageUsingAffineTransformation(final BufferedImage bufferedImage, T target) { BufferedImage destinationImage = generateDestinationImage(); Graphics2D graphics2D = destinationImage.createGraphics(); AffineTransform transformation = AffineTransform.getScaleInstance( ((double) getQualifiedWidth() / bufferedImage.getWidth()), ((double) getQualifiedHeight() / bufferedImage.getHeight())); graphics2D.drawRenderedImage(bufferedImage, transformation); graphics2D.addRenderingHints(retrieveRenderingHints()); try { if (target instanceof File) { LOGGER.info(String.format(M_TARGET_TYPE_OF, "File")); ImageIO.write(destinationImage, imageType.toString(), (File) target); } else if (target instanceof ImageOutputStream) { LOGGER.info(String.format(M_TARGET_TYPE_OF, "ImageOutputStream")); ImageIO.write(destinationImage, imageType.toString(), (ImageOutputStream) target); } else if (target instanceof OutputStream) { LOGGER.info(String.format(M_TARGET_TYPE_OF, "OutputStream")); ImageIO.write(destinationImage, imageType.toString(), (OutputStream) target); } else { target = null; } } catch (IOException e) { e.printStackTrace(); } return target; }
public static Image get(ImageType imageType) { assertNotNull(imageType, "iamgeType"); final Image image = images[imageType.ordinal()]; assertNotNull(image, imageType.toString()); return image; }
private static ImageElement fetchImageProperties( final BufferedImage source, final ImageType imageType, final String id, final Date modifiedDate, final String mimeType) throws NullPointerException, IOException { if (verifyNotNull(source)) { ImageElement image = new ImageElement(); image.setBitDepth( verifyNotNull(source.getColorModel().getPixelSize()) ? source.getColorModel().getPixelSize() : 0); image.setHeight(verifyNotNull(source.getHeight()) ? source.getHeight() : 0); image.setWidth(verifyNotNull(source.getWidth()) ? source.getWidth() : 0); image.setSizeInBytes(determineSizeOfBufferedImage(source, imageType.toString())); image.setCreated(new Date(System.currentTimeMillis())); image.setTransparent( determineTransparencyType(source.getGraphics().getColor().getTransparency())); if (verifyNotNull(modifiedDate)) image.setModified(modifiedDate); image.setId((verifyNotNull(id) ? id : "")); image.setFontType( verifyNotNull(source.getGraphics().getFont()) ? source.getGraphics().getFont() : new Font("Default", 0, 0)); image.setMediaType(MediaType.Missing); return image; } throw new NullPointerException(E_OBJECT_WAS_NULL); }
public class Images { private static final Image[] images = new Image[ImageType.values().length]; public static void loadImageResources() throws IOException { final ClassLoader cl = Images.class.getClassLoader(); for (ImageType imageType : ImageType.values()) { images[imageType.ordinal()] = readFromImageDir(imageType.getFileName()); } } public static Image get(ImageType imageType) { assertNotNull(imageType, "iamgeType"); final Image image = images[imageType.ordinal()]; assertNotNull(image, imageType.toString()); return image; } private static void assertNotNull(Object o, String msg) { if (o == null) { throw new AssertionError("object was null: " + msg); } } private static Image readFromImageDir(String fileName) { final String relativePath = "image/" + fileName; try { InputStream imageStream = ResourceFinder.getResourceStream(relativePath); final Image img = ImageIO.read(imageStream); return img; } catch (Exception e) { throw new IllegalStateException("couldn't find image: " + relativePath); } } }
private static BufferedImage convert(final BufferedImage source, final ImageType imageType) throws NullPointerException, IOException { if (verifyNotNull(source)) { File targetedFile = null; ImageIO.write(source, imageType.toString(), targetedFile); if (verifyNotNull(targetedFile)) { return ImageIO.read(targetedFile); } } throw new NullPointerException( "Something bad happened. You should never try that again. Ever."); }
public static void loadImageResources() throws IOException { final ClassLoader cl = Images.class.getClassLoader(); for (ImageType imageType : ImageType.values()) { images[imageType.ordinal()] = readFromImageDir(imageType.getFileName()); } }