Example usage for java.awt Image getWidth

List of usage examples for java.awt Image getWidth

Introduction

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

Prototype

public abstract int getWidth(ImageObserver observer);

Source Link

Document

Determines the width of the image.

Usage

From source file:com.chinarewards.gwt.license.util.FileUploadServlet.java

/**
 * @param imgsrc/*from  w w w .ja  v a2 s  . c  o m*/
 *            
 * @param imgdist
 *            ?
 * @param widthdist
 *            
 * @param heightdist
 *            
 * @param int benchmark :0,12
 * 
 */
public void reduceImg(InputStream inputStream, String outputFilePath, String imgdist, int widthdist,
        int heightdist, int benchmark) {
    try {
        // File srcfile = new File(srcFile);
        // if (!srcfile.exists()) {
        // return;
        // }

        boolean flag = true;

        Image srcImage = javax.imageio.ImageIO.read(inputStream);
        if (srcImage != null) {
            int width = srcImage.getWidth(null);
            int height = srcImage.getHeight(null);
            if (width <= widthdist && height <= heightdist) {// ??
                BufferedOutputStream outputStream = new BufferedOutputStream(
                        new FileOutputStream(new File(outputFilePath)));
                Streams.copy(inputStream, outputStream, true);

                flag = false;
            }

            if (flag) {

                // 
                float wh = (float) width / (float) height;
                if (benchmark == 0) {
                    if (wh > 1) {
                        float tmp_heigth = (float) widthdist / wh;
                        BufferedImage tag = new BufferedImage(widthdist, (int) tmp_heigth,
                                BufferedImage.TYPE_INT_RGB);
                        tag.getGraphics().drawImage(srcImage, 0, 0, widthdist, (int) tmp_heigth, null);
                        FileOutputStream out = new FileOutputStream(imgdist);
                        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                        encoder.encode(tag);
                        out.close();
                    } else {
                        float tmp_width = (float) heightdist * wh;
                        BufferedImage tag = new BufferedImage((int) tmp_width, heightdist,
                                BufferedImage.TYPE_INT_RGB);
                        tag.getGraphics().drawImage(srcImage, 0, 0, (int) tmp_width, heightdist, null);
                        FileOutputStream out = new FileOutputStream(imgdist);
                        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                        encoder.encode(tag);
                        out.close();
                    }
                }
                if (benchmark == 1) {
                    float tmp_heigth = (float) widthdist / wh;
                    BufferedImage tag = new BufferedImage(widthdist, (int) tmp_heigth,
                            BufferedImage.TYPE_INT_RGB);
                    tag.getGraphics().drawImage(srcImage, 0, 0, widthdist, (int) tmp_heigth, null);
                    FileOutputStream out = new FileOutputStream(imgdist);
                    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                    encoder.encode(tag);
                    out.close();
                }
                if (benchmark == 2) {
                    float tmp_width = (float) heightdist * wh;
                    BufferedImage tag = new BufferedImage((int) tmp_width, heightdist,
                            BufferedImage.TYPE_INT_RGB);
                    tag.getGraphics().drawImage(srcImage, 0, 0, (int) tmp_width, heightdist, null);
                    FileOutputStream out = new FileOutputStream(imgdist);
                    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                    encoder.encode(tag);
                    out.close();
                }

            }
        }

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:ImageOps.java

public void init() {
    setBackground(Color.white);/* w  w w  .j a  v  a 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:net.sradonia.gui.SplashScreen.java

/**
 * @param image//from ww  w .  java 2 s.  c  o m
 *            the new splash screen image
 */
public void setImage(Image image) {
    if (image == null)
        throw new IllegalArgumentException("null image");
    this.image = image;
    window.setSize(image.getWidth(window), image.getHeight(window));
    window.setLocationRelativeTo(null);
    log.debug("image set. dimensions: " + window.getBounds());
}

From source file:org.polymap.core.data.image.ImageGrayscaleProcessor.java

protected Image grayscale(Image image) {
    long start = System.currentTimeMillis();

    // load image data
    new javax.swing.ImageIcon(image).getImage();

    if (!(image instanceof BufferedImage)) {
        BufferedImage bimage = new BufferedImage(image.getHeight(null), image.getWidth(null),
                BufferedImage.TYPE_4BYTE_ABGR);
        Graphics g = bimage.getGraphics();
        g.drawImage(image, 0, 0, null);//  w  ww.j  av  a2s.c  o  m
        g.dispose();

        image = bimage;
    }

    // grayscale
    ColorConvertOp filter = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);

    BufferedImage grayImage = new BufferedImage(image.getHeight(null), image.getWidth(null),
            BufferedImage.TYPE_4BYTE_ABGR);

    Graphics g = grayImage.getGraphics();
    filter.filter((BufferedImage) image, grayImage);
    g.dispose();

    log.info("Gray scaling took: " + (System.currentTimeMillis() - start) + "ms");
    return grayImage;
}

From source file:com.pureinfo.tgirls.servlet.TestServlet.java

private int[] getimgSize(FileItem _fileItem) {
    try {//  w w w .  j a  v  a  2  s  . c o m
        logger.debug("to calc img width and height.");

        Image image = ImageIO.read(_fileItem.getInputStream());
        int[] s = new int[2];
        s[0] = image.getWidth(null);
        s[1] = image.getHeight(null);

        logger.debug("width[" + s[0] + "] height[" + s[1] + "]");

        return s;
    } catch (Exception e) {
        logger.error("error when calc image size.", e);
        return new int[] { -1, -1 };
    }
}

From source file:ucar.unidata.idv.flythrough.ChartDecorator.java

/**
 * _more_/*from w  w w.  j av  a  2  s. c  om*/
 *
 * @param g2 _more_
 * @param comp _more_
 *
 * @return _more_
 */
public boolean paintDashboard(Graphics2D g2, JComponent comp) {
    try {
        List<SampleInfo> infos = new ArrayList<SampleInfo>(sampleInfos);
        if (infos.size() == 0) {
            return false;
        }
        Rectangle b = comp.getBounds();
        JFrame dummyFrame = new JFrame("");
        XYSeriesCollection dataset = new XYSeriesCollection();
        JFreeChart chart = Flythrough.createChart(dataset);
        XYPlot xyPlot = (XYPlot) chart.getPlot();

        int chartHeight = b.height - flythrough.getDashboardImage().getHeight(null);
        chartHeight = Math.max(chartHeight, 50);
        int chartWidth = Math.min(chartHeight * 4, b.width);

        int dx = b.width / 2 - chartWidth / 2;
        int dy = 0;

        Image lastImage = lastChartImage;
        if ((lastImage != null) && (lastImage.getWidth(null) == chartWidth)
                && (lastImage.getHeight(null) == chartHeight)) {
            g2.translate(dx, dy);
            g2.drawImage(lastImage, 0, 0, null);
            g2.translate(-dx, -dy);
            return false;
        }

        for (int i = 0; i < infos.size(); i++) {
            SampleInfo info = infos.get(i);
            ValueAxis rangeAxis = new NumberAxis(info.getName());
            if (info.getRange() != null) {
                rangeAxis
                        .setRange(new org.jfree.data.Range(info.getRange().getMin(), info.getRange().getMax()));
            }
            dataset = new XYSeriesCollection();
            dataset.addSeries(info.getSeries());
            xyPlot.setRangeAxis(i, rangeAxis, false);
            xyPlot.setDataset(i, dataset);
            xyPlot.mapDatasetToRangeAxis(i, i);
            final Color color = COLORS[i % COLORS.length];
            XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true, false) {
                public Paint xgetItemPaint(final int row, final int column) {
                    return color;
                }
            };
            renderer.setSeriesPaint(0, color);
            xyPlot.setRenderer(i, renderer);
        }

        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(chartWidth, chartHeight));
        dummyFrame.setContentPane(chartPanel);
        dummyFrame.pack();
        Image image = ImageUtils.getImage(chartPanel);
        lastChartImage = image;
        g2.translate(dx, dy);
        g2.drawImage(image, 0, 0, null);
        g2.translate(-dx, -dy);
    } catch (Exception exc) {
        logException("Painting chart", exc);

    }

    return false;

}

From source file:TexturedPanel.java

/**
 * Creates a new TexturePaint using the provided image.
 *///from w  w w  .  j  a v a  2 s. c o  m
private void setupImagePainter(Image texture) {
    if (texture == null) {
        ourPainter = null;
        return;
    }

    int w = texture.getWidth(this);
    int h = texture.getHeight(this);

    if (w <= 0 || h <= 0) {
        ourPainter = null;
        return;
    }

    BufferedImage buff = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);

    Graphics2D g2 = buff.createGraphics();
    g2.drawImage(texture, 0, 0, this);
    ourPainter = new TexturePaint(buff, new Rectangle(0, 0, w, h));

    g2.dispose();
}

From source file:org.openstreetmap.josm.tools.ImageProvider.java

/**
 * Creates a rotated version of the input image.
 *
 * @param c The component to get properties useful for painting, e.g. the foreground or
 * background color.//w  w  w  .ja va 2 s.c  o  m
 * @param img the image to be rotated.
 * @param rotatedAngle the rotated angle, in degree, clockwise. It could be any double but we
 * will mod it with 360 before using it.
 *
 * @return the image after rotating.
 */
public static Image createRotatedImage(Component c, Image img, double rotatedAngle) {
    // convert rotatedAngle to a value from 0 to 360
    double originalAngle = rotatedAngle % 360;
    if (rotatedAngle != 0 && originalAngle == 0) {
        originalAngle = 360.0;
    }

    // convert originalAngle to a value from 0 to 90
    double angle = originalAngle % 90;
    if (originalAngle != 0.0 && angle == 0.0) {
        angle = 90.0;
    }

    double radian = Math.toRadians(angle);

    new ImageIcon(img); // load completely
    int iw = img.getWidth(null);
    int ih = img.getHeight(null);
    int w;
    int h;

    if ((originalAngle >= 0 && originalAngle <= 90) || (originalAngle > 180 && originalAngle <= 270)) {
        w = (int) (iw * Math.sin(DEGREE_90 - radian) + ih * Math.sin(radian));
        h = (int) (iw * Math.sin(radian) + ih * Math.sin(DEGREE_90 - radian));
    } else {
        w = (int) (ih * Math.sin(DEGREE_90 - radian) + iw * Math.sin(radian));
        h = (int) (ih * Math.sin(radian) + iw * Math.sin(DEGREE_90 - radian));
    }
    BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    Graphics g = image.getGraphics();
    Graphics2D g2d = (Graphics2D) g.create();

    // calculate the center of the icon.
    int cx = iw / 2;
    int cy = ih / 2;

    // move the graphics center point to the center of the icon.
    g2d.translate(w / 2, h / 2);

    // rotate the graphics about the center point of the icon
    g2d.rotate(Math.toRadians(originalAngle));

    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g2d.drawImage(img, -cx, -cy, c);

    g2d.dispose();
    new ImageIcon(image); // load completely
    return image;
}

From source file:com.krawler.esp.handlers.FileUploadHandler.java

public final void imgResize(String sourcePath, int Width, int Height, String destPath, boolean isCompany,
        boolean ori) throws IOException {
    try {/*from   w  w  w  . ja  va 2  s  . c  om*/
        String ext = getImageExt();
        String type = "jpeg";
        int typeRGB = BufferedImage.TYPE_INT_RGB;
        Image sourceImage = new ImageIcon(Toolkit.getDefaultToolkit().getImage(sourcePath)).getImage();
        if (isCompany) {
            ext = getCompanyImageExt();
            type = "PNG";
            typeRGB = BufferedImage.TYPE_INT_ARGB;
            int imageWidth = sourceImage.getWidth(null);
            int imageHeight = sourceImage.getHeight(null);
            if (ori) {
                Width = imageWidth;
                Height = imageHeight;
            } else {
                Width = imageWidth < Width ? imageWidth : Width;
                Height = imageHeight < Height ? imageHeight : Height;
                float imageRatio = ((float) imageWidth / (float) imageHeight);
                float framemageratio = ((float) Width / (float) Height);
                if (imageRatio > framemageratio) {
                    float value = Width / imageRatio;
                    Height = (int) value;

                } else {
                    float value = Height * imageRatio;
                    Width = (int) value;
                }
            }
        }

        BufferedImage resizedImage = this.scaleImage(sourceImage, Width, Height, typeRGB);
        ImageIO.write(resizedImage, type, new File(destPath + ext));
    } catch (Exception e) {
        Logger.getInstance(FileUploadHandler.class).error(e, e);
    }
}

From source file:org.shredzone.cilla.admin.AbstractImageBean.java

/**
 * Scales a {@link BufferedImage} to the given size, keeping the aspect ratio. If the
 * image is smaller than the resulting size, it will be magnified.
 *
 * @param image/*  ww  w .java 2s  . c o  m*/
 *            {@link BufferedImage} to scale
 * @param width
 *            Maximum result width
 * @param height
 *            Maximum result height
 * @return {@link BufferedImage} with the scaled image
 */
private BufferedImage scale(BufferedImage image, int width, int height) {
    ImageObserver observer = null;

    Image scaled = null;
    if (image.getWidth() > image.getHeight()) {
        scaled = image.getScaledInstance(width, -1, Image.SCALE_SMOOTH);
    } else {
        scaled = image.getScaledInstance(-1, height, Image.SCALE_SMOOTH);
    }

    BufferedImage result = new BufferedImage(scaled.getWidth(observer), scaled.getHeight(observer),
            BufferedImage.TYPE_INT_RGB);
    result.createGraphics().drawImage(scaled, 0, 0, observer);
    return result;
}