How to convert Colors to Grayscale in java with just the java.io.*; library?

后端 未结 3 646
无人共我
无人共我 2021-01-21 20:09

Well, i got this.. cause in my place they request it like that.. Well i have this: //some variables and coments are a possible code.. But i mean i don\'t know how to do it exact

相关标签:
3条回答
  • 2021-01-21 20:35

    Most image formats have header information before the pixel data, so when you're reading these types of files, you need to take that into consideration...

    Frankly, it's much easier to rely on pre-existing library where you can.

    ImageIO allows you to read and write a number of different file formats, including BMP.

    Take a look at

    • Reading/Loading an Image
    • Writing/Saving an Image

    The next decision is - do you convert the image yourself or use a pre-existing filter. You'll have to do some of your own metrics, but in the past, I've found pixel manipulation of an image to be slow, slower than the in built filters at the very least...

    enter image description here

    Original, manual grayscale, automatical/filter grayscale

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridLayout;
    import java.awt.color.ColorSpace;
    import java.awt.image.BufferedImage;
    import java.awt.image.ColorConvertOp;
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class GrayScaleImage {
    
        public static void main(String[] args) {
            new GrayScaleImage();
        }
    
        public GrayScaleImage() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            public TestPane() {
                setLayout(new GridLayout(0, 3));
                try {
                    BufferedImage master = ImageIO.read(new File("/path/to/file.bmp"));
                    BufferedImage gray = ImageIO.read(new File("/path/to/file.bmp"));
    
                    // Manual manipulation...
                    for (int x = 0; x < gray.getWidth(); x++) {
                        for (int y = 0; y < gray.getHeight(); y++) {
                            Color color = new Color(gray.getRGB(x, y));
                            int red = color.getRed();
                            int green = color.getGreen();
                            int blue = color.getBlue();
    
                            red = green = blue = (int)(red * 0.299 + green * 0.587 + blue * 0.114);
                            color = new Color(red, green, blue);
                            int rgb = color.getRGB();
                            gray.setRGB(x, y, rgb);
                        }
                    }
    
                    BufferedImage grayScale = ImageIO.read(new File("/path/to/file.bmp"));
    
                    // Automatic converstion....
                    ColorConvertOp op = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
                    op.filter(grayScale, grayScale);
    
                    add(new JLabel(new ImageIcon(master)));
                    add(new JLabel(new ImageIcon(gray)));
                    add(new JLabel(new ImageIcon(grayScale)));
                } catch (IOException ex) {
                    Logger.getLogger(GrayScaleImage.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }        
    }
    

    Now, writing the image (which is not demonstrated above) would be as simple as...

    ImageIO.write(grayScale, "BMP", new File("/path/to/grayscale file.bmp"));
    
    0 讨论(0)
  • 2021-01-21 20:41
    // constant factors
                final double GS_RED   = 0.299;
                final double GS_GREEN = 0.587;
                final double GS_BLUE  = 0.114;
    

    Now retrieve each and gett it's red, blue, green component and alpha if exist and multiply them with their corresponding factor.

    R = G = B = (int)(GS_RED * R + GS_GREEN * G + GS_BLUE * B);
    
    0 讨论(0)
  • 2021-01-21 20:46

    For each pixel you have the RGB values, take only the R value and copy it to G and B. So that each pixel will contain the red value in the 3 RGB this will cause the image to be gray. Example. data [0]=123 //red data [1]=43 //Green data [2]=78 //blue Then data [0]=123 data [1]=123 data [2]=123

    0 讨论(0)
提交回复
热议问题