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:Java2DExample.java

public void displayOriginalImage() {
    displayImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);

    Graphics2D graphics = displayImage.createGraphics();
    graphics.drawImage(originalImage, null, null);
    repaint();//from w  w w  .j a v a  2s. co m
}

From source file:br.ufrgs.enq.jcosmo.ui.SigmaProfileAreaPanel.java

public void addProfile(String label, double[] sigma, double[] area, int rgb) {
    int n = sigma.length;
    XYSeries comp = new XYSeries(label, false, false);

    // charges represent the center of the segments
    comp.add(sigma[0], area[0]);/* w ww  .  j  a v a 2  s  .c om*/
    for (int j = 1; j < n; ++j) {
        double dsigma = (sigma[j] - sigma[j - 1]) / 2;
        comp.add(sigma[j] - dsigma, area[j]);
        comp.add(sigma[j] + dsigma + 1e-6, area[j]);
    }
    dataset.addSeries(comp);
    int series = dataset.getSeriesCount() - 1;

    int patternSize = 6;
    BufferedImage bi = new BufferedImage(patternSize, patternSize, BufferedImage.TYPE_INT_RGB);
    Rectangle r = new Rectangle(0, 0, patternSize, patternSize);
    Graphics2D big = bi.createGraphics();
    big.setColor(new Color(rgb, rgb, rgb));
    big.fillRect(0, 0, patternSize, patternSize);

    int pattern = series % 4;
    if (pattern == 1 || pattern == 3) {
        big.setColor(Color.WHITE);
        big.drawLine(0, patternSize / 2, patternSize, patternSize / 2);
    }
    if (pattern == 2 || pattern == 3) {
        big.setColor(Color.WHITE);
        big.drawLine(patternSize / 2, 0, patternSize / 2, patternSize);
    }

    TexturePaint paint = new TexturePaint(bi, r);
    sigmaProfilePlot.getRenderer().setSeriesPaint(series, paint); // new Color(rgb, rgb, rgb));
}

From source file:org.jax.haplotype.analysis.visualization.SimplePhylogenyTreeImageFactory.java

/**
 * {@inheritDoc}//from   www .  ja v a 2s .co  m
 */
public BufferedImage createPhylogenyImage(PhylogenyTreeNode phylogenyTree, int imageWidth, int imageHeight) {
    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("Creating phylogeny image for: "
                + phylogenyTree.resolveToSingleStrainLeafNodes(0.0).toNewickFormat());
    }

    BufferedImage bi = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_4BYTE_ABGR);
    Graphics2D graphics = bi.createGraphics();
    graphics.setStroke(LINE_STROKE);
    graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    graphics.setColor(Color.BLACK);

    this.paintPhylogenyTree(graphics, phylogenyTree, imageWidth, imageHeight);

    return bi;
}

From source file:com.galenframework.utils.GalenUtils.java

public static File makeFullScreenshot(WebDriver driver) throws IOException, InterruptedException {
    // scroll up first
    scrollVerticallyTo(driver, 0);/* w w  w.jav a  2 s.c o m*/
    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()) {
        try {
            resultingImage = GalenUtils.resizeScreenshotIfNeeded(driver, resultingImage);
        } catch (Exception ex) {
            LOG.trace("Couldn't resize screenshot", ex);
        }
    }

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

From source file:ImageBorderHack.java

public BufferedImage createBufferedImage(Image img) {
    BufferedImage buff = new BufferedImage(img.getWidth(null), img.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);
    Graphics gfx = buff.createGraphics();
    gfx.drawImage(img, 0, 0, null);/*from  w  w  w. j  ava2  s .co m*/
    gfx.dispose();
    return buff;
}

From source file:com.neophob.sematrix.core.generator.Textwriter.java

/**
 * create image./*from  ww  w.  j a va2  s  . c  o  m*/
 *
 * @param text the text
 */
public void createTextImage(String text) {
    //only load if needed
    if (StringUtils.equals(text, this.text)) {
        return;
    }

    this.text = text;

    BufferedImage img = new BufferedImage(TEXT_BUFFER_X_SIZE, internalBufferYSize, BufferedImage.TYPE_INT_RGB);

    Graphics2D g2 = img.createGraphics();
    FontRenderContext frc = g2.getFontRenderContext();
    TextLayout layout = new TextLayout(text, font, frc);
    Rectangle2D rect = layout.getBounds();

    int h = (int) (0.5f + rect.getHeight());
    //head and tailing space
    maxXPos = (int) (0.5f + rect.getWidth()) + 2 * internalBufferXSize;
    int ypos = internalBufferYSize - (internalBufferYSize - h) / 2;

    img = new BufferedImage(maxXPos, internalBufferYSize, BufferedImage.TYPE_BYTE_GRAY);
    g2 = img.createGraphics();

    g2.setColor(new Color(128));
    g2.setFont(font);
    g2.setClip(0, 0, maxXPos, internalBufferYSize);

    g2.drawString(text, internalBufferXSize, ypos);
    DataBufferByte dbi = (DataBufferByte) img.getRaster().getDataBuffer();
    byte[] textBuffer = dbi.getData();
    g2.dispose();

    xofs = 0;

    textAsImage = new int[maxXPos * internalBufferYSize];
    for (int i = 0; i < textAsImage.length; i++) {
        if (textBuffer[i] > 10) {
            textAsImage[i] = 127;
        } else {
            textAsImage[i] = 0;
        }
    }

    //clear internalbuffer
    Arrays.fill(this.internalBuffer, 0);
}

From source file:org.n52.server.sos.generator.DiagramGenerator.java

/**
 * Produce legend.//from  w  w w.  j ava2s  . c o  m
 * 
 * @param options
 *            the options
 * @param out
 *            the out
 * @throws OXFException
 *             the oXF exception
 */
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:
        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++;
    }

    // draw legend into image:
    JPEGEncodeParam p = new JPEGEncodeParam();
    p.setQuality(1f);
    ImageEncoder encoder = ImageCodec.createImageEncoder("jpeg", out, p);

    encoder.encode(legendImage);
}

From source file:com.alkacon.opencms.v8.weboptimization.CmsOptimizationSprite.java

/**
 * Add a new image to the given sprite.<p>
 * //from   w  w  w.  j av a2  s  .  co m
 * @param sprite the sprite to modify
 * @param image the image to add
 * @param options the sprite options
 * 
 * @return the modified sprite
 */
public BufferedImage addImage(BufferedImage sprite, BufferedImage image, CmsOptimizationSpriteOptions options) {

    // check the sprite size
    int newWidth = -1;
    if (sprite.getWidth() < options.getX() + image.getWidth()) {
        newWidth = options.getX() + image.getWidth();
    }
    int newHeight = -1;
    if (sprite.getHeight() < options.getY() + image.getHeight()) {
        newHeight = options.getY() + image.getHeight();
    }
    Graphics2D g2d = sprite.createGraphics();
    if ((newHeight > 0) || (newWidth > 0)) {
        // adjust sprite size if needed
        if (newHeight < 0) {
            newHeight = sprite.getHeight();
        }
        if (newWidth < 0) {
            newWidth = sprite.getWidth();
        }
        // clone the sprite
        BufferedImage s2 = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_4BYTE_ABGR);
        g2d = s2.createGraphics();
        g2d.drawImage(sprite, 0, 0, null);
        sprite = s2;
    }
    // put the image into the sprite
    g2d.drawImage(image, options.getX(), options.getY(), null);
    return sprite;
}

From source file:com.xebia.visualreview.PixelComparator.java

public static DiffReport processImage(File beforeFile, File afterFile, java.util.Map maskInfo,
        String compareSettings) {
    try {//from   w  w  w  . j  a va  2s  .c  om
        int precision = getPrecision(compareSettings);

        PixelGrabber beforeGrab = grabImage(beforeFile);
        PixelGrabber afterGrab = grabImage(afterFile);

        int[] beforeData = null;
        int[] afterData = null;
        int beforeWidth = 0;
        int afterWidth = 0;
        int y1 = 0;
        int y2 = 0;

        if (beforeGrab.grabPixels()) {
            beforeWidth = beforeGrab.getWidth();
            y1 = beforeGrab.getHeight();
            beforeData = (int[]) beforeGrab.getPixels();
        }

        if (afterGrab.grabPixels()) {
            afterWidth = afterGrab.getWidth();
            y2 = afterGrab.getHeight();
            afterData = (int[]) afterGrab.getPixels();
        }

        int minX = Math.min(beforeWidth, afterWidth);
        int minY = Math.min(y1, y2);
        int diffWidth = Math.max(beforeWidth, afterWidth);
        int diffHeight = Math.max(y1, y2);
        int[] diffData = new int[diffWidth * diffHeight];
        int differentPixels = 0;
        boolean hasMask = (maskInfo != null);
        BufferedImage maskImage = generateMask(maskInfo, diffWidth, diffHeight);
        for (int y = 0; y < diffHeight; y++) {
            for (int x = 0; x < diffWidth; x++) {
                if (maskImage.getRGB(x, y) != DIFFCOLOUR) {
                    if (x >= minX || y >= minY || hasDifference(beforeData[y * beforeWidth + x],
                            afterData[y * afterWidth + x], precision)) {
                        diffData[y * diffWidth + x] = DIFFCOLOUR;
                        differentPixels++;
                    }
                }
            }
        }

        BufferedImage diffImage = new BufferedImage(diffWidth, diffHeight, BufferedImage.TYPE_INT_ARGB);
        diffImage.setRGB(0, 0, diffWidth, diffHeight, diffData, 0, diffWidth);

        DiffReport report = new DiffReport(beforeFile, afterFile, diffImage, differentPixels);
        if (hasMask) {
            report.setMaskImage(maskImage);
        }
        return report;

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:gui.images.CodebookVectorProfilePanel.java

/**
 * Sets the data to be shown.//from  w w w  .  j  av  a2  s  . com
 *
 * @param occurrenceProfile Double array that is the neighbor occurrence
 * profile of this visual word.
 * @param codebookIndex Integer that is the index of this visual word.
 * @param classColors Color[] of class colors.
 * @param classNames String[] of class names.
 */
public void setResults(double[] occurrenceProfile, int codebookIndex, Color[] classColors,
        String[] classNames) {
    int numClasses = Math.min(classNames.length, occurrenceProfile.length);
    this.codebookIndex = codebookIndex;
    this.occurrenceProfile = occurrenceProfile;
    DefaultPieDataset pieData = new DefaultPieDataset();
    for (int cIndex = 0; cIndex < numClasses; cIndex++) {
        pieData.setValue(classNames[cIndex], occurrenceProfile[cIndex]);
    }
    JFreeChart chart = ChartFactory.createPieChart3D("codebook vect " + codebookIndex, pieData, true, true,
            false);
    PiePlot plot = (PiePlot) chart.getPlot();
    plot.setDirection(Rotation.CLOCKWISE);
    plot.setForegroundAlpha(0.5f);
    PieRenderer prend = new PieRenderer(classColors);
    prend.setColor(plot, pieData);
    ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new Dimension(140, 140));
    chartPanel.setVisible(true);
    chartPanel.revalidate();
    chartPanel.repaint();
    JPanel jp = new JPanel();
    jp.setPreferredSize(new Dimension(140, 140));
    jp.setMinimumSize(new Dimension(140, 140));
    jp.setMaximumSize(new Dimension(140, 140));
    jp.setSize(new Dimension(140, 140));
    jp.setLayout(new FlowLayout());
    jp.add(chartPanel);
    jp.setVisible(true);
    jp.validate();
    jp.repaint();

    JFrame frame = new JFrame();
    frame.setBackground(Color.WHITE);
    frame.setUndecorated(true);
    frame.getContentPane().add(jp);
    frame.pack();
    BufferedImage bi = new BufferedImage(jp.getWidth(), jp.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics = bi.createGraphics();
    jp.print(graphics);
    graphics.dispose();
    frame.dispose();
    imPanel.removeAll();
    imPanel.setImage(bi);
    imPanel.setVisible(true);
    imPanel.revalidate();
    imPanel.repaint();
}