Example usage for java.awt RenderingHints VALUE_INTERPOLATION_BILINEAR

List of usage examples for java.awt RenderingHints VALUE_INTERPOLATION_BILINEAR

Introduction

In this page you can find the example usage for java.awt RenderingHints VALUE_INTERPOLATION_BILINEAR.

Prototype

Object VALUE_INTERPOLATION_BILINEAR

To view the source code for java.awt RenderingHints VALUE_INTERPOLATION_BILINEAR.

Click Source Link

Document

Interpolation hint value -- the color samples of the 4 nearest neighboring integer coordinate samples in the image are interpolated linearly to produce a color sample.

Usage

From source file:doge.photo.DogePhotoManipulator.java

private void setGraphicsHints(Graphics2D graphics) {
    graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
}

From source file:ch.rasc.downloadchart.DownloadChartServlet.java

private static void handleJpg(HttpServletResponse response, byte[] imageData, Integer width, Integer height,
        String filename, JpegOptions options) throws IOException {

    response.setContentType("image/jpeg");
    response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + ".jpg\";");

    BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageData));

    Dimension newDimension = calculateDimension(image, width, height);
    int imgWidth = image.getWidth();
    int imgHeight = image.getHeight();
    if (newDimension != null) {
        imgWidth = newDimension.width;/*w ww. j  a v a  2s .  c o m*/
        imgHeight = newDimension.height;
    }

    BufferedImage newImage = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);

    Graphics2D g = newImage.createGraphics();
    g.drawImage(image, 0, 0, imgWidth, imgHeight, Color.BLACK, null);
    g.dispose();

    if (newDimension != null) {
        g.setComposite(AlphaComposite.Src);
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    }

    try (ImageOutputStream ios = ImageIO.createImageOutputStream(response.getOutputStream())) {
        Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpg");
        ImageWriter writer = iter.next();
        ImageWriteParam iwp = writer.getDefaultWriteParam();
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        if (options != null && options.quality != null && options.quality != 0 && options.quality != 100) {
            iwp.setCompressionQuality(options.quality / 100f);
        } else {
            iwp.setCompressionQuality(1);
        }
        writer.setOutput(ios);
        writer.write(null, new IIOImage(newImage, null, null), iwp);
        writer.dispose();
    }
}

From source file:ScaleTest_2008.java

/**
 * Progressive bilinear scaling: for any downscale size, scale
 * iteratively by halves using BILINEAR filtering until the proper 
 * size is reached./*from  ww w. j av  a 2s. c om*/
 */
private BufferedImage getOptimalScalingImage(BufferedImage inputImage, int startSize, int endSize) {
    int currentSize = startSize;
    BufferedImage currentImage = inputImage;
    int delta = currentSize - endSize;
    int nextPow2 = currentSize >> 1;
    while (currentSize > 1) {
        if (delta <= nextPow2) {
            if (currentSize != endSize) {
                BufferedImage tmpImage = new BufferedImage(endSize, endSize, BufferedImage.TYPE_INT_RGB);
                Graphics g = tmpImage.getGraphics();
                ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                        RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                g.drawImage(currentImage, 0, 0, tmpImage.getWidth(), tmpImage.getHeight(), null);
                currentImage = tmpImage;
            }
            return currentImage;
        } else {
            BufferedImage tmpImage = new BufferedImage(currentSize >> 1, currentSize >> 1,
                    BufferedImage.TYPE_INT_RGB);
            Graphics g = tmpImage.getGraphics();
            ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g.drawImage(currentImage, 0, 0, tmpImage.getWidth(), tmpImage.getHeight(), null);
            currentImage = tmpImage;
            currentSize = currentImage.getWidth();
            delta = currentSize - endSize;
            nextPow2 = currentSize >> 1;
        }
    }
    return currentImage;
}

From source file:com.jaeksoft.searchlib.util.ImageUtils.java

public final static BufferedImage reduceImage(BufferedImage image, int width, int height) {
    int type = (image.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;
    BufferedImage ret = (BufferedImage) image;
    int w = image.getWidth();
    int h = image.getHeight();

    while (w != width || h != height) {
        if (w > width) {
            w /= 2;//from   ww w .  j  a  va2  s.  c  o m
            if (w < width)
                w = width;
        }

        if (h > height) {
            h /= 2;
            if (h < height)
                h = height;
        }

        BufferedImage tmp = new BufferedImage(w, h, type);

        Graphics2D g2 = tmp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(ret, 0, 0, w, h, null);
        g2.dispose();
        ret = tmp;
    }
    return ret;
}

From source file:com.openbravo.pos.util.ThumbNailBuilder.java

private Image createThumbNail(Image img) {
    //            MaskFilter filter = new MaskFilter(Color.WHITE);
    //            ImageProducer prod = new FilteredImageSource(img.getSource(), filter);
    //            img = Toolkit.getDefaultToolkit().createImage(prod);

    int targetw;/*from  w w  w .j  a  v a 2s.  c  o m*/
    int targeth;

    double scalex = (double) m_width / (double) img.getWidth(null);
    double scaley = (double) m_height / (double) img.getHeight(null);
    if (scalex < scaley) {
        targetw = m_width;
        targeth = (int) (img.getHeight(null) * scalex);
    } else {
        targetw = (int) (img.getWidth(null) * scaley);
        targeth = (int) m_height;
    }

    int midw = img.getWidth(null);
    int midh = img.getHeight(null);
    BufferedImage midimg = null;
    Graphics2D g2d = null;

    Image previmg = img;
    int prevw = img.getWidth(null);
    int prevh = img.getHeight(null);

    do {
        if (midw > targetw) {
            midw /= 2;
            if (midw < targetw) {
                midw = targetw;
            }
        } else {
            midw = targetw;
        }
        if (midh > targeth) {
            midh /= 2;
            if (midh < targeth) {
                midh = targeth;
            }
        } else {
            midh = targeth;
        }
        if (midimg == null) {
            midimg = new BufferedImage(midw, midh, BufferedImage.TYPE_INT_ARGB);
            g2d = midimg.createGraphics();
            g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        }
        g2d.drawImage(previmg, 0, 0, midw, midh, 0, 0, prevw, prevh, null);
        prevw = midw;
        prevh = midh;
        previmg = midimg;
    } while (midw != targetw || midh != targeth);

    g2d.dispose();

    if (m_width != midimg.getWidth() || m_height != midimg.getHeight()) {
        midimg = new BufferedImage(m_width, m_height, BufferedImage.TYPE_INT_ARGB);
        int x = (m_width > targetw) ? (m_width - targetw) / 2 : 0;
        int y = (m_height > targeth) ? (m_height - targeth) / 2 : 0;
        g2d = midimg.createGraphics();
        g2d.drawImage(previmg, x, y, x + targetw, y + targeth, 0, 0, targetw, targeth, null);
        g2d.dispose();
        previmg = midimg;
    }
    return previmg;
}

From source file:bachelorthesis.ocr.domain.DomainFacade.java

private Captcha createCaptcha(String buildString, int width, int height) throws RuntimeException {
    try {//  ww  w  .j a v a  2s  . c  o m
        CaptchaBuilder captchaBuilder = new CaptchaBuilder(40, 50, buildString);
        Captcha c = captchaBuilder.buildCaptcha();
        BufferedImage img = c.getImage();

        // check if size == the default size (40*50) if not scale
        if (width != 40 || height != 50) {
            BufferedImage resized = new BufferedImage(width, height, img.getType());
            Graphics2D g = resized.createGraphics();
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g.drawImage(img, 0, 0, width, height, 0, 0, img.getWidth(), img.getHeight(), null);
            g.dispose();

            //build new CAPTCHA WITH THE NEW IMAGE SIZE
            c = new Captcha(c.getBuildSequence(), c.getAnswer(), c.isCaseSensative(), img, new Date());
        }

        return c;

    } catch (ParseException ex) {
        Logger.getLogger(DomainFacade.class.getName()).log(Level.SEVERE, null, ex);
        throw new RuntimeException("error creating CAPTCHA");
    }
}

From source file:com.cubusmail.server.services.RetrieveImageServlet.java

/**
 * @param bufInputStream//from w  w  w.ja va  2s .  com
 * @param outputStream
 */
private void writeScaledImage(BufferedInputStream bufInputStream, OutputStream outputStream) {

    long millis = System.currentTimeMillis();
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        while ((bytesRead = bufInputStream.read(buffer, 0, 8192)) != -1) {
            bos.write(buffer, 0, bytesRead);
        }
        bos.close();

        byte[] imageBytes = bos.toByteArray();

        Image image = Toolkit.getDefaultToolkit().createImage(imageBytes);
        MediaTracker mediaTracker = new MediaTracker(new Container());
        mediaTracker.addImage(image, 0);
        mediaTracker.waitForID(0);
        // determine thumbnail size from WIDTH and HEIGHT
        int thumbWidth = 300;
        int thumbHeight = 200;
        double thumbRatio = (double) thumbWidth / (double) thumbHeight;
        int imageWidth = image.getWidth(null);
        int imageHeight = image.getHeight(null);
        double imageRatio = (double) imageWidth / (double) imageHeight;
        if (thumbRatio < imageRatio) {
            thumbHeight = (int) (thumbWidth / imageRatio);
        } else {
            thumbWidth = (int) (thumbHeight * imageRatio);
        }
        // draw original image to thumbnail image object and
        // scale it to the new size on-the-fly
        BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = thumbImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);

        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outputStream);
        JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
        int quality = 70;
        quality = Math.max(0, Math.min(quality, 100));
        param.setQuality((float) quality / 100.0f, false);
        encoder.setJPEGEncodeParam(param);
        encoder.encode(thumbImage);
    } catch (IOException ex) {
        log.error(ex.getMessage(), ex);
    } catch (InterruptedException ex) {
        log.error(ex.getMessage(), ex);
    } finally {
        log.debug("Time for thumbnail: " + (System.currentTimeMillis() - millis) + "ms");
    }
}

From source file:ImageBouncer.java

protected void setBilinear(Graphics2D g2) {
    if (mBilinear == false)
        return;//  w ww.j  av a2 s  .  c o m
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
}

From source file:ddf.catalog.transformer.input.pdf.PdfInputTransformer.java

private byte[] generatePdfThumbnail(PDDocument pdfDocument) throws IOException {

    PDFRenderer pdfRenderer = new PDFRenderer(pdfDocument);

    if (pdfDocument.getNumberOfPages() < 1) {
        /*/*from   w  w  w .  j  a v  a2s . co  m*/
         * Can there be a PDF with zero pages??? Should we throw an error or what? The
         * original implementation assumed that a PDF would always have at least one
         * page. That's what I've implemented here, but I don't like make those
         * kinds of assumptions :-( But I also don't want to change the original
         * behavior without knowing how it will impact the system.
         */
    }

    PDPage page = pdfDocument.getPage(0);

    BufferedImage image = pdfRenderer.renderImageWithDPI(0, RESOLUTION_DPI, ImageType.RGB);

    int largestDimension = Math.max(image.getHeight(), image.getWidth());
    float scalingFactor = IMAGE_HEIGHTWIDTH / largestDimension;
    int scaledHeight = (int) (image.getHeight() * scalingFactor);
    int scaledWidth = (int) (image.getWidth() * scalingFactor);

    BufferedImage scaledImage = new BufferedImage(scaledWidth, scaledHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = scaledImage.createGraphics();
    graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics.drawImage(image, 0, 0, scaledWidth, scaledHeight, null);
    graphics.dispose();

    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        ImageIOUtil.writeImage(scaledImage, FORMAT_NAME, outputStream, RESOLUTION_DPI, IMAGE_QUALITY);
        return outputStream.toByteArray();
    }
}

From source file:PictureScaler.java

/**
 * Render all scaled versions 10 times, timing each version and 
 * reporting the results below the appropriate scaled image.
 *//*w w w.  jav  a2 s  .c  o  m*/
protected void paintComponent(Graphics g) {
    // Scale with NEAREST_NEIGHBOR
    int xLoc = PADDING, yLoc = PADDING;
    long startTime, endTime;
    float totalTime;
    int iterations = 10;
    ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
    startTime = System.nanoTime();
    for (int i = 0; i < iterations; ++i) {
        g.drawImage(picture, xLoc, yLoc, scaleW, scaleH, null);
    }
    endTime = System.nanoTime();
    totalTime = (float) ((endTime - startTime) / 1000000) / iterations;
    g.drawString("NEAREST ", xLoc, yLoc + scaleH + PADDING);
    g.drawString(Float.toString(totalTime) + " ms", xLoc, yLoc + scaleH + PADDING + 10);
    System.out.println("NEAREST: " + ((endTime - startTime) / 1000000));

    // Scale with BILINEAR
    xLoc += scaleW + PADDING;
    ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    startTime = System.nanoTime();
    for (int i = 0; i < iterations; ++i) {
        g.drawImage(picture, xLoc, yLoc, scaleW, scaleH, null);
    }
    endTime = System.nanoTime();
    totalTime = (float) ((endTime - startTime) / 1000000) / iterations;
    g.drawString("BILINEAR", xLoc, yLoc + scaleH + PADDING);
    g.drawString(Float.toString(totalTime) + " ms", xLoc, yLoc + scaleH + PADDING + 10);
    System.out.println("BILINEAR: " + ((endTime - startTime) / 1000000));

    // Scale with BICUBIC
    xLoc += scaleW + PADDING;
    ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    startTime = System.nanoTime();
    for (int i = 0; i < iterations; ++i) {
        g.drawImage(picture, xLoc, yLoc, scaleW, scaleH, null);
    }
    endTime = System.nanoTime();
    totalTime = (float) ((endTime - startTime) / 1000000) / iterations;
    g.drawString("BICUBIC", xLoc, yLoc + scaleH + PADDING);
    g.drawString(Float.toString(totalTime) + " ms", xLoc, yLoc + scaleH + PADDING + 10);
    System.out.println("BICUBIC: " + ((endTime - startTime) / 1000000));

    // Scale with getScaledInstance
    xLoc += scaleW + PADDING;
    startTime = System.nanoTime();
    for (int i = 0; i < iterations; ++i) {
        Image scaledPicture = picture.getScaledInstance(scaleW, scaleH, Image.SCALE_AREA_AVERAGING);
        g.drawImage(scaledPicture, xLoc, yLoc, null);
    }
    endTime = System.nanoTime();
    totalTime = (float) ((endTime - startTime) / 1000000) / iterations;
    g.drawString("getScaled", xLoc, yLoc + scaleH + PADDING);
    g.drawString(Float.toString(totalTime) + " ms", xLoc, yLoc + scaleH + PADDING + 10);
    System.out.println("getScaled: " + ((endTime - startTime) / 1000000));

    // Scale with Progressive Bilinear
    xLoc += scaleW + PADDING;
    startTime = System.nanoTime();
    for (int i = 0; i < iterations; ++i) {
        Image scaledPicture = getFasterScaledInstance(picture, scaleW, scaleH,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR, true);
        g.drawImage(scaledPicture, xLoc, yLoc, null);
    }
    endTime = System.nanoTime();
    totalTime = (float) ((endTime - startTime) / 1000000) / iterations;
    g.drawString("Progressive", xLoc, yLoc + scaleH + PADDING);
    g.drawString(Float.toString(totalTime) + " ms", xLoc, yLoc + scaleH + PADDING + 10);
    System.out.println("Progressive: " + ((endTime - startTime) / 1000000));
}