How to change the color value of a pixel in a bitmap and then extract it?

How can I change and access the color value that is stored at a specific position in the image?

I tried to save the image with the pixel in coordinates (10, 10). I changed the red color to 65 to represent the ASCII A character. But when I tried to extract the value from position (10, 10), the red pixel value was not equal to the expected value.

Here is my code to change the image:

 int pixelColor = bitmapImage.getPixel(10, 10); int pixelAlpha = Color.alpha(pixelColor); int red = 65; // represents character A int green = Color.green(pixelColor); int blue = Color.blue(pixelColor); int new_pixel = Color.argb(pixelAlpha, red, green, blue); bitmapImage.setPixel(10, 10, new_pixel); 

And here is my code to extract the value:

 String data = ""; int pixelColor = bitmapImage.getPixel(10, 10); int red = Color.red(pixelColor); data += (char)red; Toast.makeText(this, "Data: " + data, 20).show(); 

So how can I extract the specified value? I want to replace the red value with an ASCII character. Am I implementing the (LSB) algorithm correctly?

Here is my complete program:

 public void saveImage(Bitmap bitmapImage, String name) throws IOException { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes); String fName = "/mnt/sdcard/pictures/" + name + ".jpg"; File f = new File(fName); f.createNewFile(); FileOutputStream fo = new FileOutputStream(f); fo.write(bytes.toByteArray()); fo.flush(); fo.close(); } private Bitmap HideMessage(Bitmap src) { Bitmap dest = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig()); for(int x = 0; x < src.getWidth(); x++) { for(int y = 0; y < src.getHeight(); y++) { dest.setPixel(x, y, src.getPixel(x, y)); } } int pixelColor = src.getPixel(10, 10); int pixelAlpha = Color.alpha(pixelColor); int red = 65; // represent character A int green = Color.green(pixelColor); int blue = Color.blue(pixelColor); int new_pixel = Color.argb(pixelAlpha, red, green, blue); dest.setPixel(10, 10, new_pixel); return dest; } public void Hide() { Bitmap dest = HideMessage(image); try { saveImage(dest, "hidden"); Toast.makeText(this, "Message Hidden in Image and saved", Toast.LENGTH_LONG).show(); } catch (IOException e) { Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show(); } } public void Show_character() { Bitmap dest = BitmapFactory.decodeFile("/mnt/sdcard/pictures/hidden.jpg"); String data = ""; int pixelColor = dest.getPixel(10,10); int red = Color.red(pixelColor); data += (char)red; Toast.makeText(this, "Data: " + data, 20).show(); } 
+4
source share

Source: https://habr.com/ru/post/1433238/


All Articles