带C#的Sony Camera API

时间:2015-04-01 08:09:12

标签: c# camera sony sony-camera-api

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

我现在正在尝试自己做所有事情,但我不确定是否需要将Camera API文件附加到我的解决方案中,如果我在哪里可以找到确切的文件,因为我下载的API文件只包含文件对于Android和iOS,它不会帮助我。

1 个答案:

答案 0 :(得分:3)

我终于让我的东西工作了,我试图以这样的方式把它放在容易理解的地方。如果有人想要我写的索尼图书馆,请告诉我,因为我也尝试过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);
    }`