Skip to content

Commit

Permalink
Merge pull request tidev#5322 from bijupmb/TIMOB-13998-SetCameraFlash…
Browse files Browse the repository at this point in the history
…ModeForOverlaysNewPR

[TIMOB-13998] Add flash on in camera activity
  • Loading branch information
hieupham007 committed Mar 6, 2014
2 parents 05f0531 + 8b3a962 commit d422fe2
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.TiContext;


@Kroll.proxy(creatableInModule=MediaModule.class)
@Kroll.proxy(creatableInModule = MediaModule.class, propertyAccessors = { TiC.PROPERTY_FLASH_MODE })
public class CameraProxy extends KrollProxy
{
private static TiCamera camera = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiBaseActivity;
import org.appcelerator.titanium.TiBlob;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.TiContext;
import org.appcelerator.titanium.io.TiBaseFile;
import org.appcelerator.titanium.io.TiFileFactory;
Expand Down Expand Up @@ -116,6 +117,9 @@ public class MediaModule extends KrollModule

@Kroll.constant public static final int CAMERA_FRONT = 0;
@Kroll.constant public static final int CAMERA_REAR = 1;
@Kroll.constant public static final int CAMERA_FLASH_OFF = 0;
@Kroll.constant public static final int CAMERA_FLASH_ON = 1;
@Kroll.constant public static final int CAMERA_FLASH_AUTO = 2;

public MediaModule()
{
Expand Down Expand Up @@ -150,6 +154,7 @@ public void showCamera(@SuppressWarnings("rawtypes") HashMap options)
KrollFunction cancelCallback = null;
KrollFunction errorCallback = null;
boolean autohide = true;
int flashMode = CAMERA_FLASH_OFF;
boolean saveToPhotoGallery = false;

if (options.containsKey("success")) {
Expand All @@ -171,6 +176,11 @@ public void showCamera(@SuppressWarnings("rawtypes") HashMap options)
if (saveToPhotoGalleryOption != null) {
saveToPhotoGallery = TiConvert.toBoolean(saveToPhotoGalleryOption);
}

Object cameraFlashModeOption = options.get(TiC.PROPERTY_FLASH_MODE);
if (cameraFlashModeOption != null) {
flashMode = TiConvert.toInt(cameraFlashModeOption);
}

// Use our own custom camera activity when an overlay is provided.
if (options.containsKey("overlay")) {
Expand All @@ -182,6 +192,7 @@ public void showCamera(@SuppressWarnings("rawtypes") HashMap options)
TiCameraActivity.errorCallback = errorCallback;
TiCameraActivity.cancelCallback = cancelCallback;
TiCameraActivity.saveToPhotoGallery = saveToPhotoGallery;
TiCameraActivity.setFlashMode(flashMode);
TiCameraActivity.whichCamera = CAMERA_REAR; // default.

// This option is only applicable when running the custom
Expand Down Expand Up @@ -678,6 +689,20 @@ public void onError(Activity activity, int requestCode, Exception e) {
}
}

@Kroll.method
@Kroll.setProperty
public void setFlashMode(int flashMode)
{
TiCameraActivity.setFlashMode(flashMode);
}

@Kroll.method
@Kroll.getProperty
public int getFlashMode()
{
return TiCameraActivity.cameraFlashMode;
}

@Kroll.method
public void openPhotoGallery(KrollDict options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public class TiCameraActivity extends TiBaseActivity implements SurfaceHolder.Ca
public static KrollFunction successCallback, errorCallback, cancelCallback;
public static boolean saveToPhotoGallery = false;
public static int whichCamera = MediaModule.CAMERA_REAR;
public static int cameraFlashMode = MediaModule.CAMERA_FLASH_OFF;
public static boolean autohide = true;

private static class PreviewLayout extends FrameLayout
Expand Down Expand Up @@ -198,7 +199,9 @@ protected void onResume()
openCamera();
}
}

if (camera != null) {
setFlashMode(cameraFlashMode);
}
if (camera == null) {
return; // openCamera will have logged error.
}
Expand All @@ -216,9 +219,24 @@ protected void onResume()
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
}

@Override
protected void onPause()
public static void setFlashMode(int cameraFlashMode)
{
TiCameraActivity.cameraFlashMode = cameraFlashMode;
if (camera != null) {
Parameters p = camera.getParameters();
if (cameraFlashMode == MediaModule.CAMERA_FLASH_OFF) {
p.setFlashMode(Parameters.FLASH_MODE_OFF);
} else if (cameraFlashMode == MediaModule.CAMERA_FLASH_ON) {
p.setFlashMode(Parameters.FLASH_MODE_ON);
} else if (cameraFlashMode == MediaModule.CAMERA_FLASH_AUTO) {
p.setFlashMode(Parameters.FLASH_MODE_AUTO);
}
camera.setParameters(p);
}
}

@Override
protected void onPause(){
super.onPause();

stopPreview();
Expand Down
5 changes: 5 additions & 0 deletions android/titanium/src/java/org/appcelerator/titanium/TiC.java
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,11 @@ public class TiC
* @module.api
*/
public static final String PROPERTY_FLAGS = "flags";

/**
* @module.api
*/
public static final String PROPERTY_FLASH_MODE = "flashMode";

/**
* @module.api
Expand Down
6 changes: 6 additions & 0 deletions apidoc/Titanium/Media/Media.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,12 @@ properties:
overlay is not set, the default Android Camera Activity is used, which is only
capable of reporting back the results of one taken photo, making `autohide`
meaningless in that context.
- name: flashMode
summary: set camera flash on or off for custom overlays.
type: Boolean
default: false
platforms: [android]
since: {android: "3.3.0"}
- name: animated
summary: Specifies if the dialog should be animated upon showing and hiding.
type: Boolean
Expand Down

0 comments on commit d422fe2

Please sign in to comment.