紫衣仙女
这是一种纯 Java 方法。创建图像不需要 Swing 代码。我们没有将图像更改为黑色和白色,而是将图像更改为黑色和透明。这就是我们如何保护那些羽毛状的边缘。如果你想要一个没有 alpha 的真正的灰度图像,制作一个 graphics2d 对象,用所需的背景颜色填充它,然后将图像绘制到它上面。至于将白人保留为白人,这是可以做到的,但必须承认两件事之一。要么放弃黑白方面并采用真正的灰度图像,要么保留黑白,但会出现锯齿状边缘,白色羽毛会融入任何其他颜色。发生这种情况是因为一旦我们击中浅色像素,我们如何知道它是浅色特征,还是白色和另一种颜色之间的过渡像素。我不知道有什么方法可以在没有边缘检测的情况下解决这个问题。public class Main { private static void createAndShowGUI() { //swing stuff JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("Alpha Mask"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.PAGE_AXIS)); JLabel picLabel = new JLabel(new ImageIcon(getImg())); frame.getContentPane().add(picLabel); BufferedImage alphaMask = createAlphaMask(getImg()); JLabel maskLabel = new JLabel(new ImageIcon(alphaMask)); frame.getContentPane().add(maskLabel); //Display the window. frame.pack(); frame.setVisible(true); } public static BufferedImage getImg() { try { return ImageIO.read(new URL("https://i.stack.imgur.com/UPmqE.png")); } catch (IOException e) { e.printStackTrace(); } return null; } public static BufferedImage createAlphaMask(BufferedImage img) { //TODO: deep copy img here if you actually use this int width = img.getWidth(); int[] data = new int[width]; for (int y = 0; y < img.getHeight(); y++) { // pull down a line if argb data img.getRGB(0, y, width, 1, data, 0, 1); for (int x = 0; x < width; x++) { //set color data to black, but preserve alpha, this will prevent harsh edges int color = data[x] & 0xFF000000; data[x] = color; } img.setRGB(0, y, width, 1, data, 0, 1); } return img; } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(() -> createAndShowGUI()); }}