2014-04-18 9 views
1

Java로 이미지 처리를하고 싶습니다. 시작하기 전에 필터 나 기타 작업을 수행하기 전에 이미지에 바이트 배열로 변환 한 다음 이미지로 변환하고 저장하여 저장 방법을 확인하십시오.Image to byte [] to Image

입력 이미지로 출력 이미지를 가져 오지 못하고 일부 출력물/데이터가 손실되어 출력이 다른 색상으로 보입니다.

제발 어떤 문제인지 말해주세요. 내가 무엇을 놓치고 있는지.

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

import javax.imageio.ImageIO; 


public class Ola { 

    BufferedImage img = null; 

    public void loadImage() { 
     try { 
      img = ImageIO.read(new File("/home/a/Pictures/Tux-vegeta.png")); 
      } catch (IOException e) { 
       System.out.println("image(s) could not load correctly, try changing the path"); 
      } 
    } 

    public byte[] convertToArray() { 
     int w = img.getWidth(); 
     int h = img.getHeight(); 

     int bands = img.getSampleModel().getNumBands(); 
     System.out.print(bands); 

     if (bands != 4) { 
      System.out.println("The image does not have 4 color bands");    
     } 

     byte bytes[] = new byte[4 * w * h]; 
     int index = 0; 

     for(int y = 0; y < h; y++) { 
      for(int x = 0; x < w; x++) { 
       int pixel = img.getRGB(x, y); 

       int alpha = (pixel >> 24) & 0xFF; 
       int red = (pixel >> 16) & 0xFF; 
       int green = (pixel >> 8) & 0xFF; 
       int blue = pixel & 0xFF; 

       bytes[index++] = (byte) alpha; 
       bytes[index++] = (byte) red; 
       bytes[index++] = (byte) green; 
       bytes[index++] = (byte) blue; 
      } 
     } 
     return bytes; 
    } 
    public void convertToImage(byte[] bytes) { 
     try { 
      int w = 300; 
      int h = 300; 
      int index = 0; 
      BufferedImage resultPNG = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 
      for (int i = 0; i < h; i++) { 
       for (int j = 0; j < w; j ++) { 
        int pixel = (bytes[index] << 24) | (bytes[index + 1] << 16) | (bytes[index + 2] << 8) | (bytes[index + 3]); 
        resultPNG.setRGB(j, i, pixel); 
        index += 4; 
       } 
      } 
      File outputImage = new File("/home/a![enter image description here][1]/Pictures/test.png"); 
      ImageIO.write(resultPNG, "png", outputImage); 
     } catch (IOException e) { 
      System.out.println("image write error"); 
     } 
    } 

    public static void main(String[] args) { 
     Ola ola = new Ola(); 
     ola.loadImage(); 
     ola.convertToImage(ola.convertToArray()); 

    } 
} 
+1

/OutputStream 및 ImageIO – MadProgrammer

답변

1

당신이 서명에 다시 서명 바이트를 돌고 누락 :

변화 당신의 라인

int pixel = (bytes[index] << 24) | (bytes[index + 1] << 16) | (bytes[index + 2] << 8) | (bytes[index + 3]); 

을 다음과 : 당신은 ByteArrayInput을 사용할 수

int pixel = ((bytes[index] & 0xFF) << 24) | ((bytes[index + 1] & 0xFF) << 16) | ((bytes[index + 2] & 0xFF) << 8) | (bytes[index + 3] & 0xFF); 
+0

내 친구에게 고마움을 전한다. 나는 그것을 놓쳤다. – Raafat

-1

당신이 당신의 목적지 알파 바이트를 무시하도록 버퍼 이미지의 원인이됩니다 RGB를 사용하는 대신 TYPE_INT_RGB의 TYPE_INT_ARGB을 사용해야 알파 채널을 원하고 있기 때문에.

PNG가 TYPE_INT_ARGB 색상 모델로로드되지 않으므로로드 된 버퍼링 된 이미지를 TYPE_INT_ARGB를 사용하여 만든 bufferedimage 개체에 그리는 그래픽 개체를 사용할 수 있습니다.

public void loadImage() { 
    try { 
     BufferedImage tempimg = ImageIO.read(new File("/home/a/Pictures/Tux-vegeta.png")); 
     img = new BufferedImage(300, 300, BufferedImage.TYPE_INT_ARGB); 
     Graphics2D g2 = img.createGraphics(); 
     g2.drawImage(tempimg,null,0,0); 
     } catch (IOException e) { 
      System.out.println("image(s) could not load correctly, try changing the path"); 
     } 
} 
+0

아니요, 여전히 동일한 출력을 얻습니다. – Raafat

+0

조금 더 살펴보면 PNG가 TYPE_INT_ARGB 또는 RGB 색상 공간에로드되지 않는 것 같습니다. PNG가로드되는 정확한 형식을 조사해야합니다. –

+0

PNG가로드되는 형식은 실제 파일에 따라 다릅니다. 특히 투명도가 포함 된 PNG 인 경우 유형은 TYPE_CUSTOM이 될 수 있습니다. 따라서 일반적으로 PNG에서 새로로드 된 BufferedImage를 TYPE_INT_ARGB 유형의 새 이미지에 페인트하는 것이 좋습니다 (페인트 성능이 향상 될 수 있음). (이것은 원래의 질문과 관련이 없었지만 답장을하지 않았다. BTW) – Marco13