我需要设置过滤器到图像.. 我做到了:
int type = BufferedImage.TYPE_INT_RGB;
double panelHeight = frame.getHeight();
double panelWidth = frame.getWidth();
BufferedImage image = null;
try {
image = ImageIO.read(new File(frame.getURL()));
} catch (IOException e1) {
e1.printStackTrace();
}
int height = image.getHeight();
int width = image.getWidth();
for(int h=1; h < height-1; h++)
{
for(int w=1; w < width-1;w++)
{
float color = ((1f/9f*image.getRGB(w-1, h-1))+(1f/9f* image.getRGB(w, h-1))+(1f/9f* image.getRGB(w+1, h-1))+
+(1f/9f* image.getRGB(w-1, h))+(1f/9f* image.getRGB(w, h))+(1f/9f* image.getRGB(w+1, h))+
(1f/9f* image.getRGB(w-1, h+1))-(1f/9f* image.getRGB(w, h+1))-(1f/9f* image.getRGB(w+1, h+1)));
image.setRGB( w, h,(int)color);
}
}
//save im into a file
frame.setFilteredImage(Resizer.resize(image, panelHeight, panelWidth));
框架包含我的图片网址。我使用frame.getURL()
结果我希望看到带有模糊滤镜的图像,但它却给了我
所以可能有人知道我在哪里弄错了???
或者您可能知道使用此类矩阵设置过滤器的另一种好方法
答案 0 :(得分:4)
使用blurFilter
public class Java2DExample extends JFrame {
private JMenu filterMenu = new JMenu("Image Filters");
private ImagePanel imagePanel;
private MyFilter invertFilter = new InvertFilter();
private MyFilter sharpenFilter = new SharpenFilter();
private MyFilter blurFilter = new BlurFilter();
private MyFilter colorFilter = new ColorFilter();
public Java2DExample() {
super("Java 2D Image Processing Demo");
imagePanel = new ImagePanel(Java2DExample.class.getResource("yourImage.png"));
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
filterMenu.setMnemonic('I');
JMenuItem originalMenuItem = new JMenuItem("Display Original");
originalMenuItem.setMnemonic('O');
originalMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent action) {
imagePanel.displayOriginalImage();
}
});
JMenuItem invertMenuItem = createMenuItem("Invert", 'I', invertFilter);
JMenuItem sharpenMenuItem = createMenuItem("Sharpen", 'S', sharpenFilter);
JMenuItem blurMenuItem = createMenuItem("Blur", 'B', blurFilter);
JMenuItem changeColorsMenuItem = createMenuItem("Change Colors", 'C', colorFilter);
filterMenu.add(originalMenuItem);
filterMenu.add(invertMenuItem);
filterMenu.add(sharpenMenuItem);
filterMenu.add(blurMenuItem);
filterMenu.add(changeColorsMenuItem);
menuBar.add(filterMenu);
getContentPane().add(imagePanel, BorderLayout.CENTER);
}
public JMenuItem createMenuItem(String menuItemName, char mnemonic, final MyFilter filter) {
JMenuItem menuItem = new JMenuItem(menuItemName);
menuItem.setMnemonic(mnemonic);
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent action) {
imagePanel.applyFilter(filter);
}
});
return menuItem;
}
public static void main(String args[]) {
Java2DExample application = new Java2DExample();
application.setDefaultCloseOperation(EXIT_ON_CLOSE);
application.pack();
application.setVisible(true);
}
}
interface MyFilter {
public abstract BufferedImage processImage(BufferedImage image);
}
class BlurFilter implements MyFilter {
public BufferedImage processImage(BufferedImage image) {
float[] blurMatrix = { 1.0f / 9.0f, 1.0f / 9.0f, 1.0f / 9.0f, 1.0f / 9.0f, 1.0f / 9.0f,
1.0f / 9.0f, 1.0f / 9.0f, 1.0f / 9.0f, 1.0f / 9.0f };
BufferedImageOp blurFilter = new ConvolveOp(new Kernel(3, 3, blurMatrix),
ConvolveOp.EDGE_NO_OP, null);
return blurFilter.filter(image, null);
}
}
class ImagePanel extends JPanel {
private BufferedImage displayImage;
private BufferedImage originalImage;
private Image image;
public ImagePanel(URL imageURL) {
image = Toolkit.getDefaultToolkit().createImage(imageURL);
MediaTracker mediaTracker = new MediaTracker(this);
mediaTracker.addImage(image, 0);
try {
mediaTracker.waitForAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
originalImage = new BufferedImage(image.getWidth(null), image.getHeight(null),
BufferedImage.TYPE_INT_RGB);
displayImage = originalImage;
Graphics2D graphics = displayImage.createGraphics();
graphics.drawImage(image, null, null);
}
public void applyFilter(MyFilter filter) {
displayImage = filter.processImage(displayImage);
repaint();
}
public void displayOriginalImage() {
displayImage = new BufferedImage(image.getWidth(null), image.getHeight(null),
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = displayImage.createGraphics();
graphics.drawImage(originalImage, null, null);
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D graphics = (Graphics2D) g;
graphics.drawImage(displayImage, 0, 0, null);
}
public Dimension getPreferredSize() {
return new Dimension(displayImage.getWidth(), displayImage.getHeight());
}
public Dimension getMinimumSize() {
return getPreferredSize();
}
}
class SharpenFilter implements MyFilter {
public BufferedImage processImage(BufferedImage image) {
float[] sharpenMatrix = { 0.0f, -1.0f, 0.0f, -1.0f, 5.0f, -1.0f, 0.0f, -1.0f, 0.0f };
BufferedImageOp sharpenFilter = new ConvolveOp(new Kernel(3, 3, sharpenMatrix),
ConvolveOp.EDGE_NO_OP, null);
return sharpenFilter.filter(image, null);
}
}
class InvertFilter implements MyFilter {
public BufferedImage processImage(BufferedImage image) {
byte[] invertArray = new byte[256];
for (int counter = 0; counter < 256; counter++)
invertArray[counter] = (byte) (255 - counter);
BufferedImageOp invertFilter = new LookupOp(new ByteLookupTable(0, invertArray), null);
return invertFilter.filter(image, null);
}
}
class ColorFilter implements MyFilter {
public BufferedImage processImage(BufferedImage image) {
float[][] colorMatrix = { { 1f, 0f, 0f }, { 0.5f, 1.0f, 0.5f }, { 0.2f, 0.4f, 0.6f } };
BandCombineOp changeColors = new BandCombineOp(colorMatrix, null);
Raster sourceRaster = image.getRaster();
WritableRaster displayRaster = sourceRaster.createCompatibleWritableRaster();
changeColors.filter(sourceRaster, displayRaster);
return new BufferedImage(image.getColorModel(), displayRaster, true, null);
}
}
参考:http://www.java2s.com/Code/Java/2D-Graphics-GUI/ImageFilter.htm