Proper steps for working on Theta API v2.1


#1

Hello,
I’m creating an application that take a picture using the Theta and then process it.
However I’m confused about the proper steps of changing the API to 2.1 then take picture.

The current process I’m working with is a button that do the following:
1\ start session
2\ change API level to 2.1
3\ take a picture
4\ show the image

However, with this, if the user didn’t like the taken image and wanted to take another image and pressed the button again. It would produced an error.

How do I handle this ?


#2

Don’t set the version before the second picture. the version will stay at 2.1 for the entire session.

https://developers.theta360.com/en/docs/v2.0/api_reference/options/client_version.html

You don’t need to set the version again until you stop the session (the camera is disconnected from Wi-Fi).

Are you running the application on a mobile device or desktop?

What language are you writing your application in?


#3

Ok, the problem is that I don’t know how many pictures the user will take. So I don’t know when to change the version. Is there is a way to detect if the user is still connected or disconnected ?

I’m using ionic framework. The application is running on a mobile device.

The laungage is typescript.


#4

Change the API version once at the beginning of the session. You don’t need to have this in the same command as takePicture.

You can use info or state to check to see the camera is still connected. info uses GET and may be easier.

Set API Version

setApiV2 = function() {
    var args = {
    data: { "name": "camera.setOptions",
          "parameters":{
            "sessionId": "SID_0001",
            "options": {
              "clientVersion": 2
            }
          } 
        },
    headers: { "Content-Type": "application/json" }
};
    client.post("http://192.168.1.1:80/osc/commands/execute", args, function (data, response) {
      console.log(data);
    var thetaResponse = document.getElementById('thetaResponse');
    thetaResponse.innerHTML = JSON.stringify(data);
    });
  }

Take Picture

takePicture = function() {
    var args = {
    data: { "name": "camera.takePicture"},
    headers: { "Content-Type": "application/json" }
};
    client.post("http://192.168.1.1:80/osc/commands/execute", args, function (data, response) {
      console.log(data);
    var thetaResponse = document.getElementById('thetaResponse');
    thetaResponse.innerHTML = JSON.stringify(data);
    });
  }

Get File

getImage = function() {
    var lastImageUrl;
    var args = {
      data: {
        "name": "camera.listFiles",
        "parameters": {
          "fileType": "image",
          "entryCount": 1,
          "maxThumbSize": 0
        }
      },
      headers: {"Content-Type": "application/json"}
    }
    client.post("http://192.168.1.1/osc/commands/execute", args, function (data, response) {
     lastImageUrl = data.results.entries[0].fileUrl;
       var download = function(uri, filename, callback){
    request.head(uri, function(err, res, body){
     console.log('content-type:', res.headers['content-type']);
     console.log('content-length:', res.headers['content-length']);

    request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
  });
};
    console.log(lastImageUrl);
    // download('http://192.168.1.1/files/744a605553442020024b0202cb00f201/100RICOH/R0012006.JPG', '360_images/lastFile.jpg', function(){
    download(lastImageUrl, '360_images/lastFile.jpg', function(){

    });
  });
  }

I have not tried this in over a year. The problems described may be resolved now.


#5

this community example takes multiple pictures.

function startSession() {
	// console.log("hello world");
	var args = {
		data: { "name": "camera.startSession" },
		headers: { "Content-Type": "application/json" }
	};
	client.post("http://192.168.1.1:80/osc/commands/execute", args, function (data, response) {
		console.log(data);
	});

};

function setVersion() {
	var args = {
		data: {     
			"name": "camera.setOptions",
		    "parameters": {
		        "sessionId": "SID_0001",
		        "options": {
		            "clientVersion": 2 
			        }
			    }
	        }, 
		headers: { "Content-Type": "application/json" }
	};
	client.post("http://192.168.1.1:80/osc/commands/execute", args, function (data, response) {
		console.log(data);
	})
}

function getState() {
	var args = {
		data: { }, 
		headers: { }
	};
	client.post("http://192.168.1.1:80/osc/state", args, function (data, response) {
		console.log(data);
	});
};

function takePicture() {
	var args = {
		data: { "name": "camera.takePicture" }, 
		headers: { "Content-Type": "application/json" }
	};
	client.post("http://192.168.1.1:80/osc/commands/execute", args, function (data, response) {
		console.log(data);
	})
	var warning = document.getElementById("output");
	warning.innerHTML = ("Camera is synthesizing image...");
	setTimeout(
		function() {
			var output = document.getElementById("output");
			output.style.display = "none";

			var view = document.getElementById("downloadButton");
			if (view.style.display==="none") {
				view.style.display = "block";
			}
			else {
				view.style.display = "none";
			}

		}, cameraSynth);
}

function checkStatus() {
	var args = {
		data: { "stateFingerprint": "FIG_0003" },
		headers: { "Content-Type": "application/json" }
	};
	client.post("http://192.168.1.1:80/osc/checkForUpdates", args, function (data, response) {
		console.log(data);
	})
};

#6

Thanks for the help @codetricity !
I did the following steps, not sure if it correct but it worked for me. I leave it in case someone needed it.

  1. check connection through /osc/info
    if it returns a response, I initialized a variable describing the state as StartSession.
  2. it would check the state, if it StartSession, it would send a request camera.startSession to /osc/commands/execute
  3. change the API version to 2.1
  4. change the variable state as connected
  5. take picture
  6. acquire the last taken picture.

Now if I’d repeat the process, I’d check the variable state to see if it need a new session or skip that and take a picture.

However, i have noticed that the last taken image using the request camera.listFiles doesn’t return the current taken picture but the last stored picture before. Any help on this matter ?


#7

On step 5, when you run takePicture(), does it return the URL of the last image taken?

https://developers.theta360.com/en/docs/v2.1/api_reference/commands/camera.take_picture.html

Another technique is to wait 10 seconds (THETA S) or 5 seconds (THETA V) before you try to download the image to your local device.

I remember talking to another developer about setting up a simple delay between the take you run takePicture() and the time you grab the file from the camera with the URI.

Thanks for posting your updates. I’m getting inspired to take another look at using JavaScript to access the camera API.