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

public void flipBufferedImage() {
    bi2 = new BufferedImage(bi1.getWidth(), bi1.getHeight(), bi1.getType());

    DataBuffer db1 = bi1.getRaster().getDataBuffer();
    DataBuffer db2 = bi2.getRaster().getDataBuffer();

    for (int i = db1.getSize() - 1, j = 0; i >= 0; --i, j++) {
        db2.setElem(j, db1.getElem(i));/*w  ww  . j  a v  a  2s  . com*/
    }
}

From source file:org.jfree.chart.demo.ChartTiming4.java

/**
 * Runs the test.// w w w. j  a  va  2s  . c  o  m
 */
public void run() {

    this.finished = false;

    // create a dataset...
    populateData();

    // create a fast scatter chart...
    final Plot plot = new FastScatterPlot(this.data, new NumberAxis("X"), new NumberAxis("Y"));
    final JFreeChart chart = new JFreeChart("Fast Scatter Plot Timing", JFreeChart.DEFAULT_TITLE_FONT, plot,
            true);

    final BufferedImage image = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB);
    final Graphics2D g2 = image.createGraphics();
    final Rectangle2D chartArea = new Rectangle2D.Double(0, 0, 400, 300);

    // set up the timer...
    final Timer timer = new Timer(10000, this);
    timer.setRepeats(false);
    int count = 0;
    timer.start();
    while (!this.finished) {
        chart.draw(g2, chartArea, null, null);
        System.out.println("Charts drawn..." + count);
        if (!this.finished) {
            count++;
        }
    }
    System.out.println("DONE");

}

From source file:com.simiacryptus.mindseye.test.data.CIFAR10.java

private static LabeledObject<BufferedImage> toImage(final byte[] b) {
    @Nonnull//from www . ja  va  2 s .  com
    final BufferedImage img = new BufferedImage(32, 32, BufferedImage.TYPE_INT_RGB);
    for (int x = 0; x < img.getWidth(); x++) {
        for (int y = 0; y < img.getHeight(); y++) {
            final int red = 0xFF & b[1 + 1024 * 0 + x + y * 32];
            final int blue = 0xFF & b[1 + 1024 * 1 + x + y * 32];
            final int green = 0xFF & b[1 + 1024 * 2 + x + y * 32];
            final int c = (red << 16) + (blue << 8) + green;
            img.setRGB(x, y, c);
        }
    }
    return new LabeledObject<>(img, Arrays.toString(new byte[] { b[0] }));
}

From source file:editeurpanovisu.ReadWriteImage.java

public static void writeTiff(Image imgImage, String strNomFich, boolean bSharpen, float sharpenLevel)
        throws ImageReadException, IOException {
    File file = new File(strNomFich);
    BufferedImage imageRGBSharpen = null;
    BufferedImage imageRGB = SwingFXUtils.fromFXImage(imgImage, null);

    Graphics2D graphics = imageRGB.createGraphics();
    graphics.drawImage(imageRGB, 0, 0, null);
    if (bSharpen) {
        imageRGBSharpen = new BufferedImage(imageRGB.getWidth(), imageRGB.getHeight(),
                BufferedImage.TYPE_INT_RGB);
        Kernel kernel = new Kernel(3, 3, sharpenMatrix);
        ConvolveOp cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
        cop.filter(imageRGB, imageRGBSharpen);
    }//w w  w .  j  a va  2 s.  com

    final ImageFormat format = ImageFormats.TIFF;
    final Map<String, Object> params = new HashMap<>();
    params.put(ImagingConstants.PARAM_KEY_COMPRESSION,
            new Integer(TiffConstants.TIFF_COMPRESSION_UNCOMPRESSED));

    if (bSharpen) {
        try {
            Imaging.writeImage(imageRGBSharpen, file, format, params);
        } catch (ImageWriteException ex) {
            Logger.getLogger(ReadWriteImage.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else {
        try {
            Imaging.writeImage(imageRGB, file, format, params);
        } catch (ImageWriteException ex) {
            Logger.getLogger(ReadWriteImage.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

From source file:org.jfree.chart.demo.ChartTiming2.java

/**
 * Runs the test.// www . ja  va  2  s.  c  o  m
 */
public void run() {

    this.finished = false;

    // create a dataset...
    final XYDataset data = new SampleXYDataset2(1, 1440);

    // create a scatter chart...
    final boolean withLegend = true;
    final JFreeChart chart = ChartFactory.createScatterPlot("Scatter plot timing", "X", "Y", data,
            PlotOrientation.VERTICAL, withLegend, false, false);

    final XYPlot plot = chart.getXYPlot();
    plot.setRenderer(new XYDotRenderer());

    final BufferedImage image = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB);
    final Graphics2D g2 = image.createGraphics();
    final Rectangle2D chartArea = new Rectangle2D.Double(0, 0, 400, 300);

    // set up the timer...
    final Timer timer = new Timer(10000, this);
    timer.setRepeats(false);
    int count = 0;
    timer.start();
    while (!this.finished) {
        chart.draw(g2, chartArea, null, null);
        System.out.println("Charts drawn..." + count);
        if (!this.finished) {
            count++;
        }
    }
    System.out.println("DONE");

}

From source file:de.bund.bfr.knime.pmm.common.chart.ChartUtilities.java

public static PNGImageContent convertToPNGImageContent(JFreeChart chart, int width, int height) {
    try {/*from w  w w.  j  a  va 2s. c  o  m*/
        BufferedImage img;

        if (chart != null) {
            ChartRenderingInfo info = new ChartRenderingInfo();

            img = chart.createBufferedImage(width, 5000, info);
            img = chart.createBufferedImage(width,
                    (int) (img.getHeight() - info.getPlotInfo().getDataArea().getHeight() + height));
        } else {
            img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        }

        return new PNGImageContent(org.jfree.chart.ChartUtilities.encodeAsPNG(img));
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:net.duckling.ddl.util.ImageUtils.java

/**
 * ???// ww w  .  ja  v a  2 s  . c  om
 * @param tmpFilePath ?
 * @return
 */
public static boolean scare(String tmpFilePath) {
    try {
        BufferedImage src = ImageIO.read(new File(tmpFilePath)); // 
        int width = src.getWidth();
        int height = src.getHeight();
        if (width > DEFAULT_WIDTH) {
            height = (DEFAULT_WIDTH * height) / width;
            width = DEFAULT_WIDTH;
        }
        Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);
        BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = tag.getGraphics();
        g.drawImage(image, 0, 0, null); // ??
        g.dispose();
        File resultFile = new File(tmpFilePath);
        ImageIO.write(tag, "JPEG", resultFile);// ?
        return true;
    } catch (IOException e) {
        LOG.error(e);
    }
    return false;
}

From source file:ImageUtil.java

public static BufferedImage scale(BufferedImage src, int width, int height) throws IOException {
    BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = dest.createGraphics();
    AffineTransform at = AffineTransform.getScaleInstance((double) width / src.getWidth(),
            (double) height / src.getHeight());
    g.drawRenderedImage(src, at);/* w  w w.  ja v  a2s  .  c om*/
    return dest;
}

From source file:imageLines.ImageHelpers.java

public static BufferedImage getScaledInstance(BufferedImage img, int targetWidth, int targetHeight, Object hint,
        boolean higherQuality) {
    int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;
    BufferedImage ret = (BufferedImage) img;
    int w, h;//from  ww  w.j  a v  a  2s .  co  m
    if (higherQuality) {
        // Use multi-step technique: start with original size, then
        // scale down in multiple passes with drawImage()
        // until the target size is reached
        w = img.getWidth();
        h = img.getHeight();
    } else {
        // Use one-step technique: scale directly from original
        // size to target size with a single drawImage() call
        w = targetWidth;
        h = targetHeight;
    }

    do {
        if (higherQuality && w > targetWidth) {
            w /= 2;
            if (w < targetWidth) {
                w = targetWidth;
            }
        }

        if (higherQuality && h > targetHeight) {
            h /= 2;
            if (h < targetHeight) {
                h = targetHeight;
            }
        }

        BufferedImage tmp = new BufferedImage(w, h, type);
        Graphics2D g2 = tmp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
        g2.drawImage(ret, 0, 0, w, h, null);
        g2.dispose();

        ret = tmp;
    } while (w != targetWidth || h != targetHeight);

    return ret;
}

From source file:org.n52.oxf.render.sos.ScatterPlotChartRenderer.java

/**
 * The resulting IVisualization shows a scatterplot.
 * //from w w w  .  jav a2  s. c  o m
 * Auf X- und Y-Achse wird jeweils eine observedProperty abgetragen.
 * 
 * Ein Punkt steht dann fr ein Wertepaar (X,Y) fr ein FOI zu einem bestimmten Zeitpunkt.
 * 
 * TODO PROBLEM: Macht nur Sinn, wenn fr jedes (FOI, Time)-Tuple jeweils ein Wert fr BEIDE
 * observedPropertys enthalten ist !!!!
 */
public IVisualization renderChart(OXFFeatureCollection observationCollection, ParameterContainer paramCon,
        int width, int height) {
    JFreeChart chart = renderChart(observationCollection, paramCon);

    Plot plot = chart.getPlot();

    // draw plot into image:

    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    Graphics2D g = image.createGraphics();

    plot.draw(g, new Rectangle2D.Float(0, 0, width, height), null, null, null);

    return new StaticVisualization(image);
}