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; rowint ta = 255, tr = 0, tg = 0, tb = 0;  
            for(int col=0; colfor(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;ifor(int j=0;jreturn 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;ifor(int j=0;j

效果如下:
JavaFX实现图像梯度效果_第1张图片
参考博文:http://blog.csdn.net/jia20003/article/details/7664777

你可能感兴趣的:(图像处理)