2013-03-13 6 views
0

JPEG의 RGB 이미지가 있습니다. 이 이미지를 픽셀로 변환하고 텍스트 파일로 표시하려고합니다. 이 작업을 수행하는 방법?자바에서 이미지를 픽셀로 변환하는 방법?

public static int[][][] getPixelData(Image image) { 

    // get Image pixels in 1D int array 
    int width = image.getWidth(null); 
    int height = image.getHeight(null); 

    int[] imgDataOneD = imageToPixels(image); 

private static int[] imageToPixels(Image image) { 

    int height = image.getHeight(null); 
    int width = image.getWidth(null); 

    int pixels[] = new int[width * height]; 

    PixelGrabber grabber = new PixelGrabber(image, 0, 0, width, height, pixels, 0, width); 

    try { 
     grabber.grabPixels(); 
    } catch (InterruptedException e) { 
    } 

    return pixels; 
} 

이 정보를 시퀀스 벡터 형식으로 텍스트 파일에 저장하는 방법은 무엇입니까?

+0

어떤 데이터를 int [], int [] [] 또는 int [] [] []에 저장 하시겠습니까? – phoenix7360

+0

텍스트 파일로 표시 하시겠습니까? 뭐, RGB 값들이 서로 섞 였니? – Zutty

답변

0

다음과 같이 사용하십시오. 도움이되기를 바랍니다

import java.io.*; 
import javax.imageio.ImageIO; 
import java.awt.image.BufferedImage; 

public class ImageToText { 
    public static void main(String args[]) throws IOException { 
     File file = new File("your file path.jpg"); 
     BufferedImage image = ImageIO.read(file); 
     // Getting pixel color by position x=100 and y=40 

     for (int i = 0; i < image.getHeight(); i++) { 
      for (int j = 0; j < image.getWidth(); j++) { 
       int color = image.getRGB(j, i); 

       // You can convert the colour to a readable format by changing 
       // to following string. It is a 6 character hex 
       // String clr = Integer.toHexString(color).substring(2); 

       // Write this int value or string value to the text file 
       // Add a comma or any other separator. Since it is always 6 
       // characters long you can avoid using comma. It will save some 
       // space. 
      } 
      // add a new line 
     } 
    } 
} 

텍스트 파일을 읽고 데이터를 검색하려면 자신의 고민에 대해 생각할 수 있습니다. :-)

0

쉼표로 구분 된 목록으로 정수를 저장할 수 있습니다.

이미지를 복원 할 수 있도록 이미지 너비를 저장해야합니다.

관련 문제