Java tutorial
import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.ConnectException; import java.net.HttpURLConnection; import java.net.URL; import java.net.UnknownHostException; /** * IPEVO iZiggi API Java Client * @author IPEVO * @version 1.0 * @see <a href="http://www.ipevo.com/developer">Link</a> * @see <a href="http://www.ipevo.com/developer">License</a> */ public class iZiggiClient { /** * @serial requestUrl Request Url, default value is http://10.10.10.1/ * @serial userId Login User ID, default value is Admin * @serial userPass Loing User Password, default value is admin * @serial api API Name, default value is ipevoapi.cgi */ public String requestUrl; public String userId; public String userPass; public String api; /** * Change requestUrl value * @param url requestUrl */ public void setUrl(String url) { if (url != null && !url.equals("")) requestUrl = url; } /** * Change userId or userPass value * @param id userId * @param pwd userPass */ public void setUser(String id, String pwd) { if (id != null && !id.equals("")) userId = id; if (pwd != null && !pwd.equals("")) userPass = pwd; } /** * Change api value * @param apiStr api */ public void setApi(String apiStr) { if (apiStr != null && !apiStr.equals("")) api = apiStr; } /** * initial class * @param url requestUrl * @param id userId * @param pwd userPass * @param apiStr api */ public iZiggiClient(String url, String id, String pwd, String apiStr) { requestUrl = "http://10.10.10.1/"; userId = "Admin"; userPass = "admin"; api = "ipevoapi.cgi"; if (url != null && !url.equals("")) requestUrl = url; if (id != null && !id.equals("")) userId = id; if (pwd != null && !pwd.equals("")) userPass = pwd; if (apiStr != null && !apiStr.equals("")) api = apiStr; } /** * Change the streaming resolution of iZiggi-HD. * @param resolutionVal Choose from a range of available resolutions: 640x480, 800x600, 1024x768, 1280x720, 1600x1200 * @return object */ public JSONObject setResolution(String resolutionVal) { return doAction("0x29", resolutionVal); } /** * Get the current streaming resolution of iZiggi-HD. * @return object */ public JSONObject getResolution() { return doAction("0x2A", ""); } /** * Set exposure value. * Configure exposure value of iZiggi-HD's camera. A smaller value darkens the image while a larger value makes the image brighter. Default value is set to "8". * @param exposureVal 1~15 * @return object */ public JSONObject setExposure(String exposureVal) { return doAction("0x03", exposureVal); } /** * Get current exposure value of iZiggi-HD's camera. * @return object */ public JSONObject getExposure() { return doAction("0x06", ""); } /** * Set auto exposure. Configure auto exposure setting of iZiggi-HD's camera. Value "0" turns on auto exposure function, and value "1" turns off auto exposure function. * @param autoExposureVal 0 : Auto, 1 : Not auto * @return object */ public JSONObject setAutoExposure(String autoExposureVal) { return doAction("0x02", autoExposureVal); } /** * Get auto exposure setting of iZiggi-HD's camera. * @return object */ public JSONObject getAutoExposure() { return doAction("0x0B", ""); } /** * List all resolutions supported by iZiggi-HD. * @return object */ public JSONObject listResolutions() { return doAction("0x2B", ""); } /** * Initiate a focus action of iZiggi-HD's camera. That is, focus the camera. * @return object */ public JSONObject initFocus() { return doAction("0x04", ""); } /** * Get focus mode setting. Get auto focus mode setting of iZiggi-HD's camera. * On single focus mode (AF-S), iZiggi-HD will focus once and stay at that focus. * On continuous focus mode (AF-C), iZiggi-HD will constantly try to get the best focus. * @return object */ public JSONObject getFocusMode() { return doAction("0x05", ""); } /** * Get the current focus status of iZiggi-HD's camera. * @return object */ public JSONObject getCurrentFocusStatus() { return doAction("0x09", ""); } /** * Set power frequency (flicker mode). Configure the power frequency value (Flicker mode) of iZiggi-HD's camera. Value 0 means 60Hz while value 1 means 50Hz. * @param powerFreqVal 0 : 60Hz, 1 : 50Hz * @return object */ public JSONObject setPowerFreq(String powerFreqVal) { return doAction("0x01", powerFreqVal); } /** * Get current power frequency (flicker mode) of iZiggi-HD's camera. * @return object */ public JSONObject getPowerFreq() { return doAction("0x07", ""); } /** * Multiple camera control status (API list #3-7) in one command. Get the exposure value, auto focus mode, focus status, power frequency and auto exposure value in one command. * @return object */ public JSONObject getAllStatus() { return doAction("0x0A", ""); } /** * Get API version. If you are not getting a response from this API, iZiggi-HD's firmware could be outdated and in need of an update. * You should display a message informing user to upgrade iZiggi-HD's firmware. * You can also use "api.cgi" to get the firmware version of your iZiggi-HD to determine if it is too old. * @return object */ public JSONObject getAPIVersion() { return doAction("0x0C", ""); } private String getMessage(int val) { // val : json.getInt("Status") String returnMessage = ""; switch (val) { case 0: returnMessage = "Successful"; break; case 1: returnMessage = "No Id or pwd or one of them is not correct"; break; case 2: returnMessage = "Execute command fail"; break; case 3: returnMessage = "No command parameter, or command is not correct"; break; case 4: returnMessage = "No value parameter, or value is not correct"; break; case 5: returnMessage = "Other error"; break; } return returnMessage; } private JSONObject doAction(String cmd, String val) { String actUrl = requestUrl + api + "?id=" + userId + "&pwd=" + userPass + "&command=" + cmd; if (!val.equals("")) actUrl += "&value=" + val; URL url = null; BufferedReader reader = null; StringBuilder stringBuilder; JSONObject json = null; String extraMessage = ""; try { // create the HttpURLConnection url = new URL(actUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // just want to do an HTTP GET here connection.setRequestMethod("GET"); // give it 15 seconds to respond connection.setReadTimeout(15 * 1000); connection.connect(); // read the output from the server reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); stringBuilder = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { stringBuilder.append(line); } json = new JSONObject(stringBuilder.toString()); } catch (UnknownHostException e) { try { json = new JSONObject("{Status:5}"); extraMessage = " - host name error"; } catch (JSONException e1) { e1.printStackTrace(); } } catch (ConnectException e) { try { json = new JSONObject("{Status:5}"); extraMessage = " - connect error"; } catch (JSONException e1) { e1.printStackTrace(); } } catch (NullPointerException e) { try { json = new JSONObject("{Status:5}"); } catch (JSONException e1) { e1.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } } try { json.put("Message", getMessage(json.getInt("Status")) + extraMessage); } catch (JSONException e) { e.printStackTrace(); } return json; } }