我正在开发一个 android 应用程序,它将加速度计和重力传感器数据发送到 ac/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
变量将始终为空。
关于我哪里做错的任何想法?