List of usage examples for java.awt Graphics2D setRenderingHint
public abstract void setRenderingHint(Key hintKey, Object hintValue);
From source file:test.be.fedict.eid.applet.JGraphTest.java
private void graphToFile(BasicVisualizationServer<String, String> visualization, File file) throws IOException { Dimension size = visualization.getSize(); int width = (int) (size.getWidth() + 1); int height = (int) (size.getHeight() + 1); LOG.debug("width: " + width); LOG.debug("height: " + height); BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D graphics = bufferedImage.createGraphics(); graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, 900, 650);/*from ww w . j ava 2 s . co m*/ visualization.setBounds(0, 0, 900, 650); visualization.paint(graphics); graphics.dispose(); ImageIO.write(bufferedImage, "png", file); }
From source file:ddf.catalog.transformer.input.pptx.PptxInputTransformer.java
/** * If the slide show doesn't contain any slides, then return null. Otherwise, return jpeg * image data of the first slide in the deck. * * @param slideShow/*from w w w. j a v a 2 s. com*/ * @return jpeg thumbnail or null if thumbnail can't be created * @throws IOException */ private byte[] generatePptxThumbnail(XMLSlideShow slideShow) throws IOException { if (slideShow.getSlides().isEmpty()) { LOGGER.info("the powerpoint file does not contain any slides, skipping thumbnail generation"); return null; } Dimension pgsize = slideShow.getPageSize(); int largestDimension = (int) Math.max(pgsize.getHeight(), pgsize.getWidth()); float scalingFactor = IMAGE_HEIGHTWIDTH / largestDimension; int scaledHeight = (int) (pgsize.getHeight() * scalingFactor); int scaledWidth = (int) (pgsize.getWidth() * scalingFactor); BufferedImage img = new BufferedImage(scaledWidth, scaledHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = img.createGraphics(); try { graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); graphics.scale(scalingFactor, scalingFactor); slideShow.getSlides().get(0).draw(graphics); try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { ImageIOUtil.writeImage(img, FORMAT_NAME, outputStream, RESOLUTION_DPI, IMAGE_QUALITY); return outputStream.toByteArray(); } } catch (RuntimeException e) { if (e.getCause() instanceof javax.imageio.IIOException) { LOGGER.warn("unable to generate thumbnail for PPTX file", e); } else { throw e; } } finally { graphics.dispose(); } return null; }
From source file:com.cubusmail.server.services.RetrieveImageServlet.java
/** * @param bufInputStream/*w w w. j a va 2 s . c o m*/ * @param outputStream */ private void writeScaledImage(BufferedInputStream bufInputStream, OutputStream outputStream) { long millis = System.currentTimeMillis(); try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); int bytesRead = 0; byte[] buffer = new byte[8192]; while ((bytesRead = bufInputStream.read(buffer, 0, 8192)) != -1) { bos.write(buffer, 0, bytesRead); } bos.close(); byte[] imageBytes = bos.toByteArray(); Image image = Toolkit.getDefaultToolkit().createImage(imageBytes); MediaTracker mediaTracker = new MediaTracker(new Container()); mediaTracker.addImage(image, 0); mediaTracker.waitForID(0); // determine thumbnail size from WIDTH and HEIGHT int thumbWidth = 300; int thumbHeight = 200; double thumbRatio = (double) thumbWidth / (double) thumbHeight; int imageWidth = image.getWidth(null); int imageHeight = image.getHeight(null); double imageRatio = (double) imageWidth / (double) imageHeight; if (thumbRatio < imageRatio) { thumbHeight = (int) (thumbWidth / imageRatio); } else { thumbWidth = (int) (thumbHeight * imageRatio); } // draw original image to thumbnail image object and // scale it to the new size on-the-fly BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = thumbImage.createGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outputStream); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage); int quality = 70; quality = Math.max(0, Math.min(quality, 100)); param.setQuality((float) quality / 100.0f, false); encoder.setJPEGEncodeParam(param); encoder.encode(thumbImage); } catch (IOException ex) { log.error(ex.getMessage(), ex); } catch (InterruptedException ex) { log.error(ex.getMessage(), ex); } finally { log.debug("Time for thumbnail: " + (System.currentTimeMillis() - millis) + "ms"); } }
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/*from w w w .ja va2 s . 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:IconDemoApp.java
/** * Resizes an image using a Graphics2D object backed by a BufferedImage. * //from w ww . ja va2s. com * @param srcImg - * source image to scale * @param w - * desired width * @param h - * desired height * @return - the new resized image */ private Image getScaledImage(Image srcImg, int w, int h) { BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = resizedImg.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(srcImg, 0, 0, w, h, null); g2.dispose(); return resizedImg; }
From source file:org.echocat.velma.dialogs.AboutDialog.java
@Nonnull @Override/*from w ww . j ava2 s. co m*/ protected Container createContainer() { return new JPanel() { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); final Graphics2D g2d = (Graphics2D) g; final Dimension size = getSize(); g2d.setRenderingHint(KEY_INTERPOLATION, VALUE_INTERPOLATION_BILINEAR); g2d.setRenderingHint(KEY_RENDERING, VALUE_RENDER_QUALITY); g2d.setRenderingHint(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON); final double sourceWidth = _backgroundImage.getWidth(); final double sourceHeight = _backgroundImage.getHeight(); final double targetWidth = size.getWidth(); final double targetHeight = size.getHeight(); final double relWidth = sourceWidth / targetWidth; final double relHeight = sourceHeight / targetHeight; final double width; final double height; final double x; final double y; if (relWidth > relHeight) { width = sourceWidth / relHeight; height = sourceHeight / relHeight; x = ((targetWidth - width) / 2); y = 0; } else { width = sourceWidth / relWidth; height = sourceHeight / relWidth; x = 0; y = ((targetHeight - height) / 2); } g2d.drawImage(_backgroundImage, (int) x, (int) y, (int) width, (int) height, this); } }; }
From source file:org.locationtech.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 w w . j av a 2 s . c om } }); 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 = WrapperUtilities.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 = WrapperUtilities.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:edu.kit.dama.ui.components.TextImage.java
/** * Get the bytes of the final image./*from ww w . j a v a 2 s .c o m*/ * * @return The byte array containing the bytes of the resulting image. * * @throws IOException if creating the image fails. */ public byte[] getBytes() throws IOException { Image transparentImage = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource( new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB).getSource(), new RGBImageFilter() { @Override public final int filterRGB(int x, int y, int rgb) { return (rgb << 8) & 0xFF000000; } })); //create the actual image and overlay it by the transparent background BufferedImage outputImage = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = outputImage.createGraphics(); g2d.drawImage(transparentImage, 0, 0, null); //draw the remaining stuff g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2d.setColor(color); g2d.fillRoundRect(0, 0, size, size, 20, 20); g2d.setColor(new Color(Math.round((float) color.getRed() * .9f), Math.round((float) color.getGreen() * .9f), Math.round((float) color.getBlue() * .9f))); g2d.drawRoundRect(0, 0, size - 1, size - 1, 20, 20); Font font = new Font("Dialog", Font.BOLD, size - 4); g2d.setFont(font); g2d.setColor(Color.WHITE); String s = text.toUpperCase().substring(0, 1); FontMetrics fm = g2d.getFontMetrics(); float x = ((float) size - (float) fm.stringWidth(s)) / 2f; float y = ((float) fm.getAscent() + (float) ((float) size - ((float) fm.getAscent() + (float) fm.getDescent())) / 2f) - 1f; g2d.drawString(s, x, y); ByteArrayOutputStream bout = new ByteArrayOutputStream(); ImageIO.write(outputImage, "png", bout); g2d.dispose(); return bout.toByteArray(); }
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();/*from w ww .ja va 2s. co m*/ } }); 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:StrokeChooserPanel.java
/** * Draws a line using the sample stroke. * * @param g the graphics device./* ww w . ja va 2 s .c om*/ */ public void paintComponent(final Graphics g) { final Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); final Dimension size = getSize(); final Insets insets = getInsets(); final double xx = insets.left; final double yy = insets.top; final double ww = size.getWidth() - insets.left - insets.right; final double hh = size.getHeight() - insets.top - insets.bottom; // calculate point one final Point2D one = new Point2D.Double(xx + 6, yy + hh / 2); // calculate point two final Point2D two = new Point2D.Double(xx + ww - 6, yy + hh / 2); // draw a circle at point one final Ellipse2D circle1 = new Ellipse2D.Double(one.getX() - 5, one.getY() - 5, 10, 10); final Ellipse2D circle2 = new Ellipse2D.Double(two.getX() - 6, two.getY() - 5, 10, 10); // draw a circle at point two g2.draw(circle1); g2.fill(circle1); g2.draw(circle2); g2.fill(circle2); // draw a line connecting the points final Line2D line = new Line2D.Double(one, two); if (this.stroke != null) { g2.setStroke(this.stroke); g2.draw(line); } }