/** * All pixels that have the specified color are rendered transparent. * * @param img * the img * @param color * the color * @return the image */ public static Image applyAlphaChannel(final Image img, final Color color) { if (color == null || img == null) { return img; } final ImageFilter filter = new RGBImageFilter() { // the color we are looking for... Alpha bits are set to opaque public final int markerRGB = color.getRGB() | 0xFF000000; @Override public final int filterRGB(final int x, final int y, final int rgb) { if ((rgb | 0xFF000000) == this.markerRGB) { // Mark the alpha bits as zero - transparent return 0x00FFFFFF & rgb; } else { // nothing to do return rgb; } } }; final ImageProducer ip = new FilteredImageSource(img.getSource(), filter); return Toolkit.getDefaultToolkit().createImage(ip); }
public BufferedImage getGrayscaledImage(BufferedImage coloredImage) { ImageFilter filter = new ImageFilter(){ public final int filterRGB(int x, int y, int rgb) { //TODO - optimization? Bit shifts, not this shits Color currentColor = new Color(rgb); if(currentColor.getRed() < 2 && currentColor.getGreen() < 2 && currentColor.getBlue() < 2) { return new Color(rgb).darker().getRGB(); } return Color.WHITE.getRGB(); } }; ImageProducer producer = new FilteredImageSource(coloredImage.getSource(), filter); Image image = Toolkit.getDefaultToolkit().createImage(producer); return toBufferedImage(image); }
/** * {@inheritDoc} * * @since 1.6 */ public Icon getDisabledIcon(JComponent component, Icon icon) { // if the component has a HI_RES_DISABLED_ICON_CLIENT_KEY // client property set to Boolean.TRUE, then use the new // hi res algorithm for creating the disabled icon (used // in particular by the WindowsFileChooserUI class) if (icon != null && component != null && Boolean.TRUE.equals(component.getClientProperty(HI_RES_DISABLED_ICON_CLIENT_KEY)) && icon.getIconWidth() > 0 && icon.getIconHeight() > 0) { BufferedImage img = new BufferedImage(icon.getIconWidth(), icon.getIconWidth(), BufferedImage.TYPE_INT_ARGB); icon.paintIcon(component, img.getGraphics(), 0, 0); ImageFilter filter = new RGBGrayFilter(); ImageProducer producer = new FilteredImageSource(img.getSource(), filter); Image resultImage = component.createImage(producer); return new ImageIconUIResource(resultImage); } return super.getDisabledIcon(component, icon); }
public static Image makeColorTransparent(Image im, final Color color) { //(C) //Copiado da internet: 13/02/2011 - http://www.rgagnon.com/javadetails/java-0265.html e http://www.coderanch.com/t/331731/GUI/java/Resize-ImageIcon // ImageFilter filter = new RGBImageFilter() { // the color we are looking for... Alpha bits are set to opaque public int markerRGB = color.getRGB() | 0xFF000000; @Override public final int filterRGB(int x, int y, int rgb) { if ((rgb | 0xFF000000) == markerRGB) { // Mark the alpha bits as zero - transparent return 0x00FFFFFF & rgb; } else { // nothing to do return rgb; } } }; ImageProducer ip = new FilteredImageSource(im.getSource(), filter); return Toolkit.getDefaultToolkit().createImage(ip); }
@SuppressWarnings("deprecation") protected void buildInitialFactories() { int minWordLength = 7; int maxWordLength = 7; int fontSize = 36; int imageWidth = 300; int imageHeight = 90; WordGenerator wordGenerator = new RandomWordGenerator("0123456789ABCDEFGHIJKLTYREWSZabcdefghjkmnpqrstuvwxyz"); TextPaster randomPaster = new DecoratedRandomTextPaster( minWordLength, maxWordLength, new RandomListColorGenerator(new Color[]{ new Color(23, 170, 27), new Color(220, 34, 11), new Color(23, 67, 172)}), new TextDecorator[]{}); BackgroundGenerator background = new UniColorBackgroundGenerator(imageWidth, imageHeight, Color.GREEN); FontGenerator font = new RandomFontGenerator(fontSize, fontSize, new Font[]{new Font("Calibri", Font.BOLD, fontSize), new Font("Times New Roman", Font.PLAIN, fontSize), new Font("Arial", Font.BOLD, fontSize)}); ImageDeformation postDef = new ImageDeformationByFilters(new ImageFilter[]{}); ImageDeformation backDef = new ImageDeformationByFilters(new ImageFilter[]{}); ImageDeformation textDef = new ImageDeformationByFilters(new ImageFilter[]{}); WordToImage word2image = new DeformedComposedWordToImage(font, background, randomPaster, backDef, textDef, postDef); addFactory(new GimpyFactory(wordGenerator, word2image)); }
public static BufferedImage makeColorTransparent(BufferedImage im, final Color color) { ImageFilter filter = new RGBImageFilter() { public int markerRGB = color.getRGB() | 0xFF000000; public final int filterRGB(int x, int y, int rgb) { if ((rgb | 0xFF000000) == markerRGB) { return 0x00FFFFFF & rgb; } else { return rgb; } } }; return imageToBufferedImage( Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(im.getSource(), filter))); }
/** * 图像切割(指定切片的宽度和高度) * @param bi 原图像 * @param x 裁剪原图像起点坐标X * @param y 裁剪原图像起点坐标Y * @param width 目标切片宽度 * @param height 目标切片高度 * @return */ public static BufferedImage cut(BufferedImage bi,int x, int y, int width, int height) { BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = tag.createGraphics(); tag = g2.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT); g2.dispose(); g2 = tag.createGraphics(); int srcWidth = bi.getHeight(); // 源图宽度 int srcHeight = bi.getWidth(); // 源图高度 if (srcWidth > 0 && srcHeight > 0) { ImageFilter cropFilter = new CropImageFilter(x, y, width, height); Image img = Toolkit.getDefaultToolkit().createImage( new FilteredImageSource(bi.getSource(),cropFilter)); g2.drawImage(img, 0, 0, width, height, null); // 绘制切割后的图 g2.dispose(); } return tag; }
/** * 图像切割(按指定起点坐标和宽高切割) * @param srcImg 源图像地址 * @param outImg 切片后的图像地址 * @param x 目标切片起点坐标X * @param y 目标切片起点坐标Y * @param width 目标切片宽度 * @param height 目标切片高度 * @param imageType 图片类型 */ public static void cut(File srcImg, File outImg, int x, int y, int width, int height,String imageType) throws Exception{ //读取源图像 BufferedImage bi = ImageIO.read(srcImg); int srcWidth = bi.getHeight();//源图宽度 int srcHeight = bi.getWidth();//源图高度 if (srcWidth > 0 && srcHeight > 0) { Image image = bi.getScaledInstance(srcWidth, srcHeight,Image.SCALE_DEFAULT); //四个参数分别为图像起点坐标和宽高 //即:CropImageFilter(int x,int y,int width,int height) ImageFilter cropFilter = new CropImageFilter(x, y, width,height); Image img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), cropFilter)); BufferedImage tag = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB); Graphics g = tag.getGraphics(); g.drawImage(img, 0, 0, width, height, null);//绘制切割后的图 g.dispose(); //输出为文件 ImageIO.write(tag, imageType, outImg); } }
public static Image makeColorTransparent(final BufferedImage im, final Color color) { final ImageFilter filter = new RGBImageFilter() { // the color we are looking for... Alpha bits are set to opaque public int markerRGB = color.getRGB() | 0xFFFFFFFF; public final int filterRGB(final int x, final int y, final int rgb) { if ((rgb | 0xFF000000) == markerRGB) { // Mark the alpha bits as zero - transparent return 0x00FFFFFF & rgb; } else { // nothing to do return rgb; } } }; final ImageProducer ip = new FilteredImageSource(im.getSource(), filter); return Toolkit.getDefaultToolkit().createImage(ip); }
/** * * @param im * @param color * @return */ public static Image setTransparentColor(BufferedImage im, final Color color) { ImageFilter filter = new RGBImageFilter() { // the color we are looking for... Alpha bits are set to opaque public int markerRGB = color.getRGB() | 0xFF000000; public final int filterRGB(int x, int y, int rgb) { if ((rgb | 0xFF000000) == markerRGB) { // Mark the alpha bits as zero - transparent return 0x00FFFFFF & rgb; } else { // nothing to do return rgb; } } }; ImageProducer ip = new FilteredImageSource(im.getSource(), filter); return Toolkit.getDefaultToolkit().createImage(ip); }
public static Image makeColorTransparent(BufferedImage im, final Color color) { ImageFilter filter = new RGBImageFilter() { // the color we are looking for... Alpha bits are set to opaque @Override public int filterRGB(int x, int y, int rgb) { int markerRGB = color.getRGB() | 0xFF000000; if ((rgb | 0xFF000000) == markerRGB) { // Mark the alpha bits as zero - transparent return 0x00FFFFFF & rgb; } else { // nothing to do return rgb; } } }; ImageProducer ip = new FilteredImageSource(im.getSource(), filter); return Toolkit.getDefaultToolkit().createImage(ip); }
private Image transformColorToTransparency(BufferedImage image, Color color) { ImageFilter filter = new RGBImageFilter() { public final int filterRGB(int x, int y, int rgb) { if (rgb == color.getRGB()) { return new Color(0, 0, 0, 0).getRGB(); } else { return rgb; } } }; ImageProducer ip = new FilteredImageSource(image.getSource(), filter); return Toolkit.getDefaultToolkit().createImage(ip); }
/** * Make a color of a image transparent * * @param im The image * @param color The color * @return Result image */ public static Image makeColorTransparent(BufferedImage im, final Color color) { ImageFilter filter = new RGBImageFilter() { // the color we are looking for... Alpha bits are set to opaque public int markerRGB = color.getRGB() | 0xFF000000; @Override public final int filterRGB(int x, int y, int rgb) { if ((rgb | 0xFF000000) == markerRGB) { // Mark the alpha bits as zero - transparent return 0x00FFFFFF & rgb; } else { // nothing to do return rgb; } } }; ImageProducer ip = new FilteredImageSource(im.getSource(), filter); return Toolkit.getDefaultToolkit().createImage(ip); }
public static Image makeColorTransparent(Image im, final Color color) { ImageFilter filter = new RGBImageFilter() { // the color we are looking for... Alpha bits are set to opaque public int markerRGB = color.getRGB() | 0xFF000000; public final int filterRGB(int x, int y, int rgb) { if ((rgb | 0xFF000000) == markerRGB) { // Mark the alpha bits as zero - transparent return 0x00FFFFFF & rgb; } else { // nothing to do return rgb; } } }; ImageProducer ip = new FilteredImageSource(im.getSource(), filter); return Toolkit.getDefaultToolkit().createImage(ip); }
public static Image makeColorTransparent(Image im, final Color color, final int alpha) { final ImageFilter filter = new RGBImageFilter() { // the color we are looking for... Alpha bits are set to opaque public int markerRGB = color.getRGB() | 0xFF000000; public final int filterRGB(int x, int y, int rgb) { if ( ( rgb | 0xFF000000 ) == markerRGB ) { // Mark the alpha bits as zero - transparent return (0x00FFFFFF | (alpha << 24)) & rgb; } else { // nothing to do return rgb; } } }; ImageProducer ip = new FilteredImageSource(im.getSource(), filter); return Toolkit.getDefaultToolkit().createImage(ip); }
/** * * Credits go to http://stackoverflow.com/a/665483 and * http://www.logikdev.com/2011/10/05/make-image-backgrounds-transparent-with-tolerance/ * * @param im * @param colorToMakeTransparent * @param tolerance * * @return */ public static Image makeColorTransparent(final BufferedImage im, final Color colorToMakeTransparent, final int tolerance) { final ImageFilter transparencyfilter = new RGBImageFilter() { @Override public int filterRGB(int x, int y, int rgb) { final Color filterColor = new Color(rgb); if(colorsAreSimilar(filterColor, colorToMakeTransparent, tolerance)) { // Mark the alpha bits as zero - transparent return 0x00FFFFFF & rgb; } else { // Nothing to do return rgb; } } }; final ImageProducer ip = new FilteredImageSource(im.getSource(), transparencyfilter); return Toolkit.getDefaultToolkit().createImage(ip); }
/** * * * @param icon * @return */ protected javafx.scene.image.Image getGreyscaleIcon(Icon icon) { javafx.scene.image.Image fxImage = null; try { BufferedImage bf = ImageIO.read(this.getClass().getResource(icon.getFileName())); ImageFilter filter = new GrayFilter(true, 70); ImageProducer producer = new FilteredImageSource(bf.getSource(), filter); Image mage = Toolkit.getDefaultToolkit().createImage(producer); fxImage = createImage(mage); } catch (Exception e) { // TODO: logging e.printStackTrace(); } return fxImage; }
/** * Make provided image transparent wherever color matches the provided * color. * * @param im * BufferedImage whose color will be made transparent. * @param color * Color in provided image which will be made transparent. * @return Image with transparency applied. */ public static Image makeColorTransparent(final BufferedImage im, final Color color) { final ImageFilter filter = new RGBImageFilter() { // the color we are looking for (white)... Alpha bits are set to // opaque public int markerRGB = color.getRGB() | 0xFFFFFFFF; public final int filterRGB(final int x, final int y, final int rgb) { if ((rgb | 0xFF000000) == markerRGB) { // Mark the alpha bits as zero - transparent return 0x00FFFFFF & rgb; } else { // nothing to do return rgb; } } }; final ImageProducer ip = new FilteredImageSource(im.getSource(), filter); return Toolkit.getDefaultToolkit().createImage(ip); }
public static Image makeTransparentWhite(BufferedImage im, final Color color){ ImageFilter filter = new RGBImageFilter() { // the color we are looking for... Alpha bits are set to opaque public int markerRGB = 0x00FFFFFF; public final int filterRGB(int x, int y, int rgb) { if ((rgb & 0x00FFFFFF) == markerRGB) { // Mark the alpha bits as zero - transparent return color.getRGB();// & 0xFFFFFFFF; } else { // nothing to do return rgb; } } }; ImageProducer ip = new FilteredImageSource(im.getSource(), filter); return Toolkit.getDefaultToolkit().createImage(ip); }
public static Image makeColorTransparent(BufferedImage im, final Color color) { ImageFilter filter = new RGBImageFilter() { // the color we are looking for... Alpha bits are set to opaque public int markerRGB = color.getRGB() | 0xFF000000; public final int filterRGB(int x, int y, int rgb) { if ((rgb | 0xFF000000) == markerRGB) { // Mark the alpha bits as zero - transparent return 0x00FFFFFF & rgb; } else { // nothing to do return rgb; } } }; ImageProducer ip = new FilteredImageSource(im.getSource(), filter); return Toolkit.getDefaultToolkit().createImage(ip); }
/** * Make a color of the image transparent. * * @param bufferedImageToProcess the image to extract the color from. * @param colorToMakeTransparent the color to make transparent. * @return the new image. */ public static BufferedImage makeColorTransparent( BufferedImage bufferedImageToProcess, final Color colorToMakeTransparent ) { ImageFilter filter = new RGBImageFilter(){ public int markerRGB = colorToMakeTransparent.getRGB() | 0xFF000000; public final int filterRGB( int x, int y, int rgb ) { if ((rgb | 0xFF000000) == markerRGB) { // Mark the alpha bits as zero - transparent return 0x00FFFFFF & rgb; } else { return rgb; } } }; ImageProducer ip = new FilteredImageSource(bufferedImageToProcess.getSource(), filter); Image image = Toolkit.getDefaultToolkit().createImage(ip); BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = bufferedImage.createGraphics(); g2.drawImage(image, 0, 0, null); g2.dispose(); return bufferedImage; }
private BufferedImage filterWhiteToTransparent(Image img) { ImageFilter filter = new RGBImageFilter() { @Override public int filterRGB(int x, int y, int rgb) { return (rgb & 0x00FFFFFF) == 0x00FFFFFF ? 0x00FFFFFF : rgb; } }; Image transparentImage = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(img.getSource(), filter)); BufferedImage canvas = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB); Graphics2D g = (Graphics2D)canvas.getGraphics(); g.drawImage(transparentImage, 0, 0, null); g.dispose(); return canvas; }
public Jimage cut(int x, int y, int width, int height) { try { BufferedImage bi = this.imageis; int srcWidth = bi.getHeight(); int srcHeight = bi.getWidth(); if (srcWidth > 0 && srcHeight > 0) { Image imagex = bi.getScaledInstance(srcWidth, srcHeight, Image.SCALE_DEFAULT); ImageFilter cropFilter = new CropImageFilter(x, y, width, height); Image img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(imagex.getSource(), cropFilter)); BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = tag.getGraphics(); g.drawImage(img, 0, 0, width, height, null); // 绘制切割后的图 g.dispose(); this.imageis = tag; } } catch (Exception e) { e.printStackTrace(); } return this; }
@Override public Object execute(FunctionParameters parameters) { Object[] executed = executeChildFormulae(parameters, true); if (executed == null) { return Functions.EXECUTION_ERROR; } if (BufferedImage.class.isInstance(executed[0]) == false) { return Functions.EXECUTION_ERROR; } BufferedImage img = (BufferedImage) executed[0]; ImageFilter filter = createFilter(executed); if (filter == null) { return Functions.EXECUTION_ERROR; } ImageProducer ip = new FilteredImageSource(img.getSource(), filter); Image ret = Toolkit.getDefaultToolkit().createImage(ip); return ImageUtils.toBufferedImage(ret); }
public static void main(String args[]) { final Image unrotated = Toolkit.getDefaultToolkit().getImage("ASL/images/Climb1d.gif"); ImageFilter filter = new RotateFilter(-60.0); ImageProducer producer = new FilteredImageSource(unrotated.getSource(), filter); final Image rotated = new javax.swing.JLabel().createImage(producer); javax.swing.JFrame f = new javax.swing.JFrame() { private static final long serialVersionUID = 1L; public void paint(Graphics g) { g.setColor(Color.blue); g.fillRect(0, 0, getSize().width, getSize().height); g.drawImage(rotated, 100, 100, this); g.drawImage(unrotated, 0, 0, this); g.drawImage(unrotated, 100 + unrotated.getWidth(this), unrotated.getHeight(this), 100, 0, 0, 0, 0 + unrotated.getWidth(this), unrotated.getHeight(this), this); } }; f.setSize(300, 300); f.setVisible(true); }
public static Image makeColorTransparent(Image image, final Color color) { ImageFilter filter = new RGBImageFilter() { public int markerRGB = color.getRGB()|0xFF000000; @Override public final int filterRGB(int x, int y, int rgb) { if((rgb|0xFF000000)==markerRGB) return 0x00FFFFFF & rgb; else return rgb; } }; return Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), filter)); }
protected void buildInitialFactories() { int minWordLength = 6; int maxWordLength = 6; int fontSize = 50; int imageWidth = 250; int imageHeight = 100; WordGenerator wordGenerator = new RandomWordGenerator("0123456789abcdefghijklmnopqrstuvwxyz"); RandomRangeColorGenerator cgen = new RandomRangeColorGenerator(new int[] { 0, 150 }, new int[] { 0, 150 }, new int[] { 0, 150 }); LineTextDecorator lineTextDecorator = new LineTextDecorator(1, cgen);// 曲线干扰 BaffleTextDecorator baffleTextDecorator = new BaffleTextDecorator(2, cgen);// 气泡干扰 TextDecorator[] textDecorators = new TextDecorator[2]; textDecorators[0] = lineTextDecorator; textDecorators[1] = baffleTextDecorator; TextPaster randomPaster = new DecoratedRandomTextPaster(minWordLength, maxWordLength, new RandomListColorGenerator( new Color[] { new Color(23, 170, 27), new Color(220, 34, 11), new Color(23, 67, 172) }), textDecorators); BackgroundGenerator background = new UniColorBackgroundGenerator(imageWidth, imageHeight, Color.white); FontGenerator font = new RandomFontGenerator(fontSize, fontSize, new Font[] { new Font("nyala", Font.BOLD, fontSize), new Font("Bell MT", Font.PLAIN, fontSize), new Font("Credit valley", Font.BOLD, fontSize) }); ImageDeformation postDef = new ImageDeformationByFilters(new ImageFilter[] {}); ImageDeformation backDef = new ImageDeformationByFilters(new ImageFilter[] {}); ImageDeformation textDef = new ImageDeformationByFilters(new ImageFilter[] {}); WordToImage word2image = new DeformedComposedWordToImage(font, background, randomPaster, backDef, textDef, postDef); addFactory(new GimpyFactory(wordGenerator, word2image)); }