0

EDIT : I've included more of my actual code for clarity.

Consider this example code snippet

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etext = findViewById(R.id.editText2);
        btn = (Button) findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
                startActivityForResult(gallery, PICK_IMAGE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){
            imageUri = data.getData();
            try {
                bmp = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
                bmp=bmp.copy(Bitmap.Config.ARGB_8888 , true);
                encode(bmp);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void encode(Bitmap bm)
    {
        int height = bm.getHeight();
        int width = bm.getWidth();
        int pixels[] = new int[height * width];
        bm.getPixels(pixels, 0, width, 0, 0, width, height);
        pixels[pixels.length - 1] = (byte) 1;
        pixels[pixels.length - 2] = (byte) 2;
        pixels[pixels.length - 3] = (byte) 3;
        bm.setPixels(pixels, 0, width, 0, 0, width, height);
    }

When I put output statements before setPixels(), I get correct values - the values that I want to store.

After setPixels(), in another part of the code I'm reading the same image. Over there when I read the values from indices I've modified, I get completely different values. The values I stored are overwritten.

  1. I have tried both - typecasting to byte and not. At multiple places. Didn't solve my issue.

  2. The values I'm storing at those indices are in the range 0-127, ASCII range. Well within the byte range.

sonofel
  • 112
  • 3
  • 8
  • What are you trying to achieve? Right now you only seem to be setting the last three pixels to a transparent-ish color. – Nicolas May 11 '20 at 13:09
  • @Nicolas I'm attempting to create a steganography app. In my actual code I select different indices, but for this example I just changed the last 3 values. – sonofel May 11 '20 at 13:14
  • the question requires more information. Kindly attach the snippet of your activity which is using this piece of code – Android1005 May 11 '20 at 13:14
  • @sonofel You say you're setting values in the 0-127 range to the pixels, but these are all transparent. Colors are stored in 32-bit ARGB, so in this case the alpha value will always be 0. – Nicolas May 11 '20 at 13:20
  • 1
    @Android1005 I have edited the post – sonofel May 11 '20 at 13:22
  • @Nicolas I don't exactly understand what you mean. I'm a newcomer to all this and some online examples I saw gave me the confidence that I could directly manipulate pixel values this way. I would greatly appreciate it if you elaborated on your answer. – sonofel May 11 '20 at 13:25
  • Not really sure but you can look into this link once, if it might solve your issue https://stackoverflow.com/questions/19019825/android-setpixels-explanation-and-example – Android1005 May 11 '20 at 13:28
  • @sonofel You could use `Color.argb` to set the colors instead. Take a look at [the docs](https://developer.android.com/reference/android/graphics/Color) to understand how colors are encoded. – Nicolas May 11 '20 at 13:41

0 Answers0