gpt4 book ai didi

java - 如何在使用字节数组更改后将图像字节数组转换为位图

转载 作者:行者123 更新时间:2023-12-02 11:36:16 24 4
gpt4 key购买 nike

我正在尝试在 Android 中实现隐写术,并且我已经实现了对图像进行逐位编码的代码。

private Bitmap add_text(Bitmap image, String text)
{
//convert all items to byte arrays: image, message, message length
byte img[] = get_byte_data(image);
byte msg[] = text.getBytes();
byte len[] = bit_conversion(msg.length);
displaybit(img, "Start");

try
{
encode_text(img, len, 0); //0 first positiong
encode_text(img, msg, 32); //4 bytes of space for length: 4bytes*8bit = 32 bits

return BitmapFactory.decodeByteArray(img, 0, img.length);
}
catch(Exception e)
{
e.printStackTrace();
}
return image;
}

private byte[] get_byte_data(Bitmap image)
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, stream);

return stream.toByteArray();
}

private byte[] bit_conversion(int i)
{
//originally integers (ints) cast into bytes
//byte byte7 = (byte)((i & 0xFF00000000000000L) >>> 56);
//byte byte6 = (byte)((i & 0x00FF000000000000L) >>> 48);
//byte byte5 = (byte)((i & 0x0000FF0000000000L) >>> 40);
//byte byte4 = (byte)((i & 0x000000FF00000000L) >>> 32);

//only using 4 bytes
byte byte3 = (byte)((i & 0xFF000000) >>> 24); //0
byte byte2 = (byte)((i & 0x00FF0000) >>> 16); //0
byte byte1 = (byte)((i & 0x0000FF00) >>> 8 ); //0
byte byte0 = (byte)((i & 0x000000FF) );
//{0,0,0,byte0} is equivalent, since all shifts >=8 will be 0
return(new byte[]{byte3,byte2,byte1,byte0});
}

private byte[] encode_text(byte[] image, byte[] addition, int offset)
{
//check that the data + offset will fit in the image
if(addition.length + offset > image.length)
{
throw new IllegalArgumentException("File not long enough!");
}
//loop through each addition byte
for(int i=0; i<addition.length; ++i)
{
//loop through the 8 bits of each byte
int add = addition[i]; //0
for(int bit=7; bit>=0; --bit, ++offset) //ensure the new offset value carries on through both loops
{
//assign an integer to b, shifted by bit spaces AND 1
//a single bit of the current byte
int b = (add >>> bit) & 1;
//assign the bit by taking: [(previous byte value) AND 0xfe] OR bit to add
//changes the last bit of the byte in the image to be the bit of addition
image[offset] = (byte)((image[offset] & 0xFE) | b );
}
}
return image;
}

但是在将数据编码到图像中后,我无法形成位图。

我已经在java中实现了相同的算法并且运行良好:

private BufferedImage add_text(BufferedImage image, String text)
{
//convert all items to byte arrays: image, message, message length
byte img[] = get_byte_data(image);
byte msg[] = text.getBytes();
byte len[] = bit_conversion(msg.length);
try
{
encode_text(img, len, 0); //0 first positiong
encode_text(img, msg, 32); //4 bytes of space for length: 4bytes*8bit = 32 bits
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,
"Target File cannot hold message!", "Error",JOptionPane.ERROR_MESSAGE);
}
return image;
}

有人可以帮我弄清楚为什么字节数组在 android 的情况下无效吗?

最佳答案

您从 JPEG 数据的 byte[] 开始。然后,您可以修改该数据,半随机地更改位,而不考虑 JPEG 数据格式。然后,您尝试解码修改后的byte[]。毫不奇怪,BitmapFactory 无法对其进行解码,因为它不再是有效的图像格式。

我最好的猜测是您认为 byte[] 是 ARGB 格式的原始像素。

如果是这种情况,则在 Bitmap 上使用 getPixel()setPixel() 等方法来修改像素,并跳过位图编码和解码步骤。

关于java - 如何在使用字节数组更改后将图像字节数组转换为位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48913084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com