JavaFX实现图像梯度效果

代码如下:


import javafx.scene.image.PixelReader;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;

public class GradientBySobel {
    //梯度效果
    public WritableImage makeGradientEffectBySobel(WritableImage image,int direction) {
        int width = (int)image.getWidth();
        int height = (int)image.getHeight();

        final int[][] SOBEL_X = new int[][]{{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};  
        final int[][] SOBEL_Y = new int[][]{{-1, -2, -1}, {0,  0,  0}, {1,  2,  1}};

        int[] inPixel = new int[width*height];
        int[] outPixel = new int[width*height];

        getRGB( image, width, height, inPixel); 
        int index = 0, index2 = 0;  
        double xred = 0, xgreen = 0, xblue = 0;  
        double yred = 0, ygreen = 0, yblue = 0;  
        int newRow, newCol;  
        for(int row=0; row<height; row++) {  
            int ta = 255, tr = 0, tg = 0, tb = 0;  
            for(int col=0; col<width; col++) {  
                index = row * width + col;  
                for(int subrow = -1; subrow <= 1; subrow++) {  
                    for(int subcol = -1; subcol <= 1; subcol++) {  
                        newRow = row + subrow;  
                        newCol = col + subcol;  
                        if(newRow < 0 || newRow >= height) {  
                            newRow = row;  
                        }  
                        if(newCol < 0 || newCol >= width) {  
                            newCol = col;  
                        }  
                        index2 = newRow * width + newCol;  
                        tr = (inPixel[index2] >> 16) & 0xff;  
                        tg = (inPixel[index2] >> 8) & 0xff;  
                        tb = inPixel[index2] & 0xff;  

                        xred += (SOBEL_X[subrow + 1][subcol + 1] * tr);  
                        xgreen +=(SOBEL_X[subrow + 1][subcol + 1] * tg);  
                        xblue +=(SOBEL_X[subrow + 1][subcol + 1] * tb);  

                        yred += (SOBEL_Y[subrow + 1][subcol + 1] * tr);  
                        ygreen +=(SOBEL_Y[subrow + 1][subcol + 1] * tg);  
                        yblue +=(SOBEL_Y[subrow + 1][subcol + 1] * tb);   
                    }  
                }  

                double mred = Math.sqrt(xred * xred + yred * yred);  
                double mgreen = Math.sqrt(xgreen * xgreen + ygreen * ygreen);  
                double mblue = Math.sqrt(xblue * xblue + yblue * yblue);  
                if(4 == direction)   
                {  
                    outPixel[index] = (ta << 24) | (clamp((int)mred) << 16) | (clamp((int)mgreen) << 8) | clamp((int)mblue);  
                }   
                else if(0 == direction)  
                {  
                    outPixel[index] = (ta << 24) | (clamp((int)yred) << 16) | (clamp((int)ygreen) << 8) | clamp((int)yblue);  
                }   
                else if(2 == direction)   
                {  
                    outPixel[index] = (ta << 24) | (clamp((int)xred) << 16) | (clamp((int)xgreen) << 8) | clamp((int)xblue);  
                }   
                else   
                {  
                    // as default, always XY gradient  
                    outPixel[index] = (ta << 24) | (clamp((int)mred) << 16) | (clamp((int)mgreen) << 8) | clamp((int)mblue);  
                }  

                // cleanup for next loop  
                newRow = newCol = 0;  
                xred = xgreen = xblue = 0;  
                yred = ygreen = yblue = 0;  
            }  
        }
        WritableImage outImage = new WritableImage(width,height);
        PixelWriter pixel = outImage.getPixelWriter();

        for(int i=0;i<height;i++) {
            for(int j=0;j<width;j++) {
                pixel.setArgb(j, i, outPixel[i*width+j]);
            }
        }

        return outImage;
    }

     public int clamp(int value) {  
            return value < 0 ? 0 : (value > 255 ? 255 : value);  
     } 

    private void getRGB(WritableImage image, int width, int height, int[] inPixel) {
        PixelReader reader = image.getPixelReader();
        for(int i=0;i<height;i++) {
            for(int j=0;j<width;j++) {
                inPixel[i*width+j] = reader.getArgb(j, i);
            }
        }
    }
}

效果如下:
这里写图片描述
参考博文:http://blog.csdn.net/jia20003/article/details/7664777

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值