我正在开发一个需要Ricoh Theta 实时预览的应用程序。理光 Theta 的医生什么也没说。我找到了一个在javascript中运行它的请求,但到目前为止还没有c#运行它。
首先,我使用camera._getLivePreview发送了 http 请求,它正在工作(至少我没有错误):
var url = requests("commands/execute");
request = WebRequest.Create(url);
request.ContentType = "application/json";
request.Method = "POST";
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
string json = "{\"name\":\"camera._getLivePreview\"," +
"\"parameters\":{\"sessionId\":\"" + sid + "\"}}";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
然后我尝试使用我在这里找到的解决方案来打印响应中以字节为单位的图像:
var response = request.GetResponse().GetResponseStream();
BinaryReader reader = new BinaryReader(new BufferedStream(response), new System.Text.ASCIIEncoding());
List<byte> imageBytes = new List<byte>();
bool isLoadStart = false;
byte oldByte = 0;
while (true)
{
byte byteData = reader.ReadByte();
if (!isLoadStart)
{
if (oldByte == 0xFF)
{
// First binary image
imageBytes.Add(0xFF);
}
if (byteData == 0xD8)
{
// Second binary image
imageBytes.Add(0xD8);
// I took the head of the image up to the end binary
isLoadStart = true;
}
}
else
{
// Put the image binaries into an array
imageBytes.Add(byteData);
// if the byte was the end byte
// 0xFF -> 0xD9 case、end byte
if (oldByte == 0xFF && byteData == 0xD9)
{
// As this is the end byte
// we'll generate the image from the data and can create the texture
// imageBytes are used to reflect the texture
// imageBytes are left empty
// the loop returns the binary image head
isLoadStart = false;
}
}
oldByte = byteData;
byteArrayToImage(imageBytes.ToArray());
}
}
由于它没有打印任何内容,我尝试制作一种将 imageurl 发送到图像 src 的方法(使用此处找到的代码):
public void byteArrayToImage(byte[] byteArrayIn)
{
string imreBase64Data = Convert.ToBase64String(byteArrayIn);
string imgDataURL = string.Format("data:image/png;base64,{0}", imreBase64Data);
imgPreview.Source = imgDataURL;
}
图像的XAML:
<Image Source=""
x:Name="imgPreview"/>
如何使它工作?
编辑1:基本上,我认为这里真正的问题是“如何从字节数组打印图像?”
编辑2:我发现布局仅在第一个调用方法结束后更新,并且通过包显示带有url的流会更容易