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:edu.umass.cs.iesl.pdf2meta.cli.extract.pdfbox.pagedrawer.BeginInlineImage.java

/**
 * process : BI : begin inline image.//from  w  w w .j a  v  a 2 s  .c o  m
 * @param operator The operator that is being executed.
 * @param arguments List
 * @throws java.io.IOException If there is an error displaying the inline image.
 */
public void process(PDFOperator operator, List<COSBase> arguments) throws IOException {
    GraphicsAwarePDFStreamEngine drawer = (GraphicsAwarePDFStreamEngine) context;

    PDPage page = drawer.getPage();
    //begin inline image object
    ImageParameters params = operator.getImageParameters();
    PDInlinedImage image = new PDInlinedImage();
    image.setImageParameters(params);
    image.setImageData(operator.getImageData());
    BufferedImage awtImage = image.createImage(context.getColorSpaces());

    if (awtImage == null) {
        log.warn("BeginInlineImage.process(): createImage returned NULL");
        return;
    }
    int imageWidth = awtImage.getWidth();
    int imageHeight = awtImage.getHeight();
    double pageHeight = drawer.getPageSize().getHeight();

    Matrix ctm = drawer.getGraphicsState().getCurrentTransformationMatrix();
    int pageRotation = page.findRotation();

    AffineTransform ctmAT = ctm.createAffineTransform();
    ctmAT.scale(1f / imageWidth, 1f / imageHeight);
    Matrix rotationMatrix = new Matrix();
    rotationMatrix.setFromAffineTransform(ctmAT);
    // calculate the inverse rotation angle
    // scaleX = m00 = cos
    // shearX = m01 = -sin
    // tan = sin/cos
    double angle = Math.atan(ctmAT.getShearX() / ctmAT.getScaleX());
    Matrix translationMatrix = null;
    if (pageRotation == 0 || pageRotation == 180) {
        translationMatrix = Matrix.getTranslatingInstance((float) (Math.sin(angle) * ctm.getXScale()),
                (float) (pageHeight - 2 * ctm.getYPosition() - Math.cos(angle) * ctm.getYScale()));
    } else if (pageRotation == 90 || pageRotation == 270) {
        translationMatrix = Matrix.getTranslatingInstance((float) (Math.sin(angle) * ctm.getYScale()),
                (float) (pageHeight - 2 * ctm.getYPosition()));
    }
    rotationMatrix = rotationMatrix.multiply(translationMatrix);
    rotationMatrix.setValue(0, 1, (-1) * rotationMatrix.getValue(0, 1));
    rotationMatrix.setValue(1, 0, (-1) * rotationMatrix.getValue(1, 0));
    AffineTransform at = new AffineTransform(rotationMatrix.getValue(0, 0), rotationMatrix.getValue(0, 1),
            rotationMatrix.getValue(1, 0), rotationMatrix.getValue(1, 1), rotationMatrix.getValue(2, 0),
            rotationMatrix.getValue(2, 1));
    drawer.drawImage(awtImage, at);
}

From source file:com.github.cmisbox.ui.BaseFrame.java

public Image getImage(String resource, Integer height, Integer width) {
    try {/*w  w  w  . ja va  2  s  . c  o  m*/

        BufferedImage image = ImageIO.read(this.getClass().getResource(resource));

        if ((height != null) && (width != null)
                && ((image.getHeight() != height) || (image.getWidth() != width))) {
            return image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        }

        return image;
    } catch (IOException e1) {
        this.log.error(e1);
    }
    return null;
}

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   w w w . j  a  va  2s.com*/
    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:com.alibaba.simpleimage.CompositeImageProcessorTest.java

protected void check(File src, File dest) throws Exception {
    FileInputStream in = null;/*from   w  w w  . j a  va  2 s. c  o m*/
    FileInputStream in2 = null;
    try {
        in = new FileInputStream(dest);
        in2 = new FileInputStream(src);
        ImageRender rr = new ReadRender(in, false);
        ImageRender rr2 = new ReadRender(in2, false);
        ImageWrapper srcImg = rr2.render();
        ImageWrapper dstImg = rr.render();
        BufferedImage dstBi = dstImg.getAsBufferedImage();

        assertTrue(dstBi.getColorModel().getColorSpace().isCS_sRGB());
        assertTrue(dstBi.getWidth() <= 1024);
        assertTrue(dstBi.getHeight() <= 1024);
        if (srcImg.getImageFormat() != ImageFormat.GIF) {
            if (srcImg.getQuality() >= 50) {
                assertTrue(dstImg.getQuality() == srcImg.getQuality());
            } else {
                assertTrue(dstImg.getQuality() == 50);
            }

            assertTrue(srcImg.getHorizontalSamplingFactor(0) == dstImg.getHorizontalSamplingFactor(0));
            assertTrue(srcImg.getVerticalSamplingFactor(0) == dstImg.getVerticalSamplingFactor(0));
            assertTrue(srcImg.getHorizontalSamplingFactor(1) == dstImg.getHorizontalSamplingFactor(1));
            assertTrue(srcImg.getVerticalSamplingFactor(1) == dstImg.getVerticalSamplingFactor(1));
            assertTrue(srcImg.getHorizontalSamplingFactor(2) == dstImg.getHorizontalSamplingFactor(2));
            assertTrue(srcImg.getVerticalSamplingFactor(2) == dstImg.getVerticalSamplingFactor(2));
        }
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(in2);
    }
}

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

/**
 * creates the Thumbnail for the given picture and stores it on the disk
 * /* www  .  j  a  va2  s  .c o  m*/
 * @param id
 * @param image
 * @param type
 */
private void createSmall(String id, BufferedImage image, String type) {
    int height = image.getHeight();
    int width = image.getWidth();
    double scale = maxHeight / (double) height;
    BufferedImage thumbnail = Scalr.resize(image, Scalr.Method.ULTRA_QUALITY,
            ((Double) (width * scale)).intValue(), ((Double) (height * scale)).intValue(), Scalr.OP_ANTIALIAS);
    try {
        ImageIO.write(thumbnail, type, fileUtil.getFileHandle(id + "_s"));
    } catch (IOException e) {
        LOG.error("failed to create thumbnail for picture", e);
    }
}

From source file:jp.canetrash.maven.plugin.bijint.BujintMojo.java

/**
 * ??????????/*  w ww.java 2s.  c  o m*/
 * 
 * @param url
 * @return
 * @throws Exception
 */
BufferedImage getFittingImage(String url) throws Exception {
    // ??
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = buildDefaultHttpMessage(new HttpGet(url));
    httpget.setHeader("Referer", "http://www.bijint.com/jp/");
    HttpResponse response = httpclient.execute(httpget);

    BufferedImage image = ImageIO.read(response.getEntity().getContent());
    httpclient.getConnectionManager().shutdown();

    int width = image.getWidth() / 10 * 4;
    int height = image.getHeight() / 10 * 4;
    BufferedImage resizedImage = null;
    resizedImage = new BufferedImage(width, height, image.getType());
    resizedImage.getGraphics().drawImage(image.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING), 0,
            0, width, height, null);
    return resizedImage;
}

From source file:com.mycompany.controllers.ClubController.java

private byte[] LogoConvertion(byte[] bytes) {
    int width = 200;
    int height = 200;
    ByteArrayInputStream in = new ByteArrayInputStream(bytes);
    try {//from   ww  w .  java2 s  .com
        BufferedImage img = ImageIO.read(in);
        if (height == 0) {
            height = (width * img.getHeight()) / img.getWidth();
        }
        if (width == 0) {
            width = (height * img.getWidth()) / img.getHeight();
        }
        Image scaledImage = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        BufferedImage imageBuff = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        imageBuff.getGraphics().drawImage(scaledImage, 0, 0, new Color(0, 0, 0), null);

        ByteArrayOutputStream buffer = new ByteArrayOutputStream();

        ImageIO.write(imageBuff, "jpg", buffer);

        bytes = buffer.toByteArray();
    } catch (IOException e) {
        log.error("File convertion error");
    }
    return bytes;
}

From source file:de.romankreisel.faktotum.beans.BundesbruderBean.java

public void rotateProfilePictureClockwise(BundesbruderEntity bundesbruder, double angle) throws IOException {
    BufferedImage image = this.getImageFromByteArray(bundesbruder.getPictureOriginal());
    double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
    int w = image.getWidth(), h = image.getHeight();
    int neww = (int) Math.floor(w * cos + h * sin), newh = (int) Math.floor(h * cos + w * sin);
    GraphicsConfiguration gc = image.createGraphics().getDeviceConfiguration();
    BufferedImage result = gc.createCompatibleImage(neww, newh, Transparency.TRANSLUCENT);
    Graphics2D g = result.createGraphics();
    g.translate((neww - w) / 2, (newh - h) / 2);
    g.rotate(angle, w / 2, h / 2);//  ww w.  j  av  a 2 s .  co m
    g.drawRenderedImage(image, null);
    g.dispose();
    this.setProfilePicture(bundesbruder, this.storeImageToByteArray(image));
}

From source file:cn.z.Ocr5.java

public static String getSingleCharOcr(BufferedImage img, Map<BufferedImage, String> map) throws Exception {
    if (useSvm) {
        final String input = new File("img/" + clazz + "/input.txt").getAbsolutePath();
        final String output = new File("result/" + clazz + "/output.txt").getAbsolutePath();
        CommonUtil.imgToSvmInput(img, input, whiteThreshold);
        svm_predict.main(new String[] { input, new File("train/" + clazz + "/data.txt.model").getAbsolutePath(),
                output });//from  w ww  .ja v a  2 s  .c  om
        final List<String> predict = IOUtils.readLines(new FileInputStream(output));
        if (predict.size() > 0 && predict.get(0).length() > 0) {
            return predict.get(0).substring(0, 1);
        }
        return "#";
    }

    String result = "#";
    img = scaleImage(img);
    final int width = img.getWidth();
    final int height = img.getHeight();
    int min = width * height;
    final boolean bNotEight = isNotEight(img);
    final boolean bNotThree = isNotThree(img);
    final boolean bNotFive = isNotFive(img);
    for (final BufferedImage bi : map.keySet()) {
        if (bNotThree && map.get(bi).startsWith("3")) {
            continue;
        }
        if (bNotEight && map.get(bi).startsWith("8")) {
            continue;
        }
        if (bNotFive && map.get(bi).startsWith("5")) {
            continue;
        }
        final double count1 = getBlackCount(img);
        final double count2 = getBlackCount(bi);
        if (Math.abs(count1 - count2) / Math.max(count1, count2) > 0.25) {
            continue;
        }
        int count = 0;
        if (width < bi.getWidth() && height < bi.getHeight()) {
            for (int m = 0; m <= bi.getWidth() - width; m++) {
                for (int n = 0; n <= bi.getHeight() - height; n++) {
                    Label1: for (int x = m; x < m + width; ++x) {
                        for (int y = n; y < n + height; ++y) {
                            if (CommonUtil.isWhite(img.getRGB(x - m, y - n), whiteThreshold) != CommonUtil
                                    .isWhite(bi.getRGB(x, y), whiteThreshold)) {
                                count++;
                                if (count >= min) {
                                    break Label1;
                                }
                            }
                        }
                    }
                }
            }
        } else {
            final int widthmin = width < bi.getWidth() ? width : bi.getWidth();
            final int heightmin = height < bi.getHeight() ? height : bi.getHeight();
            Label1: for (int x = 0; x < widthmin; ++x) {
                for (int y = 0; y < heightmin; ++y) {
                    if (CommonUtil.isWhite(img.getRGB(x, y), whiteThreshold) != CommonUtil
                            .isWhite(bi.getRGB(x, y), whiteThreshold)) {
                        count++;
                        if (count >= min) {
                            break Label1;
                        }
                    }
                }
            }
        }
        if (count < min) {
            min = count;
            result = map.get(bi);
        }
    }
    return result;
}

From source file:it.smartcommunitylab.parking.management.web.manager.MarkerIconStorage.java

private void generateMarkerWithFlag(String basePath, String company, String entity, String color)
        throws IOException {
    BufferedImage templateIcon = ImageIO.read(new File(getIconFolder(basePath, ICON_FOLDER) + TEMPLATE_FILE));
    BufferedImage icon = new BufferedImage(templateIcon.getWidth(), templateIcon.getHeight() + ICON_INCR_HEIGHT,
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics = icon.createGraphics();
    graphics.setColor(new Color(Integer.parseInt(color, 16)));
    graphics.drawRect(0, 0, icon.getWidth(), COLOR_RECT_HEIGHT);
    graphics.fillRect(0, 0, icon.getWidth(), COLOR_RECT_HEIGHT);
    graphics.drawImage(templateIcon, 0, ICON_INCR_HEIGHT, null);
    graphics.dispose();//  w  ww  .  j a va 2 s .  c  om
    ImageIO.write(icon, ICON_TYPE, new File(getIconFolder(basePath, ICON_FOLDER_CACHE) + company + "-" + entity
            + "-" + color + ICON_EXTENSION));
}