2012-08-13 8 views
0

Image_1.jpg가 있습니다. Image_1.jpg에서 2D Array에 이미지 픽셀 값이 있습니다. 다음과 같이 처음 8 * 8 블록 값을 설정했습니다.2D 이미지 배열의 값을 얻고 Java에서 값을 설정하는 방법

16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 
16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 
16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 
16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 
16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 
16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 
16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 
16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 

이미지를 Img.jpg로 구성하십시오. 다시 이것을 취하십시오 Img.jpg 이미지 이것을 2D Array.and에서 읽으십시오. 먼저 8 * 8 블록을 읽으십시오. 다음 값이 있습니다.

-16776690 , -16776690 , -16776432 , -16776176 , -16775922 , -16775927 , -16776190 , -16645120 , 
-16776944 , -16776688 , -16776432 , -16776176 , -16775922 , -16775925 , -16776188 , -16645376 , 
-16777198 , -16776940 , -16776940 , -16776684 , -16776430 , -16776434 , -16776697 , -16580096 , 
-16777196 , -16777196 , -16776939 , -16776683 , -16776428 , -16776432 , -16776695 , -16580350 , 
-16646124 , -16711660 , -16777195 , -16776937 , -16776683 , -16776686 , -16711413 , -16449532 , 
-16646126 , -16711662 , -16776940 , -16776940 , -16776940 , -16776944 , -16711413 , -16449532 , 
-16449523 , -16580594 , -16711664 , -16776944 , -16776944 , -16776947 , -16645881 , -16384000 , 
-16449529 , -16449527 , -16646133 , -16776693 , -16776695 , -16776442 , -16645632 , -16383744 , 

해당 값을 16.로 지정합니다. 다음 코드를 작성했습니다. 어떻게해야합니까? 나는 당신이 int와 직접 Image 값을 설정해야할지 모르겠어요

/* 
    * To change this template, choose Tools | Templates 
    * and open the template in the editor. 
    */ 
    package testing; 

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

    /** 
    * 
    * @author pratibha 
    */ 
    public class ConstructImage{ 
     int[][] PixelArray; 
     public ConstructImage(){ 
      try{ 

      BufferedImage bufferimage=ImageIO.read(new File("C:\\photo\\Modified\\image_1.jpg")); 
      int height=bufferimage.getHeight(); 
      int width=bufferimage.getWidth(); 
      PixelArray=new int[width][height]; 
      for(int i=0;i<width;i++){ 
       for(int j=0;j<height;j++){ 
        PixelArray[i][j]=bufferimage.getRGB(i, j); 
       } 
      } 
      ///////create Image from this PixelArray 

      for(int i=0;i<8;i++){ 
       for(int j=0;j<8;j++){ 
        PixelArray[i][j]=16; 
       } 
      } 
      for(int i=0;i<8;i++){ 
       for(int j=0;j<8;j++){ 
        System.out.print(PixelArray[i][j]+" , "); 
       } 
       System.out.println(""); 
      } 






      BufferedImage bufferImage2=new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB); 
       for(int x=0;x<width;x++){ 
        for(int y=0;y<height;y++){ 
       bufferImage2.setRGB(x, y, PixelArray[x][y]); 

       } 
      } 

      File outputfile = new File("C:\\photo\\Modified\\img.jpg"); 
      ImageIO.write(bufferImage2, "jpg", outputfile); 
      /////////////////////////////////////////////////////////////////// 
        BufferedImage bufferimage1=ImageIO.read(new File("C:\\photo\\Modified\\img.jpg")); 
      int height1=0; 
      height1=bufferimage1.getHeight(); 
      int width1=0; 
      width1=bufferimage1.getWidth(); 
      int[][] PixelArray1=new int[width1][height1]; 
      for(int i=0;i<width1;i++){ 
       for(int j=0;j<height1;j++){ 
        PixelArray1[i][j]=bufferimage1.getRGB(i, j); 
       } 
      } 
      ///////create Image from this PixelArray 

      for(int i=0;i<8;i++){ 
       for(int j=0;j<8;j++){ 
        System.out.print(PixelArray1[i][j]+" , "); 
       } 
       System.out.println(); 
      } 
      System.out.println(Integer.toBinaryString(222)); 
      System.out.println(Integer.signum(93226268));; 
      } 
      catch(Exception ee){ 
       ee.printStackTrace(); 
      } 
     } 

     public static void main(String args[]){ 
      ConstructImage c=new ConstructImage(); 
     } 
    } 
+0

도와주세요 .. – Sumit

+0

질문에 불명확합니다. 이미지를 복사 하시겠습니까? 하위 이미지를 추출 하시겠습니까? 이미지를 변환 하시겠습니까? 콘솔에 인쇄 하시겠습니까? – Matt

+0

사실 나는 값을 먼저 설정하고 싶습니다. 8 * 8 블록의 이미지를 스 레그 노 그래피 목적으로 사용하려합니다. – Sumit

답변

1

... 난 당신이 정말로 원하는 것은에 "그레이 스케일 값 16"을 설정하는 것입니다 같아요. 실제로하는 일은 픽셀을 "빨간색 0, 녹색 0, 파란색 16"으로 설정하는 것입니다.

먼저 Color(16,16,16)을 생성 한 다음 getRGB()을 호출하십시오.

BTW, JPG는 lossy compression이므로 저장 /로드 할 때 정확하게 일치하는 값을 기대할 수는 없습니다. 대신 PNG를 사용하는 것이 좋습니다.

덧붙여서 제 3 자 라이브러리의 API 요구 사항을 충족시키지 않으려면 배열 복사본을 만들지 말고 BufferedImage 클래스에 직접 액세스하는 것이 좋습니다. 압축되지 않은 이미지의 경우 이 실제로으로 큰 이 실제로 일 수 있으며 불필요하게 메모리 사용량이 두 배가되어 메모리가 부족합니다.

관련 문제