9
0
mirror of https://gitee.com/shuto/customCamera.git synced 2026-05-02 00:07:24 +08:00

Refacto pour flash + autofocus.

This commit is contained in:
Christophe BOUCAUT
2015-02-03 11:04:15 +01:00
parent 7c34e811a4
commit b177fa9f54
3 changed files with 101 additions and 89 deletions

Before

Width:  |  Height:  |  Size: 3.3 KiB

After

Width:  |  Height:  |  Size: 3.3 KiB

@@ -27,27 +27,9 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="60dp" android:layout_marginTop="60dp"
android:background="@android:color/transparent" android:background="@android:color/transparent"
android:onClick="enableFlash" android:onClick="switchFlash"
android:src="@drawable/flash" /> android:src="@drawable/flash" />
<ImageButton
android:id="@+id/noFlash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:background="@android:color/transparent"
android:onClick="enableFlash"
android:src="@drawable/no_flash" />
<ImageButton
android:id="@+id/flashAuto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:background="@android:color/transparent"
android:onClick="enableFlash"
android:src="@drawable/icon_flash_auto" />
<LinearLayout <LinearLayout
android:id="@+id/beforePhoto" android:id="@+id/beforePhoto"
android:layout_width="match_parent" android:layout_width="match_parent"
@@ -64,6 +64,10 @@ public class CameraActivity extends Activity {
public static final int DEGREE_90 = 90; public static final int DEGREE_90 = 90;
public static final int DEGREE_180 = 180; public static final int DEGREE_180 = 180;
public static final int DEGREE_270 = 270; 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. * To get camera resource or stop this activity.
@@ -150,7 +154,7 @@ public class CameraActivity extends Activity {
view.performClick(); view.performClick();
((CameraActivity) currentActivity).setCameraBackgroundColor( ((CameraActivity) currentActivity).setCameraBackgroundColor(
currentActivity.getIntent().getStringExtra("cameraBackgroundColor")); currentActivity.getIntent().getStringExtra("cameraBackgroundColor"));
((CameraActivity) currentActivity).takePhoto(); ((CameraActivity) currentActivity).startTakePhoto();
break; break;
default: default:
break; break;
@@ -189,6 +193,8 @@ public class CameraActivity extends Activity {
return; return;
} }
updateStateFlash(stateFlash);
int orientation = 0; int orientation = 0;
switch (getCustomRotation()) { switch (getCustomRotation()) {
case 0: case 0:
@@ -308,11 +314,6 @@ public class CameraActivity extends Activity {
customCamera.cancelAutoFocus(); customCamera.cancelAutoFocus();
handleZoom(event, paramsCamera, distanceBetweenFingers); handleZoom(event, paramsCamera, distanceBetweenFingers);
} }
} else {
// If we touch with one finger -> auto-focus
if (action == MotionEvent.ACTION_UP) {
handleFocus(event, paramsCamera);
}
} }
} }
@@ -376,25 +377,6 @@ public class CameraActivity extends Activity {
zoomLevel.setVisibility(View.VISIBLE); 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. */ /** To set background in the view. */
protected void setBackground() { protected void setBackground() {
// Get the base64 picture for the background only if it's exist. // Get the base64 picture for the background only if it's exist.
@@ -584,18 +566,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. * Method to take picture.
*/ */
public void takePhoto() { public void takePhoto() {
final ImageButton imgIcon = (ImageButton)findViewById(R.id.capture); ImageButton flash = (ImageButton)findViewById(R.id.flash);
final ImageButton flash = (ImageButton)findViewById(R.id.flash);
final ImageButton flashAuto = (ImageButton)findViewById(R.id.flashAuto);
final ImageButton noFlash = (ImageButton)findViewById(R.id.noFlash);
imgIcon.setEnabled(false);
flash.setVisibility(View.INVISIBLE); flash.setVisibility(View.INVISIBLE);
flashAuto.setVisibility(View.INVISIBLE);
noFlash.setVisibility(View.INVISIBLE);
// Handles the moment where picture is taken // Handles the moment where picture is taken
ShutterCallback shutterCallback = new ShutterCallback() { ShutterCallback shutterCallback = new ShutterCallback() {
public void onShutter() { public void onShutter() {
@@ -708,9 +699,7 @@ public class CameraActivity extends Activity {
} }
TransferBigData.setImgTaken(stream.toByteArray()); TransferBigData.setImgTaken(stream.toByteArray());
ImageButton imgIcon = (ImageButton)findViewById(R.id.capture);
imgIcon.setEnabled(true);
// Return to success & finish current activity. // Return to success & finish current activity.
cameraActivityCurrent.setResult(1,new Intent()); cameraActivityCurrent.setResult(1,new Intent());
cameraActivityCurrent.finish(); cameraActivityCurrent.finish();
@@ -739,16 +728,11 @@ public class CameraActivity extends Activity {
public void declinePhoto(View view) { public void declinePhoto(View view) {
ImageButton imgIcon = (ImageButton)findViewById(R.id.capture); ImageButton imgIcon = (ImageButton)findViewById(R.id.capture);
ImageButton flash = (ImageButton)findViewById(R.id.flash); ImageButton flash = (ImageButton)findViewById(R.id.flash);
ImageButton flashAuto = (ImageButton)findViewById(R.id.flashAuto);
ImageButton noFlash = (ImageButton)findViewById(R.id.noFlash);
imgIcon.setEnabled(true); imgIcon.setEnabled(true);
if (stateFlash == 0) { flash.setVisibility(View.VISIBLE);
noFlash.setVisibility(View.VISIBLE); Camera.Parameters params = customCamera.getParameters();
} else if (stateFlash == 1) { params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
flash.setVisibility(View.VISIBLE); customCamera.setParameters(params);
} else if (stateFlash == 2) {
flashAuto.setVisibility(View.VISIBLE);
}
photoTaken = null; photoTaken = null;
displayPicture(); displayPicture();
} }
@@ -774,6 +758,7 @@ public class CameraActivity extends Activity {
} }
displayPicture(); displayPicture();
updateStateFlash(stateFlash);
super.onRestoreInstanceState(savedInstanceState); super.onRestoreInstanceState(savedInstanceState);
} }
@@ -893,36 +878,78 @@ public class CameraActivity extends Activity {
* Allow to enable or disable the flash of the camera. * Allow to enable or disable the flash of the camera.
* @param view The current view. * @param view The current view.
*/ */
public void enableFlash(View view) { public void switchFlash(View view) {
ImageButton flash = (ImageButton)findViewById(R.id.flash); switch(stateFlash) {
ImageButton flashAuto = (ImageButton)findViewById(R.id.flashAuto); case CameraActivity.FLASH_DISABLE:
ImageButton noFlash = (ImageButton)findViewById(R.id.noFlash); updateStateFlash(CameraActivity.FLASH_ENABLE);
Camera.Parameters params = customCamera.getParameters(); 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()) { if (hasFlash()) {
flash.setVisibility(View.INVISIBLE); Camera.Parameters params = customCamera.getParameters();
flashAuto.setVisibility(View.INVISIBLE); List<String> supportedFlashModes = params.getSupportedFlashModes();
noFlash.setVisibility(View.INVISIBLE);
if (params.getFlashMode().equals(Camera.Parameters.FLASH_MODE_ON) if (newStateFlash == CameraActivity.FLASH_AUTO
|| params.getFlashMode().equals(Camera.Parameters.FLASH_MODE_RED_EYE) && !supportedFlashModes.contains(Camera.Parameters.FLASH_MODE_AUTO)
|| params.getFlashMode().equals(Camera.Parameters.FLASH_MODE_TORCH)) { ) {
params.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO); if (stateFlash == CameraActivity.FLASH_ENABLE) {
stateFlash = 2; newStateFlash = CameraActivity.FLASH_DISABLE;
flashAuto.setVisibility(View.VISIBLE); } else {
} else if (params.getFlashMode().equals(Camera.Parameters.FLASH_MODE_AUTO)) { newStateFlash = CameraActivity.FLASH_ENABLE;
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); }
noFlash.setVisibility(View.VISIBLE);
stateFlash = 0;
} else if (params.getFlashMode().equals(Camera.Parameters.FLASH_MODE_OFF)) {
params.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
flash.setVisibility(View.VISIBLE);
stateFlash = 1;
} }
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); customCamera.setParameters(params);
} else { } else {
flash.setVisibility(View.INVISIBLE); flash.setVisibility(View.INVISIBLE);
flashAuto.setVisibility(View.INVISIBLE);
noFlash.setVisibility(View.INVISIBLE);
} }
} }
@@ -944,8 +971,11 @@ public class CameraActivity extends Activity {
List<String> supportedFlashModes = parameters.getSupportedFlashModes(); List<String> supportedFlashModes = parameters.getSupportedFlashModes();
if (supportedFlashModes == null if (supportedFlashModes == null
|| supportedFlashModes.isEmpty() || supportedFlashModes.isEmpty()
|| supportedFlashModes.size() == 1 || (
&& supportedFlashModes.get(0).equals(Camera.Parameters.FLASH_MODE_OFF)) { supportedFlashModes.size() == 1
&& supportedFlashModes.get(0).equals(Camera.Parameters.FLASH_MODE_OFF)
)
) {
return false; return false;
} }