List of usage examples for java.awt.image BufferedImage getGraphics
public java.awt.Graphics getGraphics()
From source file:com.baidu.rigel.biplatform.ma.auth.resource.RandomValidateCode.java
/** * //from w ww . j a v a 2 s. c om * @param request * @param response * @param cacheManagerForResource */ public static void getRandcode(HttpServletRequest request, HttpServletResponse response, CacheManagerForResource cacheManagerForResource) { // BufferedImageImage,Image???? BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); Graphics g = image.getGraphics(); // ImageGraphics,????? g.fillRect(0, 0, width, height); g.setFont(new Font("Times New Roman", Font.ROMAN_BASELINE, 18)); g.setColor(getRandColor(110, 133)); // for (int i = 0; i <= lineSize; i++) { drowLine(g); } // ? String randomString = ""; for (int i = 1; i <= stringNum; i++) { randomString = drowString(g, randomString, i); } String key = null; if (request.getCookies() != null) { for (Cookie tmp : request.getCookies()) { if (tmp.getName().equals(Constants.RANDOMCODEKEY)) { key = tmp.getName(); cacheManagerForResource.removeFromCache(key); break; } } } if (StringUtils.isEmpty(key)) { key = String.valueOf(System.nanoTime()); } cacheManagerForResource.setToCache(key, randomString); final Cookie cookie = new Cookie(Constants.RANDOMCODEKEY, key); cookie.setPath(Constants.COOKIE_PATH); response.addCookie(cookie); g.dispose(); try { ImageIO.write(image, "JPEG", response.getOutputStream()); // ?? } catch (Exception e) { LOG.info(e.getMessage()); } }
From source file:edu.stanford.muse.webapp.HTMLToImage.java
/** * Convert an HTML page at the specified URL into an image who's data is * written to the provided output stream. * * @param url URL to the page that is to be imaged. * @param os An output stream that is to be opened for writing. Image * data will be written to the provided stream. The stream * will not be closed under any circumstances by this method. * @param width The desired width of the image that will be created. * @param height The desired height of the image that will be created. * * @returns true if the page at the provided URL was loaded, converted to an * image, and the image data has been written to the output stream, * false if an error has ocurred along the way. * * @throws HTMLImagerException if an error has ocurred. *///from w w w . ja v a 2 s . c o m public static boolean image(String url, OutputStream os, int width, int height) { if (log.isDebugEnabled()) log.debug("Imaging url '" + url + "'."); boolean successful = false; try { HttpClient httpClient = new HttpClient(); GetMethod getMethod = new GetMethod(url); httpClient.executeMethod(getMethod); int httpStatus = getMethod.getStatusCode(); if (httpStatus == HttpServletResponse.SC_OK) { Tidy tidy = new Tidy(); tidy.setQuiet(true); tidy.setXHTML(true); tidy.setHideComments(true); tidy.setInputEncoding("UTF-8"); tidy.setOutputEncoding("UTF-8"); tidy.setShowErrors(0); tidy.setShowWarnings(false); Document doc = tidy.parseDOM(getMethod.getResponseBodyAsStream(), null); if (doc != null) { BufferedImage buf = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = (Graphics2D) buf.getGraphics(); Graphics2DRenderer renderer = new Graphics2DRenderer(); SharedContext context = renderer.getSharedContext(); UserAgentCallback userAgent = new HTMLImagerUserAgent(url); context.setUserAgentCallback(userAgent); context.setNamespaceHandler(new XhtmlNamespaceHandler()); renderer.setDocument(doc, url); renderer.layout(graphics, new Dimension(width, height)); renderer.render(graphics); graphics.dispose(); /* JPEGEncodeParam param = JPEGCodec.getDefaultJPEGEncodeParam( buf ); param.setQuality( (float)1.0, false ); JPEGImageEncoder imageEncoder = JPEGCodec.createJPEGEncoder( os, param ); imageEncoder.encode( buf ); */ successful = true; } else { if (log.isDebugEnabled()) log.debug("Unable to image URL '" + url + "'. The HTML that was returned could not be tidied."); } } else { if (log.isDebugEnabled()) log.debug("Unable to image URL '" + url + "'. Server returned status code '" + httpStatus + "'."); } } catch (Exception e) { throw new RuntimeException("Unable to image URL '" + url + "'.", e); } return successful; }
From source file:at.gv.egiz.pdfas.common.utils.ImageUtils.java
public static BufferedImage convertRGBAToIndexed(BufferedImage src) { BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_INDEXED); Graphics g = dest.getGraphics(); g.setColor(new Color(231, 20, 189)); g.fillRect(0, 0, dest.getWidth(), dest.getHeight()); // fill with a // hideous color // and make it // transparent dest = makeTransparent(dest, 0, 0);//from w w w . j av a2 s . c o m dest.createGraphics().drawImage(src, 0, 0, null); return dest; }
From source file:net.roboconf.doc.generator.internal.GraphUtils.java
/** * Computes the width of a shape for a given component or facet. * @param type a type/*from w w w .ja v a 2 s . c o m*/ * @return the width it should take once displayed as a graph vertex */ public static int computeShapeWidth(AbstractType type) { Font font = GraphUtils.getDefaultFont(); BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB); FontMetrics fm = img.getGraphics().getFontMetrics(font); int width = fm.stringWidth(type.getName()); width = Math.max(width, 80) + 20; return width; }
From source file:com.openkm.util.ImageUtils.java
/** * cloneImage//from w w w . j a va2s . c o m */ public static BufferedImage clone(BufferedImage source) { BufferedImage img = new BufferedImage(source.getWidth(), source.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics g = img.getGraphics(); g.drawImage(source, 0, 0, null); g.dispose(); return img; }
From source file:org.spf4j.perf.impl.chart.Charts.java
public static BufferedImage createMinMaxAvgCountImg(final String chartName, final long[] timestamps, final double[] min, final double[] max, final double[] total, final double[] count, final String uom, final int width, final int height) { BufferedImage bi = Charts//from w w w. j a v a 2 s .c o m .createTimeSeriesJFreeChart(chartName, timestamps, new String[] { "min", "max", "avg" }, uom, new double[][] { min, max, Arrays.divide(total, count) }) .createBufferedImage(width, height - height / 3); BufferedImage bi2 = Charts.createTimeSeriesJFreeChart(null, timestamps, new String[] { "count" }, "count", new double[][] { count }).createBufferedImage(width, height / 3); BufferedImage combined = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); final Graphics graphics = combined.getGraphics(); try { graphics.drawImage(bi, 0, 0, null); graphics.drawImage(bi2, 0, height - height / 3, null); } finally { graphics.dispose(); } return combined; }
From source file:org.spf4j.perf.impl.chart.Charts.java
public static BufferedImage generateCountTotalChart(final String groupName, final long[][] timestamps, final String[] measurementNames, final String uom1, final double[][] measurements, final int width, final int height, final String[] measurementNames2, final String uom2, final double[][] measurements2) { BufferedImage count = Charts//from ww w .j av a 2 s . co m .createTimeSeriesJFreeChart("Measurements for " + groupName + " generated by spf4j", timestamps, measurementNames, uom1, measurements) .createBufferedImage(width, height / 2); BufferedImage total = Charts .createTimeSeriesJFreeChart(null, timestamps, measurementNames2, uom2, measurements2) .createBufferedImage(width, height / 2); BufferedImage combined = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); final Graphics graphics = combined.getGraphics(); try { graphics.drawImage(count, 0, 0, null); graphics.drawImage(total, 0, height / 2, null); } finally { graphics.dispose(); } return combined; }
From source file:com.shending.support.CompressPic.java
/** * ?/* w w w .ja va 2s. c om*/ * * @param srcFile * @param dstFile * @param widthRange * @param heightRange */ public static void cutSquare(String srcFile, String dstFile, int widthRange, int heightRange, int width, int height) { int x = 0; int y = 0; try { ImageInputStream iis = ImageIO.createImageInputStream(new File(srcFile)); Iterator<ImageReader> iterator = ImageIO.getImageReaders(iis); ImageReader reader = (ImageReader) iterator.next(); reader.setInput(iis, true); ImageReadParam param = reader.getDefaultReadParam(); int oldWidth = reader.getWidth(0); int oldHeight = reader.getHeight(0); int newWidth, newHeight; if (width <= oldWidth && height <= oldHeight) { newWidth = oldHeight * widthRange / heightRange; if (newWidth < oldWidth) { newHeight = oldHeight; x = (oldWidth - newWidth) / 2; } else { newWidth = oldWidth; newHeight = oldWidth * heightRange / widthRange; y = (oldHeight - newHeight) / 2; } Rectangle rectangle = new Rectangle(x, y, newWidth, newHeight); param.setSourceRegion(rectangle); BufferedImage bi = reader.read(0, param); BufferedImage tag = new BufferedImage((int) width, (int) height, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(bi.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null); File file = new File(dstFile); ImageIO.write(tag, reader.getFormatName(), file); } else { BufferedImage bi = reader.read(0, param); BufferedImage tag = new BufferedImage((int) width, (int) height, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = tag.createGraphics(); g2d.setColor(Color.WHITE); g2d.fillRect(0, 0, tag.getWidth(), tag.getHeight()); g2d.drawImage(bi.getScaledInstance(bi.getWidth(), bi.getHeight(), Image.SCALE_SMOOTH), (width - bi.getWidth()) / 2, (height - bi.getHeight()) / 2, null); g2d.dispose(); File file = new File(dstFile); ImageIO.write(tag, reader.getFormatName(), file); } } catch (Exception e) { e.printStackTrace(); } }
From source file:com.spstudio.common.image.ImageUtils.java
public static BufferedImage zoom(BufferedImage sourceImage, int width, int height) { BufferedImage zoomImage = new BufferedImage(width, height, sourceImage.getType()); Image image = sourceImage.getScaledInstance(width, height, Image.SCALE_SMOOTH); Graphics gc = zoomImage.getGraphics(); gc.setColor(Color.WHITE);//w w w.j a va 2 s. c o m gc.drawImage(image, 0, 0, null); return zoomImage; }
From source file:com.vaadin.testbench.screenshot.ImageUtil.java
/** * Clones the given BufferedImage/*w ww . j av a2s . c o m*/ * * @param sourceImage * The image to copy * @return A copy of sourceImage */ public static BufferedImage cloneImage(BufferedImage sourceImage) { // This method could likely be optimized but the gain is probably // small final int w = sourceImage.getWidth(); final int h = sourceImage.getHeight(); BufferedImage newImage = new BufferedImage(w, h, TYPE_INT_RGB); Graphics2D g = (Graphics2D) newImage.getGraphics(); g.drawImage(sourceImage, 0, 0, w, h, null); g.dispose(); return newImage; }