4

I have asked a previous question on SO with regards to the Sony Camera API and I did get some help but I am still having a problem. I found the following library https://github.com/kazyx/kz-remote-api that someone made to use with the Sony Camera API but I had to make changes to it to work with a WPF app as it was optimized for windows store apps.

I am now resorting to do everything myself but I am unsure if I need to attach a Camera API file to my solution and if I do where can I find the exact file because the one the API file that I downloaded only has files for Android and iOS in which won't help me.

GANDA1F
  • 307
  • 1
  • 4
  • 13
  • I am now able to to discover and my Sony A6000 and I can receive the XML document from it but my camera keeps showing on it "Connecting..." and my JSON requests keep throwing exceptions. – GANDA1F Apr 07 '15 at 06:16

1 Answers1

4

I finally got my thing working and I tried to put it in such a manner so that it is easily understandable. If anyone would like my Sony Library that I wrote that please let me know as I also tried the Kazyx library and that didn't work for me.

My code is below.

    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);
    }`
Neil Masson
  • 2,609
  • 1
  • 15
  • 23
GANDA1F
  • 307
  • 1
  • 4
  • 13
  • maybe you can share this on github? – Jinah Adam Sep 03 '15 at 05:40
  • Here is the link to the solution where I created the project. There are some other projects in there but they are unfinished and not tested. Focus on the SonyCameraControl and SonyCameraCommunication. https://github.com/RudolphKalp/SonyCamera/tree/master/SonyCameraControl – GANDA1F Oct 12 '15 at 07:18
  • 2
    Could you answer a quick question for me... Do the Sony cameras need to be manually put into API mode or is the wifi connection always on once you set it? Wondering before I buy one to tinker. – Torchify Jan 27 '16 at 16:20
  • 1
    Is there a solution via USB instead of HttpRequest methode? – Nasenbaer Feb 08 '18 at 18:33