Example usage for java.awt.image BufferedImage BufferedImage

List of usage examples for java.awt.image BufferedImage BufferedImage

Introduction

In this page you can find the example usage for java.awt.image BufferedImage BufferedImage.

Prototype

public BufferedImage(int width, int height, int imageType) 

Source Link

Document

Constructs a BufferedImage of one of the predefined image types.

Usage

From source file:ImageOps.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    int w = getSize().width;
    int h = getSize().height;

    g2.setColor(Color.black);/*ww  w  . j  av  a2s  .co m*/
    float[][] data = { { 0.1f, 0.1f, 0.1f, // low-pass filter
            0.1f, 0.2f, 0.1f, 0.1f, 0.1f, 0.1f }, SHARPEN3x3_3 };

    String theDesc[] = { "Convolve LowPass", "Convolve Sharpen", "LookupOp", "RescaleOp" };
    for (int i = 0; i < bi.length; i++) {
        int iw = bi[i].getWidth(this);
        int ih = bi[i].getHeight(this);
        int x = 0, y = 0;

        AffineTransform at = new AffineTransform();
        at.scale((w - 14) / 2.0 / iw, (h - 34) / 2.0 / ih);

        BufferedImageOp biop = null;
        BufferedImage bimg = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);

        switch (i) {
        case 0:
        case 1:
            x = i == 0 ? 5 : w / 2 + 3;
            y = 15;
            Kernel kernel = new Kernel(3, 3, data[i]);
            ConvolveOp cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
            cop.filter(bi[i], bimg);
            biop = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
            break;
        case 2:
            x = 5;
            y = h / 2 + 15;
            byte chlut[] = new byte[256];
            for (int j = 0; j < 200; j++)
                chlut[j] = (byte) (256 - j);
            ByteLookupTable blut = new ByteLookupTable(0, chlut);
            LookupOp lop = new LookupOp(blut, null);
            lop.filter(bi[i], bimg);
            biop = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
            break;
        case 3:
            x = w / 2 + 3;
            y = h / 2 + 15;
            RescaleOp rop = new RescaleOp(1.1f, 20.0f, null);
            rop.filter(bi[i], bimg);
            biop = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
        }
        g2.drawImage(bimg, biop, x, y);
        TextLayout tl = new TextLayout(theDesc[i], g2.getFont(), g2.getFontRenderContext());
        tl.draw(g2, (float) x, (float) y - 4);
    }
}

From source file:bachelorthesis.captchabuilder.builder.CaptchaBuilder.java

public CaptchaBuilder setImageSize(int width, int height) {
    this.img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    return this;
}

From source file:com.fusesource.forge.jmstest.persistence.rrd.RrdGraphPostProcessor.java

@Override
public void processData() {
    super.processData();
    RrdDb db = getDatabase().getDatabase();

    for (int i = 0; i < db.getArcCount(); i++) {
        Archive arch = db.getArchive(i);
        try {//from w  ww.j  a va  2  s  . c  om
            for (String dsName : db.getDsNames()) {
                String probeName = getDatabase().getDescriptorByPhysicalName(dsName).getName();
                try {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");

                    StringBuffer title = new StringBuffer(probeName);
                    title.append(" ");
                    title.append(sdf.format(new Date(arch.getStartTime() * 1000)));
                    title.append("-");
                    title.append(sdf.format(new Date(arch.getEndTime() * 1000)));

                    log().info("Creating Graph: " + title);

                    RrdGraphDef graphDef = new RrdGraphDef();

                    graphDef.setTitle(title.toString());
                    graphDef.setTimeSpan(arch.getStartTime(), arch.getEndTime());
                    graphDef.datasource(dsName, getDbFileName(), dsName, ConsolFun.AVERAGE);
                    graphDef.setStep(arch.getArcStep());
                    graphDef.setWidth(800);
                    graphDef.setHeight(600);
                    graphDef.line(dsName, Color.BLUE, probeName, 2.0f);
                    graphDef.setImageFormat("PNG");
                    graphDef.setFilename(getWorkDir().getAbsolutePath() + "/" + probeName + ".png");
                    RrdGraph graph = new RrdGraph(graphDef);
                    BufferedImage bi = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
                    graph.render(bi.getGraphics());
                    createThumbnail(bi, probeName, 40, 30);
                } catch (Exception e) {
                    log().error("Error generating graph for probe " + probeName, e);
                }
            }
        } catch (IOException ioe) {
            log().error("Error retrieving datasource names from RRD database ", ioe);
        }
    }
}

From source file:org.n52.server.io.DiagramGenerator.java

public void createLegend(DesignOptions options, OutputStream out) throws OXFException, IOException {

    int topMargin = 10;
    int leftMargin = 30;
    int iconWidth = 15;
    int iconHeight = 15;
    int verticalSpaceBetweenEntries = 20;
    int horizontalSpaceBetweenIconAndText = 15;

    DesignDescriptionList ddList = buildUpDesignDescriptionList(options);
    int width = 800;
    int height = topMargin + (ddList.size() * (iconHeight + verticalSpaceBetweenEntries));

    BufferedImage legendImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D legendGraphics = legendImage.createGraphics();
    legendGraphics.setColor(Color.white);
    legendGraphics.fillRect(0, 0, width, height);

    int offset = 0;
    for (RenderingDesign dd : ddList.getAllDesigns()) {
        int yPos = topMargin + offset * iconHeight + offset * verticalSpaceBetweenEntries;

        // icon://from  ww w. j a va  2s  .  c o m
        legendGraphics.setColor(dd.getColor());
        legendGraphics.fillRect(leftMargin, yPos, iconWidth, iconHeight);

        // text:
        legendGraphics.setColor(Color.black);

        legendGraphics.drawString(dd.getFeature().getLabel() + " - " + dd.getLabel(),
                leftMargin + iconWidth + horizontalSpaceBetweenIconAndText, yPos + iconHeight);

        offset++;
    }

    JPEGImageWriteParam p = new JPEGImageWriteParam(null);
    p.setCompressionMode(JPEGImageWriteParam.MODE_DEFAULT);
    write(legendImage, FORMAT, out);
}

From source file:net.mindengine.galen.utils.GalenUtils.java

public static File makeFullScreenshot(WebDriver driver) throws IOException, InterruptedException {
    // scroll up first
    scrollVerticallyTo(driver, 0);//from  w  ww . j a  v  a  2  s  . c om
    byte[] bytes = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
    BufferedImage image = ImageIO.read(new ByteArrayInputStream(bytes));
    int capturedWidth = image.getWidth();
    int capturedHeight = image.getHeight();

    long longScrollHeight = (Long) ((JavascriptExecutor) driver).executeScript(
            "return Math.max(" + "document.body.scrollHeight, document.documentElement.scrollHeight,"
                    + "document.body.offsetHeight, document.documentElement.offsetHeight,"
                    + "document.body.clientHeight, document.documentElement.clientHeight);");

    Double devicePixelRatio = ((Number) ((JavascriptExecutor) driver)
            .executeScript(JS_RETRIEVE_DEVICE_PIXEL_RATIO)).doubleValue();

    int scrollHeight = (int) longScrollHeight;

    File file = File.createTempFile("screenshot", ".png");

    int adaptedCapturedHeight = (int) (((double) capturedHeight) / devicePixelRatio);

    BufferedImage resultingImage;

    if (Math.abs(adaptedCapturedHeight - scrollHeight) > 40) {
        int scrollOffset = adaptedCapturedHeight;

        int times = scrollHeight / adaptedCapturedHeight;
        int leftover = scrollHeight % adaptedCapturedHeight;

        final BufferedImage tiledImage = new BufferedImage(capturedWidth,
                (int) (((double) scrollHeight) * devicePixelRatio), BufferedImage.TYPE_INT_RGB);
        Graphics2D g2dTile = tiledImage.createGraphics();
        g2dTile.drawImage(image, 0, 0, null);

        int scroll = 0;
        for (int i = 0; i < times - 1; i++) {
            scroll += scrollOffset;
            scrollVerticallyTo(driver, scroll);
            BufferedImage nextImage = ImageIO.read(
                    new ByteArrayInputStream(((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES)));
            g2dTile.drawImage(nextImage, 0, (i + 1) * capturedHeight, null);
        }
        if (leftover > 0) {
            scroll += scrollOffset;
            scrollVerticallyTo(driver, scroll);
            BufferedImage nextImage = ImageIO.read(
                    new ByteArrayInputStream(((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES)));
            BufferedImage lastPart = nextImage.getSubimage(0,
                    nextImage.getHeight() - (int) (((double) leftover) * devicePixelRatio),
                    nextImage.getWidth(), leftover);
            g2dTile.drawImage(lastPart, 0, times * capturedHeight, null);
        }

        scrollVerticallyTo(driver, 0);

        resultingImage = tiledImage;
    } else {
        resultingImage = image;
    }

    if (GalenConfig.getConfig().shouldAutoresizeScreenshots()) {
        resultingImage = GalenUtils.resizeScreenshotIfNeeded(driver, resultingImage);
    }

    ImageIO.write(resultingImage, "png", file);
    return file;
}

From source file:com.shending.support.CompressPic.java

public String compressPic() {
    try {/*  w w  w .  ja  v a  2s  . c o m*/
        // ?
        file = new File(inputDir);
        //System.out.println(inputDir + inputFileName);
        if (!file.exists()) {
            //throw new Exception("?");
        }
        Image img = ImageIO.read(file);
        // ??
        if (img.getWidth(null) == -1) {
            System.out.println(" can't read,retry!" + "<BR>");
            return "no";
        } else {
            int newWidth;
            int newHeight;
            // ?
            if (this.proportion == true) {
                // ?
                double rate1 = ((double) img.getWidth(null)) / (double) outputWidth + 0.1;
                double rate2 = ((double) img.getHeight(null)) / (double) outputHeight + 0.1;
                // ?
                double rate = rate1 > rate2 ? rate1 : rate2;
                newWidth = (int) (((double) img.getWidth(null)) / rate);
                newHeight = (int) (((double) img.getHeight(null)) / rate);
            } else {
                newWidth = outputWidth; // 
                newHeight = outputHeight; // 
            }
            BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);

            /*
             * Image.SCALE_SMOOTH  ?  ?? 
             */
            tag.getGraphics().drawImage(img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0,
                    null);
            File f = new File(outputDir);
            if (!f.exists()) {
                f.mkdirs();
            }
            FileOutputStream out = new FileOutputStream(outputDir + outputFileName);
            // JPEGImageEncoder??
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            encoder.encode(tag);
            out.close();
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return "ok";
}

From source file:net.chris54721.infinitycubed.utils.Utils.java

public static BufferedImage toBufferedImage(Image img) {
    if (img instanceof BufferedImage)
        return (BufferedImage) img;
    BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D bGr = bimage.createGraphics();
    bGr.drawImage(img, 0, 0, null);//from   w ww.j av a  2s .  co  m
    bGr.dispose();
    return bimage;
}

From source file:net.rptools.lib.image.ImageUtil.java

/**
 * Create a copy of the image that is compatible with the current graphics context and scaled to the supplied size
 *///www.  ja  v a  2  s .c om
public static BufferedImage createCompatibleImage(Image img, int width, int height, Map<String, Object> hints) {
    width = Math.max(width, 1);
    height = Math.max(height, 1);

    int transparency;
    if (hints != null && hints.containsKey(HINT_TRANSPARENCY)) {
        transparency = (Integer) hints.get(HINT_TRANSPARENCY);
    } else {
        transparency = pickBestTransparency(img);
    }
    BufferedImage compImg = new BufferedImage(width, height, transparency);

    Graphics2D g = null;
    try {
        g = compImg.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

        g.drawImage(img, 0, 0, width, height, null);
    } finally {
        if (g != null) {
            g.dispose();
        }
    }
    return compImg;
}

From source file:org.spf4j.perf.impl.chart.Charts.java

public static BufferedImage createMinMaxAvgCountImg(final String chartName, final long[] timestamps,
        final double[] min, final double[] max, final double[] total, final double[] count, final String uom,
        final int width, final int height) {

    BufferedImage bi = Charts//w w w  .ja v a  2s . com
            .createTimeSeriesJFreeChart(chartName, timestamps, new String[] { "min", "max", "avg" }, uom,
                    new double[][] { min, max, Arrays.divide(total, count) })
            .createBufferedImage(width, height - height / 3);
    BufferedImage bi2 = Charts.createTimeSeriesJFreeChart(null, timestamps, new String[] { "count" }, "count",
            new double[][] { count }).createBufferedImage(width, height / 3);
    BufferedImage combined = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    final Graphics graphics = combined.getGraphics();
    try {
        graphics.drawImage(bi, 0, 0, null);
        graphics.drawImage(bi2, 0, height - height / 3, null);
    } finally {
        graphics.dispose();
    }
    return combined;
}