简体   繁体   中英

Access pixels in Grayscale bufferedImage and change the pixel value in java

I want to do some sort of image processing on grayscale image.

BufferedImage bImg = new BufferedImage(img.getWidth(null),img.getHeight(null),BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g=bImg.createGraphics();
g.drawImage(img,null,null);

I am using ( BufferedImage.TYPE_BYTE_GRAY )this type to get gray scale image
But i do not know how to set the values of this grayscale BufferedImage.

I have solved this using writableRaster.

BufferedImage bImg = new BufferedImage(img.getWidth(null),img.getHeight(null),BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g=bImg.createGraphics();
g.drawImage(img,null,null);


WritableRaster raster=bImg.getRaster();
        int[] iArray=new int[1];
        for(int i=0;i<215;i++)
            for(int j=0;j<215;j++){
                raster.getPixel(i, j, iArray);
                iArray[0]=0;
                raster.setPixel(i, j, iArray);
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM