Android save and load differences in PNG bitmaps

I’ve been trying to solve my problem for several days now and I couldn’t figure it out. My application uses Bitmap to process image pixels in a similar way as steganography does. After I changed Bitmap, and let's say ModifiedBitmap and check the pixel values, everything is correct. Problems begin after saving Bitmap as PNG using the compression method. When I load the ModifiedBitmap back into memory, the pixel values ​​change. When I saved the Bitmap in PNG format with ARGB_8888, I assumed that after saving and loading the PNG image, the values ​​would be the same.

For more details, I upload a bitmap using the following code:

BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inDither = false; opt.inPreferredConfig = Bitmap.Config.ARGB_8888; BitmapFactory.decodeFile(picturePath, opt) 

Regardless of the Bitmap format, I process it to get the correct Bitmap format for my purposes. My goal is to get a Bitmap that has alpha and is in the ARGB_8888 configuration. I am preparing a new bitmap with the following:

 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 < width; i++) { for (int j = 0; j < 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); } } 

When I use this preparation method, I always get the correct bitmap format with the correct settings and configuration. So, now Bitmap has an ARGB_8888 configuration and has alpha values. Let's say I have a PreparedBitmap. I also checked the Bitmap properties, and that is exactly ARGB_8888 and hasAlpha true. So far, this is all as it should be. Then I do the pixel modification on the PreparedBitmap and it is ready to save. I use the following code to save Bitmap as PNG:

 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()); 

After that, I saved the PNG image file. Then I want to download ModifiedBitmap again and process Bitmap in reverse. My guess is that ModifiedBitmap has exactly the same pixel values ​​as I changed it in the first step. I load Bitmap with the same procedure:

 BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inDither = false; opt.inPreferredConfig = Bitmap.Config.ARGB_8888; BitmapFactory.decodeFile(picturePath, opt) 

But he loaded ModifiedBitmap when it was saved before it has different pixel values. It also has no alpha values ​​(hasAlpha is false), but the configuration is the same as ARGB_8888. Does anyone know why? This is the ARGB_8888 bitmag, so there should be no difference in pixel values. Does Android Enhance PNG Bitmap? How to save PNG images to prevent changing pixel values?

If the ModifiedBitmap pixel values ​​are in ARGB_8888 (128, 128, 128, 128) and I save it through the Bitmap.compress method, I want to get the same values ​​with alpha after loading it. I do not know how to do that.

+4
source share

All Articles