Getting Greyscale pixel value from RGB colourspace in Java using BufferedImage

前端 未结 3 1435
抹茶落季
抹茶落季 2021-01-14 03:54

Anyone know of a simple way of converting the RGBint value returned from getRGB(i,j) into a greyscale value?

I was going to simply

3条回答
  •  孤街浪徒
    2021-01-14 04:25

    This tutorial shows 3 ways to do it:

    By changing ColorSpace

    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
    ColorConvertOp op = new ColorConvertOp(cs, null);
    BufferedImage image = op.filter(bufferedImage, null);
    

    By drawing to a grayscale BufferedImage

    BufferedImage image = new BufferedImage(width, height,
        BufferedImage.TYPE_BYTE_GRAY);
    Graphics g = image.getGraphics();
    g.drawImage(colorImage, 0, 0, null);
    g.dispose();
    

    By using GrayFilter

    ImageFilter filter = new GrayFilter(true, 50);
    ImageProducer producer = new FilteredImageSource(colorImage.getSource(), filter);
    Image image = this.createImage(producer);
    

提交回复
热议问题