2016-08-31 44 views
0

在我的項目中獲取圖像的每個像素值,然後對像素執行解密併爲像素設置新值。但是在創建具有改變的像素值的新圖像之後,新圖像不能包含相同的像素值,而是改變它的原樣。主要像素值是變化的。如何解決這個問題呢。 我的代碼是繪製圖像後設置像素更改

 for(i=0;i<width;i++){ 
     for(int j=0;j<height;j++){ 

      //int p1 = pix[i][j]; 
      int a1 = ealph[i][j];   
      int r3 = ered[i][j]; 
      int g1 = ered[i][j]; 
      int b1 = ered[i][j]; 


      int p1 = (a1<<24) | (r3<<16) | (g1<<8) | b1; 
       img.setRGB(i, j, p1); 

     } 

    } 

    try{ 
     f = new File("C:\\Users\\chukkapalli\\workspace\\Des_Encryption\\decripted_img.jpg"); 
     ImageIO.write(img, "jpg", f); 
     }catch(IOException e){ 
     System.out.println(e); 
     } 


    f = null; 

img = null; 
    try{ 
      f = new File("C:\\Users\\chukkapalli\\workspace\\Des_Encryption\\decripted_img.jpg"); 
      img = ImageIO.read(f); 
     }catch(IOException e){ 
      System.out.println(e); 
     } 
    width = img.getWidth(); 
    height = img.getHeight(); 

    for(i=0;i<width;i++){ 
      for(int j=0;j<height;j++){ 

       int p1 = img.getRGB(i,j); 
       e1alph[i][j] = (p1>>24)&0xff; 
       e1red[i][j] = (p1>>16)&0xff; 
       egreen[i][j] = (p1>>8)&0xff; 
       eblue[i][j] = p1&0xff; 

       System.out.println("orginal image- "+ealph[i][j]+" "+ered[i][j]+" "+ered[i][j]+" "+ered[i][j]+" created -- "+e1alph[i][j]+" "+e1red[i][j]+" "+egreen[i][j]+" "+eblue[i][j]); 

      } 

     } 

輸出是

 orginal image- 0 33 33 33 created -- 255 35 35 35 
    orginal image- 0 38 38 38 created -- 255 41 41 41 
    orginal image- 0 140 140 140 created -- 255 142 142 142 
    orginal image- 0 81 81 81 created -- 255 51 51 51 
    orginal image- 0 22 22 22 created -- 255 21 21 21 
    orginal image- 0 127 127 127 created -- 255 135 135 135 
    orginal image- 0 126 126 126 created -- 255 127 127 127 
    orginal image- 0 63 63 63 created -- 255 62 62 62 
    orginal image- 0 244 244 244 created -- 255 250 250 250 
    orginal image- 0 11 11 11 created -- 255 0 0 0 
    orginal image- 0 13 13 13 created -- 255 11 11 11 
    orginal image- 0 171 171 171 created -- 255 175 175 175 
    orginal image- 0 126 126 126 created -- 255 130 130 130 
    orginal image- 0 8 8 8 created -- 255 0 0 0 
    orginal image- 0 6 6 6 created -- 255 14 14 14 
    orginal image- 0 248 248 248 created -- 255 244 244 244 
    orginal image- 0 109 109 109 created -- 255 128 128 128 
    orginal image- 0 107 107 107 created -- 255 86 86 86 
    orginal image- 0 50 50 50 created -- 255 48 48 48 
    orginal image- 0 104 104 104 created -- 255 131 131 131 
    orginal image- 0 110 110 110 created -- 255 95 95 95 
    orginal image- 0 127 127 127 created -- 255 128 128 128 

如何創建圖像與創建後出變化的像素值。 謝謝。

回答

1

嘗試使用像PNG這樣的無損格式。 JPG在壓縮時改變像素值,不能完全像它被創建的那樣被讀取。

+0

非常感謝你兄弟。它完美的工作。 –