2016-07-15 73 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