From da0338ad19579ad3e18e22f9008bc85e6fbe36d5 Mon Sep 17 00:00:00 2001 From: Magnum Rocha Date: Tue, 16 Jun 2015 17:12:45 -0400 Subject: Add to the sample project the custom scanner example. --- .../java/example/zxing/CustomScannerActivity.java | 101 +++++++++++++++++++++ .../src/main/java/example/zxing/MainActivity.java | 4 + 2 files changed, 105 insertions(+) create mode 100644 sample/src/main/java/example/zxing/CustomScannerActivity.java (limited to 'sample/src/main/java/example') diff --git a/sample/src/main/java/example/zxing/CustomScannerActivity.java b/sample/src/main/java/example/zxing/CustomScannerActivity.java new file mode 100644 index 00000000..5214ead8 --- /dev/null +++ b/sample/src/main/java/example/zxing/CustomScannerActivity.java @@ -0,0 +1,101 @@ +package example.zxing; + +import android.app.Activity; +import android.content.pm.PackageManager; +import android.os.Bundle; +import android.support.v7.widget.Toolbar; +import android.view.KeyEvent; +import android.view.View; +import android.widget.Button; + +import com.journeyapps.barcodescanner.CaptureManager; +import com.journeyapps.barcodescanner.CompoundBarcodeView; + +/** + * Custom Scannner Activity extending from Activity to display a custom layout form scanner view. + */ +public class CustomScannerActivity extends Activity implements + CompoundBarcodeView.FlashlightListener { + + private CaptureManager capture; + private CompoundBarcodeView barcodeScannerView; + private Button switchFlashlightButton; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_custom_scanner); + + barcodeScannerView = (CompoundBarcodeView)findViewById(R.id.zxing_barcode_scanner); + barcodeScannerView.setFlashLightListener(this); + + switchFlashlightButton = (Button)findViewById(R.id.switch_flashlight); + + // if the device does not have flashlight in its camera, + // then remove the switch flashlight button... + if (!hasFlash()) { + switchFlashlightButton.setVisibility(View.GONE); + } + + capture = new CaptureManager(this, barcodeScannerView); + capture.initializeFromIntent(getIntent(), savedInstanceState); + capture.decode(); + } + + @Override + protected void onResume() { + super.onResume(); + capture.onResume(); + } + + @Override + protected void onPause() { + super.onPause(); + capture.onPause(); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + capture.onDestroy(); + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + capture.onSaveInstanceState(outState); + } + + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + return barcodeScannerView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event); + } + + /** + * Check if the device's camera has a Flashlight. + * @return true if there is Flashlight, otherwise false. + */ + private boolean hasFlash() { + return getApplicationContext().getPackageManager() + .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH); + } + + public void switchFlashlight(View view) { + if (getString(R.string.turn_on_flashlight).equals(switchFlashlightButton.getText())) { + barcodeScannerView.turnOnFlashLight(); + } else { + barcodeScannerView.turnOffFlashLight(); + } + } + + @Override + public void onFlashlightTurnOn() { + switchFlashlightButton.setText(R.string.turn_off_flashlight); + } + + @Override + public void onFlashlightTurnOff() { + switchFlashlightButton.setText(R.string.turn_on_flashlight); + } + +} diff --git a/sample/src/main/java/example/zxing/MainActivity.java b/sample/src/main/java/example/zxing/MainActivity.java index 1c3c4348..cc568be7 100644 --- a/sample/src/main/java/example/zxing/MainActivity.java +++ b/sample/src/main/java/example/zxing/MainActivity.java @@ -53,6 +53,10 @@ public class MainActivity extends ActionBarActivity { new IntentIntegrator(this).setCaptureActivity(ToolbarCaptureActivity.class).initiateScan(); } + public void scanCustomScanner(View view) { + new IntentIntegrator(this).setCaptureActivity(CustomScannerActivity.class).initiateScan(); + } + @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); -- cgit v1.2.3 From b513c28bf99a573b3b128e68bfb11a5850871785 Mon Sep 17 00:00:00 2001 From: Magnum Rocha Date: Tue, 16 Jun 2015 17:58:19 -0400 Subject: Refactor method name. --- sample/src/main/java/example/zxing/CustomScannerActivity.java | 4 ++-- .../src/com/journeyapps/barcodescanner/CompoundBarcodeView.java | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'sample/src/main/java/example') diff --git a/sample/src/main/java/example/zxing/CustomScannerActivity.java b/sample/src/main/java/example/zxing/CustomScannerActivity.java index 5214ead8..826bfe8e 100644 --- a/sample/src/main/java/example/zxing/CustomScannerActivity.java +++ b/sample/src/main/java/example/zxing/CustomScannerActivity.java @@ -82,9 +82,9 @@ public class CustomScannerActivity extends Activity implements public void switchFlashlight(View view) { if (getString(R.string.turn_on_flashlight).equals(switchFlashlightButton.getText())) { - barcodeScannerView.turnOnFlashLight(); + barcodeScannerView.turnOnFlashlight(); } else { - barcodeScannerView.turnOffFlashLight(); + barcodeScannerView.turnOffFlashlight(); } } diff --git a/zxing-android-embedded/src/com/journeyapps/barcodescanner/CompoundBarcodeView.java b/zxing-android-embedded/src/com/journeyapps/barcodescanner/CompoundBarcodeView.java index 94448fcd..73abd407 100644 --- a/zxing-android-embedded/src/com/journeyapps/barcodescanner/CompoundBarcodeView.java +++ b/zxing-android-embedded/src/com/journeyapps/barcodescanner/CompoundBarcodeView.java @@ -211,7 +211,7 @@ public class CompoundBarcodeView extends FrameLayout { /** * Turn on the device's flashlight. */ - public void turnOnFlashLight() { + public void turnOnFlashlight() { barcodeView.setTorch(true); if (flashlightListener != null) { @@ -222,7 +222,7 @@ public class CompoundBarcodeView extends FrameLayout { /** * Turn off the device's flashlight. */ - public void turnOffFlashLight() { + public void turnOffFlashlight() { barcodeView.setTorch(false); if (flashlightListener != null) { @@ -244,10 +244,10 @@ public class CompoundBarcodeView extends FrameLayout { return true; // Use volume up/down to turn on light case KeyEvent.KEYCODE_VOLUME_DOWN: - turnOffFlashLight(); + turnOffFlashlight(); return true; case KeyEvent.KEYCODE_VOLUME_UP: - turnOnFlashLight(); + turnOnFlashlight(); return true; } return super.onKeyDown(keyCode, event); -- cgit v1.2.3 From 4da6cedea678107bb6af994e53d3713887e1ec3e Mon Sep 17 00:00:00 2001 From: Magnum Rocha Date: Tue, 16 Jun 2015 18:01:06 -0400 Subject: Refactor method name. --- sample/src/main/java/example/zxing/CustomScannerActivity.java | 2 +- .../src/com/journeyapps/barcodescanner/CompoundBarcodeView.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'sample/src/main/java/example') diff --git a/sample/src/main/java/example/zxing/CustomScannerActivity.java b/sample/src/main/java/example/zxing/CustomScannerActivity.java index 826bfe8e..a16c6182 100644 --- a/sample/src/main/java/example/zxing/CustomScannerActivity.java +++ b/sample/src/main/java/example/zxing/CustomScannerActivity.java @@ -27,7 +27,7 @@ public class CustomScannerActivity extends Activity implements setContentView(R.layout.activity_custom_scanner); barcodeScannerView = (CompoundBarcodeView)findViewById(R.id.zxing_barcode_scanner); - barcodeScannerView.setFlashLightListener(this); + barcodeScannerView.setFlashlightListener(this); switchFlashlightButton = (Button)findViewById(R.id.switch_flashlight); diff --git a/zxing-android-embedded/src/com/journeyapps/barcodescanner/CompoundBarcodeView.java b/zxing-android-embedded/src/com/journeyapps/barcodescanner/CompoundBarcodeView.java index 73abd407..a97debfa 100644 --- a/zxing-android-embedded/src/com/journeyapps/barcodescanner/CompoundBarcodeView.java +++ b/zxing-android-embedded/src/com/journeyapps/barcodescanner/CompoundBarcodeView.java @@ -253,7 +253,7 @@ public class CompoundBarcodeView extends FrameLayout { return super.onKeyDown(keyCode, event); } - public void setFlashLightListener(FlashlightListener listener) { + public void setFlashlightListener(FlashlightListener listener) { this.flashlightListener = listener; } -- cgit v1.2.3 From c32a34e0e5b493f5081dbb46526b7412b2a13a19 Mon Sep 17 00:00:00 2001 From: Ralf Kistner Date: Sun, 21 Jun 2015 14:52:03 +0200 Subject: Use "torch" instead of "flashlight" to keep terminology consistent. --- .../java/example/zxing/CustomScannerActivity.java | 13 ++++----- .../barcodescanner/CompoundBarcodeView.java | 33 +++++++++++----------- 2 files changed, 22 insertions(+), 24 deletions(-) (limited to 'sample/src/main/java/example') diff --git a/sample/src/main/java/example/zxing/CustomScannerActivity.java b/sample/src/main/java/example/zxing/CustomScannerActivity.java index a16c6182..bc5c4dc1 100644 --- a/sample/src/main/java/example/zxing/CustomScannerActivity.java +++ b/sample/src/main/java/example/zxing/CustomScannerActivity.java @@ -3,7 +3,6 @@ package example.zxing; import android.app.Activity; import android.content.pm.PackageManager; import android.os.Bundle; -import android.support.v7.widget.Toolbar; import android.view.KeyEvent; import android.view.View; import android.widget.Button; @@ -15,7 +14,7 @@ import com.journeyapps.barcodescanner.CompoundBarcodeView; * Custom Scannner Activity extending from Activity to display a custom layout form scanner view. */ public class CustomScannerActivity extends Activity implements - CompoundBarcodeView.FlashlightListener { + CompoundBarcodeView.TorchListener { private CaptureManager capture; private CompoundBarcodeView barcodeScannerView; @@ -27,7 +26,7 @@ public class CustomScannerActivity extends Activity implements setContentView(R.layout.activity_custom_scanner); barcodeScannerView = (CompoundBarcodeView)findViewById(R.id.zxing_barcode_scanner); - barcodeScannerView.setFlashlightListener(this); + barcodeScannerView.setTorchListener(this); switchFlashlightButton = (Button)findViewById(R.id.switch_flashlight); @@ -82,19 +81,19 @@ public class CustomScannerActivity extends Activity implements public void switchFlashlight(View view) { if (getString(R.string.turn_on_flashlight).equals(switchFlashlightButton.getText())) { - barcodeScannerView.turnOnFlashlight(); + barcodeScannerView.setTorchOn(); } else { - barcodeScannerView.turnOffFlashlight(); + barcodeScannerView.setTorchOff(); } } @Override - public void onFlashlightTurnOn() { + public void onTorchOn() { switchFlashlightButton.setText(R.string.turn_off_flashlight); } @Override - public void onFlashlightTurnOff() { + public void onTorchOff() { switchFlashlightButton.setText(R.string.turn_on_flashlight); } diff --git a/zxing-android-embedded/src/com/journeyapps/barcodescanner/CompoundBarcodeView.java b/zxing-android-embedded/src/com/journeyapps/barcodescanner/CompoundBarcodeView.java index 239beb38..1309c432 100644 --- a/zxing-android-embedded/src/com/journeyapps/barcodescanner/CompoundBarcodeView.java +++ b/zxing-android-embedded/src/com/journeyapps/barcodescanner/CompoundBarcodeView.java @@ -5,7 +5,6 @@ import android.content.Intent; import android.content.res.TypedArray; import android.util.AttributeSet; import android.view.KeyEvent; -import android.view.View; import android.widget.FrameLayout; import android.widget.TextView; @@ -34,9 +33,9 @@ public class CompoundBarcodeView extends FrameLayout { private TextView statusView; /** - * The instance of @link FlashlightListener to send events callback. + * The instance of @link TorchListener to send events callback. */ - private FlashlightListener flashlightListener; + private TorchListener torchListener; private class WrappedCallback implements BarcodeCallback { private BarcodeCallback delegate; @@ -205,22 +204,22 @@ public class CompoundBarcodeView extends FrameLayout { /** * Turn on the device's flashlight. */ - public void turnOnFlashlight() { + public void setTorchOn() { barcodeView.setTorch(true); - if (flashlightListener != null) { - flashlightListener.onFlashlightTurnOn(); + if (torchListener != null) { + torchListener.onTorchOn(); } } /** * Turn off the device's flashlight. */ - public void turnOffFlashlight() { + public void setTorchOff() { barcodeView.setTorch(false); - if (flashlightListener != null) { - flashlightListener.onFlashlightTurnOff(); + if (torchListener != null) { + torchListener.onTorchOff(); } } @@ -238,27 +237,27 @@ public class CompoundBarcodeView extends FrameLayout { return true; // Use volume up/down to turn on light case KeyEvent.KEYCODE_VOLUME_DOWN: - turnOffFlashlight(); + setTorchOff(); return true; case KeyEvent.KEYCODE_VOLUME_UP: - turnOnFlashlight(); + setTorchOn(); return true; } return super.onKeyDown(keyCode, event); } - public void setFlashlightListener(FlashlightListener listener) { - this.flashlightListener = listener; + public void setTorchListener(TorchListener listener) { + this.torchListener = listener; } /** - * The Listener to Flashlight events (turn on, Turn Off). + * The Listener to torch/fflashlight events (turn on, turn off). */ - public interface FlashlightListener { + public interface TorchListener { - void onFlashlightTurnOn(); + void onTorchOn(); - void onFlashlightTurnOff(); + void onTorchOff(); } -- cgit v1.2.3