Example usage for java.awt.image BufferedImage getHeight

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

Introduction

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

Prototype

public int getHeight() 

Source Link

Document

Returns the height of the BufferedImage .

Usage

From source file:dk.dma.msiproxy.common.repo.ThumbnailService.java

/**
 * Creates a thumbnail for the image file using plain old java
 * @param file the image file//w  w w.  j av a 2  s.c om
 * @param thumbFile the resulting thumbnail file
 * @param size the size of the thumbnail
 */
private void createThumbnailUsingJava(Path file, Path thumbFile, IconSize size) throws IOException {

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

        int w = image.getWidth();
        int h = image.getHeight();

        // Never scale up
        if (w <= size.getSize() && h <= size.getSize()) {
            FileUtils.copyFile(file.toFile(), thumbFile.toFile());

        } else {
            // Compute the scale factor
            double dx = (double) size.getSize() / (double) w;
            double dy = (double) size.getSize() / (double) h;
            double d = Math.min(dx, dy);

            // Create the thumbnail
            BufferedImage thumbImage = new BufferedImage((int) Math.round(w * d), (int) Math.round(h * d),
                    BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = thumbImage.createGraphics();
            AffineTransform at = AffineTransform.getScaleInstance(d, d);
            g2d.drawRenderedImage(image, at);
            g2d.dispose();

            // Save the thumbnail
            String fileName = thumbFile.getFileName().toString();
            ImageIO.write(thumbImage, FilenameUtils.getExtension(fileName), thumbFile.toFile());

            // Releas resources
            image.flush();
            thumbImage.flush();
        }

        // Update the timestamp of the thumbnail file to match the change date of the image file
        Files.setLastModifiedTime(thumbFile, Files.getLastModifiedTime(file));

    } catch (Exception e) {
        log.error("Error creating thumbnail for image " + file, e);
        throw new IOException(e);
    }
}

From source file:com.galenframework.utils.GalenUtils.java

/**
 * Check the devicePixelRatio and adapts the size of the screenshot as if the ratio was 1.0
 * @param driver// w ww  .  jav a 2  s  . co m
 * @param screenshotImage
 * @return
 */
public static BufferedImage resizeScreenshotIfNeeded(WebDriver driver, BufferedImage screenshotImage) {
    Double devicePixelRatio = ((Number) ((JavascriptExecutor) driver)
            .executeScript(JS_RETRIEVE_DEVICE_PIXEL_RATIO)).doubleValue();

    if (devicePixelRatio > 1.0 && screenshotImage.getWidth() > 0) {
        Long screenSize = (Long) ((JavascriptExecutor) driver).executeScript(
                "return Math.max(" + "document.body.scrollWidth, document.documentElement.scrollWidth,"
                        + "document.body.offsetWidth, document.documentElement.offsetWidth,"
                        + "document.body.clientWidth, document.documentElement.clientWidth);");

        Double estimatedPixelRatio = ((double) screenshotImage.getWidth()) / ((double) screenSize);

        if (estimatedPixelRatio > 1.0) {

            int newWidth = (int) (screenshotImage.getWidth() / estimatedPixelRatio);
            int newHeight = (int) (screenshotImage.getHeight() / estimatedPixelRatio);

            Image tmp = screenshotImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
            BufferedImage scaledImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);

            Graphics2D g2d = scaledImage.createGraphics();
            g2d.drawImage(tmp, 0, 0, null);
            g2d.dispose();

            return scaledImage;
        } else
            return screenshotImage;
    } else
        return screenshotImage;
}

From source file:main.MapKit.java

private BufferedImage convert(BufferedImage loadImg, Color newColor) {
    int w = loadImg.getWidth();
    int h = loadImg.getHeight();
    BufferedImage imgOut = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    BufferedImage imgColor = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g = imgColor.createGraphics();
    g.setColor(newColor);//from  ww w  . ja  va2 s.  c o m
    g.fillRect(0, 0, w + 1, h + 1);
    g.dispose();

    Graphics2D graphics = imgOut.createGraphics();
    graphics.drawImage(loadImg, 0, 0, null);
    graphics.setComposite(MultiplyComposite.Default);
    graphics.drawImage(imgColor, 0, 0, null);
    graphics.dispose();

    return imgOut;
}

From source file:net.sqs2.omr.result.export.chart.ChartImageWriter.java

private void setSectionPaint(DefaultCategoryDataset dataSet, CategoryPlot plot) {

    BarRenderer renderer = (BarRenderer) plot.getRenderer();
    renderer.setDrawBarOutline(true);/*from   www . j  a v  a  2  s. c om*/
    renderer.setItemLabelAnchorOffset(10);

    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

    if (this.itemBackgroundImages != null && 0 < this.itemBackgroundImages.length) {
        int index = 0;
        Paint outlinePaint = Color.BLACK;
        Stroke outlineStroke = new BasicStroke(1.0f);
        for (@SuppressWarnings("unused")
        Object key : dataSet.getColumnKeys()) {
            int imageIndex = index % this.itemBackgroundImages.length;
            BufferedImage texture = this.itemBackgroundImages[imageIndex];
            Rectangle2D anchor = new Rectangle2D.Double(0, 0, texture.getWidth(), texture.getHeight());
            TexturePaint fillPaint = new TexturePaint(texture, anchor);
            renderer.setSeriesFillPaint(index, fillPaint);
            renderer.setSeriesPaint(index, fillPaint);
            renderer.setSeriesOutlinePaint(index, outlinePaint);
            renderer.setSeriesOutlineStroke(index, outlineStroke);

            // renderer.setBasePaint(fillPaint);
            // renderer.setBaseOutlineStroke(outlineStroke);
            // renderer.setBaseOutlinePaint(outlinePaint);
        }

    }
}

From source file:cpcc.ros.services.RosImageConverterTest.java

@Test
public void shouldConvertRGB8Images() throws IOException {
    int height = 240;
    int width = 320;
    int step = 960;
    String encoding = "rgb8";
    String imageName = "data/test-image-rgb8.rgb8";
    String convertedImageName = "data/test-image-rgb8.png";

    InputStream stream = RosImageConverterTest.class.getResourceAsStream(imageName);
    byte[] imageData = IOUtils.toByteArray(stream);

    stream = RosImageConverterTest.class.getResourceAsStream(convertedImageName);
    byte[] convertedImageData = IOUtils.toByteArray(stream);

    when(buffer.array()).thenReturn(imageData);

    when(message.getEncoding()).thenReturn(encoding);
    when(message.getHeight()).thenReturn(height);
    when(message.getWidth()).thenReturn(width);
    when(message.getStep()).thenReturn(step);
    when(message.getData()).thenReturn(ChannelBuffers.copiedBuffer(ByteOrder.nativeOrder(), imageData));

    BufferedImage result = conv.messageToBufferedImage(message);
    assertThat(result).isNotNull();//from   w  w  w  . jav a  2  s. c  om
    assertThat(result.getHeight()).isEqualTo(height);
    assertThat(result.getWidth()).isEqualTo(width);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ImageIO.write(result, "PNG", bos);

    assertThat(bos.toByteArray()).isEqualTo(convertedImageData);
}

From source file:cpcc.ros.services.RosImageConverterTest.java

@Test
public void shouldConvertRGBA8Images() throws IOException {
    int height = 240;
    int width = 320;
    int step = 1280;
    String encoding = "rgba8";
    String imageName = "data/test-image-rgba8.rgba8";
    String convertedImageName = "data/test-image-rgba8.png";

    InputStream stream = RosImageConverterTest.class.getResourceAsStream(imageName);
    byte[] imageData = IOUtils.toByteArray(stream);

    stream = RosImageConverterTest.class.getResourceAsStream(convertedImageName);
    byte[] convertedImageData = IOUtils.toByteArray(stream);

    when(buffer.array()).thenReturn(imageData);

    when(message.getEncoding()).thenReturn(encoding);
    when(message.getHeight()).thenReturn(height);
    when(message.getWidth()).thenReturn(width);
    when(message.getStep()).thenReturn(step);
    when(message.getData()).thenReturn(ChannelBuffers.copiedBuffer(ByteOrder.nativeOrder(), imageData));

    BufferedImage result = conv.messageToBufferedImage(message);
    assertThat(result).isNotNull();//from  w ww.  j a v  a  2s. co m
    assertThat(result.getHeight()).isEqualTo(height);
    assertThat(result.getWidth()).isEqualTo(width);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ImageIO.write(result, "PNG", bos);

    assertThat(bos.toByteArray()).isEqualTo(convertedImageData);
}

From source file:com.crimelab.service.GalleryServiceImpl.java

@Override
public XWPFDocument create(GalleryResults galleryResults, HttpSession session) {
    XWPFDocument document = null;/*from   w  w w.ja va  2s .c  o m*/

    //Insert into database
    galleryDAO.insertResults(galleryResults);

    try {
        //Retrieving Template
        InputStream inpDocx = session.getServletContext()
                .getResourceAsStream("/WEB-INF/templates/CompositeSketch.docx");
        document = new XWPFDocument(inpDocx);

        //Adding the picture
        XWPFParagraph pictureHolder = document.createParagraph();
        XWPFRun pictureRun = pictureHolder.createRun();

        FileInputStream getPhoto = new FileInputStream(galleryResults.getPhotoLocation());
        FileInputStream getImage = new FileInputStream(galleryResults.getPhotoLocation());
        ImageInputStream imageInput = ImageIO.createImageInputStream(getPhoto);
        BufferedImage bi = ImageIO.read(imageInput);
        int width = bi.getWidth() - 100;
        int height = bi.getHeight() - 100;

        pictureRun.addBreak();
        pictureRun.addPicture(getImage, XWPFDocument.PICTURE_TYPE_PNG, null, Units.toEMU(width),
                Units.toEMU(height));

        pictureHolder.setBorderBottom(Borders.BASIC_BLACK_DASHES);
        pictureHolder.setBorderTop(Borders.BASIC_BLACK_DASHES);
        pictureHolder.setBorderLeft(Borders.BASIC_BLACK_DASHES);
        pictureHolder.setBorderRight(Borders.BASIC_BLACK_DASHES);
        pictureHolder.setAlignment(ParagraphAlignment.CENTER);
        //            pictureRowHolder.getCell(0).setText("IMAGE PLACER");

        //Case number and Date            
        XWPFParagraph info = document.createParagraph();
        XWPFRun caseNoAndDate = info.createRun();
        caseNoAndDate.setText("Case Number: " + galleryResults.getCaseNo());
        caseNoAndDate.addTab();
        caseNoAndDate.addTab();
        caseNoAndDate.addTab();
        caseNoAndDate.addTab();
        caseNoAndDate.setText(galleryResults.getDate());
        caseNoAndDate.setFontSize(16);

        //Sketch Details
        XWPFParagraph caseDetails = document.createParagraph();
        XWPFRun detailsParagraph = caseDetails.createRun();
        detailsParagraph.setText("Offense/Incident: " + galleryResults.getOffenseIncident());
        detailsParagraph.addBreak();
        detailsParagraph.setText("Name/AKA: " + galleryResults.getNameAKA());
        detailsParagraph.addBreak();
        detailsParagraph.setText("Sex: " + galleryResults.getSex());
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.setText("Age: " + galleryResults.getAge());
        detailsParagraph.addBreak();
        detailsParagraph.setText("Height: " + galleryResults.getHeight());
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.setText("Weight: " + galleryResults.getWeight());
        detailsParagraph.addBreak();
        detailsParagraph.setText("Built: " + galleryResults.getBuilt());
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.setText("Complexion: " + galleryResults.getComplexion());
        detailsParagraph.addBreak();
        detailsParagraph.setText("Other Information: " + galleryResults.getOtherInfo());
        detailsParagraph.addBreak();
        detailsParagraph.setText("Described by: " + galleryResults.getDescribedBy());
        detailsParagraph.addBreak();
        detailsParagraph.setText("Requesting party: " + galleryResults.getRequestingParty());

        //Details Borders
        caseDetails.setBorderBottom(Borders.BASIC_BLACK_DASHES);
        caseDetails.setBorderTop(Borders.BASIC_BLACK_DASHES);
        caseDetails.setBorderLeft(Borders.BASIC_BLACK_DASHES);
        caseDetails.setBorderRight(Borders.BASIC_BLACK_DASHES);
        caseDetails.setAlignment(ParagraphAlignment.LEFT);

        //Reference Paragraph
        XWPFParagraph outsideDetails = document.createParagraph();
        XWPFRun outsideDetailsRun = outsideDetails.createRun();
        outsideDetailsRun.addBreak();
        outsideDetailsRun.setText("Note: For reference");
        outsideDetailsRun.addBreak();
        outsideDetailsRun.setText("The witness indicates that this image is: " + galleryResults.getRating());
        getPhoto.close();
        getImage.close();
        imageInput.close();
        document.getXWPFDocument();
    } catch (IOException | InvalidFormatException e) {
        e.printStackTrace();
    }
    return document;
}

From source file:com.github.cherimojava.orchidae.controller.PictureController.java

/**
 * uploads multiple files into the system for the current user
 * /*from  www.j  av  a2  s.  c  o  m*/
 * @param request
 *            request with pictures to store
 * @return {@link org.springframework.http.HttpStatus#CREATED} if the upload was successful or
 *         {@link org.springframework.http.HttpStatus#OK} if some of the pictures couldn't be uploaded together with
 *         information which pictures couldn't be uploaded
 * @since 1.0.0
 */
@RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> handleFileUpload(MultipartHttpServletRequest request) {
    List<String> badFiles = Lists.newArrayList();
    User user = userUtil
            .getUser((String) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
    for (Iterator<String> it = request.getFileNames(); it.hasNext();) {
        MultipartFile file = request.getFile(it.next());
        // Create uuid and Picture entity
        Picture picture = factory.create(Picture.class);
        picture.setUser(user);
        picture.setTitle(StringUtils.split(file.getOriginalFilename(), ".")[0]);
        picture.setId(generateId());
        picture.setOriginalName(file.getOriginalFilename());
        picture.setUploadDate(DateTime.now());

        String type = StringUtils.substringAfterLast(file.getOriginalFilename(), ".");

        try {
            File storedPicture = fileUtil.getFileHandle(picture.getId());
            // save picture
            file.transferTo(storedPicture);

            // read some some properties from it
            BufferedImage image = ImageIO.read(storedPicture);
            picture.setHeight(image.getHeight());
            picture.setWidth(image.getWidth());
            picture.setAccess(Access.PRIVATE);// TODO for now only private access
            createSmall(picture.getId(), image, type);
            LOG.info("Uploaded {} and assigned id {}", file.getOriginalFilename(), picture.getId());
            checkBatch(picture, request);
            picture.save();
        } catch (Exception e) {
            LOG.warn("failed to store picture", e);
            badFiles.add(file.getOriginalFilename());
        }
    }
    if (badFiles.isEmpty()) {
        return new ResponseEntity<>("You successfully uploaded!", HttpStatus.CREATED);
    } else {
        return new ResponseEntity<>(
                "Could not upload all files. Failed to upload: " + Joiner.on(",").join(badFiles),
                HttpStatus.OK);
    }
}

From source file:org.iish.visualmets.services.ImageTransformation.java

/**
 * Returns a 90 degrees rotated image//w w w .  j  a v a 2  s  .  com
 *
 * @param bi image
 * @return a rotated image
 */
private BufferedImage RotateImage90Degrees(BufferedImage bi) {
    int width = bi.getWidth();
    int height = bi.getHeight();

    BufferedImage biFlip = new BufferedImage(height, width, bi.getType());

    for (int i = 0; i < width; i++)
        for (int j = 0; j < height; j++) {
            biFlip.setRGB(height - 1 - j, i, bi.getRGB(i, j));
        }

    return biFlip;
}

From source file:org.iish.visualmets.services.ImageTransformation.java

/**
 * Returns a 180 degrees rotated image// w  w  w .j ava2s .co  m
 *
 * @param bi image
 * @return a rotated image
 */
private BufferedImage RotateImage180Degrees(BufferedImage bi) {
    int width = bi.getWidth();
    int height = bi.getHeight();

    BufferedImage biFlip = new BufferedImage(width, height, bi.getType());

    for (int i = 0; i < width; i++)
        for (int j = 0; j < height; j++) {
            biFlip.setRGB(width - 1 - i, height - 1 - j, bi.getRGB(i, j));
        }

    return biFlip;
}