【发布时间】:2011-01-13 04:05:12
【问题描述】:
以下是 Picasa 存储为哈希的详细信息。它像这样存储它们:
faces=rect64(54391dc9b6a76c2b),4cd643f64b715489
[DSC_2289.jpg]
faces=rect64(1680000a5c26c82),76bc8d8d518750bc
网上的信息是这样说的:
rect64() 中包含的数字是一个 64 位的十六进制数字。
- 将其分成四个 16 位数字。
- 将每个数字除以最大无符号 16 位数 (65535),您将得到 0 到 1 之间的四个数字。
- 剩下的四个数字为您提供面部矩形的相对坐标:(左、上、右、下)。
- 如果您想以绝对坐标结束,请将左右乘以图像宽度,将顶部和底部乘以图像高度。
所以我将其转换为 RectangleF 的代码可以正常工作(仅保留相对坐标):
public static RectangleF GetRectangle(string hashstr)
{
UInt64 hash = UInt64.Parse(hashstr, System.Globalization.NumberStyles.HexNumber);
byte[] bytes = BitConverter.GetBytes(hash);
UInt16 l16 = BitConverter.ToUInt16(bytes, 6);
UInt16 t16 = BitConverter.ToUInt16(bytes, 4);
UInt16 r16 = BitConverter.ToUInt16(bytes, 2);
UInt16 b16 = BitConverter.ToUInt16(bytes, 0);
float left = l16 / 65535.0F;
float top = t16 / 65535.0F;
float right = r16 / 65535.0F;
float bottom = b16 / 65535.0F;
return new RectangleF(left, top, right - left, bottom - top);
}
现在我有一个 RectangleF,我想把它变回上面提到的哈希值。我似乎无法弄清楚这一点。看起来 picasa 使用 2 个字节,包括精度,但是 C# 中的浮点数是 8 个字节,甚至 BitConverter.ToSingle 也是 4 个字节。
任何帮助表示赞赏。
编辑:这是我现在所拥有的
public static string HashFromRectangle(RectangleCoordinates rect)
{
Console.WriteLine("{0} {1} {2} {3}", rect.Left, rect.Top, rect.Right, rect.Bottom);
UInt16 left = Convert.ToUInt16((float)rect.Left * 65535.0F);
UInt16 top = Convert.ToUInt16((float)rect.Top * 65535.0F);
UInt16 right = Convert.ToUInt16((float)rect.Right * 65535.0F);
UInt16 bottom = Convert.ToUInt16((float)rect.Bottom * 65535.0F);
byte[] lb = BitConverter.GetBytes(left);
byte[] tb = BitConverter.GetBytes(top);
byte[] rb = BitConverter.GetBytes(right);
byte[] bb = BitConverter.GetBytes(bottom);
byte[] barray = new byte[8];
barray[0] = lb[0];
barray[1] = lb[1];
barray[2] = tb[0];
barray[3] = tb[1];
barray[4] = rb[0];
barray[5] = rb[1];
barray[6] = bb[0];
barray[7] = bb[1];
return BitConverter.ToString(barray).Replace("-", "").ToLower();
}
【问题讨论】:
-
由于没有 2 字节的浮点数据类型(在 C# techotopia.com/index.php/C_Sharp_Variables_and_Constants 中),您可以尝试将其转换为 short。转换必须是显式的 (msdn.microsoft.com/en-us/library/ybs77ex4%28VS.71%29.aspx) 否则编译器会抛出错误。
-
我也试过了,但我的哈希值并没有完全一样。我在上面发布我的代码,看看我是否做错了什么。
标签: c# picasa bitconverter