gpt4 book ai didi

java - 在 Swing 中向图像添加滤镜

转载 作者:行者123 更新时间:2023-11-30 07:22:37 24 4
gpt4 key购买 nike

我需要为图像设置滤镜..我做这个:

        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 有我的图片的 url。我使用 frame.getURL()

获取它

结果我想用模糊滤镜看到我的图像,但它给了我 enter image description here

所以可能有人知道我在哪里犯了错误???

或者您可能知道另一种使用此类矩阵设置过滤器的好方法

最佳答案

使用模糊滤镜

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

关于java - 在 Swing 中向图像添加滤镜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12541081/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com