-1

I'm having quite a problem here.

When I read image and then save it, I get the same picture. But when I open the pixel value, the value of each pixel is slightly different(larger or smaller around 10 units).

Why did that pixel change? I only read the image, then save it, I don't make changes to the pixel. I create it with format RGB and save as a PNG with ByteArrayOutputStream method.

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

I will show you the difference between the image. I only read and save, and don't change the pixel. I need the pixel didn't change when I save it back.

this is the difference between the pixel of the image.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
siahaanj
  • 23
  • 4
  • Do you really think we know that? You should post your code of course. – greenapps Jul 15 '16 at 10:59
  • @greenapps sorry sir, i have upload my code now. and i give the ss of the image pixel. help me please, thanks – siahaanj Jul 15 '16 at 11:21
  • There is no code that reads an image. And you do not show what you put in the bytearrayoutputstream before saving. Please post complete reproducable code that we can test out. You are also not showing how you determine the value of the pixels before and after. – greenapps Jul 15 '16 at 11:29
  • If you are not changing the image then i wonder what `operation.setPixel()` should do. Please explain what your code does. – greenapps Jul 15 '16 at 11:37
  • @greenapps i have post my code for read image and what i put in bytearrayoutputstream. for determine the value i use phyton. each pixel consist of 'R,G,B'. that my code sir, but why the value of the pixel different ? – siahaanj Jul 15 '16 at 12:22
  • @siahaanj why are you duplicating [your own question](http://stackoverflow.com/questions/38339022/image-decryption-using-hill-cipher-in-android)? And what have you tried so far, in terms of debugging this, of course? – Sufian Jul 15 '16 at 12:34
  • You use python to compare. On your PC? You are comparing files? Which files exactly? Filenames please. – greenapps Jul 15 '16 at 12:34
  • Reading image ? Where do you read an image? Is see you have a capture image result and a select from gallery result. Why two? Please stick with one example. ANd tell what you are doing. You let us guess now. – greenapps Jul 15 '16 at 12:37
  • `bmp = bm;` You are only doing that in `onSelectFromGalleryResult`. After that you use `bmp` in the `save`function. I do not undersdtand what `onCaptureImageResult` has to do with this all. You should explain much better what you are doing. – greenapps Jul 15 '16 at 12:45
  • `destination.createNewFile();` and `gambar.createNewFile();`. You better remove those statements. – greenapps Jul 15 '16 at 12:48
  • @greenapps sir, im doing image encryption. but when i decrypting the image, what i got only more broken image. so i choose for checking one by one. and what i got, when i read a new image with my code, then save it, i got a difference pixel value. although I do not make changes to its pixels. onSelectFromGalleryResult use for picking an image from gallery. then i read the pixel in public save function, after that i save that image with gambar.createNewFile(); function and save it in my destination folder with destination.createNewFile();. – siahaanj Jul 15 '16 at 20:34
  • @Sufian i tried to have a stabil pixel value. then i cant use this code for encrypting and decrypting my project. – siahaanj Jul 15 '16 at 20:35
  • 1
    `i save that image with gambar.createNewFile(); `. Nonsense. That statement does nothing. I even said you to remove it from your code. You also did not answer the most of my questions. For the rest you should not in this way pick an image from the gallery but just load an image drectly from file knowing its file path. And.. Why dont you tell how you checked the pixel values? Do i have to ask that again? Linux? Python? Where? Which files? Answer all my questions! – greenapps Jul 15 '16 at 21:07
  • i check it with python, the file plain and filesave @greenapps – siahaanj Jul 16 '16 at 07:17
  • You are not seriously answering on my comments so i cannot help you. – greenapps Jul 16 '16 at 07:21
  • You are compressing the image at 90 percent quality. Of course the image data will change if you reduce the quality of the image. – Xaver Kapeller Jul 16 '16 at 20:35
  • And also you are using jpeg not png... What you think this does: `thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);`? – Xaver Kapeller Jul 16 '16 at 20:37
  • @XaverKapeller thanks for your solution. but even i change the format into thumbnail.compress(Bitmap.CompressFormat.PNG, 100, bytes); i still get a difference pixel value. – siahaanj Jul 17 '16 at 08:41

1 Answers1

2

As others have noticed, much of the code that you posted appears to do not much useful, indicating that you either haven't read the documentation, or haven't thought through the problem thoroughly.

However, the specific problem appears to be that you are saving your image in a lossy compression format (JPEG), in this case, at 90% quality. "Lossy" means that by definition you will never get back exactly the bitmap that you had before compression. Even setting JPEG quality to 100% is unlikely to get you exactly the same bitmap as before compression.

If you want exactly the same values back when reading the file, you'll need to write a lossless format, such as PNG or BMP.

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
  • i only have that code to read and write back the image. or maybe can you give me some code to read and write image in android? i need it so much, i need a stabil pixel value, then i can encrypt and decrypt the image. pls help me – siahaanj Jul 17 '16 at 08:39
  • Use CompressFormat.PNG instead of CompressFormat.JPG For an example, see http://stackoverflow.com/questions/8417034/how-to-make-bitmap-compress-without-change-the-bitmap-size – GreyBeardedGeek Jul 17 '16 at 15:24