diff --git a/app/src/main/java/com/simplemobiletools/camera/MainActivity.java b/app/src/main/java/com/simplemobiletools/camera/MainActivity.java index ad5dfc96..f923f0e9 100644 --- a/app/src/main/java/com/simplemobiletools/camera/MainActivity.java +++ b/app/src/main/java/com/simplemobiletools/camera/MainActivity.java @@ -8,6 +8,7 @@ import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; +import android.widget.ImageView; import android.widget.RelativeLayout; import butterknife.Bind; @@ -17,9 +18,11 @@ import butterknife.OnClick; public class MainActivity extends AppCompatActivity { @Bind(R.id.viewHolder) RelativeLayout viewHolder; @Bind(R.id.toggle_camera) View toggleCameraBtn; + @Bind(R.id.toggle_flash) ImageView toggleFlashBtn; private Preview preview; private int currCamera; + private boolean isFlashEnabled; @Override protected void onCreate(Bundle savedInstanceState) { @@ -37,15 +40,33 @@ public class MainActivity extends AppCompatActivity { @OnClick(R.id.toggle_camera) public void toggleCamera() { - if (currCamera == Camera.CameraInfo.CAMERA_FACING_BACK) + if (currCamera == Camera.CameraInfo.CAMERA_FACING_BACK) { currCamera = Camera.CameraInfo.CAMERA_FACING_FRONT; - else + } else { currCamera = Camera.CameraInfo.CAMERA_FACING_BACK; + } + disableFlash(); preview.releaseCamera(); preview.setCamera(currCamera); } + @OnClick(R.id.toggle_flash) + public void toggleFlash() { + if (isFlashEnabled) { + disableFlash(); + } else if (preview.enableFlash()) { + isFlashEnabled = preview.enableFlash(); + toggleFlashBtn.setImageResource(R.mipmap.flash_on); + } + } + + private void disableFlash() { + preview.disableFlash(); + isFlashEnabled = false; + toggleFlashBtn.setImageResource(R.mipmap.flash_off); + } + @OnClick(R.id.shutter) public void takePicture() { preview.takePicture(); diff --git a/app/src/main/java/com/simplemobiletools/camera/Preview.java b/app/src/main/java/com/simplemobiletools/camera/Preview.java index ac116eb0..8f779b0a 100644 --- a/app/src/main/java/com/simplemobiletools/camera/Preview.java +++ b/app/src/main/java/com/simplemobiletools/camera/Preview.java @@ -30,6 +30,8 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O private static boolean canTakePicture; private static Activity activity; private static int currCameraId; + private static boolean isFlashEnabled; + private static Camera.Parameters parameters; public Preview(Context context) { super(context); @@ -66,15 +68,15 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O camera = newCamera; if (camera != null) { - supportedPreviewSizes = camera.getParameters().getSupportedPreviewSizes(); + parameters = camera.getParameters(); + supportedPreviewSizes = parameters.getSupportedPreviewSizes(); requestLayout(); - final Camera.Parameters params = camera.getParameters(); - final List focusModes = params.getSupportedFocusModes(); + final List focusModes = parameters.getSupportedFocusModes(); if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) - params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); + parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); - camera.setParameters(params); + camera.setParameters(parameters); setCameraDisplayOrientation(cameraId, camera); if (canTakePicture) { @@ -119,6 +121,10 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O public void takePicture() { if (canTakePicture) { + if (isFlashEnabled) { + parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); + camera.setParameters(parameters); + } camera.takePicture(null, null, takePictureCallback); } canTakePicture = false; @@ -139,6 +145,11 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O final Camera.CameraInfo info = Utils.getCameraInfo(currCameraId); new PhotoProcessor(getContext(), info.facing).execute(data); + + if (isFlashEnabled) { + parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); + camera.setParameters(parameters); + } } }; @@ -148,7 +159,6 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O camera.cancelAutoFocus(); final Rect focusRect = calculateFocusArea(event.getX(), event.getY()); - final Camera.Parameters parameters = camera.getParameters(); if (parameters.getMaxNumFocusAreas() > 0) { final List focusAreas = new ArrayList<>(1); focusAreas.add(new Camera.Area(focusRect, 1000)); @@ -211,7 +221,6 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O private void setupPreview() { canTakePicture = true; if (camera != null && previewSize != null) { - final Camera.Parameters parameters = camera.getParameters(); parameters.setPreviewSize(previewSize.width, previewSize.height); requestLayout(); @@ -280,4 +289,17 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O focusArea(event); return false; } + + public boolean enableFlash() { + if (!Utils.hasFlash(camera)) { + return false; + } + + isFlashEnabled = true; + return true; + } + + public void disableFlash() { + isFlashEnabled = false; + } } diff --git a/app/src/main/java/com/simplemobiletools/camera/Utils.java b/app/src/main/java/com/simplemobiletools/camera/Utils.java index fc415ff2..a8e34252 100644 --- a/app/src/main/java/com/simplemobiletools/camera/Utils.java +++ b/app/src/main/java/com/simplemobiletools/camera/Utils.java @@ -4,6 +4,8 @@ import android.content.Context; import android.hardware.Camera; import android.widget.Toast; +import java.util.List; + public class Utils { public static Camera.CameraInfo getCameraInfo(int cameraId) { final Camera.CameraInfo info = new android.hardware.Camera.CameraInfo(); @@ -14,4 +16,24 @@ public class Utils { public static void showToast(Context context, int resId) { Toast.makeText(context, context.getResources().getString(resId), Toast.LENGTH_SHORT).show(); } + + public static boolean hasFlash(Camera camera) { + if (camera == null) { + return false; + } + + final Camera.Parameters parameters = camera.getParameters(); + + if (parameters.getFlashMode() == null) { + return false; + } + + final List supportedFlashModes = parameters.getSupportedFlashModes(); + if (supportedFlashModes == null || supportedFlashModes.isEmpty() || + supportedFlashModes.size() == 1 && supportedFlashModes.get(0).equals(Camera.Parameters.FLASH_MODE_OFF)) { + return false; + } + + return true; + } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index af24ef7e..50024fae 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -37,8 +37,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" - android:src="@mipmap/ic_launcher" - android:visibility="invisible"/> + android:src="@mipmap/flash_off"/> diff --git a/app/src/main/res/mipmap-hdpi/flash_off.png b/app/src/main/res/mipmap-hdpi/flash_off.png new file mode 100644 index 00000000..3b1d5903 Binary files /dev/null and b/app/src/main/res/mipmap-hdpi/flash_off.png differ diff --git a/app/src/main/res/mipmap-hdpi/flash_on.png b/app/src/main/res/mipmap-hdpi/flash_on.png new file mode 100644 index 00000000..31e1fc72 Binary files /dev/null and b/app/src/main/res/mipmap-hdpi/flash_on.png differ