gpt4 book ai didi

android - 在android中将字节数组转换为位图有什么错误? (字节数组是从 c/c++ 服务器发送的,android 是客户端)

转载 作者:太空宇宙 更新时间:2023-11-04 00:07:41 24 4
gpt4 key购买 nike

我正在开发一个 android 应用程序,它将它的加速度计和重力传感器数据发送到 c/c++ 服务器,该服务器使用 openGL 库使 3D 形状在屏幕上旋转。我想从 OpenGL 屏幕发送回 Android 设备“快照”。

在 c/c++(服务器端)我这样做:

//declarations
struct PARAMS
{
unsigned char Pic[4*256*256]; // 4 because of the GL_RGBA format
char* msg;
};
PARAMS p;

// reading content of the screen
glReadPixels(0, 0, 256, 256, GL_RGBA, GL_UNSIGNED_BYTE, p.Pic);

//sending the data
send(sConnect,(char*)p.Pic,4*256*256,0);

在 android(客户端)上,我正在尝试读取字节数组并将其转换为位图。

//declarations
socket = new Socket("192.168.1.101", 1234);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
private byte Image[]= new byte[4 * 256 * 256 ];
private int IntImage[] = new int[4 * 256 * 256];
//read data which was sent from the server
dataInputStream.readFully(Image, 0, 256 * 256 * 4); //



//transform the byte array into int array
// i'm doing byte & 0xFF to convert from byte to unsigned byte( java doesn't have unsigned Byte)
// I'm doing this to convert from GL_RGBA to ARGB_8888
for (int i = 1; i < Image.length; i = i + 4) {
int j = i - 1;
aux = (Image[i + 3] & 0xFF);
IntImage[j + 3] = (int) (Image[i + 2] & 0xFF);
IntImage[j + 2] = (int) (Image[i + 1] & 0xFF);
IntImage[j + 1] = (int) (Image[i] & 0xFF);
IntImage[j] = aux;
}

//creating the bitmap:
Bitmap bmp = Bitmap.createBitmap(256, 256, Config.ARGB_8888);
bmp.setPixels(IntImage, 0, 256, 0, 0, 256, 256);

//creating the image based on the bitmap
imv = (ImageView) findViewById(R.id.imageView1);
imv.setImageBitmap(bmp);

在这种情况下输出是空白的(就像只有透明像素的图像)

如果我这样做:

bmp = BitmapFactory.decodeByteArray(Image, 0,Image.length);

bmp 变量将始终为空。

知道我哪里做错了吗?

最佳答案

我认为你有常见的字节顺序问题。

在小端或大端处理器上使用 glReadPixels 时,您的结果是不同的。

请注意 Java 使用 network byte order (大端)。用 C++ 编写的相同代码在 PowerPC、arm(大端)和 x86_64(小端)上表现不同。

关于android - 在android中将字节数组转换为位图有什么错误? (字节数组是从 c/c++ 服务器发送的,android 是客户端),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16625637/

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