Example usage for java.awt.image BufferedImage getWidth

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

Introduction

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

Prototype

public int getWidth(ImageObserver observer) 

Source Link

Document

Returns the width of the BufferedImage .

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    BufferedImage bufferedImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);

    int rgb = bufferedImage.getRGB(1, 1);

    int w = bufferedImage.getWidth(null);
    int h = bufferedImage.getHeight(null);
    int[] rgbs = new int[w * h];
    bufferedImage.getRGB(0, 0, w, h, rgbs, 0, w);

    rgb = 0xFF00FF00; // green
    bufferedImage.setRGB(1, 1, rgb);/*  ww w  . ja  v a2 s  . c om*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    BufferedImage bufferedImage = new BufferedImage(200, 200, BufferedImage.TYPE_BYTE_INDEXED);

    AffineTransform tx = AffineTransform.getScaleInstance(-1, 1);
    tx.translate(-bufferedImage.getWidth(null), 0);
    AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    bufferedImage = op.filter(bufferedImage, null);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    BufferedImage bufferedImage = new BufferedImage(200, 200, BufferedImage.TYPE_BYTE_INDEXED);

    AffineTransform tx = AffineTransform.getScaleInstance(-1, -1);
    tx.translate(-bufferedImage.getWidth(null), -bufferedImage.getHeight(null));
    AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    bufferedImage = op.filter(bufferedImage, null);

}

From source file:Main.java

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            BufferedImage image = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB);
            Graphics2D imageGraphics = image.createGraphics();
            GradientPaint gp = new GradientPaint(20f, 20f, Color.red, 380f, 280f, Color.orange);
            imageGraphics.setPaint(gp);/*from ww w.j  a va  2 s . c  om*/
            imageGraphics.fillRect(0, 0, 400, 300);

            JLabel textLabel = new JLabel("java2s.com");
            textLabel.setSize(textLabel.getPreferredSize());

            Dimension d = textLabel.getPreferredSize();
            BufferedImage bi = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_ARGB);
            Graphics g = bi.createGraphics();
            g.setColor(new Color(255, 200, 255, 128));
            g.fillRoundRect(0, 0, bi.getWidth(f), bi.getHeight(f), 15, 10);
            g.setColor(Color.black);
            textLabel.paint(g);
            Graphics g2 = image.getGraphics();
            g2.drawImage(bi, 20, 20, f);

            ImageIcon ii = new ImageIcon(image);
            JLabel imageLabel = new JLabel(ii);

            f.getContentPane().add(imageLabel);
            f.pack();

            f.setVisible(true);
        }
    });
}

From source file:WaterMark.java

public static String execute(String src, String dest, String text, Color color, Font font) throws Exception {
    BufferedImage srcImage = ImageIO.read(new File(src));

    int width = srcImage.getWidth(null);
    int height = srcImage.getHeight(null);
    BufferedImage destImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics g = destImage.getGraphics();

    g.drawImage(srcImage, 0, 0, width, height, null);
    g.setColor(color);// w  ww . j a v a  2  s  .c o  m
    g.setFont(font);
    g.fillRect(0, 0, 50, 50);
    g.drawString(text, width / 5, height - 10);
    g.dispose();

    ImageIO.write(destImage, DEFAULT_FORMAT, new File("dest.jpg"));
    return dest;
}

From source file:ImageUtilities.java

private static BufferedImage convertToARGB(BufferedImage srcImage) {
    BufferedImage newImage = new BufferedImage(srcImage.getWidth(null), srcImage.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);
    Graphics bg = newImage.getGraphics();
    bg.drawImage(srcImage, 0, 0, null);/*from   w  w  w.  j  av a2s.com*/
    bg.dispose();
    return newImage;
}

From source file:Main.java

public static void drawImage(BufferedImage image, Graphics g, Component parent) {
    Dimension size = parent.getSize();
    Color background = parent.getBackground();
    if (image != null && size.width > 0) {
        double ratio = (double) image.getHeight(null) / image.getWidth(null);

        int effectiveWidth = 1;
        int effectiveHeight = (int) ratio;

        while (effectiveHeight < size.height && effectiveWidth < size.width) {
            effectiveWidth++;//from ww  w. ja va 2 s. c om
            effectiveHeight = (int) (ratio * effectiveWidth);
        }

        g.setColor(background);
        g.fillRect(0, 0, size.width, size.height);

        int cornerx = Math.abs((size.width - effectiveWidth) / 2);
        int cornery = Math.abs((size.height - effectiveHeight) / 2);
        g.drawImage(image, cornerx, cornery, effectiveWidth + cornerx, effectiveHeight + cornery, 0, 0,
                image.getWidth(null), image.getHeight(null), null);
    }
}

From source file:net.sf.ginp.util.GinpUtil.java

/**
 * Take a jpeg from an input stream and write it to an output.
 * stream with a scaled width and height
 * @param sos output stream for image/*www.  j a v  a 2  s. c o m*/
 * @param is input stream for image
 * @param width width
 * @param height height
 * @throws IOException if there is an error writing or reading
 */
public static void writeScaledImageToStream(final OutputStream sos, final InputStream is, final int width,
        final int height) throws IOException {
    JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(is);
    BufferedImage origImage = decoder.decodeAsBufferedImage();
    int origHeight = origImage.getHeight(null);
    int origWidth = origImage.getWidth(null);
    int scaledW = 0;
    int scaledH = 0;
    double scaleW = 1.0;
    double scaleH = 1.0;

    // close input stream
    is.close();

    // Calculate scale factors
    if (width == 0) {
        scaleW = (double) height / (double) origHeight;
        scaleH = (double) height / (double) origHeight;
    } else if (height == 0) {
        scaleW = (double) width / (double) origWidth;
        scaleH = (double) width / (double) origWidth;
    } else {
        scaleW = (double) width / (double) origWidth;
        scaleH = (double) height / (double) origHeight;
    }

    scaledW = (int) (scaleW * origWidth);
    scaledH = (int) (scaleH * origHeight);

    BufferedImage outImage = new BufferedImage(scaledW, scaledH, BufferedImage.TYPE_INT_RGB);
    AffineTransform tx = new AffineTransform();

    tx.scale(scaleW, scaleH);

    AffineTransformOp af = new AffineTransformOp(tx, null);

    af.filter(origImage, outImage);

    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);

    encoder.encode(outImage);
}

From source file:Main.java

private static int[] makeGradientPallet() {
    BufferedImage image = new BufferedImage(100, 1, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = image.createGraphics();
    Point2D start = new Point2D.Float(0f, 0f);
    Point2D end = new Point2D.Float(99f, 0f);
    float[] dist = { 0.0f, 0.5f, 1.0f };
    Color[] colors = { Color.RED, Color.YELLOW, Color.GREEN };
    g2.setPaint(new LinearGradientPaint(start, end, dist, colors));
    g2.fillRect(0, 0, 100, 1);//from w  ww  .  j a  va  2s . c o m
    g2.dispose();

    int width = image.getWidth(null);
    int[] pallet = new int[width];
    PixelGrabber pg = new PixelGrabber(image, 0, 0, width, 1, pallet, 0, width);
    try {
        pg.grabPixels();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return pallet;
}

From source file:ImageUtils.java

/**
 * Resizes an image./*w  w  w  .ja va2 s  . c  o  m*/
 * 
 * @param image
 *            The image to resize
 * @param maxWidth
 *            The image's max width
 * @param maxHeight
 *            The image's max height
 * @return A resized <code>BufferedImage</code>
 * @param type
 *            int
 */
public static BufferedImage resizeImage(BufferedImage image, int type, int maxWidth, int maxHeight) {
    Dimension largestDimension = new Dimension(maxWidth, maxHeight);

    // Original size
    int imageWidth = image.getWidth(null);
    int imageHeight = image.getHeight(null);

    float aspectRatio = (float) imageWidth / imageHeight;

    if (imageWidth > maxWidth || imageHeight > maxHeight) {
        if ((float) largestDimension.width / largestDimension.height > aspectRatio) {
            largestDimension.width = (int) Math.ceil(largestDimension.height * aspectRatio);
        } else {
            largestDimension.height = (int) Math.ceil(largestDimension.width / aspectRatio);
        }

        imageWidth = largestDimension.width;
        imageHeight = largestDimension.height;
    }

    return createHeadlessSmoothBufferedImage(image, type, imageWidth, imageHeight);
}