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 00000000..a8831823 Binary files /dev/null and b/barcodescanner/src/main/res/drawable/toggle_torch.png differ diff --git a/barcodescanner/src/main/res/layout-ldpi/capture.xml b/barcodescanner/src/main/res/layout-ldpi/capture.xml index 97187eaa..e12faac1 100755 --- a/barcodescanner/src/main/res/layout-ldpi/capture.xml +++ b/barcodescanner/src/main/res/layout-ldpi/capture.xml @@ -208,4 +208,11 @@ android:drawableLeft="@drawable/flip_camera" android:visibility="invisible"/> +