Skip to content

Commit

Permalink
Add an optional 'torch toggle' button #3
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Dec 3, 2016
1 parent c408f62 commit 084d505
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions barcodescanner/src/main/res/layout-ldpi/capture.xml
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,11 @@
android:drawableLeft="@drawable/flip_camera"
android:visibility="invisible"/>

<Button android:id="@+id/torch_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:drawableLeft="@drawable/toggle_torch"
android:visibility="invisible"/>

</merge>
8 changes: 8 additions & 0 deletions barcodescanner/src/main/res/layout/capture.xml
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,12 @@
android:drawableStart="@drawable/flip_camera"
android:visibility="invisible"/>

<Button android:id="@+id/torch_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:drawableLeft="@drawable/toggle_torch"
android:drawableStart="@drawable/toggle_torch"
android:visibility="invisible"/>

</merge>

0 comments on commit 084d505

Please sign in to comment.