List of usage examples for java.awt.image BufferedImage createGraphics
public Graphics2D createGraphics()
From source file:com.gst.infrastructure.documentmanagement.data.ImageData.java
public void resizeImage(InputStream in, OutputStream out, int maxWidth, int maxHeight) throws IOException { BufferedImage src = ImageIO.read(in); if (src.getWidth() <= maxWidth && src.getHeight() <= maxHeight) { out.write(getContent());/*from w w w .j ava 2 s . co m*/ return; } float widthRatio = (float) src.getWidth() / maxWidth; float heightRatio = (float) src.getHeight() / maxHeight; float scaleRatio = widthRatio > heightRatio ? widthRatio : heightRatio; // TODO(lindahl): Improve compressed image quality (perhaps quality // ratio) int newWidth = (int) (src.getWidth() / scaleRatio); int newHeight = (int) (src.getHeight() / scaleRatio); int colorModel = fileExtension == ContentRepositoryUtils.IMAGE_FILE_EXTENSION.JPEG ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB; BufferedImage target = new BufferedImage(newWidth, newHeight, colorModel); Graphics2D g = target.createGraphics(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage(src, 0, 0, newWidth, newHeight, Color.BLACK, null); g.dispose(); ImageIO.write(target, fileExtension != null ? fileExtension.getValueWithoutDot() : "jpeg", out); }
From source file:image.writer.ImageWriterExample2.java
private BufferedImage createAnImage(String compression, int pageNum) { boolean monochrome = compression.startsWith("CCITT"); //CCITT is for 1bit b/w only BufferedImage bimg; if (monochrome) { bimg = new BufferedImage(400, 200, BufferedImage.TYPE_BYTE_BINARY); } else {/* ww w.j av a 2 s.c o m*/ bimg = new BufferedImage(400, 200, BufferedImage.TYPE_INT_RGB); } Graphics2D g2d = bimg.createGraphics(); g2d.setBackground(Color.white); g2d.clearRect(0, 0, 400, 200); g2d.setColor(Color.black); //Paint something paintSome(g2d, pageNum); return bimg; }
From source file:org.shredzone.cilla.admin.AbstractImageBean.java
/** * Scales a {@link BufferedImage} to the given size, keeping the aspect ratio. If the * image is smaller than the resulting size, it will be magnified. * * @param image// w ww . jav a 2 s . co m * {@link BufferedImage} to scale * @param width * Maximum result width * @param height * Maximum result height * @return {@link BufferedImage} with the scaled image */ private BufferedImage scale(BufferedImage image, int width, int height) { ImageObserver observer = null; Image scaled = null; if (image.getWidth() > image.getHeight()) { scaled = image.getScaledInstance(width, -1, Image.SCALE_SMOOTH); } else { scaled = image.getScaledInstance(-1, height, Image.SCALE_SMOOTH); } BufferedImage result = new BufferedImage(scaled.getWidth(observer), scaled.getHeight(observer), BufferedImage.TYPE_INT_RGB); result.createGraphics().drawImage(scaled, 0, 0, observer); return result; }
From source file:gui.images.CodebookVectorProfilePanel.java
/** * Sets the data to be shown./*from w ww . jav a 2 s . c o m*/ * * @param occurrenceProfile Double array that is the neighbor occurrence * profile of this visual word. * @param codebookIndex Integer that is the index of this visual word. * @param classColors Color[] of class colors. * @param classNames String[] of class names. */ public void setResults(double[] occurrenceProfile, int codebookIndex, Color[] classColors, String[] classNames) { int numClasses = Math.min(classNames.length, occurrenceProfile.length); this.codebookIndex = codebookIndex; this.occurrenceProfile = occurrenceProfile; DefaultPieDataset pieData = new DefaultPieDataset(); for (int cIndex = 0; cIndex < numClasses; cIndex++) { pieData.setValue(classNames[cIndex], occurrenceProfile[cIndex]); } JFreeChart chart = ChartFactory.createPieChart3D("codebook vect " + codebookIndex, pieData, true, true, false); PiePlot plot = (PiePlot) chart.getPlot(); plot.setDirection(Rotation.CLOCKWISE); plot.setForegroundAlpha(0.5f); PieRenderer prend = new PieRenderer(classColors); prend.setColor(plot, pieData); ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new Dimension(140, 140)); chartPanel.setVisible(true); chartPanel.revalidate(); chartPanel.repaint(); JPanel jp = new JPanel(); jp.setPreferredSize(new Dimension(140, 140)); jp.setMinimumSize(new Dimension(140, 140)); jp.setMaximumSize(new Dimension(140, 140)); jp.setSize(new Dimension(140, 140)); jp.setLayout(new FlowLayout()); jp.add(chartPanel); jp.setVisible(true); jp.validate(); jp.repaint(); JFrame frame = new JFrame(); frame.setBackground(Color.WHITE); frame.setUndecorated(true); frame.getContentPane().add(jp); frame.pack(); BufferedImage bi = new BufferedImage(jp.getWidth(), jp.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D graphics = bi.createGraphics(); jp.print(graphics); graphics.dispose(); frame.dispose(); imPanel.removeAll(); imPanel.setImage(bi); imPanel.setVisible(true); imPanel.revalidate(); imPanel.repaint(); }
From source file:peakml.util.swt.widget.IPeakGraph.java
public java.awt.image.BufferedImage getGraphImage(int width, int height) { java.awt.image.BufferedImage img = new java.awt.image.BufferedImage(width, height, java.awt.image.BufferedImage.TYPE_INT_ARGB); java.awt.Graphics2D g = img.createGraphics(); g.setRenderingHint(java.awt.RenderingHints.KEY_ANTIALIASING, java.awt.RenderingHints.VALUE_ANTIALIAS_ON); linechart.draw(g, new java.awt.Rectangle(0, 0, width, height)); return img;/*ww w . ja v a 2 s.co m*/ }
From source file:com.googlecode.jchav.chart.Chart.java
/** * Creates a PNG graphic for the given data as a thumbnail image. * * @param out the stream to write the PNG to. The caller is responsible * for closing the stream./* ww w .ja v a 2s. com*/ * * @throws IOException if there was a problem creating the chart. */ public void writeThumbnail(final OutputStream out) throws IOException { // Set up the transfomration: final AffineTransform xform = new AffineTransform(); xform.scale(thumbnailScale, thumbnailScale); // Thanks to the almanac for this one: // http://javaalmanac.com/egs/java.awt.image/CreateTxImage.html?l=rel final AffineTransformOp op = new AffineTransformOp(xform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); // The thumbnail does not need so much chart chrome: you can't // read the axis and the title is given in the HTML, so removing // these elements means there's more space in the thumbnail for the data boolean thumbChrome = false; if (false == thumbChrome) { chart.setTitle((String) null); chart.clearSubtitles(); chart.removeLegend(); // Removing the axis completly looks just weird, so we just // remove the labels: chart.getCategoryPlot().getRangeAxis().setLabel(null); chart.getCategoryPlot().getRangeAxis().setTickLabelsVisible(true); chart.getCategoryPlot().getRangeAxis().setTickMarksVisible(true); chart.getCategoryPlot().getRangeAxis().setAxisLineVisible(true); // To show up at a small scale, we need a good sized axis stroke: Stroke stroke = new BasicStroke(2f); chart.getCategoryPlot().getRangeAxis().setAxisLineStroke(stroke); chart.getCategoryPlot().getRangeAxis().setTickMarkStroke(stroke); chart.getCategoryPlot().getDomainAxis().setLabel(null); chart.getCategoryPlot().getDomainAxis().setTickLabelsVisible(false); chart.getCategoryPlot().getDomainAxis().setAxisLineVisible(true); chart.getCategoryPlot().getDomainAxis().setAxisLineStroke(stroke); } final BufferedImage fullsize = chart.createBufferedImage(width, height); Graphics2D g = fullsize.createGraphics(); for (Decorator decorator : thumbnailDecorators) { decorator.decorate(g, this); } final BufferedImage thumbnail = op.filter(fullsize, null /*null means create the image for us*/); ChartUtilities.writeBufferedImageAsPNG(out, thumbnail); }
From source file:cpcc.ros.sim.osm.Camera.java
/** * @return the extracted image./*from w w w . jav a2 s. c o m*/ * @throws IOException thrown in case of errors. */ private byte[] extractImage() throws IOException { double dTilesX = bottomRightTile.getxTile() - topLeftTile.getxTile(); double dTilesY = bottomRightTile.getyTile() - topLeftTile.getyTile(); int height = (int) (dTilesY * cfg.getTileHeight()) + bottomRightTile.getyPixel() - topLeftTile.getyPixel(); int width = (int) (dTilesX * cfg.getTileWidth()) + bottomRightTile.getxPixel() - topLeftTile.getxPixel(); if (height < 1) { height = 1; } if (width < 1) { width = 1; } BufferedImage image = map.getSubimage(topLeftTile.getxPixel(), topLeftTile.getyPixel(), width, height); Image scaledImage = image.getScaledInstance(cfg.getCameraWidth(), cfg.getCameraHeight(), 0); BufferedImage cameraImage = new BufferedImage(cfg.getCameraWidth(), cfg.getCameraHeight(), BufferedImage.TYPE_INT_RGB); cameraImage.createGraphics().drawImage(scaledImage, 0, 0, null); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ImageIO.write(cameraImage, "PNG", bos); return bos.toByteArray(); }
From source file:ddf.catalog.transformer.input.pdf.PdfInputTransformer.java
private byte[] generatePdfThumbnail(PDDocument pdfDocument) throws IOException { PDFRenderer pdfRenderer = new PDFRenderer(pdfDocument); if (pdfDocument.getNumberOfPages() < 1) { /*//w w w.j av a 2 s. c o m * Can there be a PDF with zero pages??? Should we throw an error or what? The * original implementation assumed that a PDF would always have at least one * page. That's what I've implemented here, but I don't like make those * kinds of assumptions :-( But I also don't want to change the original * behavior without knowing how it will impact the system. */ } PDPage page = pdfDocument.getPage(0); BufferedImage image = pdfRenderer.renderImageWithDPI(0, RESOLUTION_DPI, ImageType.RGB); int largestDimension = Math.max(image.getHeight(), image.getWidth()); float scalingFactor = IMAGE_HEIGHTWIDTH / largestDimension; int scaledHeight = (int) (image.getHeight() * scalingFactor); int scaledWidth = (int) (image.getWidth() * scalingFactor); BufferedImage scaledImage = new BufferedImage(scaledWidth, scaledHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = scaledImage.createGraphics(); graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics.drawImage(image, 0, 0, scaledWidth, scaledHeight, null); graphics.dispose(); try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { ImageIOUtil.writeImage(scaledImage, FORMAT_NAME, outputStream, RESOLUTION_DPI, IMAGE_QUALITY); return outputStream.toByteArray(); } }
From source file:org.n52.oxf.render.sos.TimeSeriesChartRenderer4xPhenomenons.java
/** * The resulting IVisualization shows a chart which consists a TimeSeries for each FeatureOfInterest * contained in the observationCollection. *//*from w w w .ja va 2s. c o m*/ public IVisualization renderChart(OXFFeatureCollection observationCollection, ParameterContainer paramCon, int width, int height) { JFreeChart chart = renderChart(observationCollection, paramCon); // draw plot into image: Plot plot = chart.getPlot(); BufferedImage chartImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D chartGraphics = chartImage.createGraphics(); chartGraphics.setColor(Color.white); chartGraphics.fillRect(0, 0, width, height); plot.draw(chartGraphics, new Rectangle2D.Float(0, 0, width, height), null, null, null); // draw legend into image: LegendTitle legend = chart.getLegend(); BufferedImage legendImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D legendGraphics = legendImage.createGraphics(); legendGraphics.setColor(Color.white); legendGraphics.fillRect(0, 0, (int) legend.getWidth(), (int) legend.getHeight()); legend.draw(legendGraphics, new Rectangle2D.Float(0, 0, width, height)); return new StaticVisualization(chartImage, legendImage); }
From source file:edu.umn.cs.spatialHadoop.operations.GeometricPlot.java
/** * Draws an image that can be used as a scale for heat maps generated using * Plot or PlotPyramid./*from w w w .j a v a2 s . c o m*/ * @param output - Output path * @param valueRange - Range of values of interest * @param width - Width of the generated image * @param height - Height of the generated image * @throws IOException */ public static void drawScale(Path output, MinMax valueRange, int width, int height) throws IOException { BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g = image.createGraphics(); g.setBackground(Color.BLACK); g.clearRect(0, 0, width, height); // fix this part to work according to color1, color2 and gradient type for (int y = 0; y < height; y++) { Color color = NASARectangle.calculateColor(y); g.setColor(color); g.drawRect(width * 3 / 4, y, width / 4, 1); } int fontSize = 24; g.setFont(new Font("Arial", Font.BOLD, fontSize)); int step = (valueRange.maxValue - valueRange.minValue) * fontSize * 10 / height; step = (int) Math.pow(10, Math.round(Math.log10(step))); int min_value = valueRange.minValue / step * step; int max_value = valueRange.maxValue / step * step; for (int value = min_value; value <= max_value; value += step) { int y = fontSize + (height - fontSize) - value * (height - fontSize) / (valueRange.maxValue - valueRange.minValue); g.setColor(Color.WHITE); g.drawString(String.valueOf(value), 5, y); } g.dispose(); FileSystem fs = output.getFileSystem(new Configuration()); FSDataOutputStream outStream = fs.create(output, true); ImageIO.write(image, "png", outStream); outStream.close(); }