4
以下是我寫來改變灰度圖像的像素值,創建一個全白圖像(設定爲255的所有像素值)的代碼。但是,如圖2所示,我沒有獲得完整的白色圖像,而是獲得了黑色豎條。我哪裏出錯了?
package dct;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.File;
import javax.imageio.ImageIO;
public class writeGScale {
public static void main(String[] args){
File file = new File("bridge.jpg");
BufferedImage img = null;
try{
img = ImageIO.read(file);
}
catch(Exception e){
e.printStackTrace();
}
int width = img.getWidth();
int height = img.getHeight();
int[] tempArr = new int[width*height];
WritableRaster raster1=img.getRaster();
for(int i=0;i<width;i++){
for(int j=0;j<height;j++){
tempArr[0] = 255;
raster1.setPixel(i, j, tempArr);
}
}
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
image.setData(raster1);
try{
File ouptut = new File("change.png");
ImageIO.write(image, "png", ouptut);
}
catch(Exception e){
e.printStackTrace();
}
}
}
@ Fast Snail Superrr ....我得到了圖像。再次感謝。 BUt爲什麼以前的代碼失敗?任何想法...? –