读取图像并计算其字节大小在 C 和 Go 中产生不同的结果:
使用相同的图像,这是我在 c 中的 readFile 函数:
FILE *inputFile = fopen(inputFilename, "rb");
if (inputFile == NULL)
{
printf("cannot open file %s", inputFilename);
return 0;
}
else
{
fseek(inputFile, 0, SEEK_END);
long fsize = ftell(inputFile);
rewind(inputFile);
return(fsize);
}
在 Go 中,相同的图像:
// requests the same image as above
req, _ := http.NewRequest("GET", url, nil)
resp, _ := client.Do(req)
// Read the image into memory and set up the bytes buffer
img, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
size := len(img)
fmt.Println("%s\n", size)
return size
图片的C长度为:2275674 在 Go 中,大小为:1901248
最佳答案
在 C 中,您正在读取原始文件。它不会被解释为图像文件,而只是字节的集合。
在 Go 中(我实际上并不熟悉),看起来你正在使用一个图像加载器,它将数据解释为图像,对其进行解码(如果它被压缩则解压缩),给出解码图像的大小.
两者通常会有所不同(除了某些原始图像类型外,可能总是不同)。
关于c - C 和 GO 之间的图像大小不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32570883/