Note that there are some explanatory texts on larger screens.

plurals
  1. POandroid saving and loading PNG bitmaps differences
    primarykey
    data
    text
    <p>I'm trying to solve my problem for a several days now and I couldn't figure it out. My application uses a Bitmap to process image's pixels in a similar way steganography does. After I modify Bitmap and get lets say ModifiedBitmap and check the pixel values everything is correct. Problems begins after I save the Bitmap as PNG with compress method. When I load ModifiedBitmap back to memory pixel values are changed. As I store the Bitmap in a PNG format with ARGB_8888 I assumed that the values will be the same after saving and loading the image in PNG format.</p> <p>For more detail I'm loading the bitmap with the following code:</p> <pre><code>BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inDither = false; opt.inPreferredConfig = Bitmap.Config.ARGB_8888; BitmapFactory.decodeFile(picturePath, opt) </code></pre> <p>Whatever the Bitmap format is I process it to get the right Bitmap format for my purposes. My aim is to get Bitmap that has alpha and is in the ARGB_8888 config. I prepare new Bitmap with the following:</p> <pre><code>int width = this.imageToProcess.getWidth(); int height = this.imageToProcess.getHeight(); Bitmap dest = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); for (int i = 0; i &lt; width; i++) { for (int j = 0; j &lt; height; j++) { int pixel = this.imageToProcess.getPixel(i, j); int newColor = 0; if (this.imageToProcess.hasAlpha()) { newColor = Color.argb(Color.alpha(pixel), Color.red(pixel), Color.green(pixel), Color.blue(pixel)); } else { newColor = Color.argb(255, Color.red(pixel), Color.green(pixel), Color.blue(pixel)); } dest.setPixel(i, j, newColor); } } </code></pre> <p>When I use such a prepare method I always get the right format of the Bitmap with the right parameters and configuration. So now, the Bitmap has config ARGB_8888 and has the alpha values. Lets say I've got the PreparedBitmap. I also checked the Bitmap properties and it is exactly ARGB_8888 and hasAlpha is true. So far now, it is all as it should be. Then I make some sort of pixel modifing on the PreparedBitmap and it is ready to be saved. I use the following code for saving the Bitmap as a PNG:</p> <pre><code>String path = Environment.getExternalStorageDirectory().toString(); OutputStream fOut = null; File file = new File(path, "image.png"); fOut = new FileOutputStream(file); modifiedBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut); fOut.flush(); fOut.close(); MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName()); </code></pre> <p>After that I have stored PNG image file. Then I want to load ModifiedBitmap again and process the Bitmap in inverse manner. My prerequisitie is that the ModifiedBitmap has exactly same pixel values as I modified it in the first step. I load the Bitmap through the same procedure:</p> <pre><code>BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inDither = false; opt.inPreferredConfig = Bitmap.Config.ARGB_8888; BitmapFactory.decodeFile(picturePath, opt) </code></pre> <p>But he loaded ModifiedBitmap when it was saved before has different pixel values. Also it hasn't alpha values (hasAlpha is false) but the config is the same ARGB_8888. Does anybody know why is that? It is ARGB_8888 Bitmap so there shouldn't be a differences in pixel values. Does android compress PNG Bitmap somehow? How should I save the PNG images to prevent from pixel values changes?</p> <p>If the ModifiedBitmap pixel values is in ARGB_8888 (128, 128, 128, 128) and I save it through Bitmap.compress method I want to get the same values with alpha after loading it again. I'm out of all ideas how to do that.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload