[英]How do I change the LSB of each pixel according to my message
I am trying to implement a simple encoding program where I can hide a message in the LSB of the pixels of an image. 我正在尝试实现一个简单的编码程序,在该程序中我可以在图像像素的LSB中隐藏一条消息。 So far I've got the byte array from the message
到目前为止,我已经从消息中获得了字节数组
private static byte[] ConvertMessageToByte(String message,
byte[] messageBytes) {
// takes in the message and stores them into bytes
// returns message byte array
byte[] messageByteArray = message.getBytes();
return messageByteArray;
}
I have also got the byte array for the corresponding image that i want to encode onto 我也有要编码到的对应图像的字节数组
private static byte[] getPixelByteArray(BufferedImage bufferedImage) {
WritableRaster raster = bufferedImage.getRaster();
DataBufferByte buffer = (DataBufferByte) raster.getDataBuffer();
return buffer.getData();
}
Till this point I don't quite understand my following steps after. 到目前为止,我还不太了解接下来的步骤。 Do I iterate through the image byte array and store each ARGB values in another byte array?
是否要遍历图像字节数组并将每个ARGB值存储在另一个字节数组中? Also how would I apply the message bit values to the pixels?
另外,如何将消息位值应用于像素?
private static byte[] ConvertMessageToByte(String message, byte[] messageBytes) {
byte[] messageByteArray = message.getBytes();
return messageByteArray;
}
Regarding this method: convertMessageToBytes
is a better name as a lowercase first letter is more conventional as well as the fact that you are producing an array of multiple bytes and not just one. 关于此方法:
convertMessageToBytes
是一个更好的名称,因为小写的首字母更为常规,而且您生成的是多个字节的数组,而不仅仅是一个字节。 This method does not require the second byte[]
parameter as it can be simplified to return message.getBytes();
此方法不需要第二个
byte[]
参数,因为它可以简化为return message.getBytes();
and produce the same effect. 并产生相同的效果。 Furthermore,
String.getBytes()
is typically called in the parent function of this as it is not widely considered worthy of wrapping given that is one line. 此外,通常在此方法的父函数中调用
String.getBytes()
,因为考虑到只有一行,它不被普遍认为值得包装。 In conclusion: remove this method and use byte[] ba = s.getBytes();
结论:删除此方法,并使用
byte[] ba = s.getBytes();
in your main code instead. 在您的主代码中。
Personally, I process images as 3_BYTE_RGB
as that is how they are most commonly thought of and, indeed, represented on a physical monitor or printer and stored in the case of many image formats. 就我个人而言,我将图像处理为
3_BYTE_RGB
因为这是人们最常想到的方式,并且实际上将其表示在物理监视器或打印机上,并以多种图像格式存储。 HSL (LSB) is typically a user-end representation of colour. HSL(LSB)通常是颜色的用户端表示。 You should be thinking in RGB not HSL.
您应该考虑使用RGB,而不是HSL。
Instead of dealing with the image as a byte[]
, use int BufferedImage.getRGB(int x, int y)
as it's easier to do and easier to access. 与其将图像作为
byte[]
,还不如使用int BufferedImage.getRGB(int x, int y)
因为这样做更容易访问。 The following function may be of use. 以下功能可能有用。 I'll let you write the reverse.
我让你写反面。
private static int[] getColourAt(BufferedImage image, int x, int, y) {
int rgb = image.getRGB(x, y);
int r = rgb >> 16;
int g = (rgb >> 8) % 256;
int b = rgb % 256;
return new int[] {r, g, b};
}
From there, you should loop through each pixel and adjust as you like. 从那里,您应该遍历每个像素并根据需要进行调整。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.