gpt4 book ai didi

java - 从 j2me 读取图像到 c++

转载 作者:行者123 更新时间:2023-11-30 17:56:56 25 4
gpt4 key购买 nike

我正在学习 C++,以便创建一个显示图像流的小应用程序。这些图像来自 j2me 设备,它没有存储为文件(所以我只有字节)。

我想我需要首先以 int 形式发送图像的大小,以便客户端知道要在流中读取该特定图像的量。

我的问题是大小总是太大 - 当我最初尝试读取大小时,不是图像的大小(我在 java 服务器中以 socket.write(int) 发送此长度,并尝试了 dataoutputstream。写Int)。我将发布一些代码,因为它可能非常简单。

为什么尺寸与我发送的不同?

 ssize_t
readLine(int fd, char *buffer, size_t n)
{
ssize_t numRead, tt; /* # of bytes fetched by last read() */
size_t totRead; /* Total bytes read so far */
char *buf;
char ch;

if (n <= 0 || buffer == NULL) {

return -1;
}

buf = buffer; /* No pointer arithmetic on "void *" */

totRead = 0;
for (;;) {
numRead = read(fd, &ch, 1);
tt += numRead;
if (numRead == -1) {

return -1; /* Some other error */

} else if (numRead == 0) { /* EOF */
if (totRead == 0) /* No bytes read; return 0 */
return 0;
else /* Some bytes read; add '\0' */
break;

} else { /* 'numRead' must be 1 if we get here */
if (totRead < n - 1) { /* Discard > (n - 1) bytes */
totRead++;
*buf++ = ch;
}

if (ch == '\n')
break;
}
}

printf("read line %s ", buf);
fflush(stdout);
int test = (int)buf;
printf("read line int %i ", tt);
fflush(stdout);
*buf = '\0';
return totRead;

}

最佳答案

WBXML 定义了一种独立于平台的方式来写入 int 值:Multy-byte integers .

A multi-byte integer consists of a series of octets, where the most significant bit is the continuation flag and the remaining seven bits are a scalar value. The continuation flag indicates that an octet is not the end of the multi-byte sequence. A single integer value is encoded into a sequence of N octets. The first N-1 octets have the continuation flag set to a value of one (1). The final octet in the series has a continuation flag value of zero (0).
The remaining seven bits in each octet are encoded in a big-endian order, eg, most significant bit first. The octets are arranged in a big-endian order, eg, the most significant seven bits are transmitted first. In the situation where the initial octet has less than seven bits of value, all unused bits must be set to zero (0).
For example, the integer value 0xA0 would be encoded with the two-byte sequence 0x81 0x20. The integer value 0x60 would be encoded with the one-byte sequence 0x60.

我为 Java ME 和 Bada 做到了这一点,但用任何语言实现都非常简单。

关于java - 从 j2me 读取图像到 c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13101473/

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