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:de.romankreisel.faktotum.beans.BundesbruderBean.java

/**
 * @param originalPicture// w w w.j a  va 2s. c o m
 * @param pictureHeight
 * @param pictureWidth
 * @return
 * @throws IOException
 */
public Byte[] resizeProfilePicture(Byte[] originalPicture, int width, int height) throws IOException {
    BufferedImage originalImage = this.getImageFromByteArray(originalPicture);
    Dimension newDimension = this.getScaledDimension(
            new Dimension(originalImage.getWidth(), originalImage.getHeight()), new Dimension(width, height));
    Image scaledImage = originalImage.getScaledInstance(newDimension.width, newDimension.height,
            Image.SCALE_SMOOTH);
    return this.storeImageToByteArray(scaledImage);
}

From source file:baocaoxla.xuly_compare.java

public ChartPanel displayhistogram(BufferedImage image) {

    HistogramDataset dataset = new HistogramDataset();

    final int w = image.getWidth();
    final int h = image.getHeight();
    double[] r = new double[w * h];
    double[] g = new double[w * h];
    double[] b = new double[w * h];
    int dem = 0;//from   w w  w .ja v  a 2s. c  om
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            Color c = new Color(image.getRGB(j, i));
            r[dem] = c.getRed();
            g[dem] = c.getGreen();
            b[dem] = c.getBlue();
            dem++;
        }
    }
    //r = raster.getSamples(0, 0, w, h, 0, r);
    dataset.addSeries("Red", r, 256);
    //r = raster.getSamples(0, 0, w, h, 1, r);
    dataset.addSeries("Green", g, 256);
    // r = raster.getSamples(0, 0, w, h, 2, r);
    dataset.addSeries("Blue", b, 256);
    // chart
    JFreeChart chart = ChartFactory.createHistogram("Histogram", "Value", "Count", dataset,
            PlotOrientation.VERTICAL, true, true, false);
    ChartPanel panel = new ChartPanel(chart);
    return panel;

}

From source file:blusunrize.immersiveengineering.client.render.TileRenderAutoWorkbench.java

public static BlueprintLines getBlueprintDrawable(ItemStack stack, World world) {
    if (stack.isEmpty())
        return null;
    EntityPlayer player = ClientUtils.mc().player;
    ArrayList<BufferedImage> images = new ArrayList<>();
    try {/*from   w  w w.  j a v a2 s .  com*/
        IBakedModel ibakedmodel = ClientUtils.mc().getRenderItem().getItemModelWithOverrides(stack, world,
                player);
        HashSet<String> textures = new HashSet();
        Collection<BakedQuad> quads = ibakedmodel.getQuads(null, null, 0);
        for (BakedQuad quad : quads)
            if (quad != null && quad.getSprite() != null)
                textures.add(quad.getSprite().getIconName());
        for (String s : textures) {
            ResourceLocation rl = new ResourceLocation(s);
            rl = new ResourceLocation(rl.getNamespace(),
                    String.format("%s/%s%s", "textures", rl.getPath(), ".png"));
            IResource resource = ClientUtils.mc().getResourceManager().getResource(rl);
            BufferedImage bufferedImage = TextureUtil.readBufferedImage(resource.getInputStream());
            if (bufferedImage != null)
                images.add(bufferedImage);
        }
    } catch (Exception e) {
    }
    if (images.isEmpty())
        return null;
    ArrayList<Pair<TexturePoint, TexturePoint>> lines = new ArrayList();
    HashSet testSet = new HashSet();
    HashMultimap<Integer, TexturePoint> area = HashMultimap.create();
    int wMax = 0;
    for (BufferedImage bufferedImage : images) {
        Set<Pair<TexturePoint, TexturePoint>> temp_lines = new HashSet<>();

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

        if (h > w)
            h = w;
        if (w > wMax)
            wMax = w;
        for (int hh = 0; hh < h; hh++)
            for (int ww = 0; ww < w; ww++) {
                int argb = bufferedImage.getRGB(ww, hh);
                float r = (argb >> 16 & 255) / 255f;
                float g = (argb >> 8 & 255) / 255f;
                float b = (argb & 255) / 255f;
                float intesity = (r + b + g) / 3f;
                int alpha = (argb >> 24) & 255;
                if (alpha > 0) {
                    boolean added = false;
                    //Check colour sets for similar colour to shade it later
                    TexturePoint tp = new TexturePoint(ww, hh, w);
                    if (!testSet.contains(tp)) {
                        for (Integer key : area.keySet()) {
                            for (TexturePoint p : area.get(key)) {
                                float mod = w / (float) p.scale;
                                int pColour = bufferedImage.getRGB((int) (p.x * mod), (int) (p.y * mod));
                                float dR = (r - (pColour >> 16 & 255) / 255f);
                                float dG = (g - (pColour >> 8 & 255) / 255f);
                                float dB = (b - (pColour & 255) / 255f);
                                double delta = Math.sqrt(dR * dR + dG * dG + dB * dB);
                                if (delta < .25) {
                                    area.put(key, tp);
                                    added = true;
                                    break;
                                }
                            }
                            if (added)
                                break;
                        }
                        if (!added)
                            area.put(argb, tp);
                        testSet.add(tp);
                    }
                    //Compare to direct neighbour
                    for (int i = 0; i < 4; i++) {
                        int xx = (i == 0 ? -1 : i == 1 ? 1 : 0);
                        int yy = (i == 2 ? -1 : i == 3 ? 1 : 0);
                        int u = ww + xx;
                        int v = hh + yy;

                        int neighbour = 0;
                        float delta = 1;
                        boolean notTransparent = false;
                        if (u >= 0 && u < w && v >= 0 && v < h) {
                            neighbour = bufferedImage.getRGB(u, v);
                            notTransparent = ((neighbour >> 24) & 255) > 0;
                            if (notTransparent) {
                                float neighbourIntesity = ((neighbour >> 16 & 255) + (neighbour >> 8 & 255)
                                        + (neighbour & 255)) / 765f;
                                float intesityDelta = Math.max(0,
                                        Math.min(1, Math.abs(intesity - neighbourIntesity)));
                                float rDelta = Math.max(0,
                                        Math.min(1, Math.abs(r - (neighbour >> 16 & 255) / 255f)));
                                float gDelta = Math.max(0,
                                        Math.min(1, Math.abs(g - (neighbour >> 8 & 255) / 255f)));
                                float bDelta = Math.max(0, Math.min(1, Math.abs(b - (neighbour & 255) / 255f)));
                                delta = Math.max(intesityDelta, Math.max(rDelta, Math.max(gDelta, bDelta)));
                                delta = delta < .25 ? 0 : delta > .4 ? 1 : delta;
                            }
                        }
                        if (delta > 0) {
                            Pair<TexturePoint, TexturePoint> l = Pair.of(
                                    new TexturePoint(ww + (i == 0 ? 0 : i == 1 ? 1 : 0),
                                            hh + (i == 2 ? 0 : i == 3 ? 1 : 0), w),
                                    new TexturePoint(ww + (i == 0 ? 0 : i == 1 ? 1 : 1),
                                            hh + (i == 2 ? 0 : i == 3 ? 1 : 1), w));
                            temp_lines.add(l);
                        }
                    }
                }
            }
        lines.addAll(temp_lines);
    }

    ArrayList<Integer> lumiSort = new ArrayList<>(area.keySet());
    Collections.sort(lumiSort, (rgb1, rgb2) -> Double.compare(getLuminance(rgb1), getLuminance(rgb2)));
    HashMultimap<ShadeStyle, Point> complete_areaMap = HashMultimap.create();
    int lineNumber = 2;
    int lineStyle = 0;
    for (Integer i : lumiSort) {
        complete_areaMap.putAll(new ShadeStyle(lineNumber, lineStyle), area.get(i));
        ++lineStyle;
        lineStyle %= 3;
        if (lineStyle == 0)
            lineNumber += 1;
    }

    Set<Pair<Point, Point>> complete_lines = new HashSet<>();
    for (Pair<TexturePoint, TexturePoint> line : lines) {
        TexturePoint p1 = line.getKey();
        TexturePoint p2 = line.getValue();
        complete_lines.add(Pair.of(
                new Point((int) (p1.x / (float) p1.scale * wMax), (int) (p1.y / (float) p1.scale * wMax)),
                new Point((int) (p2.x / (float) p2.scale * wMax), (int) (p2.y / (float) p2.scale * wMax))));
    }
    return new BlueprintLines(wMax, complete_lines, complete_areaMap);
}

From source file:DBScan.java

private double eucDistance(BufferedImage img1, BufferedImage img2) {
    double distance = 0;
    for (int x = 0; x < Math.max(img1.getWidth(), img2.getWidth()); x++)
        for (int y = 0; y < Math.max(img1.getHeight(), img2.getHeight()); y++) {
            distance += minDistance(x, y, img1, img2);
            distance += minDistance(x, y, img2, img1);

            if (distance > epsSqr + 1)
                break;
        }/*www . jav a2  s.c o m*/
    distance = Math.sqrt(distance);

    return distance;
}

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

private void setSectionPaint(PieDataset dataSet, PiePlot piePlot) {
    Paint outlinePaint = Color.BLACK;
    Stroke outlineStroke = new BasicStroke(1.0f);

    if (this.itemBackgroundImages != null && 0 < this.itemBackgroundImages.length) {
        int index = 0;
        for (Object key : dataSet.getKeys()) {
            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);
            piePlot.setSectionPaint((Comparable<?>) key, fillPaint);
            piePlot.setSectionOutlinePaint((Comparable<?>) key, outlinePaint);
            piePlot.setSectionOutlineStroke((Comparable<?>) key, outlineStroke);
            index++;// www. j a  v a2s  .c om
        }
    }
    piePlot.setOutlineVisible(true);
    piePlot.setOutlinePaint(outlinePaint);
    piePlot.setOutlineStroke(outlineStroke);
    piePlot.setSectionOutlinesVisible(true);
    piePlot.setBaseSectionOutlinePaint(outlinePaint);
    piePlot.setBaseSectionOutlineStroke(outlineStroke);
}

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

@Test
public void shouldReturnAnEmptyImageForACorruptedPNG() throws IOException {
    int height = 90;
    int width = 120;
    String encoding = "PNG";
    String imageName = "data/test-image.png";

    InputStream stream = RosImageConverterTest.class.getResourceAsStream(imageName);
    byte[] saneImageData = IOUtils.toByteArray(stream);
    byte[] imageData = Arrays.copyOfRange(saneImageData, 0, saneImageData.length - 100);

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

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

    BufferedImage result = conv.messageToBufferedImage(message);
    assertThat(result).isNotNull();/* www.  j  a  v  a2 s .  co  m*/

    assertThat(result.getHeight()).isEqualTo(height);
    assertThat(result.getWidth()).isEqualTo(width);

    assertThatImageIsEmpty(result);
}

From source file:edu.emory.library.tast.images.ThumbnailServlet.java

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    // location of images
    String baseUrl = AppConfig.getConfiguration().getString(AppConfig.IMAGES_URL);
    baseUrl = StringUtils.trimEnd(baseUrl, '/');

    // image name and size
    String imageFileName = request.getParameter("i");
    int thumbnailWidth = Integer.parseInt(request.getParameter("w"));
    int thumbnailHeight = Integer.parseInt(request.getParameter("h"));

    // image dir/*  w w w. java  2s .co m*/
    String imagesDir = AppConfig.getConfiguration().getString(AppConfig.IMAGES_DIRECTORY);

    // create the thumbnail name
    String thumbnailFileName = FilenameUtils.getBaseName(imageFileName) + "-" + thumbnailWidth + "x"
            + thumbnailHeight + ".png";

    // does it exist?
    File thumbnailFile = new File(imagesDir, thumbnailFileName);
    if (thumbnailFile.exists()) {
        response.sendRedirect(baseUrl + "/" + thumbnailFileName);
        return;
    }

    // read the image
    File imageFile = new File(imagesDir, imageFileName);
    BufferedImage image = ImageIO.read(imageFile);
    int imageWidth = image.getWidth();
    int imageHeight = image.getHeight();
    BufferedImage imageCopy = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
    imageCopy.getGraphics().drawImage(image, 0, 0, imageWidth, imageHeight, 0, 0, imageWidth, imageHeight,
            null);

    // height is calculated automatically
    if (thumbnailHeight == 0)
        thumbnailHeight = (int) ((double) thumbnailWidth / (double) imageWidth * (double) imageHeight);

    // width is calculated automatically
    if (thumbnailWidth == 0)
        thumbnailWidth = (int) ((double) thumbnailHeight / (double) imageHeight * (double) imageWidth);

    // create an empty thumbnail
    BufferedImage thumbnail = new BufferedImage(thumbnailWidth, thumbnailHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D gr = thumbnail.createGraphics();
    gr.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    gr.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

    // determine the piece of the image which we want to clip
    int clipX1, clipX2, clipY1, clipY2;
    if (imageWidth * thumbnailHeight > thumbnailWidth * imageHeight) {

        int clipWidth = (int) Math
                .round(((double) thumbnailWidth / (double) thumbnailHeight) * (double) imageHeight);
        int imgCenterX = imageWidth / 2;
        clipX1 = imgCenterX - clipWidth / 2;
        clipX2 = imgCenterX + clipWidth / 2;
        clipY1 = 0;
        clipY2 = imageHeight;
    } else {
        int clipHeight = (int) Math
                .round(((double) thumbnailHeight / (double) thumbnailWidth) * (double) imageWidth);
        int imgCenterY = imageHeight / 2;
        clipX1 = 0;
        clipX2 = imageWidth;
        clipY1 = imgCenterY - clipHeight / 2;
        clipY2 = imgCenterY + clipHeight / 2;
    }

    // we filter the image first to get better results when shrinking
    if (2 * thumbnailWidth < clipX2 - clipX1 || 2 * thumbnailHeight < clipY2 - clipY1) {

        int kernelDimX = (clipX2 - clipX1) / (thumbnailWidth);
        int kernelDimY = (clipY2 - clipY1) / (thumbnailHeight);

        if (kernelDimX % 2 == 0)
            kernelDimX++;
        if (kernelDimY % 2 == 0)
            kernelDimY++;

        if (kernelDimX < kernelDimY)
            kernelDimX = kernelDimY;
        if (kernelDimY < kernelDimX)
            kernelDimY = kernelDimX;

        float[] blurKernel = new float[kernelDimX * kernelDimY];
        for (int i = 0; i < kernelDimX; i++)
            for (int j = 0; j < kernelDimY; j++)
                blurKernel[i * kernelDimX + j] = 1.0f / (float) (kernelDimX * kernelDimY);

        BufferedImageOp op = new ConvolveOp(new Kernel(kernelDimX, kernelDimY, blurKernel));
        imageCopy = op.filter(imageCopy, null);

    }

    // draw the thumbnail
    gr.drawImage(imageCopy, 0, 0, thumbnailWidth, thumbnailHeight, clipX1, clipY1, clipX2, clipY2, null);

    // and we are done
    gr.dispose();
    ImageIO.write(thumbnail, "png", thumbnailFile);

    // redirect to it
    response.sendRedirect(baseUrl + "/" + thumbnailFileName);

}

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

@Test
public void getPicture() throws Exception {
    // verify no picture there
    mvc.perform(get(url("acd3131"))).andExpect(status().isNotFound());
    // upload one picture
    createPicture("test", "jpeg");
    // retrieve its id
    MvcResult result = getLatest(10).andReturn();
    List<Picture> pictures = factory.readList(Picture.class, result.getResponse().getContentAsString());
    assertEquals(pictures.size(), 1);/*from  w  w w .  java2  s. co  m*/
    ByteArrayInputStream bais = new ByteArrayInputStream(
            mvc.perform(get(url(pictures.get(0).getId())).param("f", "o")).andExpect(status().isOk())
                    .andReturn().getResponse().getContentAsByteArray());
    BufferedImage image = ImageIO.read(bais);
    assertEquals(42, image.getWidth());
    assertEquals(42, image.getHeight());
}

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

@Test(dataProvider = "imageDataprovider")
public void shouldConvertGenericImages(int height, int width, String encoding, String imageName)
        throws IOException {
    InputStream stream = RosImageConverterTest.class.getResourceAsStream(imageName);
    byte[] imageData = IOUtils.toByteArray(stream);

    when(buffer.array()).thenReturn(imageData);
    //        when(buffer.hasArray()).thenReturn(true);

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

    BufferedImage result = conv.messageToBufferedImage(message);
    assertThat(result).isNotNull();/*from   w w  w  . j a  va  2s  . com*/

    assertThat(result.getHeight()).isEqualTo(height);
    assertThat(result.getWidth()).isEqualTo(width);

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

    byte[] resultImageData = bos.toByteArray();

    assertThat(resultImageData.length).isEqualTo(imageData.length);
    // assertThat(resultImageData).isEqualTo(imageData);
}

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

@Test
public void getSmall() throws Exception {
    controller.maxHeight = 10;/*w  w w .  j av  a2 s  .com*/
    // verify no picture there
    mvc.perform(get(url("acd3131"))).andExpect(status().isNotFound());
    // upload one picture
    createPicture("test", "jpeg");
    // retrieve its id
    MvcResult result = getLatest(10).andReturn();
    List<Picture> pictures = factory.readList(Picture.class, result.getResponse().getContentAsString());
    assertEquals(pictures.size(), 1);
    ByteArrayInputStream bais = new ByteArrayInputStream(
            mvc.perform(get(url(pictures.get(0).getId())).param("f", "s")).andExpect(status().isOk())
                    .andReturn().getResponse().getContentAsByteArray());
    BufferedImage image = ImageIO.read(bais);
    assertEquals(10, image.getWidth());
    assertEquals(10, image.getHeight());
}