From 084d505cb6534c2f250a8454097ef75fe99a24af Mon Sep 17 00:00:00 2001 From: Eddy Verbruggen Date: Sat, 3 Dec 2016 17:42:53 +0100 Subject: [PATCH] Add an optional 'torch toggle' button #3 --- .../zxing/client/android/CaptureActivity.java | 23 ++++++++++++++++++ .../google/zxing/client/android/Intents.java | 5 ++++ .../client/android/camera/CameraManager.java | 17 +++++++------ .../src/main/res/drawable/toggle_torch.png | Bin 0 -> 4084 bytes .../src/main/res/layout-ldpi/capture.xml | 7 ++++++ .../src/main/res/layout/capture.xml | 8 ++++++ 6 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 barcodescanner/src/main/res/drawable/toggle_torch.png diff --git a/barcodescanner/src/main/java/com/google/zxing/client/android/CaptureActivity.java b/barcodescanner/src/main/java/com/google/zxing/client/android/CaptureActivity.java index 4b7dcdf7..18bf0717 100755 --- a/barcodescanner/src/main/java/com/google/zxing/client/android/CaptureActivity.java +++ b/barcodescanner/src/main/java/com/google/zxing/client/android/CaptureActivity.java @@ -18,6 +18,8 @@ import android.content.BroadcastReceiver; import android.content.Context; +import android.content.pm.FeatureInfo; +import android.content.pm.PackageManager; import android.content.res.Configuration; import android.hardware.Camera; import android.support.v4.content.LocalBroadcastManager; @@ -109,6 +111,7 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal private ViewfinderView viewfinderView; private TextView statusView; private Button flipButton; + private Button torchButton; private View resultView; private Result lastResult; private boolean hasSurface; @@ -190,6 +193,7 @@ protected void onResume() { resultView = findViewById(R.id.result_view); statusView = (TextView) findViewById(R.id.status_view); flipButton = (Button) findViewById(R.id.flip_button); + torchButton = (Button) findViewById(R.id.torch_button); handler = null; lastResult = null; @@ -814,6 +818,25 @@ public void onClick(View v) { }); } } + + if (getIntent().getBooleanExtra(Intents.Scan.SHOW_TORCH_BUTTON, false)) { + // only draw the button in case we're using the back camera + final int reqCamId = getIntent().getIntExtra(Intents.Scan.CAMERA_ID, OpenCameraInterface.NO_REQUESTED_CAMERA); + if (reqCamId != 1) { + for (final FeatureInfo feature : this.getPackageManager().getSystemAvailableFeatures()) { + if (PackageManager.FEATURE_CAMERA_FLASH.equalsIgnoreCase(feature.name)) { + torchButton.setVisibility(View.VISIBLE); + torchButton.setOnClickListener(new Button.OnClickListener() { + @Override + public void onClick(View v) { + cameraManager.setTorch(!cameraManager.isTorchOn()); + } + }); + break; + } + } + } + } } public void drawViewfinder() { diff --git a/barcodescanner/src/main/java/com/google/zxing/client/android/Intents.java b/barcodescanner/src/main/java/com/google/zxing/client/android/Intents.java index f1caa8c4..865c2761 100755 --- a/barcodescanner/src/main/java/com/google/zxing/client/android/Intents.java +++ b/barcodescanner/src/main/java/com/google/zxing/client/android/Intents.java @@ -178,6 +178,11 @@ public static final class Scan { */ public static final String SHOW_FLIP_CAMERA_BUTTON = "SHOW_FLIP_CAMERA_BUTTON"; + /** + * Set to true if we want to show the button to toggle the torch (if available) + */ + public static final String SHOW_TORCH_BUTTON = "SHOW_TORCH_BUTTON"; + /** * Set to true if you want to enable bulk scan mode */ diff --git a/barcodescanner/src/main/java/com/google/zxing/client/android/camera/CameraManager.java b/barcodescanner/src/main/java/com/google/zxing/client/android/camera/CameraManager.java index cf063f8f..ee929c6e 100755 --- a/barcodescanner/src/main/java/com/google/zxing/client/android/camera/CameraManager.java +++ b/barcodescanner/src/main/java/com/google/zxing/client/android/camera/CameraManager.java @@ -23,8 +23,6 @@ import android.hardware.Camera; import android.os.Handler; import android.util.Log; -import android.view.Display; -import android.view.Surface; import android.view.SurfaceHolder; import android.view.WindowManager; @@ -173,11 +171,16 @@ public synchronized void stopPreview() { } } - /** - * Convenience method for {@link com.google.zxing.client.android.CaptureActivity} - * - * @param newSetting if {@code true}, light should be turned on if currently off. And vice versa. - */ + public synchronized boolean isTorchOn() { + return camera != null && + configManager.getTorchState(camera.getCamera()); + } + + /** + * Convenience method for {@link com.google.zxing.client.android.CaptureActivity} + * + * @param newSetting if {@code true}, light should be turned on if currently off. And vice versa. + */ public synchronized void setTorch(boolean newSetting) { OpenCamera theCamera = camera; if (theCamera != null) { diff --git a/barcodescanner/src/main/res/drawable/toggle_torch.png b/barcodescanner/src/main/res/drawable/toggle_torch.png new file mode 100644 index 0000000000000000000000000000000000000000..a8831823ca9b0a95e7acbe72253646eabc1a2b2e GIT binary patch literal 4084 zcmV#P)KLZ*U+IBfRsybQWXdwQbLP>6pAqfylh#{fb6;Z(vMMVS~$e@S=j*ftg6;Uhf59&ghTmgWD0l;*T zI709Y^p6lP1rIRMx#05C~cW=H_Aw*bJ-5DT&Z2n+x)QHX^p z00esgV8|mQcmRZ%02D^@S3L16t`O%c004NIvOKvYIYoh62rY33S640`D9%Y2D-rV&neh&#Q1i z007~1e$oCcFS8neI|hJl{-P!B1ZZ9hpmq0)X0i`JwE&>$+E?>%_LC6RbVIkUx0b+_+BaR3cnT7Zv!AJxW zizFb)h!jyGOOZ85F;a?DAXP{m@;!0_IfqH8(HlgRxt7s3}k3K`kFu>>-2Q$QMFfPW!La{h336o>X zu_CMttHv6zR;&ZNiS=X8v3CR#fknUxHUxJ0uoBa_M6WNWeqIg~6QE69c9o#eyhGvpiOA@W-aonk<7r1(?fC{oI5N*U!4 zfg=2N-7=cNnjjOr{yriy6mMFgG#l znCF=fnQv8CDz++o6_Lscl}eQ+l^ZHARH>?_s@|##Rr6KLRFA1%Q+=*RRWnoLsR`7U zt5vFIcfW3@?wFpwUVxrVZ>QdQz32KIeJ}k~{cZZE^+ya? z2D1z#2HOnI7(B%_ac?{wFUQ;QQA1tBKtrWrm0_3Rgps+?Jfqb{jYbcQX~taRB;#$y zZN{S}1|}gUOHJxc?wV3fxuz+mJ4`!F$IZ;mqRrNsHJd##*D~ju=bP7?-?v~|cv>vB zsJ6IeNwVZxrdjT`yl#bBIa#GxRa#xMMy;K#CDyyGyQdMSxlWT#tDe?p!?5wT$+oGt z8L;Kp2HUQ-ZMJ=3XJQv;x5ci*?vuTfeY$;({XGW_huIFR9a(?@3)XSs8O^N5RyOM=TTmp(3=8^+zp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