接着上面的Theta第一篇
我们先来改造展示全景相片的View,没有圆球效果,只是将Demo版的View伸展到全屏。
找到 GLRenderer 这个类的 onSurfaceChanged 这个方法,改为:
/**
* onSurfaceChanged Method
* @param gl GLObject (not used)
* @param width Screen width
* @param height Screen height
*/
@Override
public void onSurfaceChanged(final GL10 gl, final int width, final int height) {
int _height = height;
mScreenAspectRatio = (float) width / (float) (_height == 0 ? 1 : _height);
GLES20.glViewport(0, _height, width, _height);
Matrix.setLookAtM(mViewMatrix, 0, mCameraPosX, mCameraPosY, mCameraPosZ, mCameraDirectionX, mCameraDirectionY, mCameraDirectionZ, 0.0f, 1.0f, 0.0f);
Matrix.perspectiveM(mProjectionMatrix, 0, mCameraFovDegree, mScreenAspectRatio, Z_NEAR, Z_FAR);
gl.glViewport(0, 0, width, height);
// for a fixed camera, set the projection too
float ratio = (float) width / height;
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
return;
}
重新运行你的Demo,建议设置为横屏,布局设置为全屏,体验更佳!
好了 ,关于Demo久这么多了,但是想要完善项目中连接Theta的功能,还需要填一些坑。
现在市场上应该就 Theta V,Theta SC,ThetaS 这三款比较常见
Theta 官网时常还会发布一些新的固件,让你更新,版本号不同,数据请求也会有些不同,想不到吧~
Theta就是这么坑
我是这么做的,连上Theta后,首先请求该Theta的固件版本号是多少,然后在接下来的各种请求中按版本号来区别开来。
String THETAURL=”http://192.168.1.1/”
获取版本号url:BaseUrl.THETAURL+”osc/info”
返回各种信息,其中就有我们需要的版本号 “firmwareVersion”,将版本号保存下来,后面会用到。
先说下哪些请求会用到版本号的区别:
1,获取实时图像
2,获取缩略图
3,拍照
4,获取拍照的图像
5,删除图片
6,获取iso,快门,曝光,自动的参数
7,设置iso,快门,曝光,自动的参数
8,获取相机的状态(存储),电量,相片大小
下面是获取实时图像的请求:
getLivePreview方法:
private final String THETA_VERSION_1_00 = "01.00";
private final String THETA_VERSION_1_20 = "01.20";
public InputStream getLivePreview(String thetaVersion) throws IOException, JSONException {
if (thetaVersion.equals(THETA_VERSION_1_00)||thetaVersion.equals(THETA_VERSION_1_20)) {
mSessionId = connect();
setImageCaptureMode(mSessionId);
} else {
setImageCaptureMode();
}
HttpURLConnection postConnection = createHttpConnection("POST", "/osc/commands/execute");
JSONObject input = new JSONObject();
InputStream is = null;
try {
if (thetaVersion.equals(THETA_VERSION_1_00)||thetaVersion.equals(THETA_VERSION_1_20)) {
input.put("name", "camera._getLivePreview");
JSONObject parameters = new JSONObject();
parameters.put("sessionId", mSessionId);
input.put("parameters", parameters);
} else {
input.put("name", "camera.getLivePreview");
}
OutputStream os = postConnection.getOutputStream();
os.write(input.toString().getBytes());
postConnection.connect();
os.flush();
os.close();
is = postConnection.getInputStream();
} catch (IOException e) {
e.printStackTrace();
String errorMessage = null;
InputStream es = postConnection.getErrorStream();
try {
if (es != null) {
String errorData = InputStreamToString(es);
JSONObject output = new JSONObject(errorData);
JSONObject errors = output.getJSONObject("error");
errorMessage = errors.getString("message");
}
} catch (IOException e1) {
e1.printStackTrace();
} catch (JSONException e1) {
e1.printStackTrace();
} finally {
if (es != null) {
try {
es.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
throw e;
} catch (JSONException e) {
e.printStackTrace();
throw e;
}
return is;
}
用到的这两个方法:
private String setImageCaptureMode(String sessionId) {
HttpURLConnection postConnection = createHttpConnection("POST", "/osc/commands/execute");
JSONObject input = new JSONObject();
String responseData;
String errorMessage = null;
InputStream is = null;
try {
// send HTTP POST
input.put("name", "camera.setOptions");
JSONObject parameters = new JSONObject();
parameters.put("sessionId", sessionId);
JSONObject options = new JSONObject();
options.put("captureMode", "image");
parameters.put("options", options);
input.put("parameters", parameters);
OutputStream os = postConnection.getOutputStream();
os.write(input.toString().getBytes());
postConnection.connect();
os.flush();
os.close();
is = postConnection.getInputStream();
responseData = InputStreamToString(is);
// parse JSON data
JSONObject output = new JSONObject(responseData);
String status = output.getString("state");
if (status.equals("error")) {
JSONObject errors = output.getJSONObject("error");
errorMessage = errors.getString("message");
}
} catch (IOException e) {
e.printStackTrace();
errorMessage = e.toString();
InputStream es = postConnection.getErrorStream();
try {
if (es != null) {
String errorData = InputStreamToString(es);
JSONObject output = new JSONObject(errorData);
JSONObject errors = output.getJSONObject("error");
errorMessage = errors.getString("message");
}
} catch (IOException e1) {
e1.printStackTrace();
} catch (JSONException e1) {
e1.printStackTrace();
} finally {
if (es != null) {
try {
es.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
} catch (JSONException e) {
e.printStackTrace();
errorMessage = e.toString();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return errorMessage;
}
private String setImageCaptureMode() {
HttpURLConnection postConnection = createHttpConnection("POST", "/osc/commands/execute");
JSONObject input = new JSONObject();
String responseData;
String errorMessage = null;
InputStream is = null;
try {
// send HTTP POST
input.put("name", "camera.setOptions");
JSONObject parameters = new JSONObject();
JSONObject options = new JSONObject();
options.put("captureMode", "image");
parameters.put("options", options);
input.put("parameters", parameters);
OutputStream os = postConnection.getOutputStream();
os.write(input.toString().getBytes());
postConnection.connect();
os.flush();
os.close();
is = postConnection.getInputStream();
responseData = InputStreamToString(is);
// parse JSON data
JSONObject output = new JSONObject(responseData);
String status = output.getString("state");
if (status.equals("error")) {
JSONObject errors = output.getJSONObject("error");
errorMessage = errors.getString("message");
}
} catch (IOException e) {
e.printStackTrace();
errorMessage = e.toString();
InputStream es = postConnection.getErrorStream();
try {
if (es != null) {
String errorData = InputStreamToString(es);
JSONObject output = new JSONObject(errorData);
JSONObject errors = output.getJSONObject("error");
errorMessage = errors.getString("message");
}
} catch (IOException e1) {
e1.printStackTrace();
} catch (JSONException e1) {
e1.printStackTrace();
} finally {
if (es != null) {
try {
es.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
} catch (JSONException e) {
e.printStackTrace();
errorMessage = e.toString();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return errorMessage;
}
还有其他的连接的接口,后面有空会一 一放 上来。