Example usage for org.apache.commons.imaging ImageFormat IMAGE_FORMAT_PNG

List of usage examples for org.apache.commons.imaging ImageFormat IMAGE_FORMAT_PNG

Introduction

In this page you can find the example usage for org.apache.commons.imaging ImageFormat IMAGE_FORMAT_PNG.

Prototype

ImageFormat IMAGE_FORMAT_PNG

To view the source code for org.apache.commons.imaging ImageFormat IMAGE_FORMAT_PNG.

Click Source Link

Usage

From source file:com.pronoiahealth.olhie.server.services.BookCoverImageService.java

/**
 * Create the front cover//  ww w . jav  a 2s.  com
 * 
 * @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.pronoiahealth.olhie.server.services.BookCoverImageService.java

/**
 * Creates a small front cover//from  ww  w.  j  a  v  a  2 s. c om
 * 
 * @param book
 * @param category
 * @param cover
 * @param logo
 * @param authorName
 * @return
 * @throws Exception
 */
public String createDefaultSmallFrontCoverEncoded(Book book, BookCategory category, BookCover cover,
        byte[] logo, String authorName) throws Exception {
    return createFrontCoverEncoded(book.getCoverName(), logo, authorName, book.getBookTitle(),
            category.getColor(), cover.getAuthorTextColor(), cover.getCoverTitleTextColor(), 75, 100,
            BufferedImage.TYPE_INT_ARGB, ImageFormat.IMAGE_FORMAT_PNG, 128);
}

From source file:com.pronoiahealth.olhie.server.services.BookCoverImageService.java

/**
 * Creates a small front cover//  w  w w .  j  av  a 2  s .  c  o  m
 * 
 * @param book
 * @param category
 * @param cover
 * @param logo
 * @param authorName
 * @return
 * @throws Exception
 */
public byte[] createDefaultSmallFrontCover(Book book, BookCategory category, BookCover cover, byte[] logo,
        String authorName) throws Exception {
    return createFrontCover(book.getCoverName(), logo, authorName, book.getBookTitle(), category.getColor(),
            cover.getAuthorTextColor(), cover.getCoverTitleTextColor(), 75, 100, BufferedImage.TYPE_INT_ARGB,
            ImageFormat.IMAGE_FORMAT_PNG, 128);
}

From source file:com.pronoiahealth.olhie.server.services.BookCoverImageService.java

/**
 * //from w ww  .  j av  a  2 s.c  o m
 * @param book
 * @param category
 * @param cover
 * @param logo
 * @param author
 * @return
 * @throws Exception
 */
public String createDefaultFrontCoverEncoded(Book book, BookCategory category, BookCover cover, byte[] logo,
        String authorName) throws Exception {
    return createFrontCoverEncoded(book.getCoverName(), logo, authorName, book.getBookTitle(),
            category.getColor(), cover.getAuthorTextColor(), cover.getCoverTitleTextColor(), 300, 400,
            BufferedImage.TYPE_INT_ARGB, ImageFormat.IMAGE_FORMAT_PNG, 128);
}

From source file:com.pronoiahealth.olhie.server.services.BookCoverImageService.java

public byte[] createDefaultFrontCover(Book book, BookCategory category, BookCover cover, byte[] logo,
        String authorName) throws Exception {
    return createFrontCover(book.getCoverName(), logo, authorName, book.getBookTitle(), category.getColor(),
            cover.getAuthorTextColor(), cover.getCoverTitleTextColor(), 300, 400, BufferedImage.TYPE_INT_ARGB,
            ImageFormat.IMAGE_FORMAT_PNG, 128);
}

From source file:com.pronoiahealth.olhie.server.services.BookCoverImageService.java

/**
 * Create a back cover/*from   ww w  .  j  a v  a  2 s  .  c o  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:com.pronoiahealth.olhie.server.services.BookCoverImageService.java

/**
 * @param book/*ww  w  . ja  va 2s.  com*/
 * @param category
 * @return
 * @throws Exception
 */
public String createDefaultBackCoverEncoded(Book book, BookCover cover, BookCategory category)
        throws Exception {
    return createBackCoverEncoded(book.getCoverName(), book.getBookTitle(), category.getColor(),
            cover.getCoverTitleTextColor(), 300, 400, BufferedImage.TYPE_INT_ARGB, ImageFormat.IMAGE_FORMAT_PNG,
            128);
}

From source file:com.pronoiahealth.olhie.server.services.BookCoverImageService.java

/**
 * @param book//  w  ww .java 2s  .  c  o  m
 * @param category
 * @return
 * @throws Exception
 */
public byte[] createDefaultBackCover(Book book, BookCover cover, BookCategory category) throws Exception {
    return createBackCover(book.getCoverName(), book.getBookTitle(), category.getColor(),
            cover.getCoverTitleTextColor(), 300, 400, BufferedImage.TYPE_INT_ARGB, ImageFormat.IMAGE_FORMAT_PNG,
            128);
}