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() 

Source Link

Document

Returns the width of the BufferedImage .

Usage

From source file:gr.iti.mklab.reveal.forensics.util.Util.java

public static float[][][] getResizedImageDifference(BufferedImage image1, BufferedImage image2, int newWidth,
        int newHeight) {
    // Calculate the subsampled difference between two buffered images
    float[][][] outputMap = new float[3][newWidth][newHeight];
    float widthModifier = (float) image1.getWidth() / newWidth;
    float heightModifier = (float) image1.getHeight() / newHeight;
    Color tmpColor1, tmpColor2;//from w  w w  . j  av  a2s . com
    for (int ii = 0; ii < newHeight; ii++) {
        for (int jj = 0; jj < newWidth; jj++) {
            try {
                tmpColor1 = new Color(
                        image1.getRGB(Math.round(jj * widthModifier), Math.round(ii * heightModifier)));
                tmpColor2 = new Color(
                        image2.getRGB(Math.round(jj * widthModifier), Math.round(ii * heightModifier)));
                outputMap[0][jj][ii] = (float) (tmpColor1.getRed() - tmpColor2.getRed())
                        * (tmpColor1.getRed() - tmpColor2.getRed());
                outputMap[1][jj][ii] = (float) (tmpColor1.getGreen() - tmpColor2.getGreen())
                        * (tmpColor1.getGreen() - tmpColor2.getGreen());
                outputMap[2][jj][ii] = (float) (tmpColor1.getBlue() - tmpColor2.getBlue())
                        * (tmpColor1.getBlue() - tmpColor2.getBlue());
            } catch (Exception e) {
                System.out.println(newHeight + " " + newWidth + " " + image1.getHeight() + " "
                        + image1.getWidth() + " " + ii + " " + jj + " " + Math.round(ii * heightModifier) + " "
                        + Math.round(jj * widthModifier) + " " + heightModifier + " " + widthModifier);
                e.printStackTrace();
                return outputMap;
            }
        }
    }
    return outputMap;
}

From source file:org.smigo.constraints.CommonsMultipartFileImageValidator.java

public boolean isValid(CommonsMultipartFile file, ConstraintValidatorContext constraintContext) {
    log.debug("Validating:" + file.getOriginalFilename() + " contenttype:" + file.getContentType()
            + " storagedescrip:" + file.getStorageDescription());
    if (file.isEmpty())
        return true;
    try {//from  www.  j  av  a2s  .c om
        BufferedImage image = ImageIO.read(file.getInputStream());
        if (image.getHeight() == height && image.getWidth() == width)
            return true;
    } catch (Exception e) {
        return false;
    }
    return false;
}

From source file:at.tuwien.ifs.somtoolbox.apps.viewer.fileutils.ExportUtils.java

public static void drawLinkInfo(GrowingLayer growingLayer, MapPNode mapPnode, double unitWidth,
        Graphics2D graphics, String dataFilesPrefix) {
    int width = (int) unitWidth;
    for (int x = 0; x < growingLayer.getXSize(); x++) {
        for (int y = 0; y < growingLayer.getYSize(); y++) {
            try {
                if (growingLayer.getUnit(x, y) != null) {
                    String[] mappedData = growingLayer.getUnit(x, y).getMappedInputNames();
                    if (mappedData != null) {
                        boolean hasValidLink = false;
                        for (String element : mappedData) {
                            if (new File(dataFilesPrefix + element).exists()) {
                                hasValidLink = true;
                                break;
                            }/*from   ww w.j a  va  2s  . co  m*/
                        }
                        if (hasValidLink) {
                            try {
                                BufferedImage icon = ImageIO.read(new FileInputStream(ExportUtils.class
                                        .getResource(RESOURCE_PATH_ICONS + "note.png").getFile()));
                                // icon = scaleImageByHeight(icon, (int) (unitWidth - 2));
                                int iconHeight = (int) (unitWidth * 0.7);
                                int iconWidth = (int) ((double) iconHeight / (double) icon.getHeight()
                                        * icon.getWidth());
                                int restWidth = (width - iconWidth) / 2;
                                int restHeight = (width - iconHeight) / 2;
                                graphics.drawImage(icon, x * width + restWidth, y * width + restHeight,
                                        iconWidth, iconHeight, null);
                            } catch (FileNotFoundException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }
                }
            } catch (LayerAccessException e) {
                // should not happen
                e.printStackTrace();
            }

        }
    }
}

From source file:ch.admin.isb.hermes5.util.ImageUtilsTest.java

@Test
public void testScaleByWidth() {
    byte[] gifStream = getResourceAsStream("logo.jpg");
    BufferedImage bufferedImage = imageUtil.toBufferedImage(gifStream);
    BufferedImage makeSmallerToMax = imageUtil.makeSmallerToMax(bufferedImage, 100, 90);
    assertEquals(100, makeSmallerToMax.getWidth());
    assertEquals((int) (10300.0 / 241), makeSmallerToMax.getHeight());
}

From source file:Main.java

public void createThumbnail(File file) throws Exception {
    BufferedImage img = ImageIO.read(file);
    BufferedImage thumb = new BufferedImage(100, 200, BufferedImage.TYPE_INT_RGB);

    Graphics2D g2d = (Graphics2D) thumb.getGraphics();
    g2d.drawImage(img, 0, 0, thumb.getWidth() - 1, thumb.getHeight() - 1, 0, 0, img.getWidth() - 1,
            img.getHeight() - 1, null);// w  w  w .java 2s . c  o  m
    g2d.dispose();
    ImageIO.write(thumb, "PNG", new File("thumb.png"));
}

From source file:fi.helsinki.opintoni.service.ImageService.java

private BufferedImage toJpg(BufferedImage bufferedImage) {
    BufferedImage jpgImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(),
            BufferedImage.TYPE_INT_RGB);
    jpgImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.BLACK, null);
    return jpgImage;
}

From source file:com.silverpeas.thumbnail.control.ThumbnailController.java

protected static void cropFromPath(String pathOriginalFile, ThumbnailDetail thumbDetailComplete,
        int thumbnailHeight, int thumbnailWidth) throws IOException, ThumbnailException {
    File originalFile = new File(pathOriginalFile);
    BufferedImage bufferOriginal = ImageIO.read(originalFile);
    if (bufferOriginal == null) {
        SilverTrace.error("thumbnail",
                "ThumbnailController.cropFromPath(int thumbnailWidth, "
                        + "int thumbnailHeight,ThumbnailDetail thumbDetailComplete)",
                "thumbnail.EX_MSG_NOT_AN_IMAGE", "pathOriginalFile=" + pathOriginalFile);
        throw new ThumbnailException("ThumbnailBmImpl.cropFromPath()", SilverpeasException.ERROR,
                "thumbnail.EX_MSG_NOT_AN_IMAGE");
    } else {/*w ww.  ja  v a  2  s.c  o  m*/
        thumbDetailComplete.setXStart(0);
        thumbDetailComplete.setYStart(0);
        thumbDetailComplete.setXLength(bufferOriginal.getWidth());
        thumbDetailComplete.setYLength(bufferOriginal.getHeight());

        String pathCropFile = getImageDirectory(thumbDetailComplete.getInstanceId())
                + thumbDetailComplete.getCropFileName();
        createCropThumbnailFileOnServer(pathOriginalFile,
                getImageDirectory(thumbDetailComplete.getInstanceId()), pathCropFile, thumbDetailComplete,
                thumbnailWidth, thumbnailHeight);
        getThumbnailService().updateThumbnail(thumbDetailComplete);
    }
}

From source file:exemplos.PegandoPixelsSemJAI.java

public void pegaPixel() throws IOException {

    File f = new File("D:\\ProjetosNetBeans\\PDI\\src\\imagens\\ebola.png"); //seleciona o arquivo
    BufferedImage image = ImageIO.read(f); //le o arquivo
    System.out.println("Dimenses: " + image.getWidth() + "x" + image.getHeight() + "pixels"); //exibe suas dimenses

    Raster raster = image.getRaster(); //pega os valores dos pixels
    double pixel[] = new double[3]; //cria um array com 3 posies
    int brancos = 0; //cria uma varivel para contar os pixels brancos
    for (int h = 0; h < image.getHeight(); h++)
        for (int w = 0; w < image.getWidth(); w++) {
            raster.getPixel(w, h, pixel);
            if ((pixel[0] == 255) && (pixel[1] == 255) && (pixel[2] == 255))//confirma se o RGB  igual a 255, ou seja, branco
                brancos++;//from   ww w  .  jav  a 2 s .  c om

        }

}

From source file:net.pkhsolutions.pecsapp.control.PictureTransformer.java

/**
 * TODO Document me//from w w w  . j a  va2s . c om
 *
 * @param image
 * @param maxHeightInPx
 * @param maxWidthInPx
 * @return
 */
@NotNull
public BufferedImage scaleIfNecessary(@NotNull BufferedImage image, int maxHeightInPx, int maxWidthInPx) {
    int w = image.getWidth();
    int h = image.getHeight();

    int newW;
    int newH;

    if (w <= maxWidthInPx && h <= maxHeightInPx) {
        return image;
    } else if (w > h) {
        newW = maxWidthInPx;
        newH = newW * h / w;
    } else {
        newH = maxHeightInPx;
        newW = newH * w / h;
    }
    LOGGER.info("Original size was w{} x h{}, new size is w{} x h{}", w, h, newW, newH);
    Image tmp = image.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
    BufferedImage img = new BufferedImage(newW, newH, image.getType());
    Graphics2D g2d = img.createGraphics();
    g2d.drawImage(tmp, 0, 0, null);
    g2d.dispose();
    return img;
}

From source file:csns.util.ImageUtils.java

public File resize(File file, int targetSize, boolean always) {
    File newFile = null;//w w w  .  j a v  a2s . c  o  m

    try {
        BufferedImage image = ImageIO.read(fileIO.getDiskFile(file));

        if (!always && image.getWidth() < targetSize && image.getHeight() < targetSize)
            return file;

        BufferedImage newImage = Scalr.resize(image, targetSize);
        java.io.File tempFile = java.io.File.createTempFile("temp", null);
        ImageIO.write(newImage, "jpg", tempFile);

        newFile = new File();
        newFile.setName(file.getName() + "_" + targetSize + ".jpg");
        newFile.setType("image/jpeg");
        newFile.setSize(tempFile.length());
        newFile.setOwner(file.getOwner());
        newFile.setParent(file.getParent());
        newFile.setPublic(file.isPublic());
        newFile = fileDao.saveFile(newFile);

        tempFile.renameTo(fileIO.getDiskFile(newFile, false));
    } catch (IOException e) {
        logger.error("Fail to resize image " + file.getName(), e);
    }

    return newFile;
}