Example usage for java.awt.image BufferedImage BufferedImage

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

Introduction

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

Prototype

public BufferedImage(int width, int height, int imageType) 

Source Link

Document

Constructs a BufferedImage of one of the predefined image types.

Usage

From source file:ch.fork.AdHocRailway.ui.utils.ImageTools.java

private BufferedImage ImageToBufferedImage(Image image, int width, int height) {
    BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = dest.createGraphics();
    g2.drawImage(image, 0, 0, null);/*from  w w  w.  j  a  v a 2  s.  com*/
    g2.dispose();
    return dest;
}

From source file:de.anycook.upload.UploadHandler.java

/**
 * speichert eine kleine Version des Bildes
 *
 * @param image    BufferedImage/* w  ww . java2s .co  m*/
 * @param filename Name der zu erzeugenden Datei
 */
private void saveSmallImage(BufferedImage image, String filename) throws IOException {
    int height = image.getHeight();
    int width = image.getWidth();
    double imageRatio = (double) width / (double) height;

    int xtranslate = 0;
    int ytranslate = 0;

    if (imageRatio > 1) {
        xtranslate = (width - height) / 2;
    } else {
        ytranslate = (height - width) / 2;
    }

    BufferedImage tempImage = image.getSubimage(xtranslate, ytranslate, width - xtranslate * 2,
            height - ytranslate * 2);
    BufferedImage newImage = new BufferedImage(smallSize, smallSize, BufferedImage.TYPE_INT_RGB);
    newImage.getGraphics().drawImage(tempImage.getScaledInstance(smallSize, smallSize, Image.SCALE_SMOOTH), 0,
            0, null);

    imageSaver.save("small/", filename, newImage);
}

From source file:ColorApp.java

public void createBufferedImage() {
    bi = new BufferedImage(displayImage.getWidth(this), displayImage.getHeight(this),
            BufferedImage.TYPE_INT_ARGB);

    big = bi.createGraphics();/*from   www .  j av a 2s.co  m*/
    big.drawImage(displayImage, 0, 0, this);
}

From source file:org.n52.oxf.render.sos.ProportionalCircleMapRenderer.java

private Image renderLegend(ObservationSeriesCollection obsValues, String observedProperty) {

    double lowestValue = (Double) obsValues.getMinimum(0);
    double highestValue = (Double) obsValues.getMaximum(0);

    double classDistance = (highestValue - lowestValue) / (NUMBER_OF_CLASSES - 2);
    int dotSizeDistance = (MAX_DOT_SIZE - MIN_DOT_SIZE) / (NUMBER_OF_CLASSES - 2);

    BufferedImage image = new BufferedImage(LEGEND_WIDTH, LEGEND_HEIGHT, BufferedImage.TYPE_INT_RGB);

    Graphics2D g = image.createGraphics();

    // draw white background:
    g.setColor(Color.WHITE);/*from   w  w  w .  j  av  a  2s  .  co m*/
    g.fillRect(0, 0, LEGEND_WIDTH, LEGEND_HEIGHT);

    // draw information:
    observedProperty = observedProperty.split(":")[observedProperty.split(":").length - 1];
    g.setColor(Color.BLACK);
    g.drawString("Observed Property:   '" + observedProperty + "'", 25, 25);

    for (int i = 1; i <= NUMBER_OF_CLASSES; i++) {
        // draw text:
        int x_stringLocation = 100;
        int y_location = i * 60;
        g.setColor(Color.BLACK);

        DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(Locale.GERMAN);
        df.applyPattern("#,###,##0.00");

        double lowerBorder = lowestValue + classDistance * (i - 1);
        double upperBorder = lowestValue + classDistance * i;

        g.drawString(i + ". class: " + df.format(lowerBorder) + " - " + df.format(upperBorder),
                x_stringLocation, y_location + 10);

        // draw symbol:
        int x_symbolLocation = 30;
        g.setColor(POINT_INNER_COLOR);
        g.fillOval(x_symbolLocation, y_location, i * dotSizeDistance, i * dotSizeDistance);
    }

    return image;
}

From source file:com.tomtom.speedtools.json.ImageSerializer.java

@Nonnull
private static BufferedImage convertToBufferedImage(@Nonnull final Image image) throws IOException {
    assert image != null;

    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }//w w w  . ja  va  2s .com

    /**
     * Load the image in the background and wait
     * until is is downloaded.
     */
    final MediaTracker tracker = new MediaTracker(new Component() {
        // Empty.
    });
    tracker.addImage(image, 0);
    try {
        tracker.waitForAll();
    } catch (final InterruptedException e) {
        throw new IOException(e.getMessage(), e);
    }

    /**
     * Create a buffered image with the right dimensions.
     */
    final BufferedImage bufImage = new BufferedImage(image.getWidth(null), image.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);

    /**
     * Draw the image in the buffer and return it as base64 data.
     */
    final Graphics g = bufImage.createGraphics();
    g.drawImage(image, 0, 0, null);
    return bufImage;
}

From source file:eu.udig.style.advanced.points.PointStyleManager.java

private TableViewer createStylesTableViewer(Composite parent) {
    final StyleFilter filter = new StyleFilter();
    final Text searchText = new Text(parent, SWT.BORDER | SWT.SEARCH);
    searchText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
    searchText.addKeyListener(new KeyAdapter() {
        public void keyReleased(KeyEvent ke) {
            filter.setSearchText(searchText.getText());
            stylesViewer.refresh();/*  w ww . jav  a2 s .  com*/
        }

    });

    stylesViewer = new TableViewer(parent, SWT.SINGLE | SWT.BORDER);
    Table table = stylesViewer.getTable();
    GridData tableGD = new GridData(SWT.FILL, SWT.FILL, true, true);
    tableGD.heightHint = 200;
    table.setLayoutData(tableGD);

    stylesViewer.addFilter(filter);
    stylesViewer.setContentProvider(new IStructuredContentProvider() {
        public Object[] getElements(Object inputElement) {
            if (inputElement instanceof List<?>) {
                List<?> styles = (List<?>) inputElement;
                StyleWrapper[] array = (StyleWrapper[]) styles.toArray(new StyleWrapper[styles.size()]);
                return array;
            }
            return null;
        }

        public void dispose() {
        }

        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        }
    });

    stylesViewer.setLabelProvider(new LabelProvider() {
        public Image getImage(Object element) {
            if (element instanceof StyleWrapper) {
                StyleWrapper styleWrapper = (StyleWrapper) element;
                List<FeatureTypeStyleWrapper> featureTypeStyles = styleWrapper
                        .getFeatureTypeStylesWrapperList();
                int iconSize = 48;
                BufferedImage image = new BufferedImage(iconSize, iconSize, BufferedImage.TYPE_INT_ARGB);
                Graphics2D g2d = image.createGraphics();
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                for (FeatureTypeStyleWrapper featureTypeStyle : featureTypeStyles) {
                    List<RuleWrapper> rulesWrapperList = featureTypeStyle.getRulesWrapperList();
                    BufferedImage tmpImage = Utilities.pointRulesWrapperToImage(rulesWrapperList, iconSize,
                            iconSize);
                    g2d.drawImage(tmpImage, 0, 0, null);
                }
                g2d.dispose();
                Image convertToSWTImage = AWTSWTImageUtils.convertToSWTImage(image);
                return convertToSWTImage;
            }
            return null;
        }

        public String getText(Object element) {
            if (element instanceof StyleWrapper) {
                StyleWrapper styleWrapper = (StyleWrapper) element;
                String styleName = styleWrapper.getName();
                if (styleName == null || styleName.length() == 0) {
                    styleName = Utilities.DEFAULT_STYLENAME;
                    styleName = Utilities.checkSameNameStyle(getStyles(), styleName);
                    styleWrapper.setName(styleName);
                }
                return styleName;
            }
            return ""; //$NON-NLS-1$
        }
    });

    stylesViewer.addSelectionChangedListener(new ISelectionChangedListener() {

        public void selectionChanged(SelectionChangedEvent event) {
            ISelection selection = event.getSelection();
            if (!(selection instanceof IStructuredSelection)) {
                return;
            }
            IStructuredSelection sel = (IStructuredSelection) selection;
            if (sel.isEmpty()) {
                return;
            }

            Object selectedItem = sel.getFirstElement();
            if (selectedItem == null) {
                // unselected, show empty panel
                return;
            }

            if (selectedItem instanceof StyleWrapper) {
                currentSelectedStyleWrapper = (StyleWrapper) selectedItem;
            }
        }

    });
    return stylesViewer;
}

From source file:algorithm.ImageInformationEmbeddingFrame.java

/**
 * Create empty image with enough capacity to embed the payload
 * /*from  ww w.  j a v  a 2  s. c  o  m*/
 * @param carrier
 * @param payload
 * @return image for payload
 * @throws IOException
 */
private BufferedImage createPayloadImage(File carrier, File payload) throws IOException {
    BufferedImage carrierImage = ImageIO.read(carrier);
    byte[] payloadBytes = FileUtils.readFileToByteArray(payload);
    int neededPayloadHeight = getNeededHeight(carrierImage.getWidth(), payloadBytes.length);
    return new BufferedImage(carrierImage.getWidth(), neededPayloadHeight, BufferedImage.TYPE_INT_RGB);
}

From source file:CustomAlphaTest.java

public CustomAlphaTest() {
    try {/*ww w  . j a va  2  s.c  o m*/
        // HACK:
        // if we are running as an Applet
        // the getWorkingDirectory() call will throw an NPE
        // as we cannot call getCodeBase until "start" has been called
        // (below).
        m_Alpha = new FileAlpha(new URL(getWorkingDirectory(), "values.xls"));
        m_Image = new BufferedImage(m_kWidth, m_kHeight, BufferedImage.TYPE_INT_RGB);

        buildUi();
        initJava3d();
    } catch (Exception e) {
    }
}

From source file:org.n52.io.img.ChartRenderer.java

private BufferedImage drawChartToImage() {
    int width = getChartStyleDefinitions().getWidth();
    int height = getChartStyleDefinitions().getHeight();
    BufferedImage chartImage = new BufferedImage(width, height, TYPE_INT_RGB);
    Graphics2D chartGraphics = chartImage.createGraphics();
    chartGraphics.fillRect(0, 0, width, height);
    chartGraphics.setColor(WHITE);//from ww  w  .j  a va2  s  .c  o  m

    chart.setTextAntiAlias(true);
    chart.setAntiAlias(true);
    chart.draw(chartGraphics, new Rectangle2D.Float(0, 0, width, height));
    return chartImage;
}

From source file:com.liusoft.dlog4j.util.ImageUtils.java

/**
 * ???/*from   www  . j  a  v  a2s.c om*/
 * ?????
 * 3: 180
 * 6: 90
 * 8: 27090
 * @param img_fn
 * @param orient
 * @throws IOException 
 */
public static boolean rotateImage(String img_fn, int orient, String dest_fn) throws IOException {
    double radian = 0;
    switch (orient) {
    case 3:
        radian = 180.0;
        break;
    case 6:
        radian = 90.0;
        break;
    case 8:
        radian = 270.0;
        break;
    default:
        return false;
    }
    BufferedImage old_img = (BufferedImage) ImageIO.read(new File(img_fn));
    int width = old_img.getWidth();
    int height = old_img.getHeight();

    BufferedImage new_img = new BufferedImage(height, width, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = new_img.createGraphics();

    AffineTransform origXform = g2d.getTransform();
    AffineTransform newXform = (AffineTransform) (origXform.clone());
    // center of rotation is center of the panel
    double xRot = 0;
    double yRot = 0;
    switch (orient) {
    case 3:
        xRot = width / 2.0;
        yRot = height / 2.0;
    case 6:
        xRot = height / 2.0;
        yRot = xRot;
        break;
    case 8:
        xRot = width / 2.0;
        yRot = xRot;
        break;
    default:
        return false;
    }
    newXform.rotate(Math.toRadians(radian), xRot, yRot);

    g2d.setTransform(newXform);
    // draw image centered in panel
    g2d.drawImage(old_img, 0, 0, null);
    // Reset to Original
    g2d.setTransform(origXform);

    FileOutputStream out = new FileOutputStream(dest_fn);
    try {
        ImageIO.write(new_img, "JPG", out);
    } finally {
        out.close();
    }
    return true;
}