9
0
mirror of https://gitee.com/shuto/customCamera.git synced 2024-10-06 10:22:07 +08:00

Merge pull request #63 from geneanet/feature-flash

Feature flash
This commit is contained in:
Thomas BOY 2015-02-03 12:04:29 +01:00
commit a5cc941edd
8 changed files with 205 additions and 44 deletions

View File

@ -68,6 +68,14 @@ L'objet `options` contient les options de configuration de l'appareil photo.
- **Type :** `boolean`
- **Valeur par défaut :** `true`
+ **defaultFlash :** Séléctionne un mode par défaut pour le flash.
- **Type :** `integer`
- **Valeur par défaut :** `0`
+ **switchFlash :** Permet d'activer ou non le bouton pour changer le mode du flash. `true` : Active l'option. `false` : Désactive l'option.
- **Type :** `boolean`
- **Valeur par défaut :** `true`
#### *{Function}* onSuccess
La fonction `onSuccess` est appelée lorsque la prise de vue a réussie.
@ -90,6 +98,18 @@ La fonction `onFail` est appelée lorsque la prise de vue a échouée.
+ **Type :** `string`
+ **Note :** Contient un message détaillant l'erreur.
## Constantes
+ **CustomCamera.FlashModes.DISABLE :**
- **Type :** `integer`
- **Valeur :** `0`
+ **CustomCamera.FlashModes.ACTIVE :**
- **Type :** `integer`
- **Valeur :** `1`
+ **CustomCamera.FlashModes.AUTO :**
- **Type :** `integer`
- **Valeur :** `2`
## Implémentation
### Exemple

View File

@ -84,6 +84,9 @@ public class CameraLauncher extends CordovaPlugin {
intent.putExtra("opacity", args.getBoolean(7));
intent.putExtra("startOrientation", this.cordova.getActivity().getResources().getConfiguration().orientation);
intent.putExtra("defaultFlash", args.getInt(8));
intent.putExtra("switchFlash", args.getBoolean(9));
cordova.startActivityForResult((CordovaPlugin) this, intent,
CameraLauncher.REQUEST_CODE);

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

View File

@ -21,6 +21,15 @@
android:alpha="0.2"
android:scaleType="fitXY" />
<ImageButton
android:id="@+id/flash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:background="@android:color/transparent"
android:onClick="switchFlash"
android:src="@drawable/flash" />
<LinearLayout
android:id="@+id/beforePhoto"
android:layout_width="match_parent"

View File

@ -57,11 +57,17 @@ public class CameraActivity extends Activity {
private Bitmap photoTaken = null;
// Flag to active or disable opacity function.
private Boolean opacity = true;
// Flag to save state of flash -> 0 : off, 1 : on, 2 : auto.
private int stateFlash = 0;
public static final int DEGREE_0 = 0;
public static final int DEGREE_90 = 90;
public static final int DEGREE_180 = 180;
public static final int DEGREE_270 = 270;
public static final int FLASH_DISABLE = 0;
public static final int FLASH_ENABLE = 1;
public static final int FLASH_AUTO = 2;
/**
* To get camera resource or stop this activity.
@ -103,6 +109,11 @@ public class CameraActivity extends Activity {
Button miniature = (Button) findViewById(R.id.miniature);
miniature.setVisibility(View.INVISIBLE);
}
if (!this.getIntent().getBooleanExtra("switchFlash", true)) {
ImageButton flash = (ImageButton)findViewById(R.id.flash);
flash.setVisibility(View.INVISIBLE);
}
// The opacity bar
SeekBar switchOpacity = (SeekBar) findViewById(R.id.switchOpacity);
@ -148,7 +159,7 @@ public class CameraActivity extends Activity {
view.performClick();
((CameraActivity) currentActivity).setCameraBackgroundColor(
currentActivity.getIntent().getStringExtra("cameraBackgroundColor"));
((CameraActivity) currentActivity).takePhoto();
((CameraActivity) currentActivity).startTakePhoto();
break;
default:
break;
@ -187,6 +198,10 @@ public class CameraActivity extends Activity {
return;
}
stateFlash = this.getIntent().getIntExtra("defaultFlash", CameraActivity.FLASH_DISABLE);
updateStateFlash(stateFlash);
int orientation = 0;
switch (getCustomRotation()) {
case 0:
@ -283,6 +298,7 @@ public class CameraActivity extends Activity {
}
/** Method to pause the activity. */
@Override
protected void onPause() {
super.onPause();
ManagerCamera.clearCameraAccess();
@ -290,7 +306,10 @@ public class CameraActivity extends Activity {
preview.removeAllViews();
}
/** Event on touch screen to call the manager of the zoom & the auto focus. */
/**
* Event on touch screen to call the manager of the zoom & the auto focus.
* @return boolean
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
if (photoTaken == null) {
@ -306,11 +325,6 @@ public class CameraActivity extends Activity {
customCamera.cancelAutoFocus();
handleZoom(event, paramsCamera, distanceBetweenFingers);
}
} else {
// If we touch with one finger -> auto-focus
if (action == MotionEvent.ACTION_UP) {
handleFocus(event, paramsCamera);
}
}
}
@ -357,7 +371,7 @@ public class CameraActivity extends Activity {
zoom--;
}
}
distanceBetweenFingers = newDist;
paramsCamera.setZoom(zoom);
customCamera.setParameters(paramsCamera);
}
@ -374,25 +388,6 @@ public class CameraActivity extends Activity {
zoomLevel.setVisibility(View.VISIBLE);
}
/**
* Manage the focus.
* @param event Current event which start this action.
* @param paramsCamera Camera's parameter.
*/
public void handleFocus(MotionEvent event, Camera.Parameters paramsCamera) {
if (photoTaken == null) {
List<String> supportedFocusModes = paramsCamera.getSupportedFocusModes();
if (supportedFocusModes != null
&& supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
customCamera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean bool, Camera camera) {
}
});
}
}
}
/** To set background in the view. */
protected void setBackground() {
// Get the base64 picture for the background only if it's exist.
@ -457,7 +452,10 @@ public class CameraActivity extends Activity {
}
}
/** Resize and mask the miniature button. */
/**
* Resize and mask the miniature button.
* @param view
*/
public void buttonMiniature(View view) {
ImageView background = (ImageView) findViewById(R.id.background);
final Button miniature = (Button) view;
@ -582,12 +580,27 @@ public class CameraActivity extends Activity {
}
}
/**
* Start to take photo.
*/
public void startTakePhoto() {
ImageButton buttonCapture = (ImageButton)findViewById(R.id.capture);
buttonCapture.setEnabled(false);
setFlashMode();
customCamera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean bool, Camera camera) {
takePhoto();
}
});
}
/**
* Method to take picture.
*/
public void takePhoto() {
ImageButton imgIcon = (ImageButton)findViewById(R.id.capture);
imgIcon.setEnabled(false);
ImageButton flash = (ImageButton)findViewById(R.id.flash);
flash.setVisibility(View.INVISIBLE);
// Handles the moment where picture is taken
ShutterCallback shutterCallback = new ShutterCallback() {
public void onShutter() {
@ -700,14 +713,11 @@ public class CameraActivity extends Activity {
}
TransferBigData.setImgTaken(stream.toByteArray());
ImageButton imgIcon = (ImageButton)findViewById(R.id.capture);
imgIcon.setEnabled(true);
// Return to success & finish current activity.
cameraActivityCurrent.setResult(1,new Intent());
cameraActivityCurrent.finish();
} catch (IOException e) {
e.printStackTrace();
}
}
@ -731,6 +741,15 @@ public class CameraActivity extends Activity {
public void declinePhoto(View view) {
ImageButton imgIcon = (ImageButton)findViewById(R.id.capture);
imgIcon.setEnabled(true);
if (hasFlash()) {
ImageButton flash = (ImageButton)findViewById(R.id.flash);
flash.setVisibility(View.VISIBLE);
}
Camera.Parameters params = customCamera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
customCamera.setParameters(params);
photoTaken = null;
displayPicture();
}
@ -740,6 +759,7 @@ public class CameraActivity extends Activity {
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean("modeMiniature", modeMiniature);
outState.putParcelable("photoTaken", photoTaken);
outState.putInt("stateFlash", stateFlash);
super.onSaveInstanceState(outState);
}
@ -748,12 +768,14 @@ public class CameraActivity extends Activity {
protected void onRestoreInstanceState(Bundle savedInstanceState) {
modeMiniature = savedInstanceState.getBoolean("modeMiniature");
photoTaken = savedInstanceState.getParcelable("photoTaken");
stateFlash = savedInstanceState.getInt("stateFlash");
if (modeMiniature) {
buttonMiniature(findViewById(R.id.miniature));
}
displayPicture();
updateStateFlash(stateFlash);
super.onRestoreInstanceState(savedInstanceState);
}
@ -781,10 +803,10 @@ public class CameraActivity extends Activity {
}
/**
* Resize the bitmap saved when you rotate the device.
*
* @return the new bitmap.
*/
* Resize the bitmap saved when you rotate the device.
*
* @return the new bitmap.
*/
protected Bitmap resizePictureTaken() {
// Initialize the new bitmap resized
Bitmap newBitmap = null;
@ -815,10 +837,10 @@ public class CameraActivity extends Activity {
}
/**
* Allow to lock the screen or not.
*
* @param boolean lock Do we have to lock or not ?
*/
* Allow to lock the screen or not.
*
* @param lock Do we have to lock or not ?
*/
protected void lockScreen(boolean lock) {
if (lock == false) {
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
@ -868,4 +890,105 @@ public class CameraActivity extends Activity {
this.setResult(3);
this.finish();
}
/**
* Allow to enable or disable the flash of the camera.
* @param view The current view.
*/
public void switchFlash(View view) {
switch(stateFlash) {
case CameraActivity.FLASH_DISABLE:
updateStateFlash(CameraActivity.FLASH_ENABLE);
break;
case CameraActivity.FLASH_ENABLE:
updateStateFlash(CameraActivity.FLASH_AUTO);
break;
case CameraActivity.FLASH_AUTO:
updateStateFlash(CameraActivity.FLASH_DISABLE);
break;
}
}
protected void updateStateFlash(int newStateFlash) {
ImageButton flash = (ImageButton)findViewById(R.id.flash);
if (hasFlash()) {
Camera.Parameters params = customCamera.getParameters();
List<String> supportedFlashModes = params.getSupportedFlashModes();
if (newStateFlash == CameraActivity.FLASH_AUTO
&& !supportedFlashModes.contains(Camera.Parameters.FLASH_MODE_AUTO)
) {
if (stateFlash == CameraActivity.FLASH_ENABLE) {
newStateFlash = CameraActivity.FLASH_DISABLE;
} else {
newStateFlash = CameraActivity.FLASH_ENABLE;
}
}
stateFlash = newStateFlash;
int imgResource = R.drawable.no_flash;
switch(stateFlash) {
case CameraActivity.FLASH_DISABLE:
imgResource = R.drawable.no_flash;
break;
case CameraActivity.FLASH_ENABLE:
imgResource = R.drawable.flash;
break;
case CameraActivity.FLASH_AUTO:
imgResource = R.drawable.flash_auto;
break;
}
flash.setImageResource(imgResource);
customCamera.setParameters(params);
} else {
flash.setVisibility(View.INVISIBLE);
}
}
protected void setFlashMode() {
ImageButton flash = (ImageButton)findViewById(R.id.flash);
if (hasFlash()) {
String mode = Camera.Parameters.FLASH_MODE_OFF;
switch(stateFlash) {
case CameraActivity.FLASH_DISABLE:
mode = Camera.Parameters.FLASH_MODE_OFF;
break;
case CameraActivity.FLASH_ENABLE:
mode = Camera.Parameters.FLASH_MODE_ON;
break;
case CameraActivity.FLASH_AUTO:
mode = Camera.Parameters.FLASH_MODE_AUTO;
break;
}
Camera.Parameters params = customCamera.getParameters();
params.setFlashMode(mode);
customCamera.setParameters(params);
} else {
flash.setVisibility(View.INVISIBLE);
}
}
/**
* Check if camera has a flash feature.
* @return boolean.
*/
public boolean hasFlash() {
if (customCamera == null) {
return false;
}
Camera.Parameters parameters = customCamera.getParameters();
if (parameters.getFlashMode() == null) {
return false;
}
List<String> supportedFlashModes = parameters.getSupportedFlashModes();
return !(supportedFlashModes == null || supportedFlashModes.isEmpty() ||
(supportedFlashModes.size() == 1 && supportedFlashModes.get(0).equals(Camera.Parameters.FLASH_MODE_OFF))
);
}
}

View File

@ -7,6 +7,8 @@
// constructor.
function CustomCameraExport() {}
CustomCameraExport.prototype.FlashModes = {DISABLE: 0, ACTIVE: 1, AUTO: 2};
/**
* Start custom camera.
*
@ -24,7 +26,9 @@
cameraBackgroundColorPressed: "#dc453d", // color of the pressed camera button.
// To get supported color formats, go to see method parseColor : http://developer.android.com/reference/android/graphics/Color.html#parseColor(java.lang.String)
quality: 100, // picture's quality : range 0 - 100 : http://developer.android.com/reference/android/graphics/Bitmap.html#compress(android.graphics.Bitmap.CompressFormat, int, java.io.OutputStream) (parameter "quality")
opacity: true // active or disable the opacity function.
opacity: true, // active or disable the opacity function.
defaultFlash: this.FlashModes.DISABLE, // default state for flash. Corrects values = 0 (disable) / 1 (active) / 2 (auto)
switchFlash: true // active or disable the switch flash button.
};
for (var nameOption in defaultOptions) {
@ -54,7 +58,9 @@
options.cameraBackgroundColor,
options.cameraBackgroundColorPressed,
options.quality,
options.opacity
options.opacity,
options.defaultFlash,
options.switchFlash
]
);
};