/** * Returns the given page as an RGB or ARGB image at the given scale. * * @param pageIndex the zero-based index of the page to be converted * @param scale the scaling factor, where 1 = 72 DPI * @param imageType the type of image to return * @return the rendered page image * @throws IOException if the PDF cannot be read */ public BufferedImage renderImage(int pageIndex, float scale, ImageType imageType) throws IOException { PDPage page = document.getPage(pageIndex); PDRectangle cropbBox = page.getCropBox(); float widthPt = cropbBox.getWidth(); float heightPt = cropbBox.getHeight(); int widthPx = Math.round(widthPt * scale); int heightPx = Math.round(heightPt * scale); int rotationAngle = page.getRotation(); // swap width and height BufferedImage image; if (rotationAngle == 90 || rotationAngle == 270) { image = new BufferedImage(heightPx, widthPx, imageType.toBufferedImageType()); } else { image = new BufferedImage(widthPx, heightPx, imageType.toBufferedImageType()); } // use a transparent background if the imageType supports alpha Graphics2D g = image.createGraphics(); if (imageType == ImageType.ARGB) { g.setBackground(new Color(0, 0, 0, 0)); } else { g.setBackground(Color.WHITE); } renderPage(page, g, image.getWidth(), image.getHeight(), scale, scale); g.dispose(); return image; }
public static Image get(ImageType imageType) { assertNotNull(imageType, "iamgeType"); final Image image = images[imageType.ordinal()]; assertNotNull(image, imageType.toString()); return image; }
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 ImageType getTypeFromString(String val) { for (ImageType t : ImageType.values()) { if (t.getTypeId().equals(val)) { return t; } } return null; }
/** * Gets the actual thumbnail image data of an image. * * @return Returns said image data or <code>null</code> is this image has no such image data. */ public synchronized RenderedImage getThumbnailImage() throws BadImageFileException, ColorProfileException, IOException, UnknownImageTypeException { RenderedImage strongRef = m_thumbnailImageRef != null ? m_thumbnailImageRef.get() : null; if (strongRef == null) { final ImageType t = getImageType(); strongRef = t.getThumbnailImage(this); if (strongRef != null) m_thumbnailImageRef = new WeakReference<RenderedImage>(strongRef); } return strongRef; }
/** * Gets the actual image data of an image. * * @param thread The thread that will do the getting. * @param read2ndTIFFImage If <code>true</code> and the image file is a TIFF file and it has a * second TIFF image, read it instead of the first image. If the file is not a TIFF file, this * flag has no effect. * @return Returns said image data. */ public synchronized PlanarImage getImage(ProgressThread thread, boolean read2ndTIFFImage) throws BadImageFileException, ColorProfileException, IOException, UnknownImageTypeException, UserCanceledException { PlanarImage strongRef = m_imageRef != null ? m_imageRef.get() : null; if (strongRef == null) { final ImageType t = getImageType(); if (t instanceof TIFFImageType) strongRef = TIFFImageType.getImage(this, thread, read2ndTIFFImage); else strongRef = t.getImage(this, thread); if (strongRef != null) m_imageRef = new WeakReference<PlanarImage>(strongRef); } return strongRef; }
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); }
private Map<Album, Map<ImageType, Resource>> collectAlbumTargets( Library library, final ResourceLocator resourceLocator, Collection<Album> changedAlbums) { Map<Album, Map<ImageType, Resource>> result = new HashMap<>(); for (Album album : library.getAlbums()) { Resource artworkAssetResource = album.artworkAssetResource(); if (artworkAssetResource != null) { Map<ImageType, Resource> targets = new HashMap<ImageType, Resource>(); for (ImageType type : ImageType.values()) { String imagePath = resourceLocator.getAlbumImagePath(album, type); if (imagePath != null) { Resource resource = resourceLocator.getResource(imagePath); try { if (changedAlbums.contains(album) || !resource.exists()) { resource.getParent().mkdirs(); targets.put(type, resource); } } catch (IOException e) { LOGGER.warning("Could not write image file: " + resource.getPath().toAbsolutePath()); } } } if (!targets.isEmpty()) { result.put(album, targets); } } } return result; }
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 GrayFilterImageResource(BufferedImage image, ImageType imageType) { super(image.getWidth(), image.getHeight(), imageType.getFormat()); setType(BufferedImage.TYPE_INT_ARGB); grayImage = SmallMindGrayFilter.createDisabledImage(image); }
private void writeImage(BufferedImage image, ImageType type, OutputStream output) throws IOException { double scaleFactor = type.getScaleFactor(image.getWidth(), image.getHeight()); if (retina) { scaleFactor = scaleFactor + scaleFactor; } BufferedImage scaledImage = null; if (scaleFactor < 1.0) { // scale down only scaledImage = Thumbnails.of(image).scale(scaleFactor).asBufferedImage(); } try { ImageIO.write(scaledImage != null ? scaledImage : image, type.getFileType(), output); } finally { if (scaledImage != null) { scaledImage.flush(); } } }
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."); }
@XmlType @XmlEnum(String.class) public static enum ImageType { @XmlEnumValue("iso") ISO("iso"), @XmlEnumValue("floppy") FLOPPY("floppy"), UNRECOGNIZED("unrecognized"); public static final List<ImageType> ALL = ImmutableList.of(ISO, FLOPPY); protected final String stringValue; ImageType(String stringValue) { this.stringValue = stringValue; } public String value() { return stringValue; } protected static final Map<String, ImageType> STATUS_BY_ID = Maps.uniqueIndex( ImmutableSet.copyOf(ImageType.values()), new Function<ImageType, String>() { @Override public String apply(ImageType input) { return input.stringValue; } }); public static ImageType fromValue(String value) { ImageType type = STATUS_BY_ID.get(checkNotNull(value, "stringValue")); return type == null ? UNRECOGNIZED : type; } }
/** * Determines the preliminary {@link ImageType} for this image based solely on the image file's * filename extension. Unfortunately, this may not be correct because of the special case of some * raw images being in files having a <code>.TIF</code> extension. * * <p>The reason that determining the {@link ImageType} has to be split into two parts is to * prevent an infinite loop during final {@link ImageType} determination that can use an image's * metadata that in turn needs to know the {@link ImageType} in order to read the metadata. (The * preliminary {@link ImageType} is good enough for reading metadata since all raw images in files * having a <code>.TIF</code> extension use TIFF metadata.) * * @throws UnknownImageTypeException if the image type could not be determined. * @see #determineFinalImageType() */ private void determinePreliminaryImageType() throws UnknownImageTypeException { if (m_imageType == null) { m_imageType = ImageType.determineTypeByExtensionOf(m_imageFile); if (m_imageType == null) throw new UnknownImageTypeException(m_imageFile.getName()); } }
/** {@inheritDoc} */ @Override protected TransitionDrawable doInBackground(final String... params) { // Define the key mKey = params[0]; // The result Bitmap bitmap = null; // First, check the disk cache for the image if (mKey != null && mImageCache != null && !isCancelled() && getAttachedImageView() != null) { bitmap = mImageCache.getCachedBitmap(mKey); } // Define the album id now mAlbumId = Long.valueOf(params[3]); // Second, if we're fetching artwork, check the device for the image if (bitmap == null && mImageType.equals(ImageType.ALBUM) && mAlbumId >= 0 && mKey != null && !isCancelled() && getAttachedImageView() != null && mImageCache != null) { bitmap = mImageCache.getCachedArtwork(mContext, mKey, mAlbumId); } // Third, by now we need to download the image if (bitmap == null && ApolloUtils.isOnline(mContext) && !isCancelled() && getAttachedImageView() != null) { // Now define what the artist name, album name, and url are. mArtistName = params[1]; mAlbumName = params[2]; mUrl = processImageUrl(mArtistName, mAlbumName, mImageType); if (mUrl != null) { bitmap = processBitmap(mUrl); } } // Fourth, add the new image to the cache if (bitmap != null && mKey != null && mImageCache != null) { addBitmapToCache(mKey, bitmap); } // Add the second layer to the transiation drawable if (bitmap != null) { final BitmapDrawable layerTwo = new BitmapDrawable(mResources, bitmap); layerTwo.setFilterBitmap(false); layerTwo.setDither(false); mArrayDrawable[1] = layerTwo; // Finally, return the image final TransitionDrawable result = new TransitionDrawable(mArrayDrawable); result.setCrossFadeEnabled(true); result.startTransition(FADE_IN_TIME); return result; } return null; }
public static void loadImageResources() throws IOException { final ClassLoader cl = Images.class.getClassLoader(); for (ImageType imageType : ImageType.values()) { images[imageType.ordinal()] = readFromImageDir(imageType.getFileName()); } }
/** * Gets the actual preview image data of an image. * * @param maxWidth The maximum width of the image to get, rescaling if necessary. A value of 0 * means don't scale. * @param maxHeight The maximum height of the image to get, rescaling if necessary. A value of 0 * means don't scale. * @return Returns said image data or <code>null</code> is this image has no such image data. */ public synchronized RenderedImage getPreviewImage(int maxWidth, int maxHeight) throws BadImageFileException, ColorProfileException, IOException, UnknownImageTypeException { RenderedImage strongRef = m_previewImageRef != null ? m_previewImageRef.get() : null; if (strongRef == null) { final ImageType t = getImageType(); strongRef = t.getPreviewImage(this, maxWidth, maxHeight); if (strongRef != null) m_previewImageRef = new WeakReference<RenderedImage>(strongRef); } return strongRef; }