Example usage for java.awt.image BufferedImage TYPE_INT_RGB

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

Introduction

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

Prototype

int TYPE_INT_RGB

To view the source code for java.awt.image BufferedImage TYPE_INT_RGB.

Click Source Link

Document

Represents an image with 8-bit RGB color components packed into integer pixels.

Usage

From source file:fr.gael.dhus.datastore.processing.impl.ProcessingUtils.java

/**
 * Cut the quicklook that have a big width/height ratio.
 * Each sub-part is dispatched on multiple bands.
 *
 * @param quick_look the quick_look to be cut
 * @param max_ratio the maximum ratio between quick_look width and height.
 * @param margin the margin between each band. default 5.
 *//*  ww  w. java2s  .co  m*/
public static RenderedImage cutQuickLook(RenderedImage input_image, double max_ratio, int margin) {
    ColorModel color_model = input_image.getColorModel();
    if ((color_model == null) && (input_image.getSampleModel() != null)) {
        color_model = ColorRenderer.createColorModel(input_image.getSampleModel());
    }

    BufferedImage quick_look;
    try {
        quick_look = PlanarImage.wrapRenderedImage(input_image).getAsBufferedImage(
                new Rectangle(input_image.getWidth(), input_image.getHeight()), color_model);
    } catch (Exception e) {
        logger.error("Problem getting buffered image.", e);
        throw new IllegalArgumentException("Problem getting buffered image", e);
    }

    if ((quick_look != null) && ((quick_look.getWidth() > 0) && (quick_look.getHeight() > 0))) {
        //Compute width/height ratio
        int ql_width = quick_look.getWidth();
        int ql_height = quick_look.getHeight();
        int ratio = (int) Math.sqrt(Math.max(ql_width, ql_height) / Math.min(ql_width, ql_height));

        //Check if the quicklook has a strong width/height ratio
        if ((ratio < max_ratio) || (ratio <= 1))
            return PlanarImage.wrapRenderedImage(quick_look);

        /**
         * Cut the wider side (width or height) into "ratio" bands.
         * Ex: If height = 3 * width then we cut 3 bands along columns
         *     So height' = height / 3   (extract 1 band / 3 from height)
         *        width'  = width  * 3   (dispatch 3 bands along lines)
         */
        int width = ql_width; //width of the bands
        int height = ql_height; //height of the bands

        if (ql_width < ql_height) //cut along height
        {
            width = (ql_width + margin) * ratio;
            height = ql_height / ratio;
        } else //cut along width
        {
            width = ql_width / ratio;
            height = (ql_height + margin) * ratio;
        }

        //Dispatch the sub-parts
        BufferedImage quick_look_cut = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = quick_look_cut.createGraphics();

        for (int k = 0; k < ratio; k++) {
            BufferedImage ql_band = null;
            //Dispatch on columns
            if (ql_width < ql_height) {
                ql_band = quick_look.getSubimage(0, (k * ql_height) / ratio, ql_width, ql_height / ratio);
                g2.drawImage(ql_band, null, k * (ql_width + margin), 0);
            }
            //Dispatch on lines
            else {
                ql_band = quick_look.getSubimage((k * ql_width) / ratio, 0, ql_width / ratio, ql_height);
                g2.drawImage(ql_band, null, 0, k * (ql_height + margin));
            }
        } //for each band

        g2.dispose();
        return PlanarImage.wrapRenderedImage(quick_look_cut);
    }
    return PlanarImage.wrapRenderedImage(quick_look);
}

From source file:com.liud.dailynote.ThumbnailatorTest.java

public void testYS7() throws IOException {
    String result = "src/main/resources/images/";
    BufferedImage bi = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bi.createGraphics();
    g.setColor(Color.LIGHT_GRAY);
    g.drawRect(0, 0, 10, 10);//from   w w  w.  j  a  v  a 2  s  .co  m
    char[] data = "liudTest".toCharArray();
    g.drawChars(data, 0, data.length, 5, 32);

    // watermark ? 1.? 2.? 3.?
    Thumbnails.of(result + "sijili.jpg").scale(1.0f).watermark(Positions.CENTER, bi, 1.0f)
            .toFile(result + "image_warter_liud.jpg");
}

From source file:com.devbury.mkremote.server.QuickLaunchServiceImpl.java

public ArrayList<LaunchItem> list(String dir) {
    Base64 base64 = new Base64();
    JFileChooser chooser = new JFileChooser();
    File new_dir = newFileDir(dir);
    logger.debug("Looking for files in {}", new_dir.getAbsolutePath());
    ArrayList<LaunchItem> items = new ArrayList<LaunchItem>();
    if (isSupported()) {
        if (new_dir.isDirectory()) {
            FilenameFilter filter = new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return !name.startsWith(".");
                }//ww w.  ja va  2 s.c o  m
            };
            for (File f : new_dir.listFiles(filter)) {
                if (!f.isHidden()) {
                    LaunchItem item = new LaunchItem();
                    item.setName(f.getName());
                    item.setPath(dir);
                    if (f.isDirectory()) {
                        if (isMac() && f.getName().endsWith(".app")) {
                            item.setType(LaunchItem.FILE_TYPE);
                            item.setName(f.getName().substring(0, f.getName().length() - 4));
                        } else {
                            item.setType(LaunchItem.DIR_TYPE);
                        }
                    } else {
                        item.setType(LaunchItem.FILE_TYPE);
                    }
                    Icon icon = chooser.getIcon(f);
                    BufferedImage bi = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
                            BufferedImage.TYPE_INT_RGB);
                    icon.paintIcon(null, bi.createGraphics(), 0, 0);
                    ByteArrayOutputStream os = new ByteArrayOutputStream();
                    try {
                        ImageIO.write(bi, "png", os);
                        item.setIcon(base64.encodeToString(os.toByteArray()));
                    } catch (IOException e) {
                        logger.debug("could not write image {}", e);
                        item.setIcon(null);
                    }
                    logger.debug("Adding LaunchItem : {}", item);
                    items.add(item);
                } else {
                    logger.debug("Skipping hidden file {}", f.getName());
                }
            }
        }
    } else {
        new Thread() {
            @Override
            public void run() {
                JOptionPane.showMessageDialog(null,
                        "We are sorry but quick launch is not supported on your platform",
                        "Quick Launch Not Supported", JOptionPane.ERROR_MESSAGE);
            }
        }.start();
    }
    return items;
}

From source file:com.eywa.impl.app.controllers.session.impl.RenderToolPDF.java

private void render(final DBObject item, final String pdf_path) throws Exception {
    // render only in not rendered till now
    if (!StringUtils.hasText(ItemSessionDataFile.getBase64(item))) {
        final File pdf_file = new File(pdf_path);
        final String output_root = pdf_file.getParent();
        final int imageType = BufferedImage.TYPE_INT_RGB;
        final int resolution = 96;
        final int page_number = ItemSessionDataFile.getPageNumber(item);
        final List<String> files = PDFUtils.toImage(pdf_file, output_root, true, imageType, resolution,
                page_number, page_number);
        if (!files.isEmpty()) {
            final String file_path = files.get(0);
            final File file = new File(file_path);
            final ImageSize size = ImageIOUtils.getImageSize(file);
            ItemSessionDataFile.setFileTempPath(item, file_path);
            ItemSessionDataFile.setHeight(item, size.getHeight());
            ItemSessionDataFile.setWidth(item, size.getWidth());
            ItemSessionDataFile.setBase64(item, ImageIOUtils.readBase64(file));
        }// w ww .ja v a2s  .com
    }
}

From source file:de.bund.bfr.knime.chart.ChartUtils.java

public static ImagePortObject getImage(JFreeChart chart, boolean asSvg, int width, int height) {
    if (asSvg) {/*  w  w w . j  a  va  2  s  .co  m*/
        SVGDocument document = (SVGDocument) new SVGDOMImplementation().createDocument(null, "svg", null);
        SVGGraphics2D g = new SVGGraphics2D(document);

        g.setSVGCanvasSize(new Dimension(width, height));

        if (chart != null) {
            chart.draw(g, new Rectangle2D.Double(0, 0, width, height));
        }

        g.dispose();
        document.replaceChild(g.getRoot(), document.getDocumentElement());
        return new ImagePortObject(new SvgImageContent(document), new ImagePortObjectSpec(SvgCell.TYPE));
    } else {
        try {
            BufferedImage img = chart != null ? chart.createBufferedImage(width, height)
                    : new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

            return new ImagePortObject(new PNGImageContent(ChartUtilities.encodeAsPNG(img)),
                    new ImagePortObjectSpec(PNGImageContent.TYPE));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

From source file:org.jfree.chart.demo.ChartTiming3.java

/**
 * Runs the test.//w  ww.ja  va 2  s .  c  om
 */
public void run() {

    this.finished = false;

    // create a dataset...
    final XYSeries series = new XYSeries("Random Data");
    for (int i = 0; i < 1440; i++) {
        final double x = Math.random();
        final double y = Math.random();
        series.add(x, y);
    }
    final XYDataset data = new XYSeriesCollection(series);

    // create a scatter chart...
    final boolean withLegend = true;
    final JFreeChart chart = ChartFactory.createScatterPlot("Scatter plot timing", "X", "Y", data,
            PlotOrientation.VERTICAL, withLegend, false, false);

    final XYPlot plot = chart.getXYPlot();
    plot.setRenderer(new XYDotRenderer());

    final BufferedImage image = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB);
    final Graphics2D g2 = image.createGraphics();
    final Rectangle2D chartArea = new Rectangle2D.Double(0, 0, 400, 300);

    // set up the timer...
    final Timer timer = new Timer(10000, this);
    timer.setRepeats(false);
    int count = 0;
    timer.start();
    while (!this.finished) {
        chart.draw(g2, chartArea, null, null);
        System.out.println("Charts drawn..." + count);
        if (!this.finished) {
            count++;
        }
    }
    System.out.println("DONE");

}

From source file:game.com.HandleUploadGameThumbServlet.java

public static BufferedImage resizeImage(final Image image, int width, int height) {
    final BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    final Graphics2D graphics2D = bufferedImage.createGraphics();
    graphics2D.setComposite(AlphaComposite.Src);
    //below three lines are for RenderingHints for better image quality at cost of higher processing time
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    graphics2D.drawImage(image, 0, 0, width, height, null);
    graphics2D.dispose();/*  w  w w .j  av  a2  s .  c o  m*/
    return bufferedImage;
}

From source file:at.tuwien.ifs.somtoolbox.visualization.ClusterConnectionsVisualizer.java

@Override
public BufferedImage createVisualization(int variantIndex, GrowingSOM gsom, int width, int height)
        throws SOMToolboxException {
    GrowingLayer layer = gsom.getLayer();
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = image.createGraphics();

    g.setBackground(Color.WHITE);
    g.clearRect(0, 0, width, height);/* w w  w  . ja  v a  2  s .  c  om*/

    // need to calculate some params relative to the unit width/height, which is relative to the desired output size
    // a unitWidth/Height of 10, as used previously, works by default only fine in the SOMViewer
    int somWidth = layer.getXSize();
    int somHeight = layer.getYSize();
    double unitWidth = width / somWidth;
    double unitHeight = height / somHeight;

    DoubleMatrix2D unitDistanceMatrix = gsom.getLayer().getUnitDistanceMatrix();

    for (int col = 0; col < somWidth; col++) {
        for (int row = 0; row < somHeight; row++) {

            if (col < somWidth - 1) {
                // draw horizontal connection
                double distanceRight = unitDistanceMatrix.get(layer.getUnitIndex(col, row),
                        layer.getUnitIndex(col + 1, row));
                g.setPaint(getColor(distanceRight));
                int xPos = (int) (col * unitHeight + unitHeight * 0.7);
                int yPos = (int) (row * unitWidth + unitWidth * 0.4);
                g.fillRect(xPos, yPos, (int) (unitWidth * 0.6), (int) (unitHeight * 0.2));
            }

            if (row < somHeight - 1) {
                // draw vertical connection
                double distanceLower = unitDistanceMatrix.get(layer.getUnitIndex(col, row),
                        layer.getUnitIndex(col, row + 1));
                g.setPaint(getColor(distanceLower));

                int xPos = (int) (col * unitHeight + unitHeight * 0.4);
                int yPos = (int) (row * unitWidth + unitWidth * 0.7);
                g.fillRect(xPos, yPos, (int) (unitWidth * 0.2), (int) (unitHeight * 0.6));
            }
        }
    }

    return image;
}

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

/**
 * Returns a scaled BufferedImage/*www.ja v a2s  .  com*/
 *
 * @param bi        current image
 * @param maxWidth  new width
 * @param maxHeight new height
 * @return a new/scaled image
 */

/*public static BufferedImage ScaleImage(BufferedImage image, int width, int height) throws IOException {
   int imageWidth  = image.getWidth();
   int imageHeight = image.getHeight();
        
   double scaleX = (double)width/imageWidth;
   double scaleY = (double)height/imageHeight;
   AffineTransform scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
   AffineTransformOp bilinearScaleOp = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR);
        
   return bilinearScaleOp.filter(
    image,
    new BufferedImage(width, height, image.getType()));
}*/

public BufferedImage ScaleImage(BufferedImage bi, int maxWidth, int maxHeight) {
    double originalWidth = bi.getWidth() * 1.0;
    double originalHeight = bi.getHeight() * 1.0;

    double widthRatio = (maxWidth * 1.0) / originalWidth;
    double heightRatio = (maxHeight * 1.0) / originalHeight;
    double newImageRatio = 0;
    if (widthRatio < heightRatio) {
        newImageRatio = widthRatio;
    } else {
        newImageRatio = heightRatio;
    }

    BufferedImage bdest = new BufferedImage((int) (originalWidth * newImageRatio),
            (int) (originalHeight * newImageRatio), BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bdest.createGraphics();
    AffineTransform at = AffineTransform.getScaleInstance(newImageRatio, newImageRatio);
    g.drawRenderedImage(bi, at);

    return bdest;
}

From source file:ConvertUtil.java

/**
 * Converts the source image to 24-bit colour (RGB). No transparency.
 * //ww  w .j  a v  a 2  s  . co m
 * @param src
 *            the source image to convert
 * @return a copy of the source image with a 24-bit colour depth
 */
public static BufferedImage convert24(BufferedImage src) {
    BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_RGB);
    ColorConvertOp cco = new ColorConvertOp(src.getColorModel().getColorSpace(),
            dest.getColorModel().getColorSpace(), null);
    cco.filter(src, dest);
    return dest;
}