List of usage examples for java.awt Graphics2D setComposite
public abstract void setComposite(Composite comp);
From source file:ch.rasc.downloadchart.DownloadChartServlet.java
private static void handleJpg(HttpServletResponse response, byte[] imageData, Integer width, Integer height, String filename, JpegOptions options) throws IOException { response.setContentType("image/jpeg"); response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + ".jpg\";"); BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageData)); Dimension newDimension = calculateDimension(image, width, height); int imgWidth = image.getWidth(); int imgHeight = image.getHeight(); if (newDimension != null) { imgWidth = newDimension.width;/*from w ww. j a v a2 s.c o m*/ imgHeight = newDimension.height; } BufferedImage newImage = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB); Graphics2D g = newImage.createGraphics(); g.drawImage(image, 0, 0, imgWidth, imgHeight, Color.BLACK, null); g.dispose(); if (newDimension != null) { g.setComposite(AlphaComposite.Src); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); } try (ImageOutputStream ios = ImageIO.createImageOutputStream(response.getOutputStream())) { Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpg"); ImageWriter writer = iter.next(); ImageWriteParam iwp = writer.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); if (options != null && options.quality != null && options.quality != 0 && options.quality != 100) { iwp.setCompressionQuality(options.quality / 100f); } else { iwp.setCompressionQuality(1); } writer.setOutput(ios); writer.write(null, new IIOImage(newImage, null, null), iwp); writer.dispose(); } }
From source file:com.zacwolf.commons.email.Email.java
public static BufferedImage makeRoundedBanner(BufferedImage image, int cornerRadius) { int w = image.getWidth(); int h = image.getHeight() + 10; BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = output.createGraphics(); g2.setComposite(AlphaComposite.Src); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(Color.WHITE);/*ww w . j av a 2 s . c o m*/ g2.fill(new RoundRectangle2D.Float(0, 0, w, h, cornerRadius, cornerRadius)); g2.setComposite(AlphaComposite.SrcAtop); g2.drawImage(image, 0, 0, null); g2.setComposite(AlphaComposite.SrcOver); // g2.setColor(new Color(153,153,153));//slight grey border // g2.drawRoundRect(0, 0, w-1, h, cornerRadius, cornerRadius); g2.dispose(); return output.getSubimage(0, 0, image.getWidth(), image.getHeight()); }
From source file:com.zacwolf.commons.email.Email.java
public static BufferedImage makeRoundedFooter(int width, int cornerRadius, Color bgcolor, Color border) throws Exception { int height = (cornerRadius * 2) + 10; BufferedImage output = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = output.createGraphics(); g2.setComposite(AlphaComposite.Src); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(bgcolor);//from w ww . j a v a 2 s . c om g2.fillRoundRect(0, 0, width, height - 1, cornerRadius, cornerRadius); g2.setComposite(AlphaComposite.SrcOver); if (border != null) { g2.setColor(border); g2.drawRoundRect(0, 0, width - 1, height - 2, cornerRadius, cornerRadius); } g2.dispose(); Rectangle clip = createClip(output, new Dimension(width, cornerRadius), 0, height - cornerRadius - 1); return output.getSubimage(clip.x, clip.y, clip.width, clip.height); }
From source file:net.rptools.lib.image.ImageUtil.java
public static void clearImage(BufferedImage image) { if (image == null) return;//from w w w . j a v a 2s. co m Graphics2D g = null; try { g = (Graphics2D) image.getGraphics(); Composite oldComposite = g.getComposite(); g.setComposite(AlphaComposite.Clear); g.fillRect(0, 0, image.getWidth(), image.getHeight()); g.setComposite(oldComposite); } finally { if (g != null) { g.dispose(); } } }
From source file:com.t3.image.ImageUtil.java
public static void clearImage(BufferedImage image) { if (image == null) { return;/*from w ww . ja v a 2 s. co m*/ } Graphics2D g = null; try { g = (Graphics2D) image.getGraphics(); Composite oldComposite = g.getComposite(); g.setComposite(AlphaComposite.Clear); g.fillRect(0, 0, image.getWidth(), image.getHeight()); g.setComposite(oldComposite); } finally { if (g != null) { g.dispose(); } } }
From source file:com.cmart.PageControllers.SellItemImagesController.java
private static BufferedImage resizeImageWithHint(BufferedImage originalImage, int type, int width, int height) { BufferedImage resizedImage = new BufferedImage(width, width, type); Graphics2D g = resizedImage.createGraphics(); g.drawImage(originalImage, 0, 0, width, width, null); g.dispose();//from www.j a v a2 s. c o m g.setComposite(AlphaComposite.Src); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); return resizedImage; }
From source file:nl.b3p.viewer.image.ImageTool.java
/** * Draws the image to the graphics object. * @param gbi graphics object//from ww w . ja v a2 s.c o m * @param image the referenced image. */ private static void drawImage(Graphics2D gbi, ReferencedImage image) { if (image.getAlpha() != null) { gbi.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, image.getAlpha())); } else { gbi.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f)); } Integer x = image.getX(); Integer y = image.getY(); if (x == null) { x = 0; } if (y == null) { y = 0; } if (image.getHeight() != null && image.getWidth() != null) { gbi.drawImage(image.getImage(), x, y, image.getWidth(), image.getHeight(), null); } else { gbi.drawImage(image.getImage(), x, y, null); } }
From source file:nl.b3p.imagetool.ImageTool.java
/** * Draws the image to the graphics object. * * @param gbi graphics object//from w ww .j a va 2 s . co m * @param image the referenced image. */ private static void drawImage(Graphics2D gbi, ReferencedImage image) throws Exception { if (image.getAlpha() != null) { gbi.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, image.getAlpha())); } else { gbi.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f)); } Integer x = image.getX(); Integer y = image.getY(); if (x == null) { x = 0; } if (y == null) { y = 0; } if (image.getHeight() != null && image.getWidth() != null) { gbi.drawImage(image.getImage(), x, y, image.getWidth(), image.getHeight(), null); } else { gbi.drawImage(image.getImage(), x, y, null); } }
From source file:net.shopxx.util.ImageUtil.java
/** * ?/* w w w .j av a 2 s. c om*/ * @param srcFile ? * @param destFile * @param watermarkFile ? * @param watermarkPosition ?? * @param alpha ?? */ public static void addWatermark(File srcFile, File destFile, File watermarkFile, WatermarkPosition watermarkPosition, int alpha) { Assert.notNull(srcFile); Assert.notNull(destFile); Assert.state(alpha >= 0); Assert.state(alpha <= 100); if (watermarkFile == null || !watermarkFile.exists() || watermarkPosition == null || watermarkPosition == WatermarkPosition.no) { return; } if (type == Type.jdk) { try { BufferedImage srcBufferedImage = ImageIO.read(srcFile); int srcWidth = srcBufferedImage.getWidth(); int srcHeight = srcBufferedImage.getHeight(); BufferedImage destBufferedImage = new BufferedImage(srcWidth, srcHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = destBufferedImage.createGraphics(); graphics2D.setBackground(BACKGROUND_COLOR); graphics2D.clearRect(0, 0, srcWidth, srcHeight); graphics2D.drawImage(srcBufferedImage, 0, 0, null); graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha / 100.0F)); BufferedImage watermarkBufferedImage = ImageIO.read(watermarkFile); int watermarkImageWidth = watermarkBufferedImage.getWidth(); int watermarkImageHeight = watermarkBufferedImage.getHeight(); int x = srcWidth - watermarkImageWidth; int y = srcHeight - watermarkImageHeight; if (watermarkPosition == WatermarkPosition.topLeft) { x = 0; y = 0; } else if (watermarkPosition == WatermarkPosition.topRight) { x = srcWidth - watermarkImageWidth; y = 0; } else if (watermarkPosition == WatermarkPosition.center) { x = (srcWidth - watermarkImageWidth) / 2; y = (srcHeight - watermarkImageHeight) / 2; } else if (watermarkPosition == WatermarkPosition.bottomLeft) { x = 0; y = srcHeight - watermarkImageHeight; } else if (watermarkPosition == WatermarkPosition.bottomRight) { x = srcWidth - watermarkImageWidth; y = srcHeight - watermarkImageHeight; } graphics2D.drawImage(watermarkBufferedImage, x, y, watermarkImageWidth, watermarkImageHeight, null); graphics2D.dispose(); FileOutputStream out = new FileOutputStream(destFile); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(destBufferedImage); param.setQuality((float) DEST_QUALITY / 100, false); encoder.setJPEGEncodeParam(param); encoder.encode(destBufferedImage); out.close(); } catch (IOException e) { e.printStackTrace(); } } else { String gravity = "SouthEast"; if (watermarkPosition == WatermarkPosition.topLeft) { gravity = "NorthWest"; } else if (watermarkPosition == WatermarkPosition.topRight) { gravity = "NorthEast"; } else if (watermarkPosition == WatermarkPosition.center) { gravity = "Center"; } else if (watermarkPosition == WatermarkPosition.bottomLeft) { gravity = "SouthWest"; } else if (watermarkPosition == WatermarkPosition.bottomRight) { gravity = "SouthEast"; } IMOperation operation = new IMOperation(); operation.gravity(gravity); operation.dissolve(alpha); operation.quality((double) DEST_QUALITY); operation.addImage(watermarkFile.getPath()); operation.addImage(srcFile.getPath()); operation.addImage(destFile.getPath()); if (type == Type.graphicsMagick) { CompositeCmd compositeCmd = new CompositeCmd(true); if (graphicsMagickPath != null) { compositeCmd.setSearchPath(graphicsMagickPath); } try { compositeCmd.run(operation); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IM4JavaException e) { e.printStackTrace(); } } else { CompositeCmd compositeCmd = new CompositeCmd(false); if (imageMagickPath != null) { compositeCmd.setSearchPath(imageMagickPath); } try { compositeCmd.run(operation); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IM4JavaException e) { e.printStackTrace(); } } } }
From source file:nl.b3p.viewer.image.ImageTool.java
public static BufferedImage drawGeometries(BufferedImage bi, CombineImageSettings settings, int srid, Bbox bbox, int width, int height) throws Exception { List wktGeoms = settings.getWktGeoms(); if (wktGeoms == null || wktGeoms.size() <= 0) { return bi; }// w w w.j ava 2s.c om BufferedImage newBufIm = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE); // BufferedImage newBufIm = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D gbi = newBufIm.createGraphics(); gbi.drawImage(bi, 0, 0, null); for (int i = 0; i < wktGeoms.size(); i++) { gbi.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f)); CombineImageWkt ciw = (CombineImageWkt) wktGeoms.get(i); Color color = settings.getDefaultWktGeomColor(); if (ciw.getColor() != null) { color = ciw.getColor(); } gbi.setColor(color); String wktGeom = ciw.getWktGeom(); Geometry geom = geometrieFromText(wktGeom, srid); Shape shape = createImage(geom, srid, bbox, width, height); Point centerPoint = null; if (geom instanceof Polygon) { gbi.fill(shape); } else if (geom instanceof com.vividsolutions.jts.geom.Point) { centerPoint = calculateCenter(shape, srid, bbox, width, height); gbi.fill(new Ellipse2D.Double(centerPoint.getX(), centerPoint.getY(), 8, 8)); } else { float strokeWidth = ciw.getStrokeWidth() != null ? ciw.getStrokeWidth() : 3f; gbi.setStroke(new BasicStroke(strokeWidth)); gbi.draw(shape); } if (ciw.getLabel() != null) { if (centerPoint == null) { centerPoint = calculateCenter(shape, srid, bbox, width, height); } gbi.setColor(Color.black); gbi.drawString(ciw.getLabel(), (float) centerPoint.getX(), (float) centerPoint.getY()); } } gbi.dispose(); return newBufIm; }