diff options
| author | Magnum Rocha <magnum.rocha@fpf.br> | 2015-06-17 00:12:45 +0300 |
|---|---|---|
| committer | Magnum Rocha <magnum.rocha@fpf.br> | 2015-06-17 00:12:45 +0300 |
| commit | da0338ad19579ad3e18e22f9008bc85e6fbe36d5 (patch) | |
| tree | 5570ed09ee41ee5e1e5ae44838da545a445c98a9 /sample/src/main/java/example/zxing/CustomScannerActivity.java | |
| parent | fe1ae0c1020d788b9cb62b734b24d948b3e2ced6 (diff) | |
| download | zxing-android-embedded-da0338ad19579ad3e18e22f9008bc85e6fbe36d5.tar.xz | |
Add to the sample project the custom scanner example.
Diffstat (limited to 'sample/src/main/java/example/zxing/CustomScannerActivity.java')
| -rw-r--r-- | sample/src/main/java/example/zxing/CustomScannerActivity.java | 101 |
1 files changed, 101 insertions, 0 deletions
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); + } + +} |
