다음은 BufferedImage를 사용하여 RGB 값을 읽은 다음 다시 파일에 다시 쓰는 코드입니다. 결과 이미지가 완벽하고 잘 보입니다. 걱정 마세요.BufferedImage를 사용하여 이미지 파일을 읽고 쓰려면
인쇄 테스트를 실행하여 처음 10 RBG int 값을 인쇄합니다. 이것은 "test.png"파일을 테스트 한 다음 결과 이미지 인 "new-test.png"를 테스트하는 것입니다. 몇 가지 이유로 두 파일간에 서로 다른 RBG 값을 얻고 있습니다.
예. (제 3의 RGB 값 INT)
test.png : -16704215, -16704215, -16704215
새로운 test.png : -16638935, -16638935, -16573142
누구나 두 테스트 파일에 대해 다른 RBG 값을 출력하는 이유를 파악할 수 있습니까?
try
{
BufferedImage imgBuf = ImageIO.read(new File("test.png"));//also testing with GIFs, JPEGs
int w = imgBuf.getWidth();
int h = imgBuf.getHeight();
int[] RGBarray = imgBuf.getRGB(0,0,w,h,null,0,w);
//Arrays to store their respective component values
int [][] redPixels = new int [h][w];
int [][] greenPixels = new int [h][w];
int [][] bluePixels = new int [h][w];
for(int i=0; i<=10; i++)
{
//print out the first 10 RGB int values - testing purposes
System.out.println(RGBarray[i]);
}
//Separate the RGB int values into 3 array, red, green and blue ....
int x=0;
for(int row=0; row<h; row++)
{
for(int col=0; col<w; col++)
{
redPixels[row][col] = ((RGBarray[x]>>16)&0xff);
greenPixels[row][col] = ((RGBarray[x]>>8)&0xff);
bluePixels[row][col] = (RGBarray[x]&0xff);
x++;
}
}
//set pixels back using the setRBG() ...
BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
for(int row=0; row<h; row++)
{
for(int col=0; col<w; col++)
{
//use bit shifting to re-form the RGB int again
int rgb = (redPixels[row][col] & 0xff) << 16 | (greenPixels[row][col] & 0xff) << 8 | (bluePixels[row][col] & 0xff);
bufferedImage.setRGB(col, row, rgb);
}
}
}
catch(IOException i){}; // This exception format is only temporary !
new-test.png는 어떻게 작성합니까? – malun
마지막 편집을 확인하십시오. 빨강, 초록, 파랑의 각 색상 요소를 나타 내기 위해 사용 중입니다. –
다음에'ImageIO.write()'를 사용하여 bufferedImage를 new-test로 저장하고 있습니다. PNG? – malun