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:com.iiitd.qre.CreateZSignedQR.java

public static BufferedImage toBufferedImage(ByteMatrix matrix) {
    int width = matrix.getWidth();
    int height = matrix.getHeight();
    int scale = 6;
    int margin = 3;
    BufferedImage image = new BufferedImage(scale * (width + 2 * margin), scale * (height + 2 * margin),
            BufferedImage.TYPE_INT_ARGB);

    int imgX, imgY = margin * scale;
    for (int y = 0; y < height; y++) {
        for (int i = 0; i < scale; ++i) {
            imgX = margin * scale;/*from  w  w w  . j  a v  a  2 s  . c o m*/
            for (int x = 0; x < width; x++) {
                for (int j = 0; j < scale; ++j) {
                    if (matrix.get(x, y) != 0)
                        image.setRGB(imgX, imgY, BLACK);
                    ++imgX;
                }
            }
            ++imgY;
        }
    }
    return image;
}

From source file:TextureWithBufferedImage.java

public void paint(Graphics g) {
    Graphics2D g2D = (Graphics2D) g;
    Rectangle2D rec1, rec2, rec3, rec4, rec5;
    rec1 = new Rectangle2D.Float(25, 25, 75, 150);
    rec2 = new Rectangle2D.Float(125, 25, 10, 75);
    rec3 = new Rectangle2D.Float(75, 125, 125, 75);
    rec4 = new Rectangle2D.Float(25, 15, 12, 75);
    rec5 = new Rectangle2D.Float(15, 50, 15, 15);

    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1);
    g2D.setComposite(ac);//from  w  w w. jav  a2  s  . c o m

    g2D.setStroke(new BasicStroke(5.0f));
    g2D.draw(rec1);
    GradientPaint gp = new GradientPaint(125f, 25f, Color.yellow, 225f, 100f, Color.blue);
    g2D.setPaint(gp);
    g2D.fill(rec2);
    BufferedImage bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);
    Graphics2D big = bi.createGraphics();
    big.setColor(Color.magenta);
    big.fillRect(0, 0, 5, 5);
    big.setColor(Color.black);
    big.drawLine(0, 0, 5, 5);
    Rectangle r = new Rectangle(0, 0, 5, 5);
    TexturePaint tp = new TexturePaint(bi, r);

    g2D.setPaint(tp);
    g2D.fill(rec3);
    g2D.setColor(Color.green);
    g2D.fill(rec4);
    g2D.setColor(Color.red);
    g2D.fill(rec5);
}

From source file:org.primefaces.examples.view.DynamicImageController.java

public DynamicImageController() {
    try {/*from  www .  j  a va2  s  .c  om*/
        //Graphic Text
        BufferedImage bufferedImg = new BufferedImage(100, 25, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = bufferedImg.createGraphics();
        g2.drawString("This is a text", 0, 10);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(bufferedImg, "png", os);
        graphicText = new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()), "image/png");

        //Chart
        JFreeChart jfreechart = ChartFactory.createPieChart("Turkish Cities", createDataset(), true, true,
                false);
        File chartFile = new File("dynamichart");
        ChartUtilities.saveChartAsPNG(chartFile, jfreechart, 375, 300);
        chart = new DefaultStreamedContent(new FileInputStream(chartFile), "image/png");

        //Barcode
        File barcodeFile = new File("dynamicbarcode");
        BarcodeImageHandler.saveJPEG(BarcodeFactory.createCode128("PRIMEFACES"), barcodeFile);
        barcode = new DefaultStreamedContent(new FileInputStream(barcodeFile), "image/jpeg");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.uksf.mf.core.utility.loaders.ImageLoad.java

/**
 * Changes colour of all non-transparent pixels for given image to given colour
 * @param image - image to change colours in
 * @param newColour colour to change to/*  www  . jav a  2 s . co  m*/
 * @return image with changed colours
 */
private static ImageIcon changeImageColour(ImageIcon image, int newColour) {
    LogHandler.logSeverity(INFO, "Converting image: " + image + " colour to: " + newColour);
    BufferedImage bufferedImage = new BufferedImage(image.getIconWidth(), image.getIconHeight(),
            BufferedImage.TYPE_INT_ARGB);
    Graphics graphics = bufferedImage.createGraphics();
    image.paintIcon(null, graphics, 0, 0);
    graphics.dispose();
    for (int x = 0; x < bufferedImage.getWidth(); x++) {
        for (int y = 0; y < bufferedImage.getHeight(); y++) {
            int colour = bufferedImage.getRGB(x, y);
            colour = (((colour >> 24) & 0xff) << 24) | (((colour & 0x00ff0000) >> 16) << 16)
                    | (((colour & 0x0000ff00) >> 8) << 8) | (colour & 0x000000ff);
            if (colour != COLOUR_TRANSPARENT.getRGB()) {
                newColour = (((colour >> 24) & 0xff) << 24) | (((newColour & 0x00ff0000) >> 16) << 16)
                        | (((newColour & 0x0000ff00) >> 8) << 8) | (newColour & 0x000000ff);
                bufferedImage.setRGB(x, y, newColour);
            }
        }
    }
    return new ImageIcon(bufferedImage);
}

From source file:at.gv.egiz.pdfas.common.utils.ImageUtils.java

public static BufferedImage removeAlphaChannel(BufferedImage src) {
    //if (src.getColorModel().hasAlpha()) {
    BufferedImage image = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics2D g = image.createGraphics();
    g.setComposite(AlphaComposite.Src);
    g.drawImage(src, 0, 0, null);/*from   w w w  .  j  a va2s  . c o  m*/
    g.dispose();
    return image;
    //}
    //return src;
    /*
     * BufferedImage rgbImage = new BufferedImage(src.getWidth(),
     * src.getHeight(), BufferedImage.TYPE_3BYTE_BGR); for (int x = 0; x <
     * src.getWidth(); ++x) { for (int y = 0; y < src.getHeight(); ++y) {
     * rgbImage.setRGB(x, y, src.getRGB(x, y) & 0xFFFFFF); } } return
     * rgbImage;
     */
}

From source file:imageprocessingproject.ImageHistogram.java

public static BufferedImage autoContrastImage(BufferedImage image) {
    createContrastLUT(image);//from   w  w  w  . ja  va  2 s  .c  om

    int height = image.getHeight();
    int width = image.getWidth();
    int r, g, b;
    Color c;

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

    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            c = new Color(image.getRGB(i, j));
            r = (int) (255 * contrast_lut[c.getRed()]);
            g = (int) (255 * contrast_lut[c.getGreen()]);
            b = (int) (255 * contrast_lut[c.getBlue()]);
            tempImage.setRGB(i, j, new Color(r, g, b, c.getAlpha()).getRGB());
        }
    }

    return tempImage;
}

From source file:net.d53dev.dslfy.web.service.GifService.java

public BufferedImage convertRGBAToGIF(BufferedImage src, int transColor) {
    BufferedImage dst = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
    Graphics g = dst.getGraphics();
    g.setColor(new Color(transColor));
    g.fillRect(0, 0, dst.getWidth(), dst.getHeight());
    {//from w w w .  j  a  va 2s.  c o  m
        IndexColorModel indexedModel = (IndexColorModel) dst.getColorModel();
        WritableRaster raster = dst.getRaster();
        int sample = raster.getSample(0, 0, 0);
        int size = indexedModel.getMapSize();
        byte[] rr = new byte[size];
        byte[] gg = new byte[size];
        byte[] bb = new byte[size];
        indexedModel.getReds(rr);
        indexedModel.getGreens(gg);
        indexedModel.getBlues(bb);
        IndexColorModel newModel = new IndexColorModel(8, size, rr, gg, bb, sample);
        dst = new BufferedImage(newModel, raster, dst.isAlphaPremultiplied(), null);
    }
    dst.createGraphics().drawImage(src, 0, 0, null);
    return dst;
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.canvas.rendering.AwtRenderingBackend.java

/**
 * Constructor.//from ww  w  . j a  va 2  s  .co  m
 * @param imageWidth the width
 * @param imageHeight the height
 */
public AwtRenderingBackend(final int imageWidth, final int imageHeight) {
    image_ = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
    graphics2D_ = image_.createGraphics();
}

From source file:BufferedImageMouseDrag.java

DisplayCanvas() {
    setBackground(Color.white);/*  w w w  . j  a  v  a2 s  .  c  o  m*/
    setSize(450, 400);
    addMouseMotionListener(new MouseMotionHandler());

    Image image = getToolkit().getImage("largeJava2sLogo.gif");

    MediaTracker mt = new MediaTracker(this);
    mt.addImage(image, 1);
    try {
        mt.waitForAll();
    } catch (Exception e) {
        System.out.println("Exception while loading image.");
    }

    if (image.getWidth(this) == -1) {
        System.out.println("no gif file");
        System.exit(0);
    }

    bi = new BufferedImage(image.getWidth(this), image.getHeight(this), BufferedImage.TYPE_INT_ARGB);
    Graphics2D big = bi.createGraphics();
    big.drawImage(image, 0, 0, this);
}

From source file:image.writer.ImageWriterExample2.java

private BufferedImage createAnImage(String compression, int pageNum) {
    boolean monochrome = compression.startsWith("CCITT"); //CCITT is for 1bit b/w only

    BufferedImage bimg;// w  w w  .j  a  v a  2s  . c o m
    if (monochrome) {
        bimg = new BufferedImage(400, 200, BufferedImage.TYPE_BYTE_BINARY);
    } else {
        bimg = new BufferedImage(400, 200, BufferedImage.TYPE_INT_RGB);
    }

    Graphics2D g2d = bimg.createGraphics();
    g2d.setBackground(Color.white);
    g2d.clearRect(0, 0, 400, 200);
    g2d.setColor(Color.black);

    //Paint something
    paintSome(g2d, pageNum);

    return bimg;
}