使用 C# 的索尼相机 API

Sony Camera API with C#

我之前已经问过一个关于索尼相机的问题 API,我确实得到了一些帮助,但我仍然遇到问题。我找到了以下库 https://github.com/kazyx/kz-remote-api,有人将其与 Sony Camera API 一起使用,但我必须对其进行更改才能与 WPF 应用程序一起使用,因为它针对 windows 商店应用程序进行了优化.

我现在求助于自己做所有事情,但我不确定是否需要将相机 API 文件附加到我的解决方案中,如果我这样做,我在哪里可以找到确切的文件,因为 [=我下载的 19=] 文件只有 Android 和 iOS 的文件,其中对我没有帮助。

我终于让我的东西工作了,我试着用一种易于理解的方式来表达它。如果有人想要我写的索尼库,请告诉我,因为我也尝试过 Kazyx 库,但它对我不起作用。

我的代码如下。

    private string cameraURL;

    private bool recModeActive;

    public void ControlCamera(string cameraResp)
    {
        cameraURL = string.Format("{0}/{1}", GetCameraURL(cameraResp), GetActionType(cameraResp));
    }

    private string CameraRequest(string cameraUrl, string cameraRequest)
    {
        Uri urlURI = new Uri(cameraURL);

        HttpWebRequest cameraReq = (HttpWebRequest)WebRequest.Create(cameraURL);
        cameraReq.Method = "POST";
        cameraReq.AllowWriteStreamBuffering = false;
        cameraReq.ContentType = "application/json; charset=utf-8";
        cameraReq.Accept = "Accept-application/json";
        cameraReq.ContentLength = cameraRequest.Length;
        using (var cameraWrite = new StreamWriter(cameraReq.GetRequestStream()))
        {
            cameraWrite.Write(cameraRequest);
        }
        var cameraResp = (HttpWebResponse)cameraReq.GetResponse();
        Stream cameraStream = cameraResp.GetResponseStream();
        StreamReader cameraRead = new StreamReader(cameraStream);
        string readCamera = cameraRead.ReadToEnd();

        return readCamera;
    }

    public string GetCameraURL(string cameraResp)
    {
        string[] cameraXML = cameraResp.Split('\n');
        string cameraURL = "";
        foreach (string cameraString in cameraXML)
        {
            string getCameraURL = "";
            if (cameraString.Contains("<av:X_ScalarWebAPI_ActionList_URL>"))
            {
                getCameraURL = cameraString.Substring(cameraString.IndexOf('>') + 1);
                cameraURL = getCameraURL.Substring(0, getCameraURL.IndexOf('<'));
            }
        }
        return cameraURL;
    }

    public string GetActionType(string cameraResp)
    {
        string[] cameraXML = cameraResp.Split('\n');
        string actionType = "";
        foreach (string cameraString in cameraXML)
        {
            string getType = "";
            if (cameraString.Contains("<av:X_ScalarWebAPI_ServiceType>"))
            {
                getType = cameraString.Substring(cameraString.IndexOf('>') + 1);
                actionType = getType.Substring(0, getType.IndexOf('<'));
                if (actionType == "camera")
                {
                    break;
                }
            }
        }
        return actionType;
    }

    public string StartRecMode()
    {
        string startRecMode = JsonConvert.SerializeObject(new Camera.CameraSetup
        {
            method = "startRecMode",
            @params = new List<string> { },
            id = 1,
            version = "1.0"
        });

        recModeActive = true;

        return CameraRequest(cameraURL, startRecMode);
    }

    public string TriggerCamera()
    {
        string _triggerCamera = JsonConvert.SerializeObject(new Camera.StillCapture
        {
            method = "actTakePicture",
            @params = new List<string> { },
            id = 1,
            version = "1.0"
        });

        return CameraRequest(cameraURL, _triggerCamera);
    }

    public string EchoRequest()
    {
        string _echoRequest = JsonConvert.SerializeObject(new Camera.TestCameraComm
        {
            method = "getEvent",
            @params = new List<bool> { true },
            id = 1,
            version = "1.0"
        });

        return CameraRequest(cameraURL, _echoRequest);
    }

    public string StopRecMode()
    {
        string stopRecMode = JsonConvert.SerializeObject(new Camera.CameraSetup
        {
            method = "stopRecMode",
            @params = new List<string> { },
            id = 1,
            version = "1.0"
        });

        recModeActive = false;

        return CameraRequest(cameraURL, stopRecMode);
    }

    public string SetImageQuality()
    {
        string qualityReq = JsonConvert.SerializeObject(new Camera.CameraSetup
        {
            method = "setStillSize",
            @params = new List<string> { "4:3", "20M"},
            id = 1,
            version = "1.0"
        });

        recModeActive = false;

        return CameraRequest(cameraURL, qualityReq);
    }`