List of usage examples for java.awt.image BufferedImage getGraphics
public java.awt.Graphics getGraphics()
From source file:com.baobao121.baby.common.SimpleUploaderServlet.java
public static void changeDimension(InputStream bis, FileOutputStream desc, int w, int h) throws IOException { Image src = javax.imageio.ImageIO.read(bis); // Image int width = src.getWidth(null); // ? int height = src.getHeight(null); // ? if (width <= w && height <= h) { BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(src, 0, 0, width, height, null); // ?? JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(desc); encoder.encode(tag);//from ww w .j a va 2 s .co m desc.flush(); desc.close(); bis.close(); } else { if (width != height) { if (w * height < h * width) { h = (int) (height * w / width); } else { w = (int) (width * h / height); } } BufferedImage tag = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(src, 0, 0, w, h, null); // ?? JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(desc); encoder.encode(tag); desc.flush(); desc.close(); bis.close(); } // JPEG? }
From source file:org.polymap.core.data.image.ImageTransparencyProcessor.java
public static BufferedImage transparency(Image image, final Color markerColor) throws IOException { long start = System.currentTimeMillis(); RGBImageFilter filter = new RGBImageFilter() { // the color we are looking for... Alpha bits are set to opaque public int markerRGB = markerColor.getRGB() | 0xFF000000; byte threshold = 25; double range = ((double) 0xFF) / (3 * threshold); public final int filterRGB(int x, int y, int rgb) { Color probe = new Color(rgb); //log.info( "probe=" + probe + ", marker=" + markerColor ); // delta values int dRed = markerColor.getRed() - probe.getRed(); int dGreen = markerColor.getGreen() - probe.getGreen(); int dBlue = markerColor.getBlue() - probe.getBlue(); //log.info( " dRed=" + dRed + ", dGreen=" + dGreen ); if (dRed >= 0 && dRed < threshold && dGreen >= 0 && dGreen < threshold && dBlue >= 0 && dBlue < threshold) { int alpha = (int) Math.round(range * (dRed + dGreen + dBlue)); //log.info( " -> alpha=" + alpha ); return ((alpha << 24) | 0x00FFFFFF) & rgb; } else { // nothing to do return rgb; }/*from ww w . jav a 2 s . c o m*/ } }; // BufferedImage bimage = null; // if (image instanceof BufferedImage) { // bimage = (BufferedImage)image; // } // else { // bimage = new BufferedImage( // image.getHeight( null ), image.getWidth( null ), BufferedImage.TYPE_INT_ARGB ); // Graphics g = bimage.getGraphics(); // g.drawImage( image, 0, 0, null ); // g.dispose(); // } ImageProducer ip = new FilteredImageSource(image.getSource(), filter); Image result = Toolkit.getDefaultToolkit().createImage(ip); BufferedImage bresult = new BufferedImage(image.getHeight(null), image.getWidth(null), BufferedImage.TYPE_INT_ARGB); Graphics g = bresult.getGraphics(); g.drawImage(result, 0, 0, null); g.dispose(); // // XXX this can surely be done any more clever // int width = bimage.getWidth(); // int height = bimage.getHeight(); // for (int x=bimage.getMinX(); x<width; x++) { // for (int y=bimage.getMinY(); y<height; y++) { // int filtered = filter.filterRGB( x, y, bimage.getRGB( x, y ) ); // result.setRGB( x, y, filtered ); // } // } log.debug("Transparency done. (" + (System.currentTimeMillis() - start) + "ms)"); return bresult; }
From source file:net.yacy.cora.util.Html2Image.java
/** * render a html page with a JEditorPane, which can do html up to html v 3.2. No CSS supported! * @param url/*from ww w . j a v a2 s.c om*/ * @param size * @throws IOException */ public static void writeSwingImage(String url, Dimension size, File destination) throws IOException { // set up a pane for rendering final JEditorPane htmlPane = new JEditorPane(); htmlPane.setSize(size); htmlPane.setEditable(false); final HTMLEditorKit kit = new HTMLEditorKit() { private static final long serialVersionUID = 1L; @Override public Document createDefaultDocument() { HTMLDocument doc = (HTMLDocument) super.createDefaultDocument(); doc.setAsynchronousLoadPriority(-1); return doc; } @Override public ViewFactory getViewFactory() { return new HTMLFactory() { @Override public View create(Element elem) { View view = super.create(elem); if (view instanceof ImageView) { ((ImageView) view).setLoadsSynchronously(true); } return view; } }; } }; htmlPane.setEditorKitForContentType("text/html", kit); htmlPane.setContentType("text/html"); htmlPane.addPropertyChangeListener(new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { } }); // load the page try { htmlPane.setPage(url); } catch (IOException e) { e.printStackTrace(); } // render the page Dimension prefSize = htmlPane.getPreferredSize(); BufferedImage img = new BufferedImage(prefSize.width, htmlPane.getPreferredSize().height, BufferedImage.TYPE_INT_ARGB); Graphics graphics = img.getGraphics(); htmlPane.setSize(prefSize); htmlPane.paint(graphics); ImageIO.write(img, destination.getName().endsWith("jpg") ? "jpg" : "png", destination); }
From source file:com.igormaznitsa.sciareto.ui.UiUtils.java
@Nonnull public static Image iconToImage(@Nonnull Component context, @Nullable final Icon icon) { if (icon instanceof ImageIcon) { return ((ImageIcon) icon).getImage(); }/*from w ww. ja va 2 s . c o m*/ final int width = icon == null ? 16 : icon.getIconWidth(); final int height = icon == null ? 16 : icon.getIconHeight(); final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); if (icon != null) { final Graphics g = image.getGraphics(); try { icon.paintIcon(context, g, 0, 0); } finally { g.dispose(); } } return image; }
From source file:org.jfree.experimental.swt.SWTUtils.java
/** * Converts an AWT image to SWT.//from w ww. j av a2s . c o m * * @param image the image (<code>null</code> not permitted). * * @return Image data. */ public static ImageData convertAWTImageToSWT(Image image) { ParamChecks.nullNotPermitted(image, "image"); int w = image.getWidth(null); int h = image.getHeight(null); if (w == -1 || h == -1) { return null; } BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics g = bi.getGraphics(); g.drawImage(image, 0, 0, null); g.dispose(); return convertToSWT(bi); }
From source file:com.reydentx.core.common.PhotoUtils.java
public static byte[] waterMarkJPG(String baseImagePath, String waterMartPath) { try {//from ww w.j a va2 s. com File origFile = new File(baseImagePath); ImageIcon icon = new ImageIcon(origFile.getPath()); BufferedImage bufferedImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB); Graphics graphics = bufferedImage.getGraphics(); graphics.drawImage(icon.getImage(), 0, 0, null); ImageIcon png = new ImageIcon(waterMartPath); graphics.drawImage(png.getImage(), 0, 0, null); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(bufferedImage, "jpg", baos); baos.flush(); byte[] imageInByte = baos.toByteArray(); return imageInByte; } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:net.sf.webphotos.tools.Thumbnail.java
private static Image estampar(Image im) { try {/*from ww w . java 2s. c om*/ Image temp = new ImageIcon(im).getImage(); BufferedImage buf = new BufferedImage(temp.getWidth(null), temp.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D g2 = (Graphics2D) buf.getGraphics(); g2.drawImage(temp, 0, 0, null); g2.setBackground(Color.BLUE); Dimension dimensaoFoto = new Dimension(im.getWidth(null), im.getHeight(null)); // aplicar texto if (texto != null) { Util.out.println("aplicando texto " + texto); Font fonte = new Font(txFamilia, txEstilo, txTamanho); g2.setFont(fonte); FontMetrics fm = g2.getFontMetrics(fonte); Dimension dimensaoTexto = new Dimension(fm.stringWidth(texto), fm.getHeight()); Point posTexto = calculaPosicao(dimensaoFoto, dimensaoTexto, txMargem, txPosicao); g2.setPaint(txCorFundo); g2.drawString(texto, (int) posTexto.getX() + 1, (int) posTexto.getY() + 1 + fm.getHeight()); g2.setPaint(txCorFrente); g2.drawString(texto, (int) posTexto.getX(), (int) posTexto.getY() + fm.getHeight()); } // aplicar marca dagua if (marcadagua != null) { Image marca = new ImageIcon(marcadagua).getImage(); int rule = AlphaComposite.SRC_OVER; float alpha = (float) mdTransparencia / 100; Point pos = calculaPosicao(dimensaoFoto, new Dimension(marca.getWidth(null), marca.getHeight(null)), mdMargem, mdPosicao); g2.setComposite(AlphaComposite.getInstance(rule, alpha)); g2.drawImage(marca, (int) pos.getX(), (int) pos.getY(), null); } g2.dispose(); //return (Image) buf; return Toolkit.getDefaultToolkit().createImage(buf.getSource()); } catch (Exception e) { Util.err.println("[Thumbnail.estampar]/ERRO: Inesperado - " + e.getMessage()); e.printStackTrace(Util.err); return im; } }
From source file:net.rptools.lib.image.ImageUtil.java
public static void clearImage(BufferedImage image) { if (image == null) return;// w w w. j av a2 s. c om 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:de.darkblue.bongloader2.utils.ToolBox.java
public static BufferedImage grayScale(BufferedImage image) { BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY); Graphics g = newImage.getGraphics(); g.drawImage(image, 0, 0, null);/*w ww . j a v a 2s . c om*/ g.dispose(); return newImage; }
From source file:com.t3.image.ImageUtil.java
public static void clearImage(BufferedImage image) { if (image == null) { return;//from ww w .j a va 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(); } } }