2016-07-15 72 views
-1

我在這裏遇到了相當多的問題。讀取和寫入圖像後,我得到不同的像素值

當我讀取圖像,然後保存它,我得到相同的圖片。但是,當我打開像素值時,每個像素的值稍有不同(大約10個單位左右)。

爲什麼該像素會改變?我只讀取圖像,然後保存圖像,但不對像素進行更改。我創建它與格式RGB並保存爲PNGByteArrayOutputStream方法。

private void onCaptureImageResult(Intent data) { 
    Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
    thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes); 

    File destination = new File(Environment.getExternalStorageDirectory(), 
      System.currentTimeMillis() + ".jpg"); 

    FileOutputStream fo; 
    try { 
     destination.createNewFile(); 
     fo = new FileOutputStream(destination); 
     fo.write(bytes.toByteArray()); 
     fo.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    ivImage.setImageBitmap(thumbnail); 
} 

private void onSelectFromGalleryResult(Intent data) { 

     Bitmap bm=null; 
     if (data != null) { 
      try { 
       bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData()); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

     bmp = bm; 
    } 

public void save(View view){ 
    operation= Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(),bmp.getConfig()); 
    int size = bmp.getRowBytes() * bmp.getHeight(); 
    bytearrayoutputstream = new ByteArrayOutputStream(); 
    int[] gambarR = new int[size]; 
    int[] gambarG = new int[size]; 
    int[] gambarB = new int[size]; 
    int[] gambarA = new int[size]; 
    int k = 0; 

    for(int i=0; i<bmp.getWidth(); i++){ 
     for(int j=0; j<bmp.getHeight(); j++){ 
      int p = bmp.getPixel(i, j); 
      gambarR[k] = Color.red(p); 
      gambarG[k] = Color.green(p); 
      gambarB[k] = Color.blue(p); 
      gambarA[k] = Color.alpha(p); 
      k++; 
     } 
    } 
    int l = 0; 

    for(int i = 0; i<bmp.getWidth(); i++){ 
     for(int j = 0; j<bmp.getHeight();j++){ 
      operation.setPixel(i, j, Color.rgb(gambarR[l], gambarG[l], gambarB[l])); 
      l++; 
     } 
    } 
    String fileName = "_hasil.bmp"; 
    Long tsLong = System.currentTimeMillis()/1000; 
    String ts = tsLong.toString(); 
    String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath(); 
    File gambar = new File(baseDir + File.separator + ts + fileName); 
    try 
    { 
     gambar.createNewFile(); 
     fileoutputstream = new FileOutputStream(gambar); 
     fileoutputstream.write(bytearrayoutputstream.toByteArray()); 
     fileoutputstream.close(); 

    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 

    } 

    ivImage.setImageBitmap(operation); 
} 

我會告訴你圖像之間的區別。我只讀取並保存,不要更改像素。我需要像素沒有改變時,我把它保存。

this is the difference between the pixel of the image.

+0

你真的認爲我們知道嗎?你當然應該發佈你的代碼。 – greenapps

+0

@greenapps對不起,先生,我已經上傳我的代碼了。並給出圖像像素的ss。請幫助我,謝謝 – siahaanj

+0

沒有讀取圖像的代碼。並且在保存之前不顯示你放入bytearrayoutputstream的內容。請發佈我們可以測試的完整可重現代碼。您也沒有展示如何確定前後像素的值。 – greenapps

回答

1

正如其他人已經注意到,許多代碼,您張貼的似乎並不太有用,這說明你要麼沒有閱讀文檔,或沒有經過這個問題想透。

但是,具體問題似乎是,您將圖像保存爲有損壓縮格式(JPEG),在這種情況下,質量爲90%。 「有損」意味着根據定義,您將永遠無法完全恢復壓縮之前的位圖。即使將JPEG質量設置爲100%也不太可能使您獲得與以前的壓縮完全相同的位圖。

如果您在讀取文件時想返回完全相同的值,則需要編寫無損格式,例如PNG或BMP。

+0

我只有該代碼才能讀取和寫回圖像。或者也許你可以給我一些代碼來讀取和寫入Android的圖像?我需要這麼多,我需要一個穩定的像素值,然後我可以加密和解密圖像。請幫助我 – siahaanj

+0

使用CompressFormat.PNG而不是CompressFormat.JPG例如,請參閱http://stackoverflow.com/questions/8417034/how-to-make-bitmap-compress-without-change-the-bitmap-size – GreyBeardedGeek