2014-03-12 3 views
1

다음은 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 ! 
+1

new-test.png는 어떻게 작성합니까? – malun

+0

마지막 편집을 확인하십시오. 빨강, 초록, 파랑의 각 색상 요소를 나타 내기 위해 사용 중입니다. –

+0

다음에'ImageIO.write()'를 사용하여 bufferedImage를 new-test로 저장하고 있습니다. PNG? – malun

답변

2

코드를 약간 수정하여 이미지를 저장 한 다음 다시 읽습니다. 이 코드를 사용하면 전후에 정확히 동일한 int 값을 얻을 수 있습니다.

import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 

import javax.imageio.ImageIO; 

import org.junit.Test; 


public class PngReadWriteTest { 

@Test 
public void test(){ 
    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]; 

    System.out.println("IMAGE FIRST READ:"); 
    printPixelValues(imgBuf); 

    //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); 
     } 
    } 

    System.out.println("IMAGE BEFORE SAVE:"); 
    printPixelValues(bufferedImage); 

    //Save image as png 
    ImageIO.write(bufferedImage, "png", new File("new-test.png")); 

    BufferedImage newImage = ImageIO.read(new File("new-test.png"));//also testing with GIFs, JPEGs 

    System.out.println("IMAGE AFTER SECOND READ:"); 
    printPixelValues(newImage); 

    } 
    catch(IOException i){}; // This exception format is only temporary ! 
} 

private void printPixelValues(BufferedImage image){ 
    int w = image.getWidth(); 
    int h = image.getHeight(); 
    int[] RGBarray = image.getRGB(0,0,w,h,null,0,w); 

    for(int i=0; i<=10; i++) 
    { 
     //print out the first 10 RGB int values - testing purposes 
     System.out.println(RGBarray[i]); 
    } 
} 

} 

이것은 무손실이기 때문에 PNG를 사용하는 경우에만 작동합니다. JPG를 사용하면 손실이 있으므로 동일한 결과를 얻지 못할 것입니다. 파일을 JPG로 저장하면 첫 번째 이미지와 정확히 동일한 바이트를 포함하는 파일이 생성되지 않습니다. 압축 수준은 이미지에 영향을 미칩니다. 따라서 파일이 눈으로 보일지라도 정확히 똑같지는 않습니다.

관련 문제