List of usage examples for java.awt Graphics2D drawImage
public abstract void drawImage(BufferedImage img, BufferedImageOp op, int x, int y);
From source file:info.magnolia.cms.taglibs.util.TextToImageTag.java
/** * Create an image file that is a scaled version of the original image * @param the original BufferedImage//from www. j a v a 2 s .c o m * @param the scale factor * @return the new BufferedImage */ private BufferedImage scaleImage(BufferedImage oriImgBuff, double scaleFactor) { // get the dimesnions of the original image int oriWidth = oriImgBuff.getWidth(); int oriHeight = oriImgBuff.getHeight(); // get the width and height of the new image int newWidth = new Double(oriWidth * scaleFactor).intValue(); int newHeight = new Double(oriHeight * scaleFactor).intValue(); // create the thumbnail as a buffered image Image newImg = oriImgBuff.getScaledInstance(newWidth, newHeight, Image.SCALE_AREA_AVERAGING); BufferedImage newImgBuff = new BufferedImage(newImg.getWidth(null), newImg.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D g = newImgBuff.createGraphics(); g.drawImage(newImg, 0, 0, null); g.dispose(); // return the newImgBuff return newImgBuff; }
From source file:edu.ku.brc.specify.ui.containers.ContainerTreeRenderer.java
/** * @param g2d//from www . j ava2s . co m * @param xc * @param yc * @param imgIcon * @param hitsInx * @return */ private int drawIcon(final Graphics2D g2d, final int xc, final int yc, final ImageIcon imgIcon, final int hitsInx) { g2d.drawImage(imgIcon.getImage(), xc, yc, null); hitRects[hitsInx].setBounds(xc, yc, imgIcon.getIconWidth(), imgIcon.getIconHeight()); Point p = hitRects[hitsInx].getLocation(); SwingUtilities.convertPointToScreen(p, this); hitRects[hitsInx].setLocation(p); return imgIcon.getIconWidth() + iconSep; }
From source file:com.igormaznitsa.jhexed.values.HexSVGImageValue.java
@Override public BufferedImage makeIcon(final int width, final int height, final Path2D shape, final boolean allowAlpha) { try {/*from w ww . java 2 s . co m*/ final BufferedImage img = this.image.rasterize(width, height, BufferedImage.TYPE_INT_ARGB); if (shape == null) { return img; } else { final BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); final Graphics2D g = result.createGraphics(); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); g.setClip(makeTransformedPathForSize(width, height, shape)); g.drawImage(img, 0, 0, null); g.dispose(); return result; } } catch (Exception ex) { ex.printStackTrace(); return null; } }
From source file:org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMap.java
private void createImageStream(PDDocument doc, BufferedImage bi) throws IOException { BufferedImage alphaImage = null; BufferedImage rgbImage = null; int width = bi.getWidth(); int height = bi.getHeight(); if (bi.getColorModel().hasAlpha()) { // extract the alpha information WritableRaster alphaRaster = bi.getAlphaRaster(); ColorModel cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_GRAY), false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE); alphaImage = new BufferedImage(cm, alphaRaster, false, null); // create a RGB image without alpha rgbImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); Graphics2D g = rgbImage.createGraphics(); g.setComposite(AlphaComposite.Src); g.drawImage(bi, 0, 0, null); } else {/*from w ww . j a v a2 s .co m*/ rgbImage = bi; } java.io.OutputStream os = null; try { int numberOfComponents = rgbImage.getColorModel().getNumComponents(); if (numberOfComponents == 3) { setColorSpace(PDDeviceRGB.INSTANCE); } else { if (numberOfComponents == 1) { setColorSpace(new PDDeviceGray()); } else { throw new IllegalStateException(); } } byte[] outData = new byte[width * height * numberOfComponents]; rgbImage.getData().getDataElements(0, 0, width, height, outData); // add FlateDecode compression getPDStream().addCompression(); os = getCOSStream().createUnfilteredStream(); os.write(outData); COSDictionary dic = getCOSStream(); dic.setItem(COSName.FILTER, COSName.FLATE_DECODE); dic.setItem(COSName.SUBTYPE, COSName.IMAGE); dic.setItem(COSName.TYPE, COSName.XOBJECT); if (alphaImage != null) { PDPixelMap smask = new PDPixelMap(doc, alphaImage); dic.setItem(COSName.SMASK, smask); } setBitsPerComponent(8); setHeight(height); setWidth(width); } finally { os.close(); } }
From source file:eu.udig.style.advanced.utils.Utilities.java
/** * Creates an {@link Image} for the given rule. * /*from ww w . jav a 2 s . c o m*/ * @param rule the rule for which to create the image. * @param width the image width. * @param height the image height. * @return the generated image. */ public static BufferedImage polygonRuleToImage(final Rule rule, int width, int height) { DuplicatingStyleVisitor copyStyle = new DuplicatingStyleVisitor(); rule.accept(copyStyle); Rule newRule = (Rule) copyStyle.getCopy(); Stroke stroke = null; Symbolizer[] symbolizers = newRule.getSymbolizers(); if (symbolizers.length > 0) { Symbolizer symbolizer = symbolizers[0]; if (symbolizer instanceof PolygonSymbolizer) { PolygonSymbolizer polygonSymbolizer = (PolygonSymbolizer) symbolizer; stroke = SLDs.stroke(polygonSymbolizer); } } int strokeSize = 0; if (stroke != null) { strokeSize = SLDs.width(stroke); if (strokeSize < 0) { strokeSize = 0; stroke.setWidth(ff.literal(strokeSize)); } } // pointSize = width; BufferedImage finalImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); // Polygon polygon = d.polygon(new int[]{40,30, 60,70, 30,130, 130,130, 130,30}); int[] xy = new int[] { (int) (height * 0.15), (int) (width * 0.20), (int) (height * 0.4), (int) (width * 0.3), (int) (height * 0.85), (int) (width * 0.15), (int) (height * 0.85), (int) (width * 0.85), (int) (height * 0.15), (int) (width * 0.85) }; Polygon polygon = d.polygon(xy); d.drawDirect(finalImage, d.feature(polygon), newRule); Graphics2D g2d = finalImage.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.drawImage(finalImage, 0, 0, null); g2d.dispose(); return finalImage; }
From source file:eu.udig.style.advanced.utils.Utilities.java
/** * Creates an {@link Image} for the given rule. * //from ww w . j a v a2 s . co m * @param rule the rule for which to create the image. * @param width the image width. * @param height the image height. * @return the generated image. */ public static BufferedImage lineRuleToImage(final Rule rule, int width, int height) { DuplicatingStyleVisitor copyStyle = new DuplicatingStyleVisitor(); rule.accept(copyStyle); Rule newRule = (Rule) copyStyle.getCopy(); Stroke stroke = null; Symbolizer[] symbolizers = newRule.getSymbolizers(); if (symbolizers.length > 0) { Symbolizer symbolizer = newRule.getSymbolizers()[0]; if (symbolizer instanceof LineSymbolizer) { LineSymbolizer lineSymbolizer = (LineSymbolizer) symbolizer; stroke = SLDs.stroke(lineSymbolizer); } } int strokeSize = 0; if (stroke != null) { strokeSize = SLDs.width(stroke); if (strokeSize < 0) { strokeSize = 0; stroke.setWidth(ff.literal(strokeSize)); } } // pointSize = width; BufferedImage finalImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); // Polygon polygon = d.polygon(new int[]{40,30, 60,70, 30,130, 130,130, 130,30}); int[] xy = new int[] { (int) (height * 0.15), (int) (width * 0.85), (int) (height * 0.35), (int) (width * 0.15), (int) (height * 0.75), (int) (width * 0.85), (int) (height * 0.85), (int) (width * 0.15) }; LineString line = d.line(xy); d.drawDirect(finalImage, d.feature(line), newRule); Graphics2D g2d = finalImage.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.drawImage(finalImage, 0, 0, null); g2d.dispose(); return finalImage; }
From source file:org.codice.alliance.plugin.nitf.NitfPostIngestPlugin.java
private byte[] renderToJpeg2k(final BufferedImage bufferedImage) throws IOException { BufferedImage imageToCompress = bufferedImage; if (bufferedImage.getColorModel().getNumComponents() == ARGB_COMPONENT_COUNT) { imageToCompress = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_3BYTE_BGR); Graphics2D g = imageToCompress.createGraphics(); g.drawImage(bufferedImage, 0, 0, null); }//from w ww . j a v a 2 s. c om ByteArrayOutputStream os = new ByteArrayOutputStream(); J2KImageWriter writer = new J2KImageWriter(new J2KImageWriterSpi()); J2KImageWriteParam writeParams = (J2KImageWriteParam) writer.getDefaultWriteParam(); writeParams.setLossless(false); writeParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); writeParams.setCompressionType("JPEG2000"); writeParams.setCompressionQuality(0.0f); ImageOutputStream ios = new MemoryCacheImageOutputStream(os); writer.setOutput(ios); writer.write(null, new IIOImage(imageToCompress, null, null), writeParams); writer.dispose(); ios.close(); return os.toByteArray(); }
From source file:net.groupbuy.util.ImageUtils.java
/** * /* ww w.j ava2s .com*/ * * @param srcFile * ? * @param destFile * * @param destWidth * * @param destHeight * */ public static void zoom(File srcFile, File destFile, int destWidth, int destHeight) { Assert.notNull(srcFile); Assert.notNull(destFile); Assert.state(destWidth > 0); Assert.state(destHeight > 0); if (type == Type.jdk) { Graphics2D graphics2D = null; ImageOutputStream imageOutputStream = null; ImageWriter imageWriter = null; try { BufferedImage srcBufferedImage = ImageIO.read(srcFile); int srcWidth = srcBufferedImage.getWidth(); int srcHeight = srcBufferedImage.getHeight(); int width = destWidth; int height = destHeight; if (srcHeight >= srcWidth) { width = (int) Math.round(((destHeight * 1.0 / srcHeight) * srcWidth)); } else { height = (int) Math.round(((destWidth * 1.0 / srcWidth) * srcHeight)); } BufferedImage destBufferedImage = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB); graphics2D = destBufferedImage.createGraphics(); graphics2D.setBackground(BACKGROUND_COLOR); graphics2D.clearRect(0, 0, destWidth, destHeight); graphics2D.drawImage(srcBufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), (destWidth / 2) - (width / 2), (destHeight / 2) - (height / 2), null); imageOutputStream = ImageIO.createImageOutputStream(destFile); imageWriter = ImageIO.getImageWritersByFormatName(FilenameUtils.getExtension(destFile.getName())) .next(); imageWriter.setOutput(imageOutputStream); ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam(); imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); imageWriteParam.setCompressionQuality((float) (DEST_QUALITY / 100.0)); imageWriter.write(null, new IIOImage(destBufferedImage, null, null), imageWriteParam); imageOutputStream.flush(); } catch (IOException e) { e.printStackTrace(); } finally { if (graphics2D != null) { graphics2D.dispose(); } if (imageWriter != null) { imageWriter.dispose(); } if (imageOutputStream != null) { try { imageOutputStream.close(); } catch (IOException e) { } } } } else { IMOperation operation = new IMOperation(); operation.thumbnail(destWidth, destHeight); operation.gravity("center"); operation.background(toHexEncoding(BACKGROUND_COLOR)); operation.extent(destWidth, destHeight); operation.quality((double) DEST_QUALITY); operation.addImage(srcFile.getPath()); operation.addImage(destFile.getPath()); if (type == Type.graphicsMagick) { ConvertCmd convertCmd = new ConvertCmd(true); if (graphicsMagickPath != null) { convertCmd.setSearchPath(graphicsMagickPath); } try { convertCmd.run(operation); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IM4JavaException e) { e.printStackTrace(); } } else { ConvertCmd convertCmd = new ConvertCmd(false); if (imageMagickPath != null) { convertCmd.setSearchPath(imageMagickPath); } try { convertCmd.run(operation); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IM4JavaException e) { e.printStackTrace(); } } } }
From source file:com.el.ecom.utils.ImageUtils.java
/** * //from w w w . j a va 2 s . co m * * @param srcFile ? * @param destFile * @param destWidth * @param destHeight */ public static void zoom(File srcFile, File destFile, int destWidth, int destHeight) { Assert.notNull(srcFile); Assert.state(srcFile.exists()); Assert.state(srcFile.isFile()); Assert.notNull(destFile); Assert.state(destWidth > 0); Assert.state(destHeight > 0); if (type == Type.jdk) { Graphics2D graphics2D = null; ImageOutputStream imageOutputStream = null; ImageWriter imageWriter = null; try { BufferedImage srcBufferedImage = ImageIO.read(srcFile); int srcWidth = srcBufferedImage.getWidth(); int srcHeight = srcBufferedImage.getHeight(); int width = destWidth; int height = destHeight; if (srcHeight >= srcWidth) { width = (int) Math.round(((destHeight * 1.0 / srcHeight) * srcWidth)); } else { height = (int) Math.round(((destWidth * 1.0 / srcWidth) * srcHeight)); } BufferedImage destBufferedImage = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB); graphics2D = destBufferedImage.createGraphics(); graphics2D.setBackground(BACKGROUND_COLOR); graphics2D.clearRect(0, 0, destWidth, destHeight); graphics2D.drawImage(srcBufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), (destWidth / 2) - (width / 2), (destHeight / 2) - (height / 2), null); imageOutputStream = ImageIO.createImageOutputStream(destFile); imageWriter = ImageIO.getImageWritersByFormatName(FilenameUtils.getExtension(destFile.getName())) .next(); imageWriter.setOutput(imageOutputStream); ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam(); imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); imageWriteParam.setCompressionQuality((float) (DEST_QUALITY / 100.0)); imageWriter.write(null, new IIOImage(destBufferedImage, null, null), imageWriteParam); imageOutputStream.flush(); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } finally { if (graphics2D != null) { graphics2D.dispose(); } if (imageWriter != null) { imageWriter.dispose(); } try { if (imageOutputStream != null) { imageOutputStream.close(); } } catch (IOException e) { } } } else { IMOperation operation = new IMOperation(); operation.thumbnail(destWidth, destHeight); operation.gravity("center"); operation.background(toHexEncoding(BACKGROUND_COLOR)); operation.extent(destWidth, destHeight); operation.quality((double) DEST_QUALITY); try { operation.addImage(srcFile.getCanonicalPath()); operation.addImage(destFile.getCanonicalPath()); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } if (type == Type.graphicsMagick) { ConvertCmd convertCmd = new ConvertCmd(true); if (graphicsMagickPath != null) { convertCmd.setSearchPath(graphicsMagickPath); } try { convertCmd.run(operation); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } catch (InterruptedException e) { throw new RuntimeException(e.getMessage(), e); } catch (IM4JavaException e) { throw new RuntimeException(e.getMessage(), e); } } else { ConvertCmd convertCmd = new ConvertCmd(false); if (imageMagickPath != null) { convertCmd.setSearchPath(imageMagickPath); } try { convertCmd.run(operation); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } catch (InterruptedException e) { throw new RuntimeException(e.getMessage(), e); } catch (IM4JavaException e) { throw new RuntimeException(e.getMessage(), e); } } } }
From source file:org.apache.fop.render.pdf.pdfbox.PSPDFGraphics2D.java
private BufferedImage getImage(int width, int height, Image img, ImageObserver observer) { Dimension size = new Dimension(width, height); BufferedImage buf = buildBufferedImage(size); Graphics2D g = buf.createGraphics(); g.setComposite(AlphaComposite.SrcOver); g.setBackground(new Color(1, 1, 1, 0)); g.fillRect(0, 0, width, height);/*from ww w .j a va 2 s.c om*/ g.clip(new Rectangle(0, 0, buf.getWidth(), buf.getHeight())); if (!g.drawImage(img, 0, 0, observer)) { return null; } g.dispose(); return buf; }