From a08e42dce709cbbf4ebccced228a4df821863d04 Mon Sep 17 00:00:00 2001 From: Fulvio Date: Thu, 26 Jan 2017 16:50:12 +0100 Subject: [PATCH 1/5] Updated to conform to android 6 permission model --- src/android/CameraServer.java | 59 ++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/src/android/CameraServer.java b/src/android/CameraServer.java index 27886d0..de2770d 100644 --- a/src/android/CameraServer.java +++ b/src/android/CameraServer.java @@ -24,6 +24,16 @@ import android.content.Context; import android.content.res.AssetManager; +import android.content.pm.PackageManager; +import android.content.pm.PackageManager.NameNotFoundException; + +import org.apache.cordova.PermissionHelper; +import org.apache.cordova.CallbackContext; +import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.PluginResult; + +import android.Manifest; + /** * This class echoes a string called from JavaScript. @@ -63,6 +73,8 @@ public class CameraServer extends CordovaPlugin { private static final String OPT_BRIGHTNESS = "brightness"; private static final String OPT_ENABLED = "enabled"; + public static final int START_CAMERA_SEC = 0; + public static final int PERMISSION_DENIED_ERROR = 20; private String www_root = ""; private int port = 8080; @@ -76,8 +88,10 @@ public class CameraServer extends CordovaPlugin { private int brightness = 50; private boolean torchEnabled = false; + + private CallbackContext callbackContext; - @Override + @Override public boolean execute(String action, JSONArray inputs, CallbackContext callbackContext) throws JSONException { PluginResult result = null; if (ACTION_START_SERVER.equals(action)) { @@ -137,6 +151,19 @@ public boolean execute(String action, JSONArray inputs, CallbackContext callback return true; } + + public void onRequestPermissionResult(int requestCode, String[] permissions, + int[] grantResults) throws JSONException + { + for(int r:grantResults) + { + if(r == PackageManager.PERMISSION_DENIED) + { + this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, PERMISSION_DENIED_ERROR)); + return; + } + } + } private String __getLocalIpAddress() { try { @@ -314,6 +341,36 @@ private PluginResult startCamera(JSONArray inputs, CallbackContext callbackConte // initialize the camera manager :) CameraManager.init(cordova.getActivity().getApplicationContext()); + + boolean startCameraPermission = PermissionHelper.hasPermission(this, Manifest.permission.CAMERA); + + + + if(!startCameraPermission) { + startCameraPermission = true; + try { + PackageManager packageManager = this.cordova.getActivity().getPackageManager(); + String[] permissionsInPackage = packageManager.getPackageInfo(this.cordova.getActivity().getPackageName(), PackageManager.GET_PERMISSIONS).requestedPermissions; + if (permissionsInPackage != null) { + for (String permission : permissionsInPackage) { + if (permission.equals(Manifest.permission.CAMERA)) { + startCameraPermission = false; + break; + } + } + } + } catch (NameNotFoundException e) { + // We are requesting the info for our package, so this should + // never be caught + } + } + + if(!startCameraPermission){ + this.callbackContext = callbackContext; + PermissionHelper.requestPermission(this, START_CAMERA_SEC, Manifest.permission.CAMERA); + return null; + } + startCapture(); callbackContext.success(); From 13b99daab72553b469049ea7a13e4d4efaa5b0ec Mon Sep 17 00:00:00 2001 From: fulvio Date: Thu, 26 Jan 2017 17:50:53 +0100 Subject: [PATCH 2/5] Managed rotation --- src/android/CameraManager.java | 5 ++++- src/android/CameraServer.java | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/android/CameraManager.java b/src/android/CameraManager.java index e062e76..fdf8020 100644 --- a/src/android/CameraManager.java +++ b/src/android/CameraManager.java @@ -373,6 +373,9 @@ public int getDisplayOrientation(int cameraId) { result = (info.orientation - degrees + 360) % 360; } + if(rotation == 0) + result -= 90; + Log.d(TAG, "Image must be rotated to: " + result); return result; @@ -806,4 +809,4 @@ static public void decodeYUV420SP(int[] rgb, byte[] yuv420sp, int width, int hei } } } -} \ No newline at end of file +} diff --git a/src/android/CameraServer.java b/src/android/CameraServer.java index de2770d..a955d6f 100644 --- a/src/android/CameraServer.java +++ b/src/android/CameraServer.java @@ -163,6 +163,7 @@ public void onRequestPermissionResult(int requestCode, String[] permissions, return; } } + startCapture(); } private String __getLocalIpAddress() { From afb9ccc48870c5a4866023af114a605ffea8870f Mon Sep 17 00:00:00 2001 From: fulvio Date: Thu, 26 Jan 2017 18:25:39 +0100 Subject: [PATCH 3/5] Programmatically manage facing --- src/android/CameraManager.java | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/android/CameraManager.java b/src/android/CameraManager.java index fdf8020..8a1f960 100644 --- a/src/android/CameraManager.java +++ b/src/android/CameraManager.java @@ -41,7 +41,11 @@ public final class CameraManager { public static int mDesiredHeight = 720; public static boolean DEBUG = true; - public static String TAG = "CameraManager"; + public static String TAG = "CameraManager"; + + public static String mDesiredFacing = Camera.CameraInfo.CAMERA_FACING_FRONT; + + public static void setDesiredPreviewSize(int width, int height) { mDesiredWidth = width; @@ -96,13 +100,24 @@ public void openDriver() throws IOException { if (camera == null) { if (DEBUG) Log.i(TAG, "Camera opening..."); - camera = Camera.open(); + + int n = Camera.getNumberOfCameras(); + android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo(); + + for(int i=0; i Date: Fri, 27 Jan 2017 11:55:24 +0100 Subject: [PATCH 4/5] Managed camera facing option in startCamera method --- src/android/CameraManager.java | 268 +++++++++++++++++---------------- src/android/CameraServer.java | 116 +++++++------- www/CameraServer.js | 19 ++- 3 files changed, 215 insertions(+), 188 deletions(-) diff --git a/src/android/CameraManager.java b/src/android/CameraManager.java index 8a1f960..cfe1a68 100644 --- a/src/android/CameraManager.java +++ b/src/android/CameraManager.java @@ -39,19 +39,26 @@ public final class CameraManager { public static int mDesiredWidth = 1280; public static int mDesiredHeight = 720; - - public static boolean DEBUG = true; + + public static boolean DEBUG = true; public static String TAG = "CameraManager"; - public static String mDesiredFacing = Camera.CameraInfo.CAMERA_FACING_FRONT; + public static int mDesiredFacing = Camera.CameraInfo.CAMERA_FACING_FRONT; + - public static void setDesiredPreviewSize(int width, int height) { mDesiredWidth = width; mDesiredHeight = height; } - + + public static void setDesiredFacing(String camera){ + if(camera.equals("front")) + mDesiredFacing = Camera.CameraInfo.CAMERA_FACING_FRONT; + else + mDesiredFacing = Camera.CameraInfo.CAMERA_FACING_BACK; + } + public Point getMaxResolution() { if (camera != null) @@ -59,7 +66,7 @@ public Point getMaxResolution() { else return null; } - + public Point getNormalResolution(Point normalRes) { if (camera != null) @@ -67,7 +74,7 @@ public Point getNormalResolution(Point normalRes) { else return null; } - + public final PreviewCallback previewCallback; public static void init(Context context) { @@ -87,54 +94,55 @@ public static byte[] lastFrame() { private CameraManager(Context context) { Log.i(TAG, "Creating instance of CameraManager..."); - + this.surfaceTexture = new SurfaceTexture(10); - + this.context = context; this.configManager = new CameraConfigurationManager(context); - + previewCallback = new PreviewCallback(configManager, this); } public void openDriver() throws IOException { - + if (camera == null) { if (DEBUG) Log.i(TAG, "Camera opening..."); - + int n = Camera.getNumberOfCameras(); android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo(); - for(int i=0; i= 9) { setCameraDisplayOrientation(0, camera); - } - + } + if (surfaceTexture != null){ camera.setPreviewTexture(surfaceTexture); if (DEBUG) Log.i(TAG, "Set camera current texture"); } else { - + if (DEBUG) Log.i(TAG, "Camera texture is NULL"); } @@ -145,7 +153,7 @@ public void openDriver() throws IOException { } configManager.setDesiredCameraParameters(camera); if (DEBUG) Log.i(TAG, "Camera set desired parameters"); - + } else { if (DEBUG) Log.i(TAG, "Camera already opened"); @@ -161,41 +169,41 @@ public int getMaxZoom() { if (!cp.isZoomSupported()){ return -1; } - + List zoomRatios = cp.getZoomRatios(); - + return zoomRatios.get(zoomRatios.size()-1); } - + public void setZoom(int zoom){ - + if (camera == null) return; final Parameters cp = camera.getParameters(); - + int minDist = 100000; int bestIndex = 0; - + if (zoom == -1) { int zoomIndex = cp.getZoom() - 1; - + if (zoomIndex >= 0){ zoom = cp.getZoomRatios().get(zoomIndex); } } - + List zoomRatios = cp.getZoomRatios(); - + for (int i = 0; i < zoomRatios.size(); i++){ int z = zoomRatios.get(i); - + if (Math.abs(z - zoom) < minDist){ minDist = Math.abs(z - zoom); bestIndex = i; } } - + final int fBestIndex = bestIndex; cp.setZoom(fBestIndex); @@ -227,15 +235,15 @@ public void setTorch(final boolean enabled) { List flashModes = cp.getSupportedFlashModes(); if (flashModes != null && flashModes.contains(Parameters.FLASH_MODE_TORCH)) { - //camera.cancelAutoFocus(); - + //camera.cancelAutoFocus(); + new Handler().postDelayed(new Runnable() { - + @Override public void run() { if (camera != null){ Log.w("CameraManager", "Setting torch to " + enabled); - + if (enabled) { cp.setFlashMode(Parameters.FLASH_MODE_TORCH); @@ -244,10 +252,10 @@ public void run() { { cp.setFlashMode(Parameters.FLASH_MODE_OFF); } - + camera.setParameters(cp); } - + } }, 300); } @@ -255,59 +263,59 @@ public void run() { } } - + public float[] getExposureCompensationRange(){ - + if (camera == null) return null; try{ - + Parameters cp = camera.getParameters(); - + float ecStep = cp.getExposureCompensationStep(); float minEC = cp.getMinExposureCompensation(); float maxEC = cp.getMaxExposureCompensation(); - + float[] res = new float[3]; res[0] = minEC; res[1] = maxEC; res[2] = ecStep; - + return res; - + } catch (Exception e) { - + return null; } } - + public void setExposureCompensation(float value) { if (camera == null) return; try{ - + Parameters cp = camera.getParameters(); - - //int currentEC = cp.getExposureCompensation(); + + //int currentEC = cp.getExposureCompensation(); //float ecStep = cp.getExposureCompensationStep(); - + float minEC = cp.getMinExposureCompensation(); float maxEC = cp.getMaxExposureCompensation(); - + if (value > maxEC) value = maxEC; if (value < minEC) value = minEC; - + cp.setExposureCompensation((int) value); - + camera.setParameters(cp); - + //Log.d("exposure compensation", String.valueOf(value)); - + } catch (Exception e) { //Log.d("exposure compensation", "failed to set"); } @@ -315,71 +323,71 @@ public void setExposureCompensation(float value) { public void closeDriver() { - if (camera != null) { + if (camera != null) { camera.release(); camera = null; } } - + public void startPreview() { if (camera != null && !previewing) { - + previewing = true; - + if (DEBUG) Log.i(TAG, "Starting preview"); - + camera.startPreview(); int targetRotation = 0; - + if (android.os.Build.VERSION.SDK_INT >= 9) { targetRotation = getDisplayOrientation(0); - } - + } + previewCallback.setRotation(targetRotation); - - if (DEBUG) Log.i(TAG, "Setting Standard Callback"); + + if (DEBUG) Log.i(TAG, "Setting Standard Callback"); camera.setPreviewCallback(previewCallback); - + } } - + public void stopPreview() { if (camera != null && previewing) { if (DEBUG) Log.i(TAG, "Stopping preview"); - + camera.setPreviewCallback(null); - + camera.stopPreview(); - + previewing = false; } } - - @TargetApi(Build.VERSION_CODES.GINGERBREAD) + + @TargetApi(Build.VERSION_CODES.GINGERBREAD) public int getDisplayOrientation(int cameraId) { - + android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo(); android.hardware.Camera.getCameraInfo(cameraId, info); - - Log.d(TAG, "Camera Orientation: " + info.orientation); - + + Log.d(TAG, "Camera Orientation: " + info.orientation); + Display d = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); int rotation = d.getRotation(); - - Log.d(TAG, "Display Rotation: " + rotation); - + + Log.d(TAG, "Display Rotation: " + rotation); + int degrees = 0; switch (rotation) { case Surface.ROTATION_0: degrees = 0; break; case Surface.ROTATION_90: degrees = 90; break; case Surface.ROTATION_180: degrees = 180; break; case Surface.ROTATION_270: degrees = 270; break; - } - + } + int result; if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { result = (info.orientation + degrees) % 360; @@ -387,26 +395,26 @@ public int getDisplayOrientation(int cameraId) { } else { // back-facing result = (info.orientation - degrees + 360) % 360; } - - - Log.d(TAG, "Image must be rotated to: " + result); - + + + Log.d(TAG, "Image must be rotated to: " + result); + return result; } - + @TargetApi(Build.VERSION_CODES.GINGERBREAD) public int setCameraDisplayOrientation(int cameraId, android.hardware.Camera camera) { - + int result = getDisplayOrientation(cameraId); - - // Doesn't work for previewCallback, only displayed output :s - + + // Doesn't work for previewCallback, only displayed output :s + camera.setDisplayOrientation(result); - + //Camera.Parameters parameters = camera.getParameters(); //parameters.set("orientation", "portrait"); - //parameters.setRotation(result); - + //parameters.setRotation(result); + return result; } @@ -490,7 +498,7 @@ void setDesiredCameraParameters(Camera camera) { } } catch (Exception e){ } - + try { String vs = parameters.get("video-stabilization-ocr"); if (vs != null) { @@ -524,17 +532,17 @@ void setDesiredCameraParameters(Camera camera) { } //String focusMode = parameters.getFocusMode(); - + try{ parameters.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); camera.setParameters(parameters); } catch (Exception e){ - + try{ parameters.setFocusMode(Parameters.FOCUS_MODE_AUTO); camera.setParameters(parameters); } catch (Exception e2){ - + } } @@ -557,7 +565,7 @@ int getPreviewFormat() { String getPreviewFormatString() { return previewFormatString; } - + public static Point getMaxResolution(Camera.Parameters parameters) { List sizes = parameters.getSupportedPreviewSizes(); @@ -566,7 +574,7 @@ public static Point getMaxResolution(Camera.Parameters parameters) { int maxSize = 0; for (int i = 0; i < sizes.size(); i++) { - int size = sizes.get(i).width * sizes.get(i).height; + int size = sizes.get(i).width * sizes.get(i).height; if (size > maxSize) { maxSize = size; maxIndex = i; @@ -579,7 +587,7 @@ public static Point getMaxResolution(Camera.Parameters parameters) { public static Point getCameraResolution(Camera.Parameters parameters, Point desiredResolution) { String previewSizeValueString = parameters.get("preview-size-values"); - + if (previewSizeValueString == null) { previewSizeValueString = parameters.get("preview-size-value"); } @@ -596,35 +604,35 @@ public static Point getCameraResolution(Camera.Parameters parameters, Point desi int minDif = 99999; int minIndex = -1; - + float screenAR = ((float)CameraConfigurationManager.screenResolution.x) / CameraConfigurationManager.screenResolution.y; for (int i = 0; i < sizes.size(); i++) { - + //float resAR = ((float)sizes.get(i).width) / sizes.get(i).height; - + int dif = Math.abs(sizes.get(i).width - desiredResolution.x) + Math.abs(sizes.get(i).height - desiredResolution.y); - + //int dif = Math.abs((sizes.get(i).width * sizes.get(i).height) - (CameraManager.mDesiredWidth * CameraManager.mDesiredHeight)); - + if (dif < minDif) { minDif = dif; minIndex = i; } } - + float desiredTotalSize = desiredResolution.x * desiredResolution.y; float bestARdifference = 100; - - + + for (int i = 0; i < sizes.size(); i++) { - + float resAR = ((float)sizes.get(i).width) / sizes.get(i).height; - + float totalSize = sizes.get(i).width * sizes.get(i).height; - + float difference; - + if (totalSize >= desiredTotalSize){ difference = totalSize / desiredTotalSize; } else { @@ -632,16 +640,16 @@ public static Point getCameraResolution(Camera.Parameters parameters, Point desi } float ARdifference; - + if (resAR >= screenAR){ ARdifference = resAR / screenAR; } else { ARdifference = screenAR / resAR; } - + if (difference < 1.1 && ARdifference < bestARdifference){ bestARdifference = ARdifference; - minIndex = i; + minIndex = i; } } @@ -665,7 +673,7 @@ final class PreviewCallback implements Camera.PreviewCallback { private CameraManager parentManager; private int imageRotation = 0; private byte[] lastImage; - + private boolean newImageNeeded; private long lastImageRequest = 0; @@ -674,18 +682,18 @@ final class PreviewCallback implements Camera.PreviewCallback { this.configManager = configManager; this.parentManager = parent; this.imageRotation = 0; - + ///this.outputStream = new ByteArrayOutputStream(); - this.newImageNeeded = true; + this.newImageNeeded = true; } - + public boolean setRotation(int rotation) { this.imageRotation = rotation; - + return true; } - + public byte[] getLastFrame() { newImageNeeded = true; @@ -731,8 +739,8 @@ public void onPreviewFrame(byte[] data, Camera camera) try { Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,data.length); bitmap.compress(Bitmap.CompressFormat.JPEG, 75, outputStream); - } - catch (Exception e) + } + catch (Exception e) { e.printStackTrace(); } @@ -770,13 +778,13 @@ public void onPreviewFrame(byte[] data, Camera camera) { lastImage = outputStream.toByteArray(); } - + //Log.i("onPreviewFrame", "New lastImage has been set >> " + String.valueOf(lastImage.length)); - + } catch (Exception e) { Log.e("onPreviewFrame", "Exception >> " + e.getMessage()); } - + updateFps(); } @@ -796,7 +804,7 @@ private void updateFps() { } fpscount++; } - + static public void decodeYUV420SP(int[] rgb, byte[] yuv420sp, int width, int height) { final int frameSize = width * height; diff --git a/src/android/CameraServer.java b/src/android/CameraServer.java index a955d6f..e5d5890 100644 --- a/src/android/CameraServer.java +++ b/src/android/CameraServer.java @@ -72,6 +72,7 @@ public class CameraServer extends CordovaPlugin { private static final String OPT_LOCALHOST_ONLY = "localhost_only"; private static final String OPT_BRIGHTNESS = "brightness"; private static final String OPT_ENABLED = "enabled"; + private static final String OPT_JSON_CAMERA = "camera"; public static final int START_CAMERA_SEC = 0; public static final int PERMISSION_DENIED_ERROR = 20; @@ -88,7 +89,9 @@ public class CameraServer extends CordovaPlugin { private int brightness = 50; private boolean torchEnabled = false; - + + private String camera = "front"; + private CallbackContext callbackContext; @Override @@ -151,7 +154,7 @@ public boolean execute(String action, JSONArray inputs, CallbackContext callback return true; } - + public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException { @@ -163,7 +166,7 @@ public void onRequestPermissionResult(int requestCode, String[] permissions, return; } } - startCapture(); + startCapture(this.camera); } private String __getLocalIpAddress() { @@ -184,7 +187,7 @@ private String __getLocalIpAddress() { } catch (SocketException ex) { Log.e(LOGTAG, ex.toString()); } - + return "127.0.0.1"; } @@ -193,7 +196,7 @@ private PluginResult startServer(JSONArray inputs, CallbackContext callbackConte JSONObject options = inputs.optJSONObject(0); if(options == null) return null; - + www_root = options.optString(OPT_WWW_ROOT); port = options.optInt(OPT_PORT, 8080); localhost_only = options.optBoolean(OPT_LOCALHOST_ONLY, false); @@ -210,7 +213,7 @@ private PluginResult startServer(JSONArray inputs, CallbackContext callbackConte localPath += www_root; } } - + final CallbackContext delayCallback = callbackContext; cordova.getActivity().runOnUiThread(new Runnable(){ @Override @@ -220,12 +223,12 @@ public void run() { delayCallback.error( errmsg ); } else { url = "http://" + __getLocalIpAddress() + ":" + port; - + delayCallback.success( url ); } } }); - + return null; } @@ -245,23 +248,23 @@ public void run() { return null; } - + private String __startServer() { String errmsg = ""; try { AndroidFile f = new AndroidFile(localPath); - + Context ctx = cordova.getActivity().getApplicationContext(); AssetManager am = ctx.getResources().getAssets(); f.setAssetManager( am ); - + if(localhost_only) { InetSocketAddress localAddr = InetSocketAddress.createUnresolved("127.0.0.1", port); server = new WebServer(localAddr, f); } else { server = new WebServer(port, f); } - + Log.w(LOGTAG, "Setting jsonInfo to: " + json_info); server.SetJsonInfo(json_info); @@ -299,10 +302,10 @@ private PluginResult setInfo(JSONArray inputs, CallbackContext callbackContext) return null; } - + private PluginResult getURL(JSONArray inputs, CallbackContext callbackContext) { Log.w(LOGTAG, "getURL"); - + callbackContext.success( this.url ); return null; } @@ -316,36 +319,41 @@ private PluginResult getIPAddress(JSONArray inputs, CallbackContext callbackCont private PluginResult getLocalPath(JSONArray inputs, CallbackContext callbackContext) { Log.w(LOGTAG, "getLocalPath"); - + callbackContext.success( this.localPath ); return null; } private PluginResult getNumRequests(JSONArray inputs, CallbackContext callbackContext) { Log.w(LOGTAG, "getNumRequests"); - + int nReq = 0; - + if (server != null) { nReq = server.NumRequested(); } - + callbackContext.success( nReq ); return null; } - + private PluginResult startCamera(JSONArray inputs, CallbackContext callbackContext) { Log.w(LOGTAG, "startCamera"); + JSONObject options = inputs.optJSONObject(0); + if(options == null) return null; + + this.camera = options.optString(OPT_JSON_CAMERA); + // initialize the camera manager :) CameraManager.init(cordova.getActivity().getApplicationContext()); - + boolean startCameraPermission = PermissionHelper.hasPermission(this, Manifest.permission.CAMERA); - + if(!startCameraPermission) { startCameraPermission = true; @@ -365,17 +373,17 @@ private PluginResult startCamera(JSONArray inputs, CallbackContext callbackConte // never be caught } } - + if(!startCameraPermission){ this.callbackContext = callbackContext; PermissionHelper.requestPermission(this, START_CAMERA_SEC, Manifest.permission.CAMERA); return null; } - - startCapture(); + + startCapture(this.camera); callbackContext.success(); - + return null; } @@ -386,30 +394,30 @@ private PluginResult stopCamera(JSONArray inputs, CallbackContext callbackContex stopCapture(); callbackContext.success(); - + return null; } private PluginResult getJpegImage(JSONArray inputs, CallbackContext callbackContext) { Log.w(LOGTAG, "getJpegImage"); - + byte[] bArray = CameraManager.lastFrame(); - + if (bArray != null) { Log.w(LOGTAG, "Received " + String.valueOf(bArray.length) + " bytes..."); - + String imageEncoded = Base64.encodeToString(bArray,Base64.NO_WRAP); - //Log.e("LOOK", imageEncoded); + //Log.e("LOOK", imageEncoded); callbackContext.success( imageEncoded ); } else { - callbackContext.error(0); + callbackContext.error(0); } - + return null; } @@ -444,30 +452,30 @@ public void run() { return null; } - + private PluginResult setBrightness(JSONArray inputs, CallbackContext callbackContext) { Log.w(LOGTAG, "setBrightness"); - + JSONObject options = inputs.optJSONObject(0); if(options == null) return null; - + brightness = options.optInt(OPT_BRIGHTNESS, 50); - + final CallbackContext delayCallback = callbackContext; cordova.getActivity().runOnUiThread(new Runnable(){ @Override public void run() { - + Window w = cordova.getActivity().getWindow(); WindowManager.LayoutParams lp = w.getAttributes(); lp.screenBrightness = (float)brightness/100; if (lp.screenBrightness<.01f) lp.screenBrightness=.01f; w.setAttributes(lp); - - delayCallback.success( "True" ); + + delayCallback.success( "True" ); } }); - + return null; } @@ -475,9 +483,9 @@ public void run() { private PluginResult getTorch(JSONArray inputs, CallbackContext callbackContext) { Log.w(LOGTAG, "getTorch"); - // TODO: IMPLEMENT + // TODO: IMPLEMENT callbackContext.error(0); - + return null; } @@ -490,22 +498,22 @@ private PluginResult setTorch(JSONArray inputs, CallbackContext callbackContext) torchEnabled = options.optBoolean(OPT_ENABLED, false); CameraManager cMgr = CameraManager.get(); - + if (cMgr != null) - { + { cMgr.setTorch(torchEnabled); callbackContext.success( ); } else { - callbackContext.error(0); + callbackContext.error(0); } return null; } - private boolean startCapture(){ + private boolean startCapture(String camera){ Log.w(LOGTAG, "startCapture"); if (false){ @@ -514,28 +522,30 @@ private boolean startCapture(){ CameraManager.setDesiredPreviewSize(800, 480); } + CameraManager.setDesiredFacing(camera); + try { CameraManager.get().openDriver(); } catch (IOException e) { Log.w(LOGTAG, "Exception in openDriver"); } - - //CameraManager.get().startPreview(); + + //CameraManager.get().startPreview(); return true; } - + private boolean stopCapture(){ Log.w(LOGTAG, "stopCapture"); - - CameraManager.get().stopPreview(); - + + CameraManager.get().stopPreview(); + try { CameraManager.get().closeDriver(); } catch (Exception e) { Log.w(LOGTAG, "Exception in closeDriver"); } - + return true; } diff --git a/www/CameraServer.js b/www/CameraServer.js index 98ce387..f96dee8 100644 --- a/www/CameraServer.js +++ b/www/CameraServer.js @@ -10,14 +10,14 @@ cameraserver_exports.startServer = function(options, success, error) { 'localhost_only': false, 'json_info' : '{"AppVersion":"Undefined"}' }; - + // Merge optional settings into defaults. for (var key in defaults) { if (typeof options[key] !== 'undefined') { defaults[key] = options[key]; } } - + exec(success, error, "CameraServer", "startServer", [ defaults ]); }; @@ -41,8 +41,18 @@ cameraserver_exports.getNumRequests = function(success, error) { exec(success, error, "CameraServer", "getNumRequests", []); }; -cameraserver_exports.startCamera = function(success, error) { - exec(success, error, "CameraServer", "startCamera", []); +cameraserver_exports.startCamera = function(options, success, error) { + var defaults = { + 'camera': 'front' + }; + + // Merge optional settings into defaults. + for (var key in defaults) { + if (typeof options[key] !== 'undefined') { + defaults[key] = options[key]; + } + } + exec(success, error, "CameraServer", "startCamera", [ defaults ]); }; cameraserver_exports.stopCamera = function(success, error) { @@ -127,4 +137,3 @@ cameraserver_exports.setInfo = function(options, success, error) { module.exports = cameraserver_exports; - From 8d8164c3eea4750bf3a6e743ceec13d6df716676 Mon Sep 17 00:00:00 2001 From: fulvio Date: Fri, 27 Jan 2017 12:29:06 +0100 Subject: [PATCH 5/5] Updated version number and readme --- README.md | 20 +++++++++++--------- plugin.xml | 8 ++++---- src/android/CameraManager.java | 2 +- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 234d9f5..faa5cab 100644 --- a/README.md +++ b/README.md @@ -15,12 +15,12 @@ Why over HTTP ? ## Add the plugin to your cordova project: ## - cordova plugin add https://github.com/Moonware/cordova-cameraserver - + cordova plugin add https://github.com/MetalMaster/cordova-cameraserver + ## Removing the plugin to your cordova project: ## cordova plugin rm cordova-plugin-cameraserver - + ## Dependency ## Since Cordova 4.0, it may be required to install the **cordova-plugin-whitelist** to allow calls to **127.0.0.1** and **localhost** @@ -29,7 +29,7 @@ Without this step, your application may not be allowed to pull and display image Please follow installation and setup instructions here: [cordova-plugin-whitelist](https://github.com/apache/cordova-plugin-whitelist/) - + #Javascript APIs ```javascript @@ -44,7 +44,7 @@ getLocalPath( success_callback, error_callback ); getNumRequests( success_callback, error_callback ); -startCamera( success_callback, error_callback ); +startCamera( options, success_callback, error_callback ); stopCamera( success_callback, error_callback ); @@ -67,7 +67,7 @@ cordova.plugins.CameraServer.startServer({ // if server is up, it will return the url of http://:port/ // the ip is the active network connection // if no wifi or no cell, "127.0.0.1" will be returned. - console.log('CameraServer Started @ ' + url); + console.log('CameraServer Started @ ' + url); }, function( error ){ console.log('CameraServer Start failed: ' + error); }); @@ -76,7 +76,9 @@ cordova.plugins.CameraServer.startServer({ Start the Camera Capture (will act on demand when a HTTP request arrives) ```javascript -cordova.plugins.CameraServer.startCamera(function(){ +cordova.plugins.CameraServer.startCamera({ + 'camera':'front' + },function(){ console.log('Capture Started'); },function( error ){ console.log('CameraServer StartCapture failed: ' + error); @@ -122,8 +124,8 @@ Which is itself based on: * [NanoHTTPD](https://github.com/NanoHttpd/nanohttpd), written in java, for java / android, by psh. * [CocoaHTTPServer](https://github.com/robbiehanson/CocoaHTTPServer), written in Obj-C, for iOS / Mac OS X, by robbiehanson. -You can use this plugin for FREE. +You can use this plugin for FREE. -Feel free to fork, improve and send pull request. +Feel free to fork, improve and send pull request. We will of course appreciate if you share any improvement or addition made to the plugin. diff --git a/plugin.xml b/plugin.xml index 27dcd99..6aabd92 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,8 +1,8 @@ - CameraServer @@ -118,7 +118,7 @@ xmlns:android="http://schemas.android.com/apk/res/android"> --> - + diff --git a/src/android/CameraManager.java b/src/android/CameraManager.java index cfe1a68..2beb4d3 100644 --- a/src/android/CameraManager.java +++ b/src/android/CameraManager.java @@ -112,7 +112,7 @@ public void openDriver() throws IOException { android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo(); int i=0; - for(; i