Secondo me non puoi operare sui pixel in quel modo, ma bisognerebbe separare i canali. Io ho fatto questa classettina di prova:
codice:
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
/**
*
* @author Andrea
*/
public class ImageFilters {
private BufferedImage src,dst;
private JFrame frame;
public void BlurFilter() {
dst = new BufferedImage(src.getWidth(), src.getHeight(), src.getType());
for (int y = 1; y < src.getHeight()-1; y++) {
for (int x = 1; x < src.getWidth() - 1; x++) {
int rgb = 0;
int r_current = 0;
int g_current = 0;
int b_current = 0;
for (int h = -1; h <= 1; h++) {
for (int k = -1; k <= 1; k++) {
rgb = src.getRGB(x+k, y+h);
//shift per separare i canali, 2 byte a canale
r_current += ((rgb >> 16) & 0xff);
g_current += ((rgb >> 8) & 0xff);
b_current += (rgb & 0xff);
}
}
//faccio la media
r_current /= 9;
g_current /= 9;
b_current /= 9;
// e risistemo con lo shifting inverso
rgb = r_current << 16 | g_current << 8 | b_current;
// e setto il pixel
dst.setRGB(x,y,rgb);
}
}
}
public void show() {
frame = new JFrame("Show Result");
frame.setSize(dst.getWidth(), dst.getHeight()*2);
frame.getContentPane().setLayout(new GridLayout(2,1));
frame.getContentPane().add(new JLabel(new ImageIcon(src)));
frame.getContentPane().add(new JLabel(new ImageIcon(dst)));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
/** Creates a new instance of BlurFilter */
public ImageFilters(String path) throws Exception {
src = ImageIO.read(new File(path));
}
public static void main (String[] args) {
try {
ImageFilters blur = new ImageFilters("C:/Documents and Settings/Andrea/Desktop/nemo.jpg");
blur.BlurFilter();
blur.show();
}
catch (Exception e) {
e.printStackTrace();
}
}
}