Example usage for java.awt Image getHeight

List of usage examples for java.awt Image getHeight

Introduction

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

Prototype

public abstract int getHeight(ImageObserver observer);

Source Link

Document

Determines the height of the image.

Usage

From source file:BufferedImageMouseDrag.java

DisplayCanvas() {
    setBackground(Color.white);/*from w  w w.j  ava 2 s . c  om*/
    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:TextBouncer.java

public BufferedImage makeBufferedImage(Image image, int imageType) {
    if (waitForImage(image) == false)
        return null;

    BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), imageType);
    Graphics2D g2 = bufferedImage.createGraphics();
    g2.drawImage(image, null, null);//w w  w.  j a  va2 s. c  o  m
    return bufferedImage;
}

From source file:com.aurel.track.attachment.AttachBL.java

public static Integer getThumbnailHeight(String sessionID, TAttachmentBean attachBean) {
    Integer result = null;// w  w w.ja  v a2s .c o  m
    if (isImage(attachBean)) {
        boolean b = createTumbnail(sessionID, attachBean);
        if (b) {
            String thumbFileName = getFullThumbFileName(sessionID, attachBean);
            Image image = Toolkit.getDefaultToolkit().getImage(thumbFileName);
            return image.getHeight(null);
        }
    }

    return result;
}

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   www. j a  v a 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.dishevelled.piccolo.sprite.statemachine.AbstractStateMachineSprite.java

@Override
public final void paint(final PPaintContext paintContext) {
    if (currentAnimation != null) {
        Graphics2D g = paintContext.getGraphics();
        Image currentFrame = currentAnimation.getCurrentFrame();
        PBounds bounds = getBoundsReference();

        double w = currentFrame.getWidth(null);
        double h = currentFrame.getHeight(null);

        g.translate(bounds.getX(), bounds.getY());
        g.scale(bounds.getWidth() / w, bounds.getHeight() / h);
        g.drawImage(currentFrame, 0, 0, null);
        g.scale(w / bounds.getWidth(), h / bounds.getHeight());
        g.translate(-1 * bounds.getX(), -1 * bounds.getY());
    }//from  ww  w.  ja va2 s .  c o  m
}

From source file:org.fao.geonet.services.thumbnail.Set.java

public BufferedImage getImage(String inFile) throws IOException {
    String lcFile = inFile.toLowerCase();

    if (lcFile.endsWith(".tif") || lcFile.endsWith(".tiff")) {
        //--- load the TIFF/GEOTIFF file format

        Image image = getTiffImage(inFile);

        int width = image.getWidth(null);
        int height = image.getHeight(null);

        BufferedImage bimg = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D g = bimg.createGraphics();
        g.drawImage(image, 0, 0, null);/*from w  ww.j a  va2  s.c o m*/
        g.dispose();

        return bimg;
    }

    return ImageIO.read(new File(inFile));
}

From source file:RasterDemo.java

RasterPanel() {
    setBackground(Color.white);// w  w  w .  j a v a2  s. c  o m
    setSize(450, 400);

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

    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 jpg file");
        System.exit(0);
    }

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

From source file:Package.Projectoverviewservlet.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    frame.setAlwaysOnTop(true);//w ww.j  a v a 2 s  .  com
    if (request.getParameter("submit") != null) {
        Database database = null;
        try {
            database = new Database();
            database.Connect();
        } catch (SQLException | ClassNotFoundException ex) {
            Logger.getLogger(Projectoverviewservlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            String contentType = request.getContentType();
            if ((contentType.indexOf("multipart/form-data") >= 0)) {
                try {
                    Part filePart = request.getPart("fileupload");
                    Image image = ImageIO.read(filePart.getInputStream());
                    String INSERT_PICTURE = "INSERT INTO \"PICTURE\"(\"PICTUREID\", \"PROJECTID\", \"HEIGHT\", \"WIDTH\", \"COLORTYPE\", \"PICTURE\") VALUES (PictureSequence.nextval, 1,"
                            + image.getHeight(null) + "," + image.getWidth(null) + ", 'Color', ?)";
                    InputStream is = null;
                    PreparedStatement ps = null;
                    try {
                        is = filePart.getInputStream();
                        ps = database.myConn.prepareStatement(INSERT_PICTURE);
                        ps.setBlob(1, is);
                        ps.executeUpdate();
                        database.myConn.commit();
                        JOptionPane.showMessageDialog(frame, "De afbeelding is succesvol geupload.");
                    } finally {
                        try {
                            ps.close();
                            is.close();
                        } catch (Exception exception) {

                        }
                    }
                } catch (IOException | ServletException | SQLException ex) {
                    System.out.println(ex);
                }
            } else {
                JOptionPane.showMessageDialog(frame, "Er is iets fout gegaan probeer het opnieuw");
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(frame, ex.getMessage());
        }
        response.sendRedirect("projectoverview.jsp");
    }

    if (request.getParameter("deleteproject") != null) {
        String[] selectresults = request.getParameterValues("selectproject");
        po.deleteProject(Integer.parseInt(selectresults[0]));

    }

    if (request.getParameter("openproject") != null) {
        try {
            String[] selectresults = request.getParameterValues("selectproject");
            Project project = po.getProject(Integer.parseInt(selectresults[0]));
            request.setAttribute("project", project);
            request.getRequestDispatcher("projectoverview.jsp").forward(request, response);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(frame, ex.getMessage());
        }

    }

    if (request.getParameter("Save") != null) {

        Project project = (Project) request.getAttribute("project");
        if (project != null) {
            try {
                System.out.println(request.getParameter("startdate"));
                po.updateProject(project.getProjectID(), request.getParameter("name"),
                        request.getParameter("client"),
                        new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("startdate")),
                        new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("enddate")));
            } catch (Exception ex) {

            }
        } else {
            String username = "";
            for (Cookie cookie : request.getCookies()) {
                if (cookie.getName().equals("Email")) {
                    username = cookie.getValue();
                }
            }
            if (!username.isEmpty()) {
                try {
                    po.createProject(po.connection.getCompanyID(username), request.getParameter("name"),
                            request.getParameter("client"),
                            new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("startdate")),
                            new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("enddate")));
                } catch (ParseException ex) {
                    Logger.getLogger(Projectoverviewservlet.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            //roep create aan
        }
        request.getRequestDispatcher("projectoverview.jsp").forward(request, response);
    }

    if (request.getParameter("deleteimage") != null) {

    }

    if (request.getParameter("importimage") != null) {

    }

    if (request.getParameter("koppel") != null) {

    }

    if (request.getParameter("addemail") != null) {

    }

    if (request.getParameter("deleteemail") != null) {

    }

    if (request.getParameter("importemail") != null) {

    }

}

From source file:org.encuestame.business.images.ImageThumbnailGeneratorImpl.java

/**
 * Create a thumbnail image and save it to disk.
 *
 * This algorithm is based on:/*from  ww  w  . j  av  a2  s .c o m*/
 *      http://www.philreeve.com/java_high_quality_thumbnails.php
 *
 * @param imageIn           The image you want to scale.
 * @param fileOut           The output file.
 * @param largestDimension  The largest dimension, so that neither the width nor height
 *                          will exceed this value.
 *
 * @return the image that was created, null if imageIn or fileOut is null.
 * @throws java.io.IOException if something goes wrong when saving as jpeg
 */
public BufferedImage createThumbnailImage(Image imageIn, File fileOut, int largestDimension)
        throws IOException {
    if ((imageIn == null) || (fileOut == null)) {
        return null;
    }
    //it seems to not return the right size until the methods get called for the first time
    imageIn.getWidth(null);
    imageIn.getHeight(null);

    // Find biggest dimension
    int nImageWidth = imageIn.getWidth(null);
    int nImageHeight = imageIn.getHeight(null);
    int nImageLargestDim = Math.max(nImageWidth, nImageHeight);
    double scale = (double) largestDimension / (double) nImageLargestDim;
    int sizeDifference = nImageLargestDim - largestDimension;

    //create an image buffer to draw to
    BufferedImage imageOut = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); // 8-bit RGB
    Graphics2D g2d;
    AffineTransform tx;

    // Use a few steps if the sizes are drastically different, and only scale
    // if the desired size is smaller than the original.
    int numSteps = 0;
    if (scale < 1.0d) {
        // Make sure we have at least 1 step
        numSteps = Math.max(1, (sizeDifference / 100));
    }

    if (numSteps > 0) {
        int stepSize = sizeDifference / numSteps;
        int stepWeight = stepSize / 2;
        int heavierStepSize = stepSize + stepWeight;
        int lighterStepSize = stepSize - stepWeight;
        int currentStepSize, centerStep;
        double scaledW = imageIn.getWidth(null);
        double scaledH = imageIn.getHeight(null);

        if ((numSteps % 2) == 1) //if there's an odd number of steps
            centerStep = (int) Math.ceil((double) numSteps / 2d); //find the center step
        else
            centerStep = -1; //set it to -1 so it's ignored later

        Integer intermediateSize;
        Integer previousIntermediateSize = nImageLargestDim;

        for (Integer i = 0; i < numSteps; i++) {
            if (i + 1 != centerStep) {
                //if this isn't the center step

                if (i == numSteps - 1) {
                    //if this is the last step
                    //fix the stepsize to account for decimal place errors previously
                    currentStepSize = previousIntermediateSize - largestDimension;
                } else {
                    if (numSteps - i > numSteps / 2) //if we're in the first half of the reductions
                        currentStepSize = heavierStepSize;
                    else
                        currentStepSize = lighterStepSize;
                }
            } else {
                //center step, use natural step size
                currentStepSize = stepSize;
            }

            intermediateSize = previousIntermediateSize - currentStepSize;
            scale = intermediateSize / (double) previousIntermediateSize;
            scaledW = Math.max((int) (scaledW * scale), 1);
            scaledH = Math.max((int) (scaledH * scale), 1);

            log.info("step " + i + ": scaling to " + scaledW + " x " + scaledH);
            imageOut = new BufferedImage((int) scaledW, (int) scaledH, BufferedImage.TYPE_INT_RGB); // 8 bit RGB
            g2d = imageOut.createGraphics();
            g2d.setBackground(Color.WHITE);
            g2d.clearRect(0, 0, imageOut.getWidth(), imageOut.getHeight());
            g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            tx = new AffineTransform();
            tx.scale(scale, scale);
            g2d.drawImage(imageIn, tx, null);
            g2d.dispose();
            imageIn = new ImageIcon(imageOut).getImage();
            previousIntermediateSize = intermediateSize;
        }
    } else {
        // This enforces a rule that we always have an 8-bit image with white background for the thumbnail.  Plus, for large
        // images, this makes subsequent downscaling really fast because we are working on a large 8-bit image
        // instead of a large 12 or 24 bit image, so the downstream effect is very noticable.
        imageOut = new BufferedImage(imageIn.getWidth(null), imageIn.getHeight(null),
                BufferedImage.TYPE_INT_RGB);
        g2d = imageOut.createGraphics();
        g2d.setBackground(Color.WHITE);
        g2d.clearRect(0, 0, imageOut.getWidth(), imageOut.getHeight());
        tx = new AffineTransform();
        tx.setToIdentity(); //use identity matrix so image is copied exactly
        g2d.drawImage(imageIn, tx, null);
        g2d.dispose();
    }
    //saveImageAsJPEG(imageOut, fileOut);
    ImageIO.write(imageOut, "jpg", fileOut);
    return imageOut;
}

From source file:GIFEncoder.java

/**
 * Construct a GIFEncoder. The constructor will convert the image to
 * an indexed color array. <B>This may take some time.</B><P>
 * //from w ww. j a  v a  2s .c  om
 * @param image The image to encode. The image <B>must</B> be
 * completely loaded.
 * @exception AWTException Will be thrown if the pixel grab fails. This
 * can happen if Java runs out of memory. It may also indicate that the image
 * contains more than 256 colors.
 * */
public GIFEncoder(Image image) throws AWTException {
    width_ = (short) image.getWidth(null);
    height_ = (short) image.getHeight(null);

    int values[] = new int[width_ * height_];
    PixelGrabber grabber = new PixelGrabber(image, 0, 0, width_, height_, values, 0, width_);

    try {
        if (grabber.grabPixels() != true)
            throw new AWTException("Grabber returned false: " + grabber.status());
    } catch (InterruptedException e) {
        ;
    }

    byte r[][] = new byte[width_][height_];
    byte g[][] = new byte[width_][height_];
    byte b[][] = new byte[width_][height_];
    int index = 0;
    for (int y = 0; y < height_; ++y)
        for (int x = 0; x < width_; ++x) {
            r[x][y] = (byte) ((values[index] >> 16) & 0xFF);
            g[x][y] = (byte) ((values[index] >> 8) & 0xFF);
            b[x][y] = (byte) ((values[index]) & 0xFF);
            ++index;
        }
    ToIndexedColor(r, g, b);
}