0

我正在使用 unity3d 中的 base64 字符串对 facebook 缩略图图像数据进行编码和解码。当用户没有头像时,生成的图像是一个红色问号。有什么方法可以识别正在发送的无效图片,以便我可以用我选择的默认图片替换它?

人没有脸书头像时解码得到的结果

我正在使用 c# 中的 Convert.FromBase64String (字符串编码)将字符串转换为图像数据。

4

3 回答 3

1

我假设您使用一些 API 从 URL 中检索 base64 编码的字符串?

在这种情况下,您可以将检索到的字符串一次转储到控制台,然后将其复制到您的源代码中,并将其与您将来获得的字符串进行比较。当然,如果您使用的 facebook API 决定提供不同的图标,这将中断,在这种情况下,您将不得不转储新的“未知用户”缩略图。

string encoded = ... // however you obtain your thumbnail
print encoded; // dump the string to the console once. remove this statement later
if (encoded == "...here comes the (rather large) string you just copied")
    encoded = "...here comes some other image you like to use, encoded as string";
...

不是很优雅,但至少很容易实现。

于 2013-05-29T16:44:55.353 回答
0

我想,如果在不存在个人资料图片时可以预见地返回错误图像(例如“红色问号”图像),您可以简单地测试这种情况并用您选择的另一个图像替换。您可以选择将红色问号图像存储为资源,然后将其 Base64 字符串与请求返回的每个图像的 Base64 进行比较。

在这种情况下,代码的关键部分可能看起来像这样(假设您已经将图像的 Base64 字符串存储在资源中):

ResourceManager rm =
    new ResourceManager("ExampleAppData", typeof(ExampleApp).Assembly);

String errorImageBase64 = rm.GetString("ErrorImageBase64");

// the image you get from your request
String resultImageBase64 = GetProfileImageBase64();

Image missingProfile;
if(resultImageBase64.Equals(errorImageBase64))
{
    missingProfile = ImageFromBase64String(rm.GetString("MissingProfileBase64"));
}
else
{
    missingProfile = ImageFromBase64String(resultImageBase64);
}

参考资料:http:
//msdn.microsoft.com/en-us/library/2xsy4hac.aspx
http://ozgur.ozcitak.com/snippets/2009/12/21/base64-encoding-an-image-with-csharp .html

于 2013-05-29T17:24:46.083 回答
0

根据我的经验,这是由于错误的请求。在我的情况下,/{fbId}/picture?g&type=square&height=128&width=128 我解决了返回红色问号的错误 API 查询,删除了“?g”,现在的工作查询是 /{fbId}/picture?type=square&height=128&width=128

于 2018-08-31T07:40:49.967 回答