List of usage examples for org.apache.commons.imaging Imaging writeImageToBytes
public static byte[] writeImageToBytes(final BufferedImage src, final ImageFormat format, final Map<String, Object> params) throws ImageWriteException, IOException
From source file:com.pronoiahealth.olhie.server.services.BookCoverImageService.java
/** * Create the front cover//from w ww. j a v a2 s. c om * * @param coverId * @param logoBytes * @param authorStr * @param titleStr * @param textColor * - ex. #FFFFFF * @param width * @param height * @param type * - ex. BufferedImage.TYPE_INT_ARGB * @param imgFormat * - ex. ImageFormat.IMAGE_FORMAT_PNG * @return * @throws Exception */ public byte[] createFrontCover(String coverId, byte[] logoBytes, String authorStr, String titleStr, String spineColor, String authorTextColor, String titleTextColor, int width, int height, int type, ImageFormat imgFormat, int maxColors) throws Exception { Graphics2D g = null; try { // Front cover first // Read in base cover image BufferedImage coverImg = Imaging.getBufferedImage(coverMap.get(coverId)); // Resize cover image to the basic 300 X 400 for front cover BufferedImage frontCoverImg = resize(coverImg, 300, 400, type); g = (Graphics2D) frontCoverImg.getGraphics(); // Draw logo if present if (logoBytes != null && logoBytes.length > 0) { // Resize logo to 200x100 BufferedImage logoImg = Imaging.getBufferedImage(logoBytes); BufferedImage outLogo = null; int logoHeight = logoImg.getHeight(); int logoWidth = logoImg.getWidth(); if (logoHeight > 100 || logoWidth > 200) { outLogo = this.resize(logoImg, logoWidth > 200 ? 200 : logoWidth, logoHeight > 100 ? 100 : logoHeight, type); } else { outLogo = logoImg; } // Add to coverImg g.drawImage(outLogo, 32, 25, null); } // Add spine if present if (spineColor != null && spineColor.length() > 0) { g.setColor(Color.decode(spineColor)); g.fillRect(0, 0, 2, frontCoverImg.getHeight()); } // Add author if present if (authorStr != null && authorStr.length() > 0) { // Add author text to image BufferedImage authorTextImg = createText(40, 220, authorStr, authorTextColor, false, authorFontMap, type); g.drawImage(authorTextImg, 30, 215, null); } // Add title if present if (titleStr != null && titleStr.length() > 0) { BufferedImage titleTextImg = createText(100, 220, titleStr, titleTextColor, false, titleFontMap, type); g.drawImage(titleTextImg, 30, 240, null); } // If the requested size is not 300X400 convert the image BufferedImage outImg = null; if (width != 300 || height != 400) { outImg = resize(frontCoverImg, width, height, type); } else { outImg = frontCoverImg; } // Do we want a PNG with a fixed number of colors if (maxColors >= 2 && imgFormat == ImageFormat.IMAGE_FORMAT_PNG) { outImg = ImageUtils.reduce32(outImg, maxColors); } // Return bytes Map<String, Object> params = new HashMap<String, Object>(); byte[] outBytes = Imaging.writeImageToBytes(outImg, imgFormat, params); return outBytes; } finally { if (g != null) { g.dispose(); } } }
From source file:com.itextpdf.text.pdf.pdfcleanup.PdfCleanUpRenderListener.java
private byte[] processImage(byte[] imageBytes, List<Rectangle> areasToBeCleaned) { if (areasToBeCleaned.isEmpty()) { return imageBytes; }/*from w w w.j a v a 2 s . c om*/ try { BufferedImage image = Imaging.getBufferedImage(imageBytes); ImageInfo imageInfo = Imaging.getImageInfo(imageBytes); cleanImage(image, areasToBeCleaned); // Apache can only read JPEG, so we should use awt for writing in this format if (imageInfo.getFormat() == ImageFormats.JPEG) { return getJPGBytes(image); } else { Map<String, Object> params = new HashMap<String, Object>(); if (imageInfo.getFormat() == ImageFormats.TIFF) { params.put(ImagingConstants.PARAM_KEY_COMPRESSION, TiffConstants.TIFF_COMPRESSION_LZW); } return Imaging.writeImageToBytes(image, imageInfo.getFormat(), params); } } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.pronoiahealth.olhie.server.services.BookCoverImageService.java
/** * Create a back cover/* ww w.j a v a2 s .co m*/ * * @param coverId * @param titleStr * @param textColor * @param width * @param height * @param type * @param imgFormat * @param macColors * - for PNG images reduce the color palette (must be greater * than 2) * @return * @throws Exception */ public byte[] createBackCover(String coverId, String titleStr, String spineColor, String textColor, int width, int height, int type, ImageFormat imgFormat, int maxColors) throws Exception { Graphics2D g2D = null; try { // Front cover first // Read in base cover image BufferedImage coverImg = Imaging.getBufferedImage(coverMap.get(coverId)); // Resize cover image to the basic 300 X 400 for front cover BufferedImage backCoverImg = resize(coverImg, 300, 400, type); g2D = (Graphics2D) backCoverImg.getGraphics(); // Add title if present if (titleStr != null && titleStr.length() > 0) { BufferedImage titleTextImg = createText(82, 220, titleStr, textColor, true, backTitleFontMap, type); g2D.drawImage(titleTextImg, 40, 35, null); } // Add spine if present if (spineColor != null && spineColor.length() > 0) { g2D.setColor(Color.decode(spineColor)); g2D.fillRect(backCoverImg.getWidth() - 2, 0, 2, backCoverImg.getHeight()); } // If the requested size is not 300X400 convert the image BufferedImage outImg = null; if (width != 300 || height != 400) { outImg = resize(backCoverImg, width, height, type); } else { outImg = backCoverImg; } // Do we want a PNG with a fixed number of colors if (maxColors >= 2 && imgFormat == ImageFormat.IMAGE_FORMAT_PNG) { outImg = ImageUtils.reduce32(outImg, maxColors); } // Return bytes Map<String, Object> params = new HashMap<String, Object>(); byte[] outBytes = Imaging.writeImageToBytes(outImg, imgFormat, params); return outBytes; } finally { if (g2D != null) { g2D.dispose(); } } }
From source file:org.apache.commons.imaging.examples.ImageWriteExample.java
public static byte[] imageWriteExample(final File file) throws ImageReadException, ImageWriteException, IOException { // read image final BufferedImage image = Imaging.getBufferedImage(file); final ImageFormat format = ImageFormats.TIFF; final Map<String, Object> params = new HashMap<>(); // set optional parameters if you like params.put(ImagingConstants.PARAM_KEY_COMPRESSION, Integer.valueOf(TiffConstants.TIFF_COMPRESSION_UNCOMPRESSED)); final byte[] bytes = Imaging.writeImageToBytes(image, format, params); return bytes; }