How can I change and access the color value that is stored in a specific position of an image?
I tried to save an image with the pixel at coordinates (10, 10) changed. I altered the red color value to the value 65 to represent the ASCII character A
. But when I tried to extract the value from position (10, 10), the pixel's red value was not equal to the expected value.
Here is my code to alter 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 color 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();
}