Example usage for java.awt.image BufferedImage TYPE_INT_RGB

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

Introduction

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

Prototype

int TYPE_INT_RGB

To view the source code for java.awt.image BufferedImage TYPE_INT_RGB.

Click Source Link

Document

Represents an image with 8-bit RGB color components packed into integer pixels.

Usage

From source file:SaveImage.java

public SaveImage() {
    try {/*from  ww  w. jav  a 2s  .com*/
        bi = ImageIO.read(new File("bld.jpg"));
        w = bi.getWidth(null);
        h = bi.getHeight(null);
        if (bi.getType() != BufferedImage.TYPE_INT_RGB) {
            BufferedImage bi2 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            Graphics big = bi2.getGraphics();
            big.drawImage(bi, 0, 0, null);
            biFiltered = bi = bi2;
        }
    } catch (IOException e) {
        System.out.println("Image could not be read");
        System.exit(1);
    }
}

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

/**
 * The resulting IVisualization shows a scatterplot.
 * //  w ww . j  a  va  2  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);
}

From source file:DataBufferGrabber.java

protected void paintComponent(Graphics g) {
    // create an image
    BufferedImage bImg = new BufferedImage(SWATCH_SIZE, SWATCH_SIZE, BufferedImage.TYPE_INT_RGB);
    Graphics gImage = bImg.getGraphics();
    gImage.setColor(Color.WHITE);
    gImage.fillRect(0, 0, SWATCH_SIZE, SWATCH_SIZE);

    // Time how long it takes to copy the managed version
    long managedTime = copyImage(g, bImg, 0, 0);
    System.out.println("Managed: " + managedTime + " ms");

    // Now grab the pixel array, change the colors, re-run the test
    Raster raster = bImg.getRaster();
    DataBufferInt dataBuffer = (DataBufferInt) raster.getDataBuffer();
    int pixels[] = dataBuffer.getData();
    for (int i = 0; i < pixels.length; ++i) {
        // Make all pixels black
        pixels[i] = 0;// w  w w .j  a  v a 2s.co  m
    }

    // Time this un-managed copy
    long unmanagedTime = copyImage(g, bImg, SWATCH_SIZE, 0);
    System.out.println("Unmanaged: " + unmanagedTime + " ms");
}

From source file:ImageOps.java

public void init() {
    setBackground(Color.white);/*from w  w w.  ja  va 2  s.  c  o  m*/

    bi = new BufferedImage[4];
    String s[] = { "bld.jpg", "bld.jpg", "boat.gif", "boat.gif" };
    for (int i = 0; i < bi.length; i++) {
        Image img = getImage(getURL("images/" + s[i]));
        try {
            MediaTracker tracker = new MediaTracker(this);
            tracker.addImage(img, 0);
            tracker.waitForID(0);
        } catch (Exception e) {
        }
        int iw = img.getWidth(this);
        int ih = img.getHeight(this);
        bi[i] = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
        Graphics2D big = bi[i].createGraphics();
        big.drawImage(img, 0, 0, this);
    }
}

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

/**
 * Runs the timing./*  w ww .  j  a  va 2s.  c  om*/
 */
public void run() {
    this.finished = false;

    // create a dataset...
    final DefaultPieDataset data = new DefaultPieDataset();
    data.setValue("One", new Double(10.3));
    data.setValue("Two", new Double(8.5));
    data.setValue("Three", new Double(3.9));
    data.setValue("Four", new Double(3.9));
    data.setValue("Five", new Double(3.9));
    data.setValue("Six", new Double(3.9));

    // create a pie chart...
    final boolean withLegend = true;
    final JFreeChart chart = ChartFactory.createPieChart("Testing", data, withLegend, true, false);

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

public void createBufferedImage() {
    bi = new BufferedImage(displayImage.getWidth(this), displayImage.getHeight(this),
            BufferedImage.TYPE_INT_RGB);

    big = bi.createGraphics();/*from ww w.  j  ava  2  s.c  om*/
    big.drawImage(displayImage, 0, 0, this);
}

From source file:ImageDrawingComponent.java

public ImageDrawingComponent(URL imageSrc) {
    try {/*from  ww w  .ja  va  2 s .c  o  m*/
        bi = ImageIO.read(imageSrc);
        w = bi.getWidth(null);
        h = bi.getHeight(null);
        if (bi.getType() != BufferedImage.TYPE_INT_RGB) {
            BufferedImage bi2 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            Graphics big = bi2.getGraphics();
            big.drawImage(bi, 0, 0, null);
            bi = bi2;
        }
    } catch (IOException e) {
        System.out.println("Image could not be read");
        System.exit(1);
    }
}

From source file:edu.emory.library.tast.images.ThumbnailServlet.java

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    // location of images
    String baseUrl = AppConfig.getConfiguration().getString(AppConfig.IMAGES_URL);
    baseUrl = StringUtils.trimEnd(baseUrl, '/');

    // image name and size
    String imageFileName = request.getParameter("i");
    int thumbnailWidth = Integer.parseInt(request.getParameter("w"));
    int thumbnailHeight = Integer.parseInt(request.getParameter("h"));

    // image dir//w  ww  . jav a2  s.  c om
    String imagesDir = AppConfig.getConfiguration().getString(AppConfig.IMAGES_DIRECTORY);

    // create the thumbnail name
    String thumbnailFileName = FilenameUtils.getBaseName(imageFileName) + "-" + thumbnailWidth + "x"
            + thumbnailHeight + ".png";

    // does it exist?
    File thumbnailFile = new File(imagesDir, thumbnailFileName);
    if (thumbnailFile.exists()) {
        response.sendRedirect(baseUrl + "/" + thumbnailFileName);
        return;
    }

    // read the image
    File imageFile = new File(imagesDir, imageFileName);
    BufferedImage image = ImageIO.read(imageFile);
    int imageWidth = image.getWidth();
    int imageHeight = image.getHeight();
    BufferedImage imageCopy = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
    imageCopy.getGraphics().drawImage(image, 0, 0, imageWidth, imageHeight, 0, 0, imageWidth, imageHeight,
            null);

    // height is calculated automatically
    if (thumbnailHeight == 0)
        thumbnailHeight = (int) ((double) thumbnailWidth / (double) imageWidth * (double) imageHeight);

    // width is calculated automatically
    if (thumbnailWidth == 0)
        thumbnailWidth = (int) ((double) thumbnailHeight / (double) imageHeight * (double) imageWidth);

    // create an empty thumbnail
    BufferedImage thumbnail = new BufferedImage(thumbnailWidth, thumbnailHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D gr = thumbnail.createGraphics();
    gr.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    gr.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

    // determine the piece of the image which we want to clip
    int clipX1, clipX2, clipY1, clipY2;
    if (imageWidth * thumbnailHeight > thumbnailWidth * imageHeight) {

        int clipWidth = (int) Math
                .round(((double) thumbnailWidth / (double) thumbnailHeight) * (double) imageHeight);
        int imgCenterX = imageWidth / 2;
        clipX1 = imgCenterX - clipWidth / 2;
        clipX2 = imgCenterX + clipWidth / 2;
        clipY1 = 0;
        clipY2 = imageHeight;
    } else {
        int clipHeight = (int) Math
                .round(((double) thumbnailHeight / (double) thumbnailWidth) * (double) imageWidth);
        int imgCenterY = imageHeight / 2;
        clipX1 = 0;
        clipX2 = imageWidth;
        clipY1 = imgCenterY - clipHeight / 2;
        clipY2 = imgCenterY + clipHeight / 2;
    }

    // we filter the image first to get better results when shrinking
    if (2 * thumbnailWidth < clipX2 - clipX1 || 2 * thumbnailHeight < clipY2 - clipY1) {

        int kernelDimX = (clipX2 - clipX1) / (thumbnailWidth);
        int kernelDimY = (clipY2 - clipY1) / (thumbnailHeight);

        if (kernelDimX % 2 == 0)
            kernelDimX++;
        if (kernelDimY % 2 == 0)
            kernelDimY++;

        if (kernelDimX < kernelDimY)
            kernelDimX = kernelDimY;
        if (kernelDimY < kernelDimX)
            kernelDimY = kernelDimX;

        float[] blurKernel = new float[kernelDimX * kernelDimY];
        for (int i = 0; i < kernelDimX; i++)
            for (int j = 0; j < kernelDimY; j++)
                blurKernel[i * kernelDimX + j] = 1.0f / (float) (kernelDimX * kernelDimY);

        BufferedImageOp op = new ConvolveOp(new Kernel(kernelDimX, kernelDimY, blurKernel));
        imageCopy = op.filter(imageCopy, null);

    }

    // draw the thumbnail
    gr.drawImage(imageCopy, 0, 0, thumbnailWidth, thumbnailHeight, clipX1, clipY1, clipX2, clipY2, null);

    // and we are done
    gr.dispose();
    ImageIO.write(thumbnail, "png", thumbnailFile);

    // redirect to it
    response.sendRedirect(baseUrl + "/" + thumbnailFileName);

}

From source file:ar.com.zauber.common.image.impl.AbstractImage.java

/**
 * Creates a thumbnail//from  w  w w.  j  a v a 2  s.c  o  m
 * 
 * @param is data source
 * @param os data source
 * @throws IOException if there is a problem reading is
 */
public static void createThumbnail(final InputStream is, final OutputStream os, final int target)
        throws IOException {
    final float compression = 0.85F;
    ImageWriter writer = null;
    MemoryCacheImageOutputStream mos = null;
    Graphics2D graphics2D = null;
    try {
        final BufferedImage bi = ImageIO.read(is);
        final Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("JPG");
        if (!iter.hasNext()) {
            throw new IllegalStateException("can't find JPG subsystem");
        }

        int w = bi.getWidth(), h = bi.getHeight();
        if (w < target && h < target) {
            // nothing to recalculate, ya es chiquita.
        } else {
            if (w > h) {
                h = target * bi.getHeight() / bi.getWidth();
                w = target;
            } else {
                w = target * bi.getWidth() / bi.getHeight();
                h = target;
            }
        }
        // draw original image to thumbnail image object and
        // scale it to the new size on-the-fly
        final BufferedImage thumbImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        graphics2D = thumbImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(bi, 0, 0, w, h, null);

        writer = (ImageWriter) iter.next();
        final ImageWriteParam iwp = writer.getDefaultWriteParam();
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        iwp.setCompressionQuality(compression);
        mos = new MemoryCacheImageOutputStream(os);
        writer.setOutput(mos);
        writer.write(null, new IIOImage(thumbImage, null, null), iwp);
    } finally {
        if (writer != null) {
            writer.dispose();
        }
        if (mos != null) {
            mos.close();
        }
        if (graphics2D != null) {
            graphics2D.dispose();
        }
        is.close();
        os.close();
    }
}

From source file:org.shredzone.commons.captcha.impl.DefaultCaptchaGenerator.java

@Override
public BufferedImage createCaptcha(char[] text) {
    if (text == null || text.length == 0) {
        throw new IllegalArgumentException("No captcha text given");
    }/*from  ww w . jav  a 2s .co m*/

    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = image.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setBackground(Color.WHITE);
    g2d.setColor(Color.BLACK);

    clearCanvas(g2d);

    if (showGrid) {
        drawGrid(g2d);
    }

    int charMaxWidth = width / text.length;
    int xPos = 0;
    for (char ch : text) {
        drawCharacter(g2d, ch, xPos, charMaxWidth);
        xPos += charMaxWidth;
    }

    g2d.dispose();
    return image;
}