From 9504fe9ef889e255400c1dde34e0f5583dfefaf1 Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 28 Apr 2016 00:10:04 +0200 Subject: [PATCH] implement Flash --- .../camera/MainActivity.java | 25 +++++++++++- .../com/simplemobiletools/camera/Preview.java | 36 ++++++++++++++---- .../com/simplemobiletools/camera/Utils.java | 22 +++++++++++ app/src/main/res/layout/activity_main.xml | 3 +- app/src/main/res/mipmap-hdpi/flash_off.png | Bin 0 -> 1404 bytes app/src/main/res/mipmap-hdpi/flash_on.png | Bin 0 -> 1335 bytes 6 files changed, 75 insertions(+), 11 deletions(-) create mode 100644 app/src/main/res/mipmap-hdpi/flash_off.png create mode 100644 app/src/main/res/mipmap-hdpi/flash_on.png 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 0000000000000000000000000000000000000000..3b1d5903e3c7ff3a4e8cb452c01b0c59ad01b24d GIT binary patch literal 1404 zcmV-?1%vvDP)q`&zlZrcT$dn6Roa_gq13gLr^qvJ5)7-HlOQEr z?}Yhoom;dnVrBfqGAb~7i)jW_w+y6!dSw=Sq5@;gXX_$Xl13FN>74z{HfsNMA`!D9 zG+%^e=@@VT(FxVF!!)aM+|O5yta8o4v2vD-jI;vh;5`+l_D;M*wEB~ocpB3+mwXklc97MvsD-4 z;Yg4_E#`yx@43el&6M2=GBDvv70}xT6((O1#5$bQ3Co9JXBZAf;cOJnM&Mkc#y%em zaBvuQcEWOE!Kq9hmS}^Dp#uI)^mHr8KwUFs4#i-J@^Kwa_cWr3hI;06l!;eM*$+k6 zU}G4L>kvw;yeY`gAryw=H(F->^DG7si2Q!HVFIRB#N*Om3KM&;*+V?V#;ek7=`pp_z(i}F9sr57D^nBJ~k%Lur1fpML&eN< z%$Y|B6*IS?Wqhwe%5>+lK^HUg_MRwRV07oQ!7K-}3K9dQ>Ouizks5 zbFKLTNg$oElUMLoV7=A!W@B;#Li$AOUfB?mxq^OcKiDrd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