码迷,mamicode.com
首页 > 其他好文 > 详细

e665. 在图像中过滤三元色

时间:2018-09-02 23:41:33      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:work   center   eve   extend   set   images   color   only   ffffff   

This example demonstrates how to create a filter that can modify any of the RGB pixel values in an image.

    // This filter removes all but the red values in an image
    class GetRedFilter extends RGBImageFilter {
        public GetRedFilter() {
            // When this is set to true, the filter will work with images
            // whose pixels are indices into a color table (IndexColorModel).
            // In such a case, the color values in the color table are filtered.
            canFilterIndexColorModel = true;
        }
    
        // This method is called for every pixel in the image
        public int filterRGB(int x, int y, int rgb) {
            if (x == -1) {
                // The pixel value is from the image‘s color table rather than the image itself
            }
            // Return only the red component
            return rgb & 0xffff0000;
        }
    }

Here‘s some code that uses the filter:

    // Get image
    Image image = new ImageIcon("image.gif").getImage();
    
    // Create the filter
    ImageFilter filter = new GetRedFilter();
    FilteredImageSource filteredSrc = new FilteredImageSource(image.getSource(), filter);
    
    // Create the filtered image
    image = Toolkit.getDefaultToolkit().createImage(filteredSrc);

 

Related Examples

e665. 在图像中过滤三元色

标签:work   center   eve   extend   set   images   color   only   ffffff   

原文地址:https://www.cnblogs.com/borter/p/9575520.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!