Compare commits
258 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d09b7c76c3 | |||
| 3a2db7ffc6 | |||
| 2a65a6c973 | |||
| 3c77f1d6c8 | |||
| b6c6421dd5 | |||
| 8173d4a8b2 | |||
| 8fdbbbef5e | |||
| 2324a5403e | |||
| c752130405 | |||
| 1c27c8fe2d | |||
| aa129af542 | |||
| 90b5db5fcd | |||
| 24d02945fe | |||
| 6ccf3a9d1a | |||
| 23f29e5a56 | |||
| 1a8d949325 | |||
| f1554b7478 | |||
| cc12c103e0 | |||
| a13ce2b161 | |||
| 0c97cf70cc | |||
| 9beeac7960 | |||
| cc34502f13 | |||
| 2221621de9 | |||
| 4d4c5a2c4e | |||
| 65d2716c71 | |||
| 7fd1ab8794 | |||
| 630e3cc0f8 | |||
| 40260272fc | |||
| f54615e43a | |||
| 1acc3a6f4f | |||
| 73093b9b4f | |||
| db85816b3d | |||
| 6a3d857019 | |||
| 6c8ea7e3fd | |||
| 35ee545f8b | |||
| 5d9e2c3756 | |||
| cce2a300ef | |||
| 13440ad875 | |||
| 5a2470f034 | |||
| 545672bb8f | |||
| 542dc66090 | |||
| ea414a236e | |||
| 1ba1ded540 | |||
| 4893bcc194 | |||
| 6498fad6a7 | |||
| f7f779d7ac | |||
| c555ffbe05 | |||
| e0d340952d | |||
| 9b924e6875 | |||
| 8df0cb554a | |||
| 09d992ddfa | |||
| 95cd4a3a4d | |||
| 7e5bbd2997 | |||
| 8d8cd7d445 | |||
| bd857c68eb | |||
| 0fdfbce9fb | |||
| eba457ee9e | |||
| 9319110de8 | |||
| ee4b181f23 | |||
| a4f751500c | |||
| 75b4aa57f5 | |||
| eb4cbd5095 | |||
| aa4fca5dae | |||
| d22b581cd3 | |||
| 9349b31ab3 | |||
| c0c8363f85 | |||
| 27c22e8ffe | |||
| d77fa29226 | |||
| 9aa5381c2b | |||
| 3a7612661a | |||
| 70f4bbb8de | |||
| 363ad77540 | |||
| 62547c10d7 | |||
| 0de49aa7c8 | |||
| 1a1c8d0e66 | |||
| 31332404fc | |||
| 28c646b9e2 | |||
| c0c0fcd745 | |||
| 98d00abcbd | |||
| 7042d74f4a | |||
| 94ae63d857 | |||
| 0e7413e1f7 | |||
| 82e8f9afcc | |||
| 6556f81558 | |||
| 5983e2fb8e | |||
| be04a7587f | |||
| 330cb2b63b | |||
| 6b49e8774f | |||
| 209e1c9a04 | |||
| c7a143499e | |||
| a4dfc2fca4 | |||
| 98b69af339 | |||
| 40010a62df | |||
| 5927f38a1d | |||
| f2a477b647 | |||
| 65aaa93a27 | |||
| 1e1b075f13 | |||
| 224bfd6ea2 | |||
| c1a3786c17 | |||
| 2af07f95cf | |||
| 4e7ee70b73 | |||
| aec45c4f10 | |||
| c1918b32fb | |||
| a2774c7bde | |||
| 2f061a1da5 | |||
| 70f4159e3a | |||
| 295193d613 | |||
| d8f3157e97 | |||
| a7d91dbed6 | |||
| e8257760da | |||
| 2f022c0a70 | |||
| 3fd9354cd7 | |||
| c98a04a407 | |||
| 8e90d15117 | |||
| fa2cd1562d | |||
| 11073ddcf9 | |||
| 17ad61674e | |||
| 8a67d852e4 | |||
| 68f17bde30 | |||
| 34788e0fbc | |||
| 45756b51b2 | |||
| adee48a8ae | |||
| e48229ac3f | |||
| 2ff44f91b5 | |||
| f30ea0c955 | |||
| cf4c0dce50 | |||
| 670000698d | |||
| 60ed28ee06 | |||
| c83b2c5a5c | |||
| eaeddd23af | |||
| 0fd028b9e2 | |||
| e2892cad8f | |||
| 1817bb198b | |||
| 29848b2b4f | |||
| 9ec92a5101 | |||
| 2cf4b60f0f | |||
| e3aed41746 | |||
| 09571d3813 | |||
| 6c250faeb6 | |||
| 4955c18a87 | |||
| 6f73f470c9 | |||
| 5d48d2ff35 | |||
| 92f6acf3d6 | |||
| 5309388c7a | |||
| b3a2c1e07e | |||
| 958bbb1491 | |||
| 34aca80673 | |||
| 0b774fba7c | |||
| 81effea784 | |||
| b4a88bfb56 | |||
| 6c3e917b15 | |||
| f484d21af8 | |||
| d3330c9663 | |||
| 964dbab7b2 | |||
| 76861f8683 | |||
| 42d09b810b | |||
| c4cbaba495 | |||
| 2373c88ca9 | |||
| 4f311133ee | |||
| 531918d28a | |||
| e3d3950d9a | |||
| 1b8fbe60cc | |||
| a2cc8e2756 | |||
| a7c8046c22 | |||
| 8992dc5710 | |||
| 33b88ed645 | |||
| 50e3acce09 | |||
| a8305bd237 | |||
| 11de8f4288 | |||
| c9e3bf415f | |||
| e27bef5518 | |||
| 9ea3e3ef08 | |||
| 03ab7214f6 | |||
| 0b07174be9 | |||
| 50ea6bb48a | |||
| aefdceaffd | |||
| 91157bc907 | |||
| 4c745d697b | |||
| 2219087d6a | |||
| c81ea8e6eb | |||
| 8ae76eaac6 | |||
| 7d47cd352a | |||
| 3e12faa379 | |||
| cac37cb7e5 | |||
| 39430544f6 | |||
| e7fce1f563 | |||
| 5ad456a5d0 | |||
| 905e4a33d6 | |||
| d34fa22d7a | |||
| cb68383a4c | |||
| 842895f0c4 | |||
| 0aebd7b795 | |||
| 706c407351 | |||
| 962592eab4 | |||
| 2e5ec9e78c | |||
| 7d2c96ffad | |||
| db3962cd3c | |||
| 3bb383147d | |||
| 062feb8453 | |||
| 9dd1022ba2 | |||
| a7ee170653 | |||
| ed3eddd357 | |||
| d0876fbd10 | |||
| 5cdd8fea69 | |||
| d75b7a7c15 | |||
| 31bba74b34 | |||
| f0e0335ca4 | |||
| b61c0dc332 | |||
| 7bb0187ece | |||
| c457d9a541 | |||
| 568692eebe | |||
| 3a05fc63a7 | |||
| cc0b16bc9d | |||
| 363db3cf73 | |||
| 74d9b82a45 | |||
| 72a90e75c1 | |||
| 25b2997a51 | |||
| a3f546bc1a | |||
| 85a93e63b6 | |||
| 56ef6c6dca | |||
| dbf0067cf6 | |||
| 77d0cdfe85 | |||
| 002c8e676e | |||
| bcf2062b2b | |||
| 878fc85f73 | |||
| 5035f3c453 | |||
| a35de1381a | |||
| 2ace11eabc | |||
| e25b6b5989 | |||
| e1de481ccd | |||
| 656363067d | |||
| 4c12440dc6 | |||
| b95f7abba2 | |||
| 856d634d5b | |||
| f0c7ad1fb3 | |||
| ec1a4e7ccd | |||
| 8b7f5cae86 | |||
| 36962bcff1 | |||
| 4210396bd0 | |||
| b8fbbb9f60 | |||
| 5cc9968a58 | |||
| f616533466 | |||
| 1deb4b55d4 | |||
| 624738f9ed | |||
| 5c97efddcf | |||
| e9d0512b9f | |||
| 0acaf239a6 | |||
| 3c04718814 | |||
| 213f020a4f | |||
| 9c75630305 | |||
| c32a34e0e5 | |||
| 60a3a99acf | |||
| 4da6cedea6 | |||
| b513c28bf9 | |||
| da0338ad19 | |||
| fe1ae0c102 | |||
| d8648583f8 | |||
| c85a5ce188 |
@@ -0,0 +1,21 @@
|
||||
name: Build Android
|
||||
|
||||
on: [pull_request]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-java@v2
|
||||
with:
|
||||
distribution: 'temurin'
|
||||
java-version: '11'
|
||||
- name: Build and Lint with Gradle
|
||||
run: ./gradlew build
|
||||
- name: Archive lint results
|
||||
uses: actions/upload-artifact@v2
|
||||
with:
|
||||
name: lint-results
|
||||
path: "**/build/reports/lint-results*"
|
||||
+4
-1
@@ -7,4 +7,7 @@ maven-repository
|
||||
*.iml
|
||||
local.properties
|
||||
mvn-clone
|
||||
|
||||
*.keystore
|
||||
.project
|
||||
.settings
|
||||
.classpath
|
||||
|
||||
-13
@@ -1,13 +0,0 @@
|
||||
language: android
|
||||
jdk: oraclejdk7
|
||||
android:
|
||||
components:
|
||||
- extra-android-support
|
||||
- extra-android-m2repository
|
||||
- extra-google-m2repository
|
||||
licenses:
|
||||
- 'android-sdk-license-.+'
|
||||
- 'google-gdk-license-.+'
|
||||
script:
|
||||
- TERM=dumb ./gradlew build
|
||||
|
||||
+107
@@ -1,3 +1,110 @@
|
||||
### 4.3.0 (2021-10-25)
|
||||
|
||||
* Minimum SDK version 19, but requires additional config (see readme) for < 24 compatibility.
|
||||
* Add ScanOptions and ScanContract for use with `registerForActivityResult()`.
|
||||
* Deprecates IntentIntegrator.
|
||||
* Use minimal AndroidX libraries.
|
||||
|
||||
### 4.2.0 (2021-03-15)
|
||||
|
||||
* Fix MediaPlayer warnings (#587).
|
||||
* Prevent CameraConfigurationUtils clash (#609).
|
||||
* Add licenses to POM (#556).
|
||||
* Bug: Crashes on SDK versions older than 21 (#645).
|
||||
|
||||
### 4.1.0 (2020-01-07)
|
||||
|
||||
* Ability to hide the laser in ViewfinderView (#503).
|
||||
* Make possibleResultPoints method in BarcodeCallback optional (#504).
|
||||
* Ability to customize or disable the permission error dialog (#505).
|
||||
|
||||
### 4.0.2 (2019-09-07)
|
||||
|
||||
* Use androidx.
|
||||
* Use zxing:core 3.4.0 by default.
|
||||
* Minimum SDK version 24.
|
||||
* Fix ArithmeticException.
|
||||
* Fix ResultPoint locations when camera is mirrored.
|
||||
|
||||
### 4.0.0 / 4.0.1 (2019-09-07)
|
||||
|
||||
* Broken release - use 4.0.2.
|
||||
|
||||
### 3.6.0 (2018-03-04)
|
||||
|
||||
* Use zxing:core 3.3.2 by default (#360).
|
||||
* Minimum SDK version 19, or 14 by using zxing:core 3.3.0.
|
||||
* Fix preview race condition (#324).
|
||||
* Request code can now specified per Intent, instead of globally (#287).
|
||||
* More helpers to specify barcode formats.
|
||||
* Allow scanning both inverted and non-inverted barcodes at the same time (alternating) (#326).
|
||||
* More examples.
|
||||
|
||||
### 3.5.0 (2017-03-20)
|
||||
|
||||
* Allow changing the REQUEST_CODE value (#234).
|
||||
* Add support for inverted scans (#235).
|
||||
* Use zxing:core 3.3.0 by default (#265).
|
||||
|
||||
Fixes:
|
||||
|
||||
* Fix memory leak when using scan timeout (#283).
|
||||
* Better handling of various camera errors (#241, #268, #270)
|
||||
|
||||
### 3.4.0 (2016-10-16)
|
||||
|
||||
Changes:
|
||||
|
||||
* Beep on scan is now controlled only by the media volume, and still plays
|
||||
even if the device is in "silent mode", as long as the media volume is not muted.
|
||||
* The 150ms delay after scanning is removed.
|
||||
|
||||
Fixes:
|
||||
|
||||
* An issue where the beep sometimes played twice is fixed (#221).
|
||||
* Fix rare crash (#209)
|
||||
* Fix orientation lock issue (#181)
|
||||
* Fix race condition with TextureView (#204)
|
||||
|
||||
|
||||
### 3.3.0 (2016-06-05)
|
||||
|
||||
* Add an optional timeout to cancel scanning. (#161)
|
||||
* Rename CompoundBarcodeView to DecoratedBarcodeView.
|
||||
* Add more internal documentation (comments).
|
||||
|
||||
### 3.2.0 (2016-02-06)
|
||||
|
||||
* Improved preview scaling strategies, configurable between centerCrop, fitCenter, fitXY (#135)
|
||||
* Fix issues with Android 6 permission support (#123)
|
||||
* Fix camera initialization issues, specifically related to orientation changes (#133)
|
||||
* More control over focus mode (#112)
|
||||
* Keep drawing viewfinder frame after scanning / pausing (#134)
|
||||
* More control over torch state, and save the state on orientation change (#136)
|
||||
|
||||
### 3.1.0 (2015-12-29)
|
||||
|
||||
* Add support for Android 6 runtime permissions (Camera only).
|
||||
* Experimental support for using TextureView instead of SurfaceView.
|
||||
* Fix build issues with custom attributes.
|
||||
* Support library version 23+ is now a requirement.
|
||||
|
||||
|
||||
### 3.1.0 (2015-12-29)
|
||||
|
||||
* Initial Android 6 permission supoprt
|
||||
|
||||
### 3.0.3 (2015-08-16)
|
||||
|
||||
* Fix for preview on Google Glass.
|
||||
* Make ViewfinderView extensible. (#75)
|
||||
* Add option to return image of barcode via Intents. (#72)
|
||||
|
||||
### 3.0.2 (2015-07-21)
|
||||
|
||||
* Add helper class for encoding barcodes.
|
||||
* Support custom layouts for CompoundBarcodeView.
|
||||
|
||||
### 3.0.1 (2015-06-10)
|
||||
|
||||
* Fix auto-focus stopping when enabling the torch.
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
## Reporting an issue
|
||||
|
||||
Please include the following details when reporting a bug:
|
||||
|
||||
* Which version of the library are you using? (e.g. 3.0.0)
|
||||
* Which phone/tablet are you using, and which Android version does it run? (e.g. Samsung Galaxy S5,
|
||||
Android 5.0)
|
||||
* Does the same happen on other devices or an emulator?
|
||||
* Can you reproduce the error in the sample project included with the library? If not, can you
|
||||
provide your own sample project or sample code that produces this error?
|
||||
|
||||
If the app crashes, please include a stacktrace. Android Studio has a log window for this,
|
||||
otherwise you can use the `adb logcat` command-line tool.
|
||||
|
||||
|
||||
Please note that any issues with the actual barcode scanning process (e.g. a specific barcode
|
||||
doesn't scan, or a format is not supported), should be reported at
|
||||
https://github.com/zxing/zxing/issues.
|
||||
@@ -0,0 +1,84 @@
|
||||
## Embedding BarcodeView
|
||||
|
||||
For more control over the UI or scanning behaviour, some components may be used directly:
|
||||
|
||||
* BarcodeView: Handles displaying the preview and decoding of the barcodes.
|
||||
* DecoratedBarcodeView: Combines BarcodeView with a viewfinder for feedback, as well as some status /
|
||||
prompt text.
|
||||
* CaptureManager: Manages the InactivityTimer, BeepManager, orientation lock, and returning of the
|
||||
barcode result.
|
||||
|
||||
These components can be used from any Activity or Fragment.
|
||||
|
||||
This is much more low-level than using IntentIntegrator. Your code becomes responsible for:
|
||||
* Setting up the BarcodeView (doesn't have all the helpers from IntentIntegrator)*
|
||||
* Requesting permission to do the Camera.
|
||||
* Making sure only one Camera instance is active at a time.
|
||||
* Handling the scan results.
|
||||
|
||||
Samples:
|
||||
* [ContinuousCaptureActivity][6]: continuously scan and display results (instead of a once-off scan).
|
||||
* [ToolbarCaptureActivity][8]: Same as the normal CaptureActivity, but with a Lollipop Toolbar.
|
||||
|
||||
|
||||
## Notes on threading
|
||||
|
||||
For a responsive user interface, all camera operations happen on a dedicated background thread.
|
||||
In most cases this doesn't matter, but it does mean that the camera is not released immediately
|
||||
when the BarcodeView is paused. If you want to start using the camera for something else
|
||||
immediately after scanning, use `BarcodeView#pauseAndWait()` instead of `BarcodeView#pause()`.
|
||||
This will block the main thread until the camera is released.
|
||||
|
||||
|
||||
## Notes on scaling
|
||||
|
||||
On each Android device, the camera has a set list of available preview sizes. When embedding the
|
||||
barcode scanning along with other components on an Activity, there will almost never be a preview
|
||||
size that matches up exactly, so we have to pick one and scale and/or crop it.
|
||||
|
||||
Also affecting this is that either SurfaceView or TextureView can be used to display the preview.
|
||||
SurfaceView has better performance, but does not support cropping. TextureView is more powerful,
|
||||
but has some performance overhead, and is only supported on Android API 14+. We use SurfaceView by
|
||||
default.
|
||||
|
||||
To avoid aspect ratio distortion, we can crop the preview. However, in some combinations of
|
||||
SurfaceView and other components, the camera preview may end up displaying outside the SurfaceView,
|
||||
and over other components. This happens especially when:
|
||||
|
||||
1. Placing the scanner inside a dialog, or:
|
||||
2. Other components are placed before the (Decorated)BarcodeView, resulting in a lower z-order.
|
||||
|
||||
For these cases we have two solutions:
|
||||
|
||||
1. Use TextureView instead of SurfaceView. This may have a performance impact, but solves the above
|
||||
issues. Note that this is only available with Android API 14+.
|
||||
|
||||
2. Use either `fitCenter` or `fitXY` for scaling, instead of `centerCrop`. Note that `fitCenter` may
|
||||
result in black bars next to the preview, and `fitXY` may distort the aspect ratio.
|
||||
|
||||
The default is to:
|
||||
|
||||
1. Use TextureView on Android API 14+, SurfaceView on lower versions.
|
||||
2. Use `centerCrop` scaling when TextureView is used.
|
||||
3. Use `fitCenter` if SurfaceView is used.
|
||||
|
||||
You can override these options:
|
||||
|
||||
```xml
|
||||
<com.journeyapps.barcodescanner.DecoratedBarcodeView
|
||||
android:layout_width="..."
|
||||
android:layout_height="..."
|
||||
app:zxing_use_texture_view="false" (defaults to true, only has an effect on Android API 14+)
|
||||
app:zxing_preview_scaling_strategy="centerCrop"/> (or fitCenter / fitXY)
|
||||
```
|
||||
|
||||
For a full-screen barcode scanner with no Toolbar, the recommended options are:
|
||||
|
||||
```
|
||||
app:zxing_use_texture_view="false"
|
||||
app:zxing_preview_scaling_strategy="centerCrop"
|
||||
```
|
||||
|
||||
|
||||
[8]: sample/src/main/java/example/zxing/ToolbarCaptureActivity.java
|
||||
[6]: sample/src/main/java/example/zxing/ContinuousCaptureActivity.java
|
||||
@@ -0,0 +1,33 @@
|
||||
#### Description of the problem:
|
||||
|
||||
|
||||
|
||||
**Which library version are you using?** E.g. 3.2.0.
|
||||
|
||||
**Which phone/tablet are you using, and which Android version does it run? (e.g. Samsung Galaxy S5,
|
||||
Android 5.0)**
|
||||
|
||||
**Does the same happen on other devices or an emulator?**
|
||||
|
||||
**Can you reproduce the issue in the sample project included with the library? If not, can you
|
||||
provide your own sample project or sample code that produces this error?**
|
||||
|
||||
**In the case of an error do you have a stack trace or adb logs?**
|
||||
|
||||
<!--
|
||||
If you have an issue scanning specific barcodes, please test with the Barcode Scanner application,
|
||||
available on the Play Store: https://play.google.com/store/apps/details?id=com.google.zxing.client.android&hl=en
|
||||
|
||||
If you are also not able to scan the barcode there, the issue may be with the underlying zxing library,
|
||||
in which case you should report the issue here: https://github.com/zxing/zxing/issues
|
||||
-->
|
||||
|
||||
<!--
|
||||
If you have a question on usage of the library, please check the documentation and sample
|
||||
application first:
|
||||
|
||||
https://github.com/journeyapps/zxing-android-embedded/blob/master/README.md
|
||||
https://github.com/journeyapps/zxing-android-embedded/blob/master/EMBEDDING.md
|
||||
https://github.com/journeyapps/zxing-android-embedded/tree/master/sample/src/main/java/example/zxing
|
||||
-->
|
||||
|
||||
@@ -1,128 +1,221 @@
|
||||
# ZXing Android Embedded
|
||||
|
||||
This is an Android library loosely based on the [ZXing Android Barcode Scanner application][2]
|
||||
for embedding in Android applications. This is not affiliated with the official ZXing project.
|
||||
Barcode scanning library for Android, using [ZXing][2] for decoding.
|
||||
|
||||
Generally it is recommended to scan a barcode [via intents][3].
|
||||
There are however some cases in which it is not feasible:
|
||||
The project is loosely based on the [ZXing Android Barcode Scanner application][2], but is not affiliated with the official ZXing project.
|
||||
|
||||
* You need to customise the barcode scanning logic.
|
||||
* You need to customise the UI.
|
||||
* The users cannot install the Barcode Scanner application.
|
||||
Features:
|
||||
|
||||
In these cases, this library may be more suitable.
|
||||
1. Can be used via Intents (little code required).
|
||||
2. Can be embedded in an Activity, for advanced customization of UI and logic.
|
||||
3. Scanning can be performed in landscape or portrait mode.
|
||||
4. Camera is managed in a background thread, for fast startup time.
|
||||
|
||||
## Version 3
|
||||
A sample application is available in [Releases](https://github.com/journeyapps/zxing-android-embedded/releases).
|
||||
|
||||
Where [version 2][4] was essentially just a stripped-down version of the [Barcode Scanner application][2],
|
||||
version 3 is a rewrite of a large part of the codebase, making it more versatile and customizable.
|
||||
|
||||
With the rewrite, many APIs for UI customization were removed. Instead, it is now recommended
|
||||
to create a custom Activity using the lower-level components directly
|
||||
(see [Customization](#customization) for details).
|
||||
|
||||
Other notable changes:
|
||||
* The camera is now loaded in a background thread, making the activity start faster.
|
||||
* The camera preview and decoding now function correctly in any orientation.
|
||||
By default, Android SDK 24+ is required because of `zxing:core` 3.4.x.
|
||||
SDK 19+ is supported with additional configuration, see [Older SDK versions](#older-sdk-versions).
|
||||
|
||||
## Adding aar dependency with Gradle
|
||||
|
||||
From version 3 this is a single library, supporting Gingerbread and later versions of Android
|
||||
(API level 9+). If you need support for earlier Android versions, use [version 2][4].
|
||||
|
||||
Add the following to your build.gradle file:
|
||||
Add the following to your `build.gradle` file:
|
||||
|
||||
```groovy
|
||||
// Config for SDK 24+
|
||||
|
||||
repositories {
|
||||
jcenter()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile 'com.journeyapps:zxing-android-embedded:3.0.1@aar'
|
||||
compile 'com.google.zxing:core:3.2.0'
|
||||
implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
|
||||
}
|
||||
```
|
||||
|
||||
## Usage with Maven
|
||||
## Older SDK versions
|
||||
|
||||
Maven is not supported currently, but it is possible that the aar can be used. Pull requests are
|
||||
welcome.
|
||||
By default, only SDK 24+ will work, even though the library specifies 19 as the minimum version.
|
||||
|
||||
## Usage with IntentIntegrator
|
||||
For SDK versions 19+, one of the changes below are required.
|
||||
Some older SDK versions below 19 may work, but this is not tested or supported.
|
||||
|
||||
Launch the intent with the default options:
|
||||
```java
|
||||
new IntentIntegrator(this).initiateScan(); // `this` is the current Activity
|
||||
### Option 1. Downgrade zxing:core to 3.3.0
|
||||
|
||||
```groovy
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation('com.journeyapps:zxing-android-embedded:4.3.0') { transitive = false }
|
||||
implementation 'com.google.zxing:core:3.3.0'
|
||||
}
|
||||
```
|
||||
|
||||
Use from a Fragment:
|
||||
```java
|
||||
IntentIntegrator.forFragment(this).initiateScan(); // `this` is the current Fragment
|
||||
### Option 2: Desugaring (Advanced)
|
||||
|
||||
// If you're using the support library, use IntentIntegrator.forSupportFragment(this) instead.
|
||||
This option does not require changing library versions, but may complicate the build process.
|
||||
|
||||
This requires Android Gradle Plugin version 4.0.0 or later.
|
||||
|
||||
See [Java 8+ API desugaring support](https://developer.android.com/studio/write/java8-support#library-desugaring).
|
||||
|
||||
Example for SDK 21+:
|
||||
|
||||
```groovy
|
||||
android {
|
||||
defaultConfig {
|
||||
minSdkVersion 21
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
// Flag to enable support for the new language APIs
|
||||
coreLibraryDesugaringEnabled true
|
||||
// Sets Java compatibility to Java 8
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
|
||||
|
||||
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
|
||||
}
|
||||
```
|
||||
|
||||
SDK 19+ additionally requires multiDex. In addition to these gradle config changes, the Application
|
||||
class must also be changed. See for details: [Configure your app for multidex](https://developer.android.com/studio/build/multidex#mdex-gradle).
|
||||
|
||||
```groovy
|
||||
android {
|
||||
defaultConfig {
|
||||
multiDexEnabled true
|
||||
minSdkVersion 19
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
// Flag to enable support for the new language APIs
|
||||
coreLibraryDesugaringEnabled true
|
||||
// Sets Java compatibility to Java 8
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
|
||||
|
||||
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
|
||||
implementation "androidx.multidex:multidex:2.0.1"
|
||||
}
|
||||
```
|
||||
|
||||
## Hardware Acceleration
|
||||
|
||||
Hardware acceleration is required since TextureView is used.
|
||||
|
||||
Make sure it is enabled in your manifest file:
|
||||
|
||||
```xml
|
||||
<application android:hardwareAccelerated="true" ... >
|
||||
```
|
||||
|
||||
## Usage with ScanContract
|
||||
|
||||
Note: `startActivityForResult` is deprecated, so this example uses `registerForActivityResult` instead.
|
||||
See for details: https://developer.android.com/training/basics/intents/result
|
||||
|
||||
`startActivityForResult` can still be used via `IntentIntegrator`, but that is not recommended anymore.
|
||||
|
||||
```java
|
||||
// Register the launcher and result handler
|
||||
private final ActivityResultLauncher<ScanOptions> barcodeLauncher = registerForActivityResult(new ScanContract(),
|
||||
result -> {
|
||||
if(result.getContents() == null) {
|
||||
Toast.makeText(MyActivity.this, "Cancelled", Toast.LENGTH_LONG).show();
|
||||
} else {
|
||||
Toast.makeText(MyActivity.this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
|
||||
}
|
||||
});
|
||||
|
||||
// Launch
|
||||
public void onButtonClick(View view) {
|
||||
barcodeLauncher.launch(new ScanOptions());
|
||||
}
|
||||
```
|
||||
|
||||
Customize options:
|
||||
```java
|
||||
IntentIntegrator integrator = new IntentIntegrator(this);
|
||||
integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
|
||||
integrator.setPrompt("Scan a barcode");
|
||||
integrator.setCameraId(0); // Use a specific camera of the device
|
||||
integrator.setBeepEnabled(false);
|
||||
integrator.initiateScan();
|
||||
ScanOptions options = new ScanOptions();
|
||||
options.setDesiredBarcodeFormats(ScanOptions.ONE_D_CODE_TYPES);
|
||||
options.setPrompt("Scan a barcode");
|
||||
options.setCameraId(0); // Use a specific camera of the device
|
||||
options.setBeepEnabled(false);
|
||||
options.setBarcodeImageEnabled(true);
|
||||
barcodeLauncher.launch(options);
|
||||
```
|
||||
|
||||
See [IntentIntegrator][5] for more options.
|
||||
See [BarcodeOptions][5] for more options.
|
||||
|
||||
### Changing the orientation
|
||||
### Generate Barcode example
|
||||
|
||||
To change the orientation, create a new Activity extending CaptureActivity, and specify the
|
||||
orientation in your `AndroidManifest.xml`.
|
||||
|
||||
Sample:
|
||||
While this is not the primary purpose of this library, it does include basic support for
|
||||
generating some barcode types:
|
||||
|
||||
```java
|
||||
public class CaptureActivityAnyOrientation extends CaptureActivity {
|
||||
try {
|
||||
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
|
||||
Bitmap bitmap = barcodeEncoder.encodeBitmap("content", BarcodeFormat.QR_CODE, 400, 400);
|
||||
ImageView imageViewQrCode = (ImageView) findViewById(R.id.qrCode);
|
||||
imageViewQrCode.setImageBitmap(bitmap);
|
||||
} catch(Exception e) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
```xml
|
||||
<activity android:name=".CaptureActivityAnyOrientation"
|
||||
android:screenOrientation="fullSensor"
|
||||
android:stateNotNeeded="true"
|
||||
android:theme="@style/zxing_CaptureTheme"
|
||||
android:windowSoftInputMode="stateAlwaysHidden">
|
||||
To customize the generated barcode image, use the `setBackgroundColor` and `setForegroundColor` functions of the
|
||||
`BarcodeEncoder` class with a [`@ColorInt`](https://developer.android.com/reference/androidx/annotation/ColorInt)
|
||||
value to update the background and foreground colors of the barcode respectively. By default, the barcode has a
|
||||
white background and black foreground.
|
||||
|
||||
</activity>
|
||||
|
||||
### Changing the orientation
|
||||
|
||||
To change the orientation, specify the orientation in your `AndroidManifest.xml` and let the `ManifestMerger` to update the Activity's definition.
|
||||
|
||||
Sample:
|
||||
|
||||
```xml
|
||||
<activity
|
||||
android:name="com.journeyapps.barcodescanner.CaptureActivity"
|
||||
android:screenOrientation="fullSensor"
|
||||
tools:replace="screenOrientation" />
|
||||
```
|
||||
|
||||
```java
|
||||
IntentIntegrator integrator = new IntentIntegrator(this);
|
||||
integrator.setCaptureActivity(CaptureActivityAnyOrientation.class);
|
||||
integrator.setOrientationLocked(false);
|
||||
integrator.initiateScan();
|
||||
ScanOptions options = new ScanOptions();
|
||||
options.setOrientationLocked(false);
|
||||
barcodeLauncher.launch(options);
|
||||
```
|
||||
|
||||
The previous API for `integrator.setOrientation()` was removed. It caused the Activity to be created
|
||||
in landscape orientation, then destroyed and re-created in the requested orientation, which creates
|
||||
a bad user experience. The only way around this is to specify the orientation in the manifest.
|
||||
### Customization and advanced options
|
||||
|
||||
### Customization
|
||||
See [EMBEDDING](EMBEDDING.md).
|
||||
|
||||
For more control over the UI or scanning behaviour, some components may be used directly:
|
||||
For more advanced options, look at the [Sample Application](https://github.com/journeyapps/zxing-android-embedded/blob/master/sample/src/main/java/example/zxing/MainActivity.java),
|
||||
and browse the source code of the library.
|
||||
|
||||
* BarcodeView: Handles displaying the preview and decoding of the barcodes.
|
||||
* CompoundBarcodeView: Combines BarcodeView with a viewfinder for feedback, as well as some status /
|
||||
prompt text.
|
||||
* CaptureManager: Manages the InactivityTimer, BeepManager, orientation lock, and returning of the
|
||||
barcode result.
|
||||
This is considered advanced usage, and is not well-documented or supported.
|
||||
|
||||
These components can be used from any Activity.
|
||||
## Android Permissions
|
||||
|
||||
Samples:
|
||||
* [ContinuousCaptureActivity][6]: continuously scan and display results (instead of a once-off scan).
|
||||
* [ToolbarCaptureActivity][8]: Same as the normal CaptureActivity, but with a Lollipop Toolbar.
|
||||
The camera permission is required for barcode scanning to function. It is automatically included as
|
||||
part of the library. On Android 6 it is requested at runtime when the barcode scanner is first opened.
|
||||
|
||||
When using BarcodeView directly (instead of via IntentIntegrator / CaptureActivity), you have to
|
||||
request the permission manually before calling `BarcodeView#resume()`, otherwise the camera will
|
||||
fail to open.
|
||||
|
||||
## Building locally
|
||||
|
||||
@@ -140,19 +233,30 @@ You can then use your local version by specifying in your `build.gradle` file:
|
||||
|
||||
## Sponsored by
|
||||
|
||||
[Journey][1] - Build enterprise mobile apps for iOS and Android. Work in the cloud, code in JavaScript and forget about back-end development.
|
||||
[JourneyApps][1]
|
||||
|
||||
|
||||
## License
|
||||
|
||||
[Apache License 2.0][7]
|
||||
Licensed under the [Apache License 2.0][7]
|
||||
|
||||
Copyright (C) 2012-2022 ZXing authors, Journey Mobile
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
|
||||
|
||||
[1]: http://journeyapps.com
|
||||
[2]: https://github.com/zxing/zxing/
|
||||
[3]: https://github.com/zxing/zxing/wiki/Scanning-Via-Intent
|
||||
[4]: https://github.com/journeyapps/zxing-android-embedded/blob/2.x/README.md
|
||||
[5]: zxing-android-embedded/src/com/google/zxing/integration/android/IntentIntegrator.java
|
||||
[6]: sample/src/main/java/example/zxing/ContinuousCaptureActivity.java
|
||||
[5]: zxing-android-embedded/src/com/journeyapps/barcodescanner/ScanOptions.java
|
||||
[7]: http://www.apache.org/licenses/LICENSE-2.0
|
||||
[8]: sample/src/main/java/example/zxing/ToolbarCaptureActivity.java
|
||||
|
||||
+14
-81
@@ -1,91 +1,24 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
jcenter()
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:1.0.1'
|
||||
classpath 'com.jakewharton.sdkmanager:gradle-plugin:0.12.0'
|
||||
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
|
||||
}
|
||||
}
|
||||
repositories {
|
||||
google()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
// Projects to be published to bintray
|
||||
def PUBLISH_PROJECTS = ['zxing-android-embedded']
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:7.0.3'
|
||||
}
|
||||
}
|
||||
|
||||
subprojects {
|
||||
repositories {
|
||||
jcenter()
|
||||
google()
|
||||
mavenLocal()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
version = '3.0.1'
|
||||
version = '4.3.0'
|
||||
group = 'com.journeyapps'
|
||||
apply plugin: 'android-sdk-manager'
|
||||
|
||||
ext.androidBuildTools = '21.1.2'
|
||||
ext.androidTargetSdk = 21
|
||||
ext.zxingCore = 'com.google.zxing:core:3.2.0'
|
||||
|
||||
if (PUBLISH_PROJECTS.contains(project.name)) {
|
||||
apply plugin: 'maven-publish'
|
||||
apply plugin: 'com.jfrog.bintray'
|
||||
|
||||
project.ext.artifactId = project.name // Default to subproject name
|
||||
|
||||
|
||||
afterEvaluate {
|
||||
|
||||
task sourceJar(type: Jar) {
|
||||
classifier = 'sources'
|
||||
from android.sourceSets.main.java.srcDirs
|
||||
}
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
// We need this check to cover Android Studio gradle sync
|
||||
if(project.tasks.findByPath('bundleRelease') != null) {
|
||||
maven(MavenPublication) {
|
||||
artifact bundleRelease
|
||||
artifactId project.artifactId
|
||||
|
||||
artifact sourceJar
|
||||
|
||||
pom.withXml {
|
||||
// HACK to add dependencies to POM.
|
||||
// When maven-publish can do this automatically for Android projects,
|
||||
// remove this section.
|
||||
def deps = asNode().appendNode('dependencies')
|
||||
|
||||
project.configurations.compile.allDependencies.each { dep ->
|
||||
def node = deps.appendNode('dependency')
|
||||
node.appendNode('groupId', dep.group)
|
||||
node.appendNode('artifactId', dep.name)
|
||||
node.appendNode('version', dep.version)
|
||||
node.appendNode('scope', 'compile')
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// To release, place bintray_user and bintray_key properties in ~/.gradle/gradle.properties,
|
||||
// and run ./gradlew clean assembleRelease bintrayUpload
|
||||
|
||||
if(project.hasProperty('bintray_user') && project.hasProperty('bintray_key')) {
|
||||
bintray {
|
||||
user = bintray_user
|
||||
key = bintray_key
|
||||
publications = ['maven']
|
||||
publish = true
|
||||
pkg {
|
||||
userOrg = 'journeyapps'
|
||||
repo = 'maven'
|
||||
name = 'zxing-android-embedded'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ext.androidTargetSdk = 30
|
||||
ext.zxingCore = 'com.google.zxing:core:3.4.1'
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
android.enableJetifier=false
|
||||
android.useAndroidX=true
|
||||
org.gradle.jvmargs=-Xmx1536M
|
||||
Vendored
BIN
Binary file not shown.
+1
-2
@@ -1,6 +1,5 @@
|
||||
#Sat Dec 20 18:56:59 SAST 2014
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
|
||||
|
||||
@@ -1,4 +1,20 @@
|
||||
#!/usr/bin/env bash
|
||||
#!/usr/bin/env sh
|
||||
|
||||
#
|
||||
# Copyright 2015 the original author or authors.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# https://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
##############################################################################
|
||||
##
|
||||
@@ -6,47 +22,6 @@
|
||||
##
|
||||
##############################################################################
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS=""
|
||||
|
||||
APP_NAME="Gradle"
|
||||
APP_BASE_NAME=`basename "$0"`
|
||||
|
||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||
MAX_FD="maximum"
|
||||
|
||||
warn ( ) {
|
||||
echo "$*"
|
||||
}
|
||||
|
||||
die ( ) {
|
||||
echo
|
||||
echo "$*"
|
||||
echo
|
||||
exit 1
|
||||
}
|
||||
|
||||
# OS specific support (must be 'true' or 'false').
|
||||
cygwin=false
|
||||
msys=false
|
||||
darwin=false
|
||||
case "`uname`" in
|
||||
CYGWIN* )
|
||||
cygwin=true
|
||||
;;
|
||||
Darwin* )
|
||||
darwin=true
|
||||
;;
|
||||
MINGW* )
|
||||
msys=true
|
||||
;;
|
||||
esac
|
||||
|
||||
# For Cygwin, ensure paths are in UNIX format before anything is touched.
|
||||
if $cygwin ; then
|
||||
[ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
|
||||
fi
|
||||
|
||||
# Attempt to set APP_HOME
|
||||
# Resolve links: $0 may be a link
|
||||
PRG="$0"
|
||||
@@ -61,9 +36,49 @@ while [ -h "$PRG" ] ; do
|
||||
fi
|
||||
done
|
||||
SAVED="`pwd`"
|
||||
cd "`dirname \"$PRG\"`/" >&-
|
||||
cd "`dirname \"$PRG\"`/" >/dev/null
|
||||
APP_HOME="`pwd -P`"
|
||||
cd "$SAVED" >&-
|
||||
cd "$SAVED" >/dev/null
|
||||
|
||||
APP_NAME="Gradle"
|
||||
APP_BASE_NAME=`basename "$0"`
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||
|
||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||
MAX_FD="maximum"
|
||||
|
||||
warn () {
|
||||
echo "$*"
|
||||
}
|
||||
|
||||
die () {
|
||||
echo
|
||||
echo "$*"
|
||||
echo
|
||||
exit 1
|
||||
}
|
||||
|
||||
# OS specific support (must be 'true' or 'false').
|
||||
cygwin=false
|
||||
msys=false
|
||||
darwin=false
|
||||
nonstop=false
|
||||
case "`uname`" in
|
||||
CYGWIN* )
|
||||
cygwin=true
|
||||
;;
|
||||
Darwin* )
|
||||
darwin=true
|
||||
;;
|
||||
MINGW* )
|
||||
msys=true
|
||||
;;
|
||||
NONSTOP* )
|
||||
nonstop=true
|
||||
;;
|
||||
esac
|
||||
|
||||
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
||||
|
||||
@@ -90,7 +105,7 @@ location of your Java installation."
|
||||
fi
|
||||
|
||||
# Increase the maximum file descriptors if we can.
|
||||
if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
|
||||
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
|
||||
MAX_FD_LIMIT=`ulimit -H -n`
|
||||
if [ $? -eq 0 ] ; then
|
||||
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
|
||||
@@ -110,10 +125,11 @@ if $darwin; then
|
||||
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
|
||||
fi
|
||||
|
||||
# For Cygwin, switch paths to Windows format before running java
|
||||
if $cygwin ; then
|
||||
# For Cygwin or MSYS, switch paths to Windows format before running java
|
||||
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
|
||||
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
|
||||
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
|
||||
JAVACMD=`cygpath --unix "$JAVACMD"`
|
||||
|
||||
# We build the pattern for arguments to be converted via cygpath
|
||||
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
|
||||
@@ -138,27 +154,30 @@ if $cygwin ; then
|
||||
else
|
||||
eval `echo args$i`="\"$arg\""
|
||||
fi
|
||||
i=$((i+1))
|
||||
i=`expr $i + 1`
|
||||
done
|
||||
case $i in
|
||||
(0) set -- ;;
|
||||
(1) set -- "$args0" ;;
|
||||
(2) set -- "$args0" "$args1" ;;
|
||||
(3) set -- "$args0" "$args1" "$args2" ;;
|
||||
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
|
||||
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
|
||||
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
|
||||
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
|
||||
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
|
||||
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
|
||||
0) set -- ;;
|
||||
1) set -- "$args0" ;;
|
||||
2) set -- "$args0" "$args1" ;;
|
||||
3) set -- "$args0" "$args1" "$args2" ;;
|
||||
4) set -- "$args0" "$args1" "$args2" "$args3" ;;
|
||||
5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
|
||||
6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
|
||||
7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
|
||||
8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
|
||||
9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
|
||||
esac
|
||||
fi
|
||||
|
||||
# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
|
||||
function splitJvmOpts() {
|
||||
JVM_OPTS=("$@")
|
||||
# Escape application args
|
||||
save () {
|
||||
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
|
||||
echo " "
|
||||
}
|
||||
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
|
||||
JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
|
||||
APP_ARGS=`save "$@"`
|
||||
|
||||
exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
|
||||
# Collect all arguments for the java command, following the shell quoting and substitution rules
|
||||
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
|
||||
|
||||
exec "$JAVACMD" "$@"
|
||||
|
||||
Vendored
+100
@@ -0,0 +1,100 @@
|
||||
@rem
|
||||
@rem Copyright 2015 the original author or authors.
|
||||
@rem
|
||||
@rem Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@rem you may not use this file except in compliance with the License.
|
||||
@rem You may obtain a copy of the License at
|
||||
@rem
|
||||
@rem https://www.apache.org/licenses/LICENSE-2.0
|
||||
@rem
|
||||
@rem Unless required by applicable law or agreed to in writing, software
|
||||
@rem distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
@rem See the License for the specific language governing permissions and
|
||||
@rem limitations under the License.
|
||||
@rem
|
||||
|
||||
@if "%DEBUG%" == "" @echo off
|
||||
@rem ##########################################################################
|
||||
@rem
|
||||
@rem Gradle startup script for Windows
|
||||
@rem
|
||||
@rem ##########################################################################
|
||||
|
||||
@rem Set local scope for the variables with windows NT shell
|
||||
if "%OS%"=="Windows_NT" setlocal
|
||||
|
||||
set DIRNAME=%~dp0
|
||||
if "%DIRNAME%" == "" set DIRNAME=.
|
||||
set APP_BASE_NAME=%~n0
|
||||
set APP_HOME=%DIRNAME%
|
||||
|
||||
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
|
||||
|
||||
@rem Find java.exe
|
||||
if defined JAVA_HOME goto findJavaFromJavaHome
|
||||
|
||||
set JAVA_EXE=java.exe
|
||||
%JAVA_EXE% -version >NUL 2>&1
|
||||
if "%ERRORLEVEL%" == "0" goto init
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:findJavaFromJavaHome
|
||||
set JAVA_HOME=%JAVA_HOME:"=%
|
||||
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||
|
||||
if exist "%JAVA_EXE%" goto init
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:init
|
||||
@rem Get command-line arguments, handling Windows variants
|
||||
|
||||
if not "%OS%" == "Windows_NT" goto win9xME_args
|
||||
|
||||
:win9xME_args
|
||||
@rem Slurp the command line arguments.
|
||||
set CMD_LINE_ARGS=
|
||||
set _SKIP=2
|
||||
|
||||
:win9xME_args_slurp
|
||||
if "x%~1" == "x" goto execute
|
||||
|
||||
set CMD_LINE_ARGS=%*
|
||||
|
||||
:execute
|
||||
@rem Setup the command line
|
||||
|
||||
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
||||
|
||||
@rem Execute Gradle
|
||||
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
|
||||
|
||||
:end
|
||||
@rem End local scope for the variables with windows NT shell
|
||||
if "%ERRORLEVEL%"=="0" goto mainEnd
|
||||
|
||||
:fail
|
||||
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
||||
rem the _cmd.exe /c_ return code!
|
||||
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
|
||||
exit /b 1
|
||||
|
||||
:mainEnd
|
||||
if "%OS%"=="Windows_NT" endlocal
|
||||
|
||||
:omega
|
||||
@@ -1 +0,0 @@
|
||||
/build
|
||||
@@ -1,24 +0,0 @@
|
||||
apply plugin: 'com.android.application'
|
||||
|
||||
|
||||
android {
|
||||
compileSdkVersion project.androidTargetSdk
|
||||
buildToolsVersion project.androidBuildTools
|
||||
|
||||
defaultConfig {
|
||||
minSdkVersion 11
|
||||
targetSdkVersion project.androidTargetSdk
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
}
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile(project(':zxing-android-embedded')) { transitive = true }
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
# Add project specific ProGuard rules here.
|
||||
# By default, the flags in this file are appended to flags specified
|
||||
# in /home/ralf/apps/android-studio/sdk/tools/proguard/proguard-android.txt
|
||||
# You can edit the include path and order by changing the ProGuard
|
||||
# include property in project.properties.
|
||||
#
|
||||
# For more details, see
|
||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||
|
||||
# Add any project specific keep options here:
|
||||
|
||||
# If your project uses WebView with JS, uncomment the following
|
||||
# and specify the fully qualified class name to the JavaScript interface
|
||||
# class:
|
||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||
# public *;
|
||||
#}
|
||||
@@ -1,21 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="example.zxing" >
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:icon="@drawable/icon"
|
||||
android:label="@string/app_name" >
|
||||
<activity
|
||||
android:name="example.zxing.MainActivity"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@style/AppTheme" >
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
@@ -1,107 +0,0 @@
|
||||
package example.zxing;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Fragment;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.google.zxing.integration.android.IntentIntegrator;
|
||||
import com.google.zxing.integration.android.IntentResult;
|
||||
|
||||
|
||||
public class MainActivity extends Activity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
}
|
||||
|
||||
public void scanBarcode(View view) {
|
||||
new IntentIntegrator((Activity)this).initiateScan();
|
||||
}
|
||||
|
||||
public void scanBarcodeCustomOptions(View view) {
|
||||
IntentIntegrator integrator = new IntentIntegrator(this);
|
||||
integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
|
||||
integrator.initiateScan();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
|
||||
if(result != null) {
|
||||
if(result.getContents() == null) {
|
||||
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
|
||||
} else {
|
||||
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
|
||||
}
|
||||
} else {
|
||||
// This is important, otherwise the result will not be passed to the fragment
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sample of scanning from a Fragment
|
||||
*/
|
||||
public static class ScanFragment extends Fragment {
|
||||
private String toast;
|
||||
|
||||
public ScanFragment() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
|
||||
displayToast();
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.fragment_scan, container, false);
|
||||
Button scan = (Button) view.findViewById(R.id.scan_from_fragment);
|
||||
scan.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
scanFromFragment();
|
||||
}
|
||||
});
|
||||
return view;
|
||||
}
|
||||
|
||||
public void scanFromFragment() {
|
||||
IntentIntegrator.forFragment(this).initiateScan();
|
||||
}
|
||||
|
||||
private void displayToast() {
|
||||
if(getActivity() != null && toast != null) {
|
||||
Toast.makeText(getActivity(), toast, Toast.LENGTH_LONG).show();
|
||||
toast = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
|
||||
if(result != null) {
|
||||
if(result.getContents() == null) {
|
||||
toast = "Cancelled from fragment";
|
||||
} else {
|
||||
toast = "Scanned from fragment: " + result.getContents();
|
||||
}
|
||||
|
||||
// At this point we may or may not have a reference to the activity
|
||||
displayToast();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 7.5 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 3.7 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 12 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 24 KiB |
@@ -1,33 +0,0 @@
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:paddingLeft="@dimen/activity_horizontal_margin"
|
||||
android:paddingRight="@dimen/activity_horizontal_margin"
|
||||
android:paddingTop="@dimen/activity_vertical_margin"
|
||||
android:paddingBottom="@dimen/activity_vertical_margin"
|
||||
android:orientation="vertical"
|
||||
tools:context="example.zxing.MainActivity">
|
||||
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/scan_barcode"
|
||||
android:onClick="scanBarcode"/>
|
||||
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/custom_options"
|
||||
android:onClick="scanBarcodeCustomOptions"/>
|
||||
|
||||
|
||||
<fragment
|
||||
android:tag="scan_fragment"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
|
||||
android:name="example.zxing.MainActivity$ScanFragment" tools:layout="@layout/fragment_scan" />
|
||||
|
||||
|
||||
</LinearLayout>
|
||||
@@ -1,16 +0,0 @@
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
tools:context="example.zxing.MainActivity"
|
||||
tools:showIn="@layout/activity_main">
|
||||
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/scan_from_fragment"
|
||||
android:id="@+id/scan_from_fragment"/>
|
||||
|
||||
|
||||
</LinearLayout>
|
||||
@@ -1,7 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<!-- Default screen margins, per the Android Design guidelines. -->
|
||||
<dimen name="activity_horizontal_margin">16dp</dimen>
|
||||
<dimen name="activity_vertical_margin">16dp</dimen>
|
||||
|
||||
</resources>
|
||||
@@ -1,9 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<string name="app_name">ZXing Sample</string>
|
||||
<string name="scan_barcode">Scan Barcode</string>
|
||||
<string name="custom_options">Custom Options</string>
|
||||
<string name="scan_from_fragment">Scan from fragment</string>
|
||||
|
||||
</resources>
|
||||
@@ -1,7 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<!-- Base application theme. -->
|
||||
<style name="AppTheme" parent="android:Theme">
|
||||
|
||||
</style>
|
||||
</resources>
|
||||
@@ -1 +0,0 @@
|
||||
/build
|
||||
+67
-11
@@ -2,32 +2,88 @@ apply plugin: 'com.android.application'
|
||||
|
||||
android {
|
||||
compileSdkVersion project.androidTargetSdk
|
||||
buildToolsVersion project.androidBuildTools
|
||||
|
||||
defaultConfig {
|
||||
minSdkVersion 9
|
||||
multiDexEnabled true
|
||||
minSdkVersion 19
|
||||
targetSdkVersion project.androidTargetSdk
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
versionCode 411
|
||||
versionName "4.1.1"
|
||||
}
|
||||
|
||||
def validConfig
|
||||
def keystoreFile
|
||||
def keystorePassword
|
||||
def keystoreAlias
|
||||
|
||||
try {
|
||||
Properties properties = new Properties()
|
||||
properties.load(project.rootProject.file('local.properties').newDataInputStream())
|
||||
keystoreFile = properties.getProperty('keystore.file')
|
||||
keystorePassword = properties.getProperty('keystore.password')
|
||||
keystoreAlias = properties.getProperty('keystore.alias')
|
||||
validConfig = keystoreFile != null && keystorePassword != null && keystoreAlias != null
|
||||
} catch (error) {
|
||||
validConfig = false
|
||||
}
|
||||
|
||||
if (validConfig) {
|
||||
System.out.println("Release signing configured with " + keystoreFile)
|
||||
signingConfigs {
|
||||
release {
|
||||
storeFile project.rootProject.file(keystoreFile)
|
||||
storePassword keystorePassword
|
||||
keyAlias keystoreAlias
|
||||
keyPassword keystorePassword
|
||||
}
|
||||
}
|
||||
} else {
|
||||
System.out.println("Specify keystore.file, keystore.alias and keystore.password in local.properties to enable release signing.")
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
if (validConfig) {
|
||||
signingConfig signingConfigs.release
|
||||
}
|
||||
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
|
||||
}
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
coreLibraryDesugaringEnabled true
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
|
||||
lintOptions {
|
||||
// Error: The lint detector
|
||||
// androidx.appcompat.view.OnClickXmlDetector
|
||||
// called context.getMainProject() during module analysis.
|
||||
disable 'UsingOnClickInXml'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
dependencies {
|
||||
// If you use this from an external project, use the following instead:
|
||||
// compile 'com.journeyapps:zxing-android-embedded:<version>@aar'
|
||||
// compile 'com.google.zxing:core:3.2.0'
|
||||
compile(project(':zxing-android-embedded')) { transitive = true }
|
||||
compile 'com.android.support:appcompat-v7:22.0.0'
|
||||
// implementation 'com.journeyapps:zxing-android-embedded:<version>'
|
||||
implementation project(':zxing-android-embedded')
|
||||
|
||||
implementation 'androidx.appcompat:appcompat:1.3.1'
|
||||
implementation 'androidx.legacy:legacy-support-v13:1.0.0'
|
||||
implementation "androidx.activity:activity:1.3.1"
|
||||
|
||||
// For development purposes only
|
||||
// Desugaring and multidex is required for API < 21.
|
||||
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
|
||||
implementation 'androidx.multidex:multidex:2.0.1'
|
||||
|
||||
// leakcanary is for development purposes only
|
||||
// https://github.com/square/leakcanary
|
||||
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1'
|
||||
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1'
|
||||
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.7'
|
||||
|
||||
// AboutLibraries
|
||||
implementation 'com.mikepenz:aboutlibraries:6.2.3'
|
||||
}
|
||||
|
||||
@@ -1,43 +1,52 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="example.zxing" >
|
||||
package="example.zxing">
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:name=".SampleApplication"
|
||||
android:allowBackup="false"
|
||||
android:icon="@drawable/icon"
|
||||
android:label="@string/app_name"
|
||||
android:name=".SampleApplication">
|
||||
android:label="@string/app_name">
|
||||
<activity
|
||||
android:name="example.zxing.MainActivity"
|
||||
android:name=".MainActivity"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@style/AppTheme" >
|
||||
android:theme="@style/AppTheme">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<activity android:name=".ContinuousCaptureActivity">
|
||||
|
||||
</activity>
|
||||
|
||||
<activity android:name=".AnyOrientationCaptureActivity"
|
||||
android:screenOrientation="fullSensor"
|
||||
android:stateNotNeeded="true"
|
||||
android:theme="@style/zxing_CaptureTheme"
|
||||
android:windowSoftInputMode="stateAlwaysHidden">
|
||||
|
||||
</activity>
|
||||
|
||||
|
||||
<activity android:name=".ToolbarCaptureActivity"
|
||||
android:clearTaskOnLaunch="true"
|
||||
android:screenOrientation="sensorLandscape"
|
||||
android:stateNotNeeded="true"
|
||||
android:theme="@style/AppCompatCaptureTheme"
|
||||
android:windowSoftInputMode="stateAlwaysHidden">
|
||||
</activity>
|
||||
<activity android:name=".ContinuousCaptureActivity"></activity>
|
||||
<activity
|
||||
android:name=".AnyOrientationCaptureActivity"
|
||||
android:screenOrientation="fullSensor"
|
||||
android:stateNotNeeded="true"
|
||||
android:theme="@style/zxing_CaptureTheme"
|
||||
android:windowSoftInputMode="stateAlwaysHidden"></activity>
|
||||
<activity
|
||||
android:name=".ToolbarCaptureActivity"
|
||||
android:clearTaskOnLaunch="true"
|
||||
android:screenOrientation="portrait"
|
||||
android:stateNotNeeded="true"
|
||||
android:theme="@style/AppCompatCaptureTheme"
|
||||
android:windowSoftInputMode="stateAlwaysHidden"></activity>
|
||||
<activity
|
||||
android:name=".CustomScannerActivity"
|
||||
android:screenOrientation="fullSensor"
|
||||
android:stateNotNeeded="true"
|
||||
android:theme="@style/zxing_CaptureTheme"
|
||||
android:windowSoftInputMode="stateAlwaysHidden"></activity>
|
||||
<activity
|
||||
android:name=".SmallCaptureActivity"
|
||||
android:screenOrientation="fullSensor"
|
||||
android:stateNotNeeded="true"
|
||||
android:theme="@style/zxing_CaptureTheme"
|
||||
android:windowSoftInputMode="stateAlwaysHidden"></activity>
|
||||
<activity
|
||||
android:name=".TabbedScanning"
|
||||
android:theme="@style/Theme.AppCompat"
|
||||
android:label="@string/title_activity_tabbed_scanning"></activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
</manifest>
|
||||
@@ -1,19 +1,22 @@
|
||||
package example.zxing;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.res.Configuration;
|
||||
import android.graphics.Color;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.View;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
import com.google.zxing.ResultPoint;
|
||||
import com.google.zxing.client.android.BeepManager;
|
||||
import com.journeyapps.barcodescanner.BarcodeCallback;
|
||||
import com.journeyapps.barcodescanner.BarcodeResult;
|
||||
import com.journeyapps.barcodescanner.CompoundBarcodeView;
|
||||
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
|
||||
import com.journeyapps.barcodescanner.DefaultDecoderFactory;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -21,17 +24,25 @@ import java.util.List;
|
||||
* a barcode is scanned.
|
||||
*/
|
||||
public class ContinuousCaptureActivity extends Activity {
|
||||
private static final String TAG = ContinuousCaptureActivity.class.getSimpleName();
|
||||
private CompoundBarcodeView barcodeView;
|
||||
private DecoratedBarcodeView barcodeView;
|
||||
private BeepManager beepManager;
|
||||
private String lastText;
|
||||
|
||||
private BarcodeCallback callback = new BarcodeCallback() {
|
||||
@Override
|
||||
public void barcodeResult(BarcodeResult result) {
|
||||
if (result.getText() != null) {
|
||||
barcodeView.setStatusText(result.getText());
|
||||
if(result.getText() == null || result.getText().equals(lastText)) {
|
||||
// Prevent duplicate scans
|
||||
return;
|
||||
}
|
||||
|
||||
lastText = result.getText();
|
||||
barcodeView.setStatusText(result.getText());
|
||||
|
||||
beepManager.playBeepSoundAndVibrate();
|
||||
|
||||
//Added preview of scanned barcode
|
||||
ImageView imageView = (ImageView) findViewById(R.id.barcodePreview);
|
||||
ImageView imageView = findViewById(R.id.barcodePreview);
|
||||
imageView.setImageBitmap(result.getBitmapWithResultPoints(Color.YELLOW));
|
||||
}
|
||||
|
||||
@@ -46,8 +57,13 @@ public class ContinuousCaptureActivity extends Activity {
|
||||
|
||||
setContentView(R.layout.continuous_scan);
|
||||
|
||||
barcodeView = (CompoundBarcodeView) findViewById(R.id.barcode_scanner);
|
||||
barcodeView = findViewById(R.id.barcode_scanner);
|
||||
Collection<BarcodeFormat> formats = Arrays.asList(BarcodeFormat.QR_CODE, BarcodeFormat.CODE_39);
|
||||
barcodeView.getBarcodeView().setDecoderFactory(new DefaultDecoderFactory(formats));
|
||||
barcodeView.initializeFromIntent(getIntent());
|
||||
barcodeView.decodeContinuous(callback);
|
||||
|
||||
beepManager = new BeepManager(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
package example.zxing;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Color;
|
||||
import android.os.Bundle;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.journeyapps.barcodescanner.CaptureManager;
|
||||
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
|
||||
import com.journeyapps.barcodescanner.ViewfinderView;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Custom Scannner Activity extending from Activity to display a custom layout form scanner view.
|
||||
*/
|
||||
public class CustomScannerActivity extends Activity implements
|
||||
DecoratedBarcodeView.TorchListener {
|
||||
|
||||
private CaptureManager capture;
|
||||
private DecoratedBarcodeView barcodeScannerView;
|
||||
private Button switchFlashlightButton;
|
||||
private ViewfinderView viewfinderView;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_custom_scanner);
|
||||
|
||||
barcodeScannerView = findViewById(R.id.zxing_barcode_scanner);
|
||||
barcodeScannerView.setTorchListener(this);
|
||||
|
||||
switchFlashlightButton = findViewById(R.id.switch_flashlight);
|
||||
|
||||
viewfinderView = findViewById(R.id.zxing_viewfinder_view);
|
||||
|
||||
// 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.setShowMissingCameraPermissionDialog(false);
|
||||
capture.decode();
|
||||
|
||||
changeMaskColor(null);
|
||||
changeLaserVisibility(true);
|
||||
}
|
||||
|
||||
@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.setTorchOn();
|
||||
} else {
|
||||
barcodeScannerView.setTorchOff();
|
||||
}
|
||||
}
|
||||
|
||||
public void changeMaskColor(View view) {
|
||||
Random rnd = new Random();
|
||||
int color = Color.argb(100, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
|
||||
viewfinderView.setMaskColor(color);
|
||||
}
|
||||
|
||||
public void changeLaserVisibility(boolean visible) {
|
||||
viewfinderView.setLaserVisibility(visible);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTorchOn() {
|
||||
switchFlashlightButton.setText(R.string.turn_off_flashlight);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTorchOff() {
|
||||
switchFlashlightButton.setText(R.string.turn_on_flashlight);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||
capture.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,6 @@ package example.zxing;
|
||||
import android.content.Intent;
|
||||
import android.hardware.Camera;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v7.app.ActionBarActivity;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
@@ -12,11 +10,34 @@ import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.google.zxing.integration.android.IntentIntegrator;
|
||||
import com.google.zxing.integration.android.IntentResult;
|
||||
import com.google.zxing.client.android.Intents;
|
||||
import com.journeyapps.barcodescanner.ScanContract;
|
||||
import com.journeyapps.barcodescanner.ScanOptions;
|
||||
import com.mikepenz.aboutlibraries.LibsBuilder;
|
||||
|
||||
import androidx.activity.result.ActivityResultLauncher;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
|
||||
public class MainActivity extends ActionBarActivity {
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
private final ActivityResultLauncher<ScanOptions> barcodeLauncher = registerForActivityResult(new ScanContract(),
|
||||
result -> {
|
||||
if(result.getContents() == null) {
|
||||
Intent originalIntent = result.getOriginalIntent();
|
||||
if (originalIntent == null) {
|
||||
Log.d("MainActivity", "Cancelled scan");
|
||||
Toast.makeText(MainActivity.this, "Cancelled", Toast.LENGTH_LONG).show();
|
||||
} else if(originalIntent.hasExtra(Intents.Scan.MISSING_CAMERA_PERMISSION)) {
|
||||
Log.d("MainActivity", "Cancelled scan due to missing camera permission");
|
||||
Toast.makeText(MainActivity.this, "Cancelled due to missing camera permission", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
} else {
|
||||
Log.d("MainActivity", "Scanned");
|
||||
Toast.makeText(MainActivity.this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -25,23 +46,45 @@ public class MainActivity extends ActionBarActivity {
|
||||
}
|
||||
|
||||
public void scanBarcode(View view) {
|
||||
new IntentIntegrator(this).initiateScan();
|
||||
barcodeLauncher.launch(new ScanOptions());
|
||||
}
|
||||
|
||||
public void scanBarcodeInverted(View view){
|
||||
ScanOptions options = new ScanOptions();
|
||||
options.addExtra(Intents.Scan.SCAN_TYPE, Intents.Scan.INVERTED_SCAN);
|
||||
barcodeLauncher.launch(options);
|
||||
}
|
||||
|
||||
public void scanMixedBarcodes(View view){
|
||||
ScanOptions options = new ScanOptions();
|
||||
options.addExtra(Intents.Scan.SCAN_TYPE, Intents.Scan.MIXED_SCAN);
|
||||
barcodeLauncher.launch(options);
|
||||
}
|
||||
|
||||
public void scanBarcodeCustomLayout(View view) {
|
||||
IntentIntegrator integrator = new IntentIntegrator(this);
|
||||
integrator.setCaptureActivity(AnyOrientationCaptureActivity.class);
|
||||
integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
|
||||
integrator.setPrompt("Scan something");
|
||||
integrator.setOrientationLocked(false);
|
||||
integrator.setBeepEnabled(false);
|
||||
integrator.initiateScan();
|
||||
ScanOptions options = new ScanOptions();
|
||||
options.setCaptureActivity(AnyOrientationCaptureActivity.class);
|
||||
options.setDesiredBarcodeFormats(ScanOptions.ONE_D_CODE_TYPES);
|
||||
options.setPrompt("Scan something");
|
||||
options.setOrientationLocked(false);
|
||||
options.setBeepEnabled(false);
|
||||
barcodeLauncher.launch(options);
|
||||
}
|
||||
|
||||
public void scanPDF417(View view) {
|
||||
ScanOptions options = new ScanOptions();
|
||||
options.setDesiredBarcodeFormats(ScanOptions.PDF_417);
|
||||
options.setPrompt("Scan something");
|
||||
options.setOrientationLocked(false);
|
||||
options.setBeepEnabled(false);
|
||||
barcodeLauncher.launch(options);
|
||||
}
|
||||
|
||||
|
||||
public void scanBarcodeFrontCamera(View view) {
|
||||
IntentIntegrator integrator = new IntentIntegrator(this);
|
||||
integrator.setCameraId(Camera.CameraInfo.CAMERA_FACING_FRONT);
|
||||
integrator.initiateScan();
|
||||
ScanOptions options = new ScanOptions();
|
||||
options.setCameraId(Camera.CameraInfo.CAMERA_FACING_FRONT);
|
||||
barcodeLauncher.launch(options);
|
||||
}
|
||||
|
||||
public void scanContinuous(View view) {
|
||||
@@ -50,82 +93,64 @@ public class MainActivity extends ActionBarActivity {
|
||||
}
|
||||
|
||||
public void scanToolbar(View view) {
|
||||
new IntentIntegrator(this).setCaptureActivity(ToolbarCaptureActivity.class).initiateScan();
|
||||
ScanOptions options = new ScanOptions().setCaptureActivity(ToolbarCaptureActivity.class);
|
||||
barcodeLauncher.launch(options);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
|
||||
if(result != null) {
|
||||
if(result.getContents() == null) {
|
||||
Log.d("MainActivity", "Cancelled scan");
|
||||
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
|
||||
} else {
|
||||
Log.d("MainActivity", "Scanned");
|
||||
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
|
||||
}
|
||||
} else {
|
||||
Log.d("MainActivity", "Weird");
|
||||
// This is important, otherwise the result will not be passed to the fragment
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
}
|
||||
public void scanCustomScanner(View view) {
|
||||
ScanOptions options = new ScanOptions().setOrientationLocked(false).setCaptureActivity(CustomScannerActivity.class);
|
||||
barcodeLauncher.launch(options);
|
||||
}
|
||||
|
||||
public void scanMarginScanner(View view) {
|
||||
ScanOptions options = new ScanOptions();
|
||||
options.setOrientationLocked(false);
|
||||
options.setCaptureActivity(SmallCaptureActivity.class);
|
||||
barcodeLauncher.launch(options);
|
||||
}
|
||||
|
||||
public void scanWithTimeout(View view) {
|
||||
ScanOptions options = new ScanOptions();
|
||||
options.setTimeout(8000);
|
||||
barcodeLauncher.launch(options);
|
||||
}
|
||||
|
||||
public void tabs(View view) {
|
||||
Intent intent = new Intent(this, TabbedScanning.class);
|
||||
startActivity(intent);
|
||||
}
|
||||
|
||||
public void about(View view) {
|
||||
new LibsBuilder().start(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sample of scanning from a Fragment
|
||||
*/
|
||||
public static class ScanFragment extends Fragment {
|
||||
private String toast;
|
||||
private final ActivityResultLauncher<ScanOptions> fragmentLauncher = registerForActivityResult(new ScanContract(),
|
||||
result -> {
|
||||
if(result.getContents() == null) {
|
||||
Toast.makeText(getContext(), "Cancelled from fragment", Toast.LENGTH_LONG).show();
|
||||
} else {
|
||||
Toast.makeText(getContext(), "Scanned from fragment: " + result.getContents(), Toast.LENGTH_LONG).show();
|
||||
}
|
||||
});
|
||||
|
||||
public ScanFragment() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
|
||||
displayToast();
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.fragment_scan, container, false);
|
||||
Button scan = (Button) view.findViewById(R.id.scan_from_fragment);
|
||||
scan.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
scanFromFragment();
|
||||
}
|
||||
});
|
||||
Button scan = view.findViewById(R.id.scan_from_fragment);
|
||||
scan.setOnClickListener(v -> scanFromFragment());
|
||||
return view;
|
||||
}
|
||||
|
||||
public void scanFromFragment() {
|
||||
IntentIntegrator.forSupportFragment(this).initiateScan();
|
||||
}
|
||||
|
||||
private void displayToast() {
|
||||
if(getActivity() != null && toast != null) {
|
||||
Toast.makeText(getActivity(), toast, Toast.LENGTH_LONG).show();
|
||||
toast = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
|
||||
if(result != null) {
|
||||
if(result.getContents() == null) {
|
||||
toast = "Cancelled from fragment";
|
||||
} else {
|
||||
toast = "Scanned from fragment: " + result.getContents();
|
||||
}
|
||||
|
||||
// At this point we may or may not have a reference to the activity
|
||||
displayToast();
|
||||
}
|
||||
fragmentLauncher.launch(new ScanOptions());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,13 @@
|
||||
package example.zxing;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
import com.squareup.leakcanary.LeakCanary;
|
||||
import androidx.multidex.MultiDexApplication;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class SampleApplication extends Application {
|
||||
public class SampleApplication extends MultiDexApplication {
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
LeakCanary.install(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package example.zxing;
|
||||
|
||||
import com.journeyapps.barcodescanner.CaptureActivity;
|
||||
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
|
||||
|
||||
/**
|
||||
* This activity has a margin.
|
||||
*/
|
||||
public class SmallCaptureActivity extends CaptureActivity {
|
||||
@Override
|
||||
protected DecoratedBarcodeView initializeContent() {
|
||||
setContentView(R.layout.capture_small);
|
||||
return (DecoratedBarcodeView)findViewById(R.id.zxing_barcode_scanner);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,236 @@
|
||||
package example.zxing;
|
||||
|
||||
|
||||
import android.os.Bundle;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.fragment.app.FragmentPagerAdapter;
|
||||
import androidx.fragment.app.FragmentTransaction;
|
||||
import androidx.legacy.app.FragmentStatePagerAdapter;
|
||||
import androidx.viewpager.widget.PagerAdapter;
|
||||
import androidx.viewpager.widget.ViewPager;
|
||||
import androidx.appcompat.app.ActionBar;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.journeyapps.barcodescanner.CameraPreview;
|
||||
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
|
||||
|
||||
public class TabbedScanning extends AppCompatActivity implements ActionBar.TabListener {
|
||||
|
||||
/**
|
||||
* The {@link PagerAdapter} that will provide
|
||||
* fragments for each of the sections. We use a
|
||||
* {@link FragmentPagerAdapter} derivative, which will keep every
|
||||
* loaded fragment in memory. If this becomes too memory intensive, it
|
||||
* may be best to switch to a
|
||||
* {@link FragmentStatePagerAdapter}.
|
||||
*/
|
||||
private SectionsPagerAdapter mSectionsPagerAdapter;
|
||||
|
||||
|
||||
/**
|
||||
* The {@link ViewPager} that will host the section contents.
|
||||
*/
|
||||
private ViewPager mViewPager;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_tabbed_scanning);
|
||||
// Create the adapter that will return a fragment for each of the three
|
||||
// primary sections of the activity.
|
||||
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
|
||||
|
||||
// Set up the ViewPager with the sections adapter.
|
||||
mViewPager = findViewById(R.id.container);
|
||||
mViewPager.setAdapter(mSectionsPagerAdapter);
|
||||
|
||||
// Set up the action bar.
|
||||
final ActionBar actionBar = getSupportActionBar();
|
||||
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
|
||||
|
||||
// When swiping between different sections, select the corresponding
|
||||
// tab. We can also use ActionBar.Tab#select() to do this if we have
|
||||
// a reference to the Tab.
|
||||
mViewPager.setOffscreenPageLimit(0);
|
||||
mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
|
||||
@Override
|
||||
public void onPageSelected(int position) {
|
||||
actionBar.setSelectedNavigationItem(position);
|
||||
}
|
||||
});
|
||||
|
||||
// For each of the sections in the app, add a tab to the action bar.
|
||||
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
|
||||
// Create a tab with text corresponding to the page title defined by
|
||||
// the adapter. Also specify this Activity object, which implements
|
||||
// the TabListener interface, as the callback (listener) for when
|
||||
// this tab is selected.
|
||||
actionBar.addTab(
|
||||
actionBar.newTab()
|
||||
.setText(mSectionsPagerAdapter.getPageTitle(i))
|
||||
.setTabListener(this));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
|
||||
// When the given tab is selected, switch to the corresponding page in
|
||||
// the ViewPager.
|
||||
mViewPager.setCurrentItem(tab.getPosition());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
|
||||
}
|
||||
|
||||
/**
|
||||
* A placeholder fragment containing a simple view.
|
||||
*/
|
||||
public static class ScanFragment extends Fragment {
|
||||
DecoratedBarcodeView barcodeView;
|
||||
|
||||
public ScanFragment() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new instance of this fragment for the given section
|
||||
* number.
|
||||
*/
|
||||
public static ScanFragment newInstance() {
|
||||
return new ScanFragment();
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
View rootView = inflater.inflate(R.layout.fragment_tabbed_scanning, container, false);
|
||||
barcodeView = rootView.findViewById(R.id.barcode_view);
|
||||
return rootView;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setUserVisibleHint(boolean isVisibleToUser) {
|
||||
super.setUserVisibleHint(isVisibleToUser);
|
||||
if(barcodeView != null) {
|
||||
if (isVisibleToUser) {
|
||||
barcodeView.resume();
|
||||
} else {
|
||||
barcodeView.pauseAndWait();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
barcodeView.pauseAndWait();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
barcodeView.resume();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A placeholder fragment containing a simple view.
|
||||
*/
|
||||
public static class CameraFragment extends Fragment {
|
||||
|
||||
private CameraPreview cameraPreview;
|
||||
|
||||
public CameraFragment() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new instance of this fragment for the given section
|
||||
* number.
|
||||
*/
|
||||
public static CameraFragment newInstance() {
|
||||
return new CameraFragment();
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
View rootView = inflater.inflate(R.layout.fragment_tabbed_camera, container, false);
|
||||
cameraPreview = rootView.findViewById(R.id.camera_preview);
|
||||
return rootView;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUserVisibleHint(boolean isVisibleToUser) {
|
||||
super.setUserVisibleHint(isVisibleToUser);
|
||||
|
||||
if (cameraPreview != null) {
|
||||
if (isVisibleToUser) {
|
||||
cameraPreview.resume();
|
||||
} else {
|
||||
cameraPreview.pauseAndWait();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
cameraPreview.pauseAndWait();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
|
||||
* one of the sections/tabs/pages.
|
||||
*/
|
||||
public class SectionsPagerAdapter extends FragmentPagerAdapter {
|
||||
|
||||
public SectionsPagerAdapter(FragmentManager fm) {
|
||||
super(fm);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Fragment getItem(int position) {
|
||||
if(position == 0) {
|
||||
return ScanFragment.newInstance();
|
||||
} else {
|
||||
return CameraFragment.newInstance();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getPageTitle(int position) {
|
||||
switch (position) {
|
||||
case 0:
|
||||
return "Scan";
|
||||
case 1:
|
||||
return "Camera";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,31 +1,31 @@
|
||||
package example.zxing;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.app.ActionBarActivity;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import android.view.KeyEvent;
|
||||
|
||||
import com.journeyapps.barcodescanner.CaptureManager;
|
||||
import com.journeyapps.barcodescanner.CompoundBarcodeView;
|
||||
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
|
||||
|
||||
/**
|
||||
* Sample Activity extending from ActionBarActivity to display a Toolbar.
|
||||
*/
|
||||
public class ToolbarCaptureActivity extends ActionBarActivity {
|
||||
public class ToolbarCaptureActivity extends AppCompatActivity {
|
||||
private CaptureManager capture;
|
||||
private CompoundBarcodeView barcodeScannerView;
|
||||
private DecoratedBarcodeView barcodeScannerView;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
setContentView(R.layout.capture_appcompat);
|
||||
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
|
||||
Toolbar toolbar = findViewById(R.id.my_awesome_toolbar);
|
||||
toolbar.setTitle("Scan Barcode");
|
||||
setSupportActionBar(toolbar);
|
||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
|
||||
barcodeScannerView = (CompoundBarcodeView)findViewById(R.id.zxing_barcode_scanner);
|
||||
barcodeScannerView = findViewById(R.id.zxing_barcode_scanner);
|
||||
|
||||
capture = new CaptureManager(this, barcodeScannerView);
|
||||
capture.initializeFromIntent(getIntent(), savedInstanceState);
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context="example.zxing.CustomScannerActivity">
|
||||
|
||||
<com.journeyapps.barcodescanner.DecoratedBarcodeView
|
||||
android:id="@+id/zxing_barcode_scanner"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
app:zxing_scanner_layout="@layout/custom_barcode_scanner">
|
||||
</com.journeyapps.barcodescanner.DecoratedBarcodeView>
|
||||
|
||||
<Button
|
||||
android:id="@+id/switch_flashlight"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/turn_on_flashlight"
|
||||
android:layout_alignParentTop="true"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:onClick="switchFlashlight"/>
|
||||
|
||||
</RelativeLayout>
|
||||
@@ -19,6 +19,24 @@
|
||||
android:text="@string/scan_barcode"
|
||||
android:onClick="scanBarcode"/>
|
||||
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Scan PDF417"
|
||||
android:onClick="scanPDF417"/>
|
||||
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Scan Inverted"
|
||||
android:onClick="scanBarcodeInverted"/>
|
||||
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Scan Normal and Inverted"
|
||||
android:onClick="scanMixedBarcodes"/>
|
||||
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
@@ -31,8 +49,8 @@
|
||||
android:text="@string/front_camera"
|
||||
android:onClick="scanBarcodeFrontCamera"/>
|
||||
|
||||
|
||||
<fragment
|
||||
android:id="@+id/fragment_scan"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
|
||||
@@ -51,6 +69,37 @@
|
||||
android:text="@string/toolbar_activity"
|
||||
android:onClick="scanToolbar"/>
|
||||
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/custom_scanner"
|
||||
android:onClick="scanCustomScanner"/>
|
||||
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/scanner_with_margin"
|
||||
android:onClick="scanMarginScanner"/>
|
||||
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/scanner_with_timeout"
|
||||
android:onClick="scanWithTimeout"/>
|
||||
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/tabs"
|
||||
android:onClick="tabs"/>
|
||||
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/about"
|
||||
android:onClick="about"/>
|
||||
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
<androidx.viewpager.widget.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/container"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context="example.zxing.TabbedScanning" />
|
||||
@@ -14,24 +14,40 @@
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<android.support.v7.widget.Toolbar
|
||||
|
||||
<androidx.appcompat.widget.Toolbar
|
||||
android:id="@+id/my_awesome_toolbar"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_width="match_parent"
|
||||
android:minHeight="?attr/actionBarSize"
|
||||
android:background="?attr/colorPrimary"
|
||||
android:layout_alignParentTop="true"
|
||||
android:layout_alignParentLeft="true"
|
||||
app:theme="@style/ThemeOverlay.AppCompat.ActionBar"/>
|
||||
|
||||
<com.journeyapps.barcodescanner.CompoundBarcodeView
|
||||
<com.journeyapps.barcodescanner.DecoratedBarcodeView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1"
|
||||
android:id="@+id/zxing_barcode_scanner"/>
|
||||
</LinearLayout>
|
||||
android:layout_below="@+id/my_awesome_toolbar"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:id="@+id/zxing_barcode_scanner"
|
||||
app:zxing_use_texture_view="true"/>
|
||||
|
||||
|
||||
<!-- Sample for a footer. Also add to DecoratedBarcodeView: android:layout_above="@+id/footer" -->
|
||||
<!-- <TextView
|
||||
android:id="@+id/footer"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="150dp"
|
||||
android:text="Footer"
|
||||
android:gravity="center"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:layout_alignParentLeft="true"/> -->
|
||||
</RelativeLayout>
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
Copyright (C) 2008 ZXing authors
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<com.journeyapps.barcodescanner.DecoratedBarcodeView
|
||||
android:layout_alignParentLeft="true"
|
||||
android:layout_alignParentRight="true"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:layout_alignParentTop="true"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginLeft="50dp"
|
||||
android:layout_marginRight="50dp"
|
||||
android:layout_marginTop="150dp"
|
||||
android:layout_marginBottom="150dp"
|
||||
android:id="@+id/zxing_barcode_scanner"
|
||||
app:zxing_use_texture_view="false"
|
||||
app:zxing_preview_scaling_strategy="fitXY"/>
|
||||
|
||||
</RelativeLayout>
|
||||
@@ -3,14 +3,14 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<com.journeyapps.barcodescanner.CompoundBarcodeView
|
||||
<com.journeyapps.barcodescanner.DecoratedBarcodeView
|
||||
android:id="@+id/barcode_scanner"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_above="@+id/buttonsLayout"
|
||||
android:layout_alignParentTop="true">
|
||||
|
||||
</com.journeyapps.barcodescanner.CompoundBarcodeView>
|
||||
</com.journeyapps.barcodescanner.DecoratedBarcodeView>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<merge xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<com.journeyapps.barcodescanner.BarcodeView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:id="@+id/zxing_barcode_surface"
|
||||
app:zxing_framing_rect_width="250dp"
|
||||
app:zxing_framing_rect_height="50dp"/>
|
||||
|
||||
<com.journeyapps.barcodescanner.ViewfinderView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:id="@+id/zxing_viewfinder_view"
|
||||
app:zxing_possible_result_points="@color/zxing_custom_possible_result_points"
|
||||
app:zxing_result_view="@color/zxing_custom_result_view"
|
||||
app:zxing_viewfinder_laser="@color/zxing_custom_viewfinder_laser"
|
||||
app:zxing_viewfinder_laser_visibility="true"
|
||||
app:zxing_viewfinder_mask="@color/zxing_custom_viewfinder_mask"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/zxing_status_view"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="bottom|center_horizontal"
|
||||
android:background="@color/zxing_transparent"
|
||||
android:text="@string/zxing_msg_default_status"
|
||||
android:textColor="@color/zxing_status_text"/>
|
||||
|
||||
</merge>
|
||||
@@ -0,0 +1,17 @@
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:paddingBottom="@dimen/activity_vertical_margin"
|
||||
android:paddingLeft="@dimen/activity_horizontal_margin"
|
||||
android:paddingRight="@dimen/activity_horizontal_margin"
|
||||
android:paddingTop="@dimen/activity_vertical_margin"
|
||||
tools:context="example.zxing.TabbedScanning$CameraFragment">
|
||||
|
||||
<com.journeyapps.barcodescanner.CameraPreview
|
||||
android:id="@+id/camera_preview"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="300dp"
|
||||
/>
|
||||
|
||||
</RelativeLayout>
|
||||
@@ -0,0 +1,18 @@
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:paddingBottom="@dimen/activity_vertical_margin"
|
||||
android:paddingLeft="@dimen/activity_horizontal_margin"
|
||||
android:paddingRight="@dimen/activity_horizontal_margin"
|
||||
android:paddingTop="@dimen/activity_vertical_margin"
|
||||
tools:context="example.zxing.TabbedScanning$ScanFragment">
|
||||
|
||||
|
||||
<com.journeyapps.barcodescanner.DecoratedBarcodeView
|
||||
android:id="@+id/barcode_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
/>
|
||||
|
||||
</RelativeLayout>
|
||||
@@ -0,0 +1,4 @@
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
tools:context="example.zxing.TabbedScanning">
|
||||
</menu>
|
||||
@@ -3,5 +3,6 @@
|
||||
<style name="AppCompatCaptureTheme" parent="Theme.AppCompat.Light.NoActionBar">
|
||||
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
|
||||
<item name="android:statusBarColor">#ffb341</item>
|
||||
<item name="android:windowTranslucentStatus">true</item>
|
||||
</style>
|
||||
</resources>
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
<resources>
|
||||
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
|
||||
(such as screen margins) for screens with more than 820dp of available width. This
|
||||
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
|
||||
<dimen name="activity_horizontal_margin">64dp</dimen>
|
||||
</resources>
|
||||
@@ -1,12 +1,21 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="MissingTranslation">
|
||||
|
||||
<string name="app_name">ZXing Sample</string>
|
||||
<string name="scan_barcode">Scan Barcode</string>
|
||||
<string name="scan_barcode_with_request_code">Scan Barcode with customized request code</string>
|
||||
<string name="any_orientation">1D Any Orientation</string>
|
||||
<string name="scan_from_fragment">Scan from fragment</string>
|
||||
<string name="front_camera">Front Camera</string>
|
||||
<string name="custom_activity">Continuous Scan</string>
|
||||
<string name="toolbar_activity">Activity with Toolbar</string>
|
||||
<string name="custom_scanner">Custom Scanner Activity</string>
|
||||
<string name="scanner_with_margin">Scanner with Margin</string>
|
||||
<string name="scanner_with_timeout">Finish Scan in 8 Seconds</string>
|
||||
<string name="turn_on_flashlight">Turn on Flashlight</string>
|
||||
<string name="turn_off_flashlight">Turn off Flashlight</string>
|
||||
<string name="title_activity_tabbed_scanning">Tabbed Scanning</string>
|
||||
<string name="tabs">Tabs</string>
|
||||
<string name="about">About</string>
|
||||
|
||||
</resources>
|
||||
|
||||
@@ -1,3 +1,2 @@
|
||||
include ':zxing-android-embedded'
|
||||
include ':sample'
|
||||
include ':sample-nosupport'
|
||||
|
||||
@@ -18,9 +18,6 @@
|
||||
|
||||
<uses-permission android:name="android.permission.CAMERA"/>
|
||||
|
||||
<!-- Support Android 2.3+. -->
|
||||
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="21"/>
|
||||
|
||||
<!-- Don't require camera, as this requires a rear camera. This allows it to work on the Nexus 7 -->
|
||||
<uses-feature android:name="android.hardware.camera" android:required="false"/>
|
||||
<uses-feature android:name="android.hardware.camera.front" android:required="false"/>
|
||||
|
||||
@@ -1,27 +1,57 @@
|
||||
apply plugin: 'com.android.library'
|
||||
apply plugin: 'maven-publish'
|
||||
apply plugin: 'signing'
|
||||
|
||||
ext.artifactId = 'zxing-android-embedded'
|
||||
|
||||
dependencies {
|
||||
compile project.zxingCore
|
||||
|
||||
// Optional dependency.
|
||||
provided 'com.android.support:support-v4:22.0.0'
|
||||
// Publishing config from https://getstream.io/blog/publishing-libraries-to-mavencentral-2021/
|
||||
ext["signing.keyId"] = ''
|
||||
ext["signing.password"] = ''
|
||||
ext["signing.secretKeyRingFile"] = ''
|
||||
ext["ossrhUsername"] = ''
|
||||
ext["ossrhPassword"] = ''
|
||||
ext["sonatypeStagingProfileId"] = ''
|
||||
|
||||
File secretPropsFile = project.rootProject.file('local.properties')
|
||||
if (secretPropsFile.exists()) {
|
||||
Properties p = new Properties()
|
||||
p.load(new FileInputStream(secretPropsFile))
|
||||
p.each { name, value ->
|
||||
ext[name] = value
|
||||
}
|
||||
} else {
|
||||
ext["signing.keyId"] = System.getenv('SIGNING_KEY_ID')
|
||||
ext["signing.password"] = System.getenv('SIGNING_PASSWORD')
|
||||
ext["signing.secretKeyRingFile"] = System.getenv('SIGNING_SECRET_KEY_RING_FILE')
|
||||
ext["ossrhUsername"] = System.getenv('OSSRH_USERNAME')
|
||||
ext["ossrhPassword"] = System.getenv('OSSRH_PASSWORD')
|
||||
ext["sonatypeStagingProfileId"] = System.getenv('SONATYPE_STAGING_PROFILE_ID')
|
||||
}
|
||||
|
||||
|
||||
dependencies {
|
||||
api project.zxingCore
|
||||
|
||||
implementation 'androidx.core:core:1.6.0'
|
||||
implementation 'androidx.fragment:fragment:1.3.6'
|
||||
|
||||
testImplementation 'junit:junit:4.13.1'
|
||||
testImplementation 'org.mockito:mockito-core:1.9.5'
|
||||
}
|
||||
|
||||
android {
|
||||
resourcePrefix 'zxing_'
|
||||
compileSdkVersion project.androidTargetSdk
|
||||
buildToolsVersion project.androidBuildTools
|
||||
|
||||
sourceSets {
|
||||
main {
|
||||
manifest.srcFile 'AndroidManifest.xml'
|
||||
java.srcDirs = ['src-orig', 'src']
|
||||
java.srcDirs = ['src']
|
||||
res.srcDirs = ['res-orig', 'res']
|
||||
assets.srcDirs = ['assets']
|
||||
}
|
||||
test.setRoot('test')
|
||||
}
|
||||
|
||||
// This is bad practice - we should fix the warnings instead.
|
||||
@@ -32,9 +62,104 @@ android {
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_7
|
||||
targetCompatibility JavaVersion.VERSION_1_7
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
|
||||
testOptions {
|
||||
// We test with primitives such as Rect, and rely on their default behaviour working.
|
||||
unitTests.returnDefaultValues = true
|
||||
}
|
||||
|
||||
defaultConfig {
|
||||
minSdkVersion 19
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
debug {
|
||||
versionNameSuffix ".debug"
|
||||
resValue "string", "app_version", "${defaultConfig.versionName}${versionNameSuffix}"
|
||||
}
|
||||
release {
|
||||
resValue "string", "app_version", "${defaultConfig.versionName}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
task sourceJar(type: Jar) {
|
||||
classifier = 'sources'
|
||||
from android.sourceSets.main.java.srcDirs
|
||||
}
|
||||
|
||||
project.afterEvaluate {
|
||||
publishing {
|
||||
publications {
|
||||
maven(MavenPublication) {
|
||||
artifact bundleReleaseAar
|
||||
artifactId project.artifactId
|
||||
|
||||
artifact sourceJar
|
||||
|
||||
pom {
|
||||
name = project.artifactId
|
||||
description = 'Barcode scanner library for Android, based on the ZXing decoder'
|
||||
url = 'https://github.com/journeyapps/zxing-android-embedded'
|
||||
|
||||
licenses {
|
||||
license {
|
||||
name = 'The Apache License, Version 2.0'
|
||||
url = 'https://github.com/journeyapps/zxing-android-embedded/blob/master/COPYING'
|
||||
}
|
||||
}
|
||||
|
||||
developers {
|
||||
developer {
|
||||
id = ''
|
||||
name = 'Ralf Kistner'
|
||||
email = 'ralf@journeyapps.com'
|
||||
organization = 'Journey Mobile, Inc'
|
||||
organizationUrl = 'https://journeyapps.com'
|
||||
}
|
||||
}
|
||||
scm {
|
||||
connection = 'scm:git:github.com/journeyapps/zxing-android-embedded.git'
|
||||
developerConnection = 'scm:git:ssh://github.com/journeyapps/zxing-android-embedded.git'
|
||||
url = 'https://github.com/journeyapps/zxing-android-embedded'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pom.withXml {
|
||||
// HACK to add dependencies to POM.
|
||||
// When maven-publish can do this automatically for Android projects,
|
||||
// remove this section.
|
||||
def deps = asNode().appendNode('dependencies')
|
||||
|
||||
project.configurations.api.allDependencies.each { dep ->
|
||||
def node = deps.appendNode('dependency')
|
||||
node.appendNode('groupId', dep.group)
|
||||
node.appendNode('artifactId', dep.name)
|
||||
node.appendNode('version', dep.version)
|
||||
node.appendNode('scope', 'api')
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
repositories {
|
||||
maven {
|
||||
name = "sonatype"
|
||||
url = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
|
||||
credentials {
|
||||
username ossrhUsername
|
||||
password ossrhPassword
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
signing {
|
||||
sign publishing.publications
|
||||
}
|
||||
|
||||
@@ -18,5 +18,5 @@
|
||||
<string name="zxing_app_name">Barcode Scanner</string>
|
||||
<string name="zxing_button_ok">D\'acord</string>
|
||||
<string name="zxing_msg_camera_framework_bug">S\'ha produït un problema amb la càmera de l\'Android. Potser haureu de reiniciar el dispositiu.</string>
|
||||
<string name="zxing_msg_default_status">Poseu un codi de barres dins el rectable per escanejar-lo.</string>
|
||||
<string name="zxing_msg_default_status">Poseu un codi de barres dins el rectangle per escanejar-lo.</string>
|
||||
</resources>
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<merge xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
<merge xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<com.journeyapps.barcodescanner.BarcodeView
|
||||
android:layout_width="match_parent"
|
||||
|
||||
@@ -15,11 +15,16 @@
|
||||
limitations under the License.
|
||||
-->
|
||||
<merge xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<com.journeyapps.barcodescanner.CompoundBarcodeView
|
||||
<!--
|
||||
This Activity is typically full-screen. Therefore we can safely use centerCrop scaling with
|
||||
a SurfaceView, without fear of weird artifacts. -->
|
||||
<com.journeyapps.barcodescanner.DecoratedBarcodeView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:id="@+id/zxing_barcode_scanner"/>
|
||||
android:id="@+id/zxing_barcode_scanner"
|
||||
app:zxing_preview_scaling_strategy="centerCrop"
|
||||
app:zxing_use_texture_view="false"/>
|
||||
|
||||
</merge>
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<resources>
|
||||
<style name="zxing_CaptureTheme" parent="android:Theme.Holo.NoActionBar.Fullscreen">
|
||||
</style>
|
||||
</resources>
|
||||
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="ResourceName,UnusedResources" >
|
||||
<string name="define_zxingandroidembedded" translatable="false" />
|
||||
<!-- Author section -->
|
||||
<string name="library_zxingandroidembedded_author" translatable="false">JourneyApps</string>
|
||||
<string name="library_zxingandroidembedded_authorWebsite" translatable="false">https://journeyapps.com/</string>
|
||||
<!-- Library section -->
|
||||
<string name="library_zxingandroidembedded_libraryName" translatable="false">ZXing Android Embedded</string>
|
||||
<string name="library_zxingandroidembedded_libraryDescription" translatable="false">Barcode scanning library for Android, using ZXing for decoding.</string>
|
||||
<string name="library_zxingandroidembedded_libraryWebsite" translatable="false">https://github.com/journeyapps/zxing-android-embedded</string>
|
||||
<string name="library_zxingandroidembedded_libraryVersion" translatable="false" />
|
||||
<!-- OpenSource section -->
|
||||
<string name="library_zxingandroidembedded_isOpenSource" translatable="false">true</string>
|
||||
<string name="library_zxingandroidembedded_repositoryLink" translatable="false">https://github.com/journeyapps/zxing-android-embedded</string>
|
||||
<!-- License section -->
|
||||
<string name="library_zxingandroidembedded_licenseId" translatable="false">apache_2_0</string>
|
||||
<!-- Custom variables section -->
|
||||
</resources>
|
||||
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<resources>
|
||||
|
||||
<declare-styleable name="zxing_view">
|
||||
<attr name="zxing_scanner_layout" format="reference"/>
|
||||
</declare-styleable>
|
||||
|
||||
<declare-styleable name="zxing_camera_preview">
|
||||
<attr name="zxing_framing_rect_width" format="dimension" />
|
||||
<attr name="zxing_framing_rect_height" format="dimension" />
|
||||
<attr name="zxing_use_texture_view" format="boolean" />
|
||||
<attr name="zxing_preview_scaling_strategy" format="enum">
|
||||
<enum name="centerCrop" value="1" />
|
||||
<enum name="fitCenter" value="2" />
|
||||
<enum name="fitXY" value="3" />
|
||||
</attr>
|
||||
</declare-styleable>
|
||||
|
||||
<declare-styleable name="zxing_finder">
|
||||
<attr name="zxing_possible_result_points" format="color"/>
|
||||
<attr name="zxing_result_view" format="color"/>
|
||||
<attr name="zxing_viewfinder_laser" format="color"/>
|
||||
<attr name="zxing_viewfinder_mask" format="color"/>
|
||||
<attr name="zxing_viewfinder_laser_visibility" format="boolean"/>
|
||||
</declare-styleable>
|
||||
|
||||
</resources>
|
||||
@@ -21,4 +21,9 @@
|
||||
<color name="zxing_transparent">#00000000</color>
|
||||
<color name="zxing_viewfinder_laser">#ffcc0000</color> <!-- Android standard ICS color -->
|
||||
<color name="zxing_viewfinder_mask">#60000000</color>
|
||||
|
||||
<color name="zxing_custom_possible_result_points">#903A89CF</color>
|
||||
<color name="zxing_custom_result_view">#b0000000</color>
|
||||
<color name="zxing_custom_viewfinder_laser">#1976D2</color>
|
||||
<color name="zxing_custom_viewfinder_mask">#60000000</color>
|
||||
</resources>
|
||||
|
||||
@@ -16,10 +16,12 @@
|
||||
-->
|
||||
<resources>
|
||||
<item type="id" name="zxing_decode"/>
|
||||
<item type="id" name="zxing_preview_failed"/>
|
||||
<item type="id" name="zxing_decode_failed"/>
|
||||
<item type="id" name="zxing_decode_succeeded"/>
|
||||
<item type="id" name="zxing_possible_result_points"/>
|
||||
<item type="id" name="zxing_back_button"/>
|
||||
<item type="id" name="zxing_prewiew_size_ready"/>
|
||||
<item type="id" name="zxing_camera_error"/>
|
||||
<item type="id" name="zxing_camera_closed"/>
|
||||
</resources>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<resources>
|
||||
<style name="zxing_CaptureTheme" parent="android:Theme.Black.NoTitleBar.Fullscreen">
|
||||
<style name="zxing_CaptureTheme" parent="android:Theme.Holo.NoActionBar.Fullscreen">
|
||||
</style>
|
||||
</resources>
|
||||
|
||||
@@ -71,12 +71,7 @@ public final class AmbientLightManager implements SensorEventListener {
|
||||
}
|
||||
|
||||
private void setTorch(final boolean on) {
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
cameraManager.setTorch(on);
|
||||
}
|
||||
});
|
||||
handler.post(() -> cameraManager.setTorch(on));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -95,5 +90,4 @@ public final class AmbientLightManager implements SensorEventListener {
|
||||
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,39 +16,39 @@
|
||||
|
||||
package com.google.zxing.client.android;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.res.AssetFileDescriptor;
|
||||
import android.media.AudioAttributes;
|
||||
import android.media.AudioManager;
|
||||
import android.media.MediaPlayer;
|
||||
import android.os.Build;
|
||||
import android.os.Vibrator;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Manages beeps and vibrations.
|
||||
*/
|
||||
public final class BeepManager implements
|
||||
MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener, Closeable {
|
||||
public final class BeepManager {
|
||||
|
||||
private static final String TAG = BeepManager.class.getSimpleName();
|
||||
|
||||
private static final float BEEP_VOLUME = 0.10f;
|
||||
private static final long VIBRATE_DURATION = 200L;
|
||||
|
||||
private final Activity activity;
|
||||
private MediaPlayer mediaPlayer;
|
||||
private boolean playBeep;
|
||||
private final Context context;
|
||||
|
||||
private boolean beepEnabled = true;
|
||||
private boolean vibrateEnabled = false;
|
||||
|
||||
public BeepManager(Activity activity) {
|
||||
this.activity = activity;
|
||||
this.mediaPlayer = null;
|
||||
updatePrefs();
|
||||
activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
|
||||
|
||||
// We do not keep a reference to the Activity itself, to prevent leaks
|
||||
this.context = activity.getApplicationContext();
|
||||
}
|
||||
|
||||
public boolean isBeepEnabled() {
|
||||
@@ -79,45 +79,44 @@ public final class BeepManager implements
|
||||
this.vibrateEnabled = vibrateEnabled;
|
||||
}
|
||||
|
||||
public synchronized void updatePrefs() {
|
||||
playBeep = shouldBeep(beepEnabled, activity);
|
||||
if (playBeep && mediaPlayer == null) {
|
||||
// The volume on STREAM_SYSTEM is not adjustable, and users found it too loud,
|
||||
// so we now play on the music stream.
|
||||
activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
|
||||
mediaPlayer = buildMediaPlayer(activity);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
public synchronized void playBeepSoundAndVibrate() {
|
||||
if (playBeep && mediaPlayer != null) {
|
||||
mediaPlayer.start();
|
||||
if (beepEnabled) {
|
||||
playBeepSound();
|
||||
}
|
||||
if (vibrateEnabled) {
|
||||
Vibrator vibrator = (Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE);
|
||||
vibrator.vibrate(VIBRATE_DURATION);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean shouldBeep(boolean beep, Context activity) {
|
||||
boolean shouldPlayBeep = beep;
|
||||
if (shouldPlayBeep) {
|
||||
// See if sound settings overrides this
|
||||
AudioManager audioService = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);
|
||||
if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) {
|
||||
shouldPlayBeep = false;
|
||||
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
|
||||
if (vibrator != null) {
|
||||
vibrator.vibrate(VIBRATE_DURATION);
|
||||
}
|
||||
}
|
||||
return shouldPlayBeep;
|
||||
}
|
||||
|
||||
private MediaPlayer buildMediaPlayer(Context activity) {
|
||||
|
||||
public MediaPlayer playBeepSound() {
|
||||
MediaPlayer mediaPlayer = new MediaPlayer();
|
||||
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
|
||||
mediaPlayer.setOnCompletionListener(this);
|
||||
mediaPlayer.setOnErrorListener(this);
|
||||
if (Build.VERSION.SDK_INT >= 21) {
|
||||
mediaPlayer.setAudioAttributes(new AudioAttributes.Builder().setContentType(
|
||||
AudioAttributes.CONTENT_TYPE_MUSIC).build());
|
||||
} else {
|
||||
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
|
||||
}
|
||||
|
||||
mediaPlayer.setOnCompletionListener(mp -> {
|
||||
mp.stop();
|
||||
mp.reset();
|
||||
mp.release();
|
||||
});
|
||||
mediaPlayer.setOnErrorListener((mp, what, extra) -> {
|
||||
Log.w(TAG, "Failed to beep " + what + ", " + extra);
|
||||
// possibly media player error, so release and recreate
|
||||
mp.stop();
|
||||
mp.reset();
|
||||
mp.release();
|
||||
return true;
|
||||
});
|
||||
try {
|
||||
AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.zxing_beep);
|
||||
AssetFileDescriptor file = context.getResources().openRawResourceFd(R.raw.zxing_beep);
|
||||
try {
|
||||
mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
|
||||
} finally {
|
||||
@@ -125,40 +124,13 @@ public final class BeepManager implements
|
||||
}
|
||||
mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
|
||||
mediaPlayer.prepare();
|
||||
mediaPlayer.start();
|
||||
return mediaPlayer;
|
||||
} catch (IOException ioe) {
|
||||
Log.w(TAG, ioe);
|
||||
mediaPlayer.reset();
|
||||
mediaPlayer.release();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCompletion(MediaPlayer mp) {
|
||||
// When the beep has finished playing, rewind to queue up another one.
|
||||
mp.seekTo(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized boolean onError(MediaPlayer mp, int what, int extra) {
|
||||
if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
|
||||
// we are finished, so put up an appropriate error toast if required and finish
|
||||
activity.finish();
|
||||
} else {
|
||||
// possibly media player error, so release and recreate
|
||||
mp.release();
|
||||
mediaPlayer = null;
|
||||
updatePrefs();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void close() {
|
||||
if (mediaPlayer != null) {
|
||||
mediaPlayer.release();
|
||||
mediaPlayer = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,14 +17,12 @@
|
||||
package com.google.zxing.client.android;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -98,5 +96,4 @@ public final class DecodeFormatManager {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -233,5 +233,4 @@ public final class DecodeHintManager {
|
||||
Log.i(TAG, "Hints from the Intent: " + hints);
|
||||
return hints;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -108,12 +108,7 @@ public final class InactivityTimer {
|
||||
// 0 indicates that we're on battery
|
||||
final boolean onBatteryNow = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) <= 0;
|
||||
// post on handler to run in main thread
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
onBattery(onBatteryNow);
|
||||
}
|
||||
});
|
||||
handler.post(() -> onBattery(onBatteryNow));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,6 +88,13 @@ public final class Intents {
|
||||
*/
|
||||
public static final String CAMERA_ID = "SCAN_CAMERA_ID";
|
||||
|
||||
/**
|
||||
* Optional parameter to switch the torch on at camera startup.
|
||||
* Enables the torch on camera startup
|
||||
* If provided, should be a boolean.
|
||||
*/
|
||||
public static final String TORCH_ENABLED = "TORCH_ENABLED";
|
||||
|
||||
/**
|
||||
* @see com.google.zxing.DecodeHintType#CHARACTER_SET
|
||||
*/
|
||||
@@ -98,6 +105,30 @@ public final class Intents {
|
||||
*/
|
||||
public static final String BEEP_ENABLED = "BEEP_ENABLED";
|
||||
|
||||
/**
|
||||
* Set to true to return a path to the barcode's image as it was captured. Defaults to false.
|
||||
*/
|
||||
public static final String BARCODE_IMAGE_ENABLED = "BARCODE_IMAGE_ENABLED";
|
||||
|
||||
/**
|
||||
* Set the time to finish the scan screen.
|
||||
*/
|
||||
public static final String TIMEOUT = "TIMEOUT";
|
||||
|
||||
/**
|
||||
* Set the time to finish the scan screen.
|
||||
*/
|
||||
public static final String MISSING_CAMERA_PERMISSION = "MISSING_CAMERA_PERMISSION";
|
||||
|
||||
/**
|
||||
* Set the time to finish the scan screen.
|
||||
*/
|
||||
public static final String SHOW_MISSING_CAMERA_PERMISSION_DIALOG = "SHOW_MISSING_CAMERA_PERMISSION_DIALOG";
|
||||
|
||||
/**
|
||||
* Set the time to finish the scan screen.
|
||||
*/
|
||||
public static final String MISSING_CAMERA_PERMISSION_DIALOG_MESSAGE = "MISSING_CAMERA_PERMISSION_DIALOG_MESSAGE";
|
||||
|
||||
/**
|
||||
* Whether or not the orientation should be locked when the activity is first started.
|
||||
@@ -162,6 +193,35 @@ public final class Intents {
|
||||
*/
|
||||
public static final String RESULT_BYTE_SEGMENTS_PREFIX = "SCAN_RESULT_BYTE_SEGMENTS_";
|
||||
|
||||
/**
|
||||
* Call {@link android.content.Intent#getStringExtra(String)} with {@link #RESULT_BARCODE_IMAGE_PATH}
|
||||
* to get a {@code String} path to a cropped and compressed png file of the barcode's image
|
||||
* as it was displayed. Only available if
|
||||
* {@link com.google.zxing.integration.android.IntentIntegrator#setBarcodeImageEnabled(boolean)}
|
||||
* is called with true.
|
||||
*/
|
||||
public static final String RESULT_BARCODE_IMAGE_PATH = "SCAN_RESULT_IMAGE_PATH";
|
||||
|
||||
/**
|
||||
* Define the scan type.
|
||||
*/
|
||||
public static final String SCAN_TYPE = "SCAN_TYPE";
|
||||
|
||||
/**
|
||||
* Scan normal barcodes white on black
|
||||
*/
|
||||
public static final int NORMAL_SCAN = 0;
|
||||
|
||||
/**
|
||||
* The scan should be inverted. White becomes black, black becomes white.
|
||||
*/
|
||||
public static final int INVERTED_SCAN = 1;
|
||||
|
||||
/**
|
||||
* Scan alternating inverted and normal barcodes.
|
||||
*/
|
||||
public static final int MIXED_SCAN = 2;
|
||||
|
||||
private Scan() {
|
||||
}
|
||||
}
|
||||
|
||||
-1
@@ -83,5 +83,4 @@ public final class OpenCameraInterface {
|
||||
return Camera.open(cameraId);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+142
-38
@@ -22,8 +22,6 @@ import android.app.Fragment;
|
||||
import android.content.Intent;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.view.Display;
|
||||
import android.view.WindowManager;
|
||||
|
||||
import com.google.zxing.client.android.Intents;
|
||||
import com.journeyapps.barcodescanner.CaptureActivity;
|
||||
@@ -36,45 +34,67 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sean Owen
|
||||
* @author Fred Lin
|
||||
* @author Isaac Potoczny-Jones
|
||||
* @author Brad Drehmer
|
||||
* @author gcstang
|
||||
* @deprecated Use ScanOptions and ScanContract instead.
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public class IntentIntegrator {
|
||||
|
||||
public static final int REQUEST_CODE = 0x0000c0de; // Only use bottom 16 bits
|
||||
|
||||
private static final String TAG = IntentIntegrator.class.getSimpleName();
|
||||
|
||||
|
||||
// supported barcode formats
|
||||
public static final Collection<String> PRODUCT_CODE_TYPES = list("UPC_A", "UPC_E", "EAN_8", "EAN_13", "RSS_14");
|
||||
|
||||
// Product Codes
|
||||
public static final String UPC_A = "UPC_A";
|
||||
public static final String UPC_E = "UPC_E";
|
||||
public static final String EAN_8 = "EAN_8";
|
||||
public static final String EAN_13 = "EAN_13";
|
||||
public static final String RSS_14 = "RSS_14";
|
||||
|
||||
// Other 1D
|
||||
public static final String CODE_39 = "CODE_39";
|
||||
public static final String CODE_93 = "CODE_93";
|
||||
public static final String CODE_128 = "CODE_128";
|
||||
public static final String ITF = "ITF";
|
||||
|
||||
public static final String RSS_EXPANDED = "RSS_EXPANDED";
|
||||
|
||||
// 2D
|
||||
public static final String QR_CODE = "QR_CODE";
|
||||
public static final String DATA_MATRIX = "DATA_MATRIX";
|
||||
public static final String PDF_417 = "PDF_417";
|
||||
|
||||
|
||||
public static final Collection<String> PRODUCT_CODE_TYPES = list(UPC_A, UPC_E, EAN_8, EAN_13, RSS_14);
|
||||
public static final Collection<String> ONE_D_CODE_TYPES =
|
||||
list("UPC_A", "UPC_E", "EAN_8", "EAN_13", "CODE_39", "CODE_93", "CODE_128",
|
||||
"ITF", "RSS_14", "RSS_EXPANDED");
|
||||
public static final Collection<String> QR_CODE_TYPES = Collections.singleton("QR_CODE");
|
||||
public static final Collection<String> DATA_MATRIX_TYPES = Collections.singleton("DATA_MATRIX");
|
||||
list(UPC_A, UPC_E, EAN_8, EAN_13, RSS_14, CODE_39, CODE_93, CODE_128,
|
||||
ITF, RSS_14, RSS_EXPANDED);
|
||||
|
||||
public static final Collection<String> ALL_CODE_TYPES = null;
|
||||
|
||||
private final Activity activity;
|
||||
private android.app.Fragment fragment;
|
||||
private android.support.v4.app.Fragment supportFragment;
|
||||
private androidx.fragment.app.Fragment supportFragment;
|
||||
|
||||
private final Map<String, Object> moreExtras = new HashMap<String, Object>(3);
|
||||
private final Map<String, Object> moreExtras = new HashMap<>(3);
|
||||
|
||||
private Collection<String> desiredBarcodeFormats;
|
||||
|
||||
private Class<?> captureActivity;
|
||||
|
||||
private int requestCode = REQUEST_CODE;
|
||||
|
||||
protected Class<?> getDefaultCaptureActivity() {
|
||||
return CaptureActivity.class;
|
||||
}
|
||||
/**
|
||||
* @param activity {@link Activity} invoking the integration
|
||||
*/
|
||||
|
||||
public IntentIntegrator(Activity activity) {
|
||||
this.activity = activity;
|
||||
}
|
||||
@@ -97,12 +117,27 @@ public class IntentIntegrator {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the request code that is used for the Intent. If it is changed, it is the caller's
|
||||
* responsibility to check the request code from the result intent.
|
||||
*
|
||||
* @param requestCode the new request code
|
||||
* @return this
|
||||
*/
|
||||
public IntentIntegrator setRequestCode(int requestCode) {
|
||||
if (requestCode <= 0 || requestCode > 0x0000ffff) {
|
||||
throw new IllegalArgumentException("requestCode out of range");
|
||||
}
|
||||
this.requestCode = requestCode;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param fragment {@link Fragment} invoking the integration.
|
||||
* {@link #startActivityForResult(Intent, int)} will be called on the {@link Fragment} instead
|
||||
* of an {@link Activity}
|
||||
*/
|
||||
public static IntentIntegrator forSupportFragment(android.support.v4.app.Fragment fragment) {
|
||||
public static IntentIntegrator forSupportFragment(androidx.fragment.app.Fragment fragment) {
|
||||
IntentIntegrator integrator = new IntentIntegrator(fragment.getActivity());
|
||||
integrator.supportFragment = fragment;
|
||||
return integrator;
|
||||
@@ -164,6 +199,18 @@ public class IntentIntegrator {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set to true to enable initial torch
|
||||
*
|
||||
* @param enabled true to enable initial torch
|
||||
* @return this
|
||||
*/
|
||||
public IntentIntegrator setTorchEnabled(boolean enabled) {
|
||||
addExtra(Intents.Scan.TORCH_ENABLED, enabled);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set to false to disable beep on scan.
|
||||
*
|
||||
@@ -175,6 +222,17 @@ public class IntentIntegrator {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set to true to enable saving the barcode image and sending its path in the result Intent.
|
||||
*
|
||||
* @param enabled true to enable barcode image
|
||||
* @return this
|
||||
*/
|
||||
public IntentIntegrator setBarcodeImageEnabled(boolean enabled) {
|
||||
addExtra(Intents.Scan.BARCODE_IMAGE_ENABLED, enabled);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the desired barcode formats to scan.
|
||||
*
|
||||
@@ -186,11 +244,33 @@ public class IntentIntegrator {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the desired barcode formats to scan.
|
||||
*
|
||||
* @param desiredBarcodeFormats names of {@code BarcodeFormat}s to scan for
|
||||
* @return this
|
||||
*/
|
||||
public IntentIntegrator setDesiredBarcodeFormats(String... desiredBarcodeFormats) {
|
||||
this.desiredBarcodeFormats = Arrays.asList(desiredBarcodeFormats);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiates a scan for all known barcode types with the default camera.
|
||||
*/
|
||||
public final void initiateScan() {
|
||||
startActivityForResult(createScanIntent(), REQUEST_CODE);
|
||||
startActivityForResult(createScanIntent(), requestCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiates a scan for all known barcode types with the default camera.
|
||||
* And starts a timer to finish on timeout
|
||||
*
|
||||
* @return Activity.RESULT_CANCELED and true on parameter TIMEOUT.
|
||||
*/
|
||||
public IntentIntegrator setTimeout(long timeout) {
|
||||
addExtra(Intents.Scan.TIMEOUT, timeout);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -244,9 +324,7 @@ public class IntentIntegrator {
|
||||
*/
|
||||
protected void startActivityForResult(Intent intent, int code) {
|
||||
if (fragment != null) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
|
||||
fragment.startActivityForResult(intent, code);
|
||||
}
|
||||
fragment.startActivityForResult(intent, code);
|
||||
} else if (supportFragment != null) {
|
||||
supportFragment.startActivityForResult(intent, code);
|
||||
} else {
|
||||
@@ -254,12 +332,9 @@ public class IntentIntegrator {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected void startActivity(Intent intent) {
|
||||
if (fragment != null) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
|
||||
fragment.startActivity(intent);
|
||||
}
|
||||
fragment.startActivity(intent);
|
||||
} else if (supportFragment != null) {
|
||||
supportFragment.startActivity(intent);
|
||||
} else {
|
||||
@@ -270,6 +345,8 @@ public class IntentIntegrator {
|
||||
/**
|
||||
* <p>Call this from your {@link Activity}'s
|
||||
* {@link Activity#onActivityResult(int, int, Intent)} method.</p>
|
||||
* <p>
|
||||
* This checks that the requestCode is equal to the default REQUEST_CODE.
|
||||
*
|
||||
* @param requestCode request code from {@code onActivityResult()}
|
||||
* @param resultCode result code from {@code onActivityResult()}
|
||||
@@ -277,27 +354,43 @@ public class IntentIntegrator {
|
||||
* @return null if the event handled here was not related to this class, or
|
||||
* else an {@link IntentResult} containing the result of the scan. If the user cancelled scanning,
|
||||
* the fields will be null.
|
||||
* @deprecated Not compatible with setRequestCode(). Use parseActivityResult(resultCode, intent) instead.
|
||||
*/
|
||||
public static IntentResult parseActivityResult(int requestCode, int resultCode, Intent intent) {
|
||||
if (requestCode == REQUEST_CODE) {
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
String contents = intent.getStringExtra(Intents.Scan.RESULT);
|
||||
String formatName = intent.getStringExtra(Intents.Scan.RESULT_FORMAT);
|
||||
byte[] rawBytes = intent.getByteArrayExtra(Intents.Scan.RESULT_BYTES);
|
||||
int intentOrientation = intent.getIntExtra(Intents.Scan.RESULT_ORIENTATION, Integer.MIN_VALUE);
|
||||
Integer orientation = intentOrientation == Integer.MIN_VALUE ? null : intentOrientation;
|
||||
String errorCorrectionLevel = intent.getStringExtra(Intents.Scan.RESULT_ERROR_CORRECTION_LEVEL);
|
||||
return new IntentResult(contents,
|
||||
formatName,
|
||||
rawBytes,
|
||||
orientation,
|
||||
errorCorrectionLevel);
|
||||
}
|
||||
return new IntentResult();
|
||||
return parseActivityResult(resultCode, intent);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse activity result, without checking the request code.
|
||||
*
|
||||
* @param resultCode result code from {@code onActivityResult()}
|
||||
* @param intent {@link Intent} from {@code onActivityResult()}
|
||||
* @return an {@link IntentResult} containing the result of the scan. If the user cancelled scanning,
|
||||
* the fields will be null.
|
||||
*/
|
||||
public static IntentResult parseActivityResult(int resultCode, Intent intent) {
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
String contents = intent.getStringExtra(Intents.Scan.RESULT);
|
||||
String formatName = intent.getStringExtra(Intents.Scan.RESULT_FORMAT);
|
||||
byte[] rawBytes = intent.getByteArrayExtra(Intents.Scan.RESULT_BYTES);
|
||||
int intentOrientation = intent.getIntExtra(Intents.Scan.RESULT_ORIENTATION, Integer.MIN_VALUE);
|
||||
Integer orientation = intentOrientation == Integer.MIN_VALUE ? null : intentOrientation;
|
||||
String errorCorrectionLevel = intent.getStringExtra(Intents.Scan.RESULT_ERROR_CORRECTION_LEVEL);
|
||||
String barcodeImagePath = intent.getStringExtra(Intents.Scan.RESULT_BARCODE_IMAGE_PATH);
|
||||
return new IntentResult(contents,
|
||||
formatName,
|
||||
rawBytes,
|
||||
orientation,
|
||||
errorCorrectionLevel,
|
||||
barcodeImagePath,
|
||||
intent);
|
||||
}
|
||||
return new IntentResult(intent);
|
||||
}
|
||||
|
||||
private static List<String> list(String... values) {
|
||||
return Collections.unmodifiableList(Arrays.asList(values));
|
||||
}
|
||||
@@ -319,10 +412,21 @@ public class IntentIntegrator {
|
||||
intent.putExtra(key, (Float) value);
|
||||
} else if (value instanceof Bundle) {
|
||||
intent.putExtra(key, (Bundle) value);
|
||||
} else if (value instanceof int[]) {
|
||||
intent.putExtra(key, (int[]) value);
|
||||
} else if (value instanceof long[]) {
|
||||
intent.putExtra(key, (long[]) value);
|
||||
} else if (value instanceof boolean[]) {
|
||||
intent.putExtra(key, (boolean[]) value);
|
||||
} else if (value instanceof double[]) {
|
||||
intent.putExtra(key, (double[]) value);
|
||||
} else if (value instanceof float[]) {
|
||||
intent.putExtra(key, (float[]) value);
|
||||
} else if (value instanceof String[]) {
|
||||
intent.putExtra(key, (String[]) value);
|
||||
} else {
|
||||
intent.putExtra(key, value.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package com.google.zxing.integration.android;
|
||||
|
||||
import android.content.Intent;
|
||||
|
||||
/**
|
||||
* <p>Encapsulates the result of a barcode scan invoked through {@link IntentIntegrator}.</p>
|
||||
*
|
||||
@@ -28,21 +30,31 @@ public final class IntentResult {
|
||||
private final byte[] rawBytes;
|
||||
private final Integer orientation;
|
||||
private final String errorCorrectionLevel;
|
||||
private final String barcodeImagePath;
|
||||
private final Intent originalIntent;
|
||||
|
||||
IntentResult() {
|
||||
this(null, null, null, null, null);
|
||||
this(null, null, null, null, null, null, null);
|
||||
}
|
||||
|
||||
IntentResult(Intent intent) {
|
||||
this(null, null, null, null, null, null, intent);
|
||||
}
|
||||
|
||||
IntentResult(String contents,
|
||||
String formatName,
|
||||
byte[] rawBytes,
|
||||
Integer orientation,
|
||||
String errorCorrectionLevel) {
|
||||
String errorCorrectionLevel,
|
||||
String barcodeImagePath,
|
||||
Intent originalIntent) {
|
||||
this.contents = contents;
|
||||
this.formatName = formatName;
|
||||
this.rawBytes = rawBytes;
|
||||
this.orientation = orientation;
|
||||
this.errorCorrectionLevel = errorCorrectionLevel;
|
||||
this.barcodeImagePath = barcodeImagePath;
|
||||
this.originalIntent = originalIntent;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,16 +92,29 @@ public final class IntentResult {
|
||||
return errorCorrectionLevel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder dialogText = new StringBuilder(100);
|
||||
dialogText.append("Format: ").append(formatName).append('\n');
|
||||
dialogText.append("Contents: ").append(contents).append('\n');
|
||||
int rawBytesLength = rawBytes == null ? 0 : rawBytes.length;
|
||||
dialogText.append("Raw bytes: (").append(rawBytesLength).append(" bytes)\n");
|
||||
dialogText.append("Orientation: ").append(orientation).append('\n');
|
||||
dialogText.append("EC level: ").append(errorCorrectionLevel).append('\n');
|
||||
return dialogText.toString();
|
||||
/**
|
||||
* @return path to a temporary file containing the barcode image, if applicable, or null otherwise
|
||||
*/
|
||||
public String getBarcodeImagePath() {
|
||||
return barcodeImagePath;
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* @return the original intent
|
||||
*/
|
||||
public Intent getOriginalIntent() {
|
||||
return originalIntent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
int rawBytesLength = rawBytes == null ? 0 : rawBytes.length;
|
||||
return "Format: " + formatName + '\n' +
|
||||
"Contents: " + contents + '\n' +
|
||||
"Raw bytes: (" + rawBytesLength + " bytes)\n" +
|
||||
"Orientation: " + orientation + '\n' +
|
||||
"EC level: " + errorCorrectionLevel + '\n' +
|
||||
"Barcode image: " + barcodeImagePath + '\n' +
|
||||
"Original intent: " + originalIntent + '\n';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,10 @@ public interface BarcodeCallback {
|
||||
*
|
||||
* Do not depend on this being called at any specific point in the decode cycle.
|
||||
*
|
||||
* This is a default method and can be omitted by the implementing class.
|
||||
*
|
||||
* @param resultPoints points potentially identifying a barcode
|
||||
*/
|
||||
void possibleResultPoints(List<ResultPoint> resultPoints);
|
||||
default void possibleResultPoints(List<ResultPoint> resultPoints) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package com.journeyapps.barcodescanner;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
import com.google.zxing.EncodeHintType;
|
||||
import com.google.zxing.MultiFormatWriter;
|
||||
import com.google.zxing.WriterException;
|
||||
import com.google.zxing.common.BitMatrix;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Helper class for encoding barcodes as a Bitmap.
|
||||
*
|
||||
* Adapted from QRCodeEncoder, from the zxing project:
|
||||
* https://github.com/zxing/zxing
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0.
|
||||
*/
|
||||
public class BarcodeEncoder {
|
||||
private int bgColor = 0xFFFFFFFF;
|
||||
private int fgColor = 0xFF000000;
|
||||
|
||||
|
||||
public BarcodeEncoder() {
|
||||
}
|
||||
|
||||
public void setBackgroundColor(int bgColor) {
|
||||
this.bgColor = bgColor;
|
||||
}
|
||||
|
||||
public void setForegroundColor(int fgColor) {
|
||||
this.fgColor = fgColor;
|
||||
}
|
||||
|
||||
public Bitmap createBitmap(BitMatrix matrix) {
|
||||
int width = matrix.getWidth();
|
||||
int height = matrix.getHeight();
|
||||
int[] pixels = new int[width * height];
|
||||
for (int y = 0; y < height; y++) {
|
||||
int offset = y * width;
|
||||
for (int x = 0; x < width; x++) {
|
||||
pixels[offset + x] = matrix.get(x, y) ? fgColor : bgColor;
|
||||
}
|
||||
}
|
||||
|
||||
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
||||
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height) throws WriterException {
|
||||
try {
|
||||
return new MultiFormatWriter().encode(contents, format, width, height);
|
||||
} catch (WriterException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
// ZXing sometimes throws an IllegalArgumentException
|
||||
throw new WriterException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) throws WriterException {
|
||||
try {
|
||||
return new MultiFormatWriter().encode(contents, format, width, height, hints);
|
||||
} catch (WriterException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new WriterException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public Bitmap encodeBitmap(String contents, BarcodeFormat format, int width, int height) throws WriterException {
|
||||
return createBitmap(encode(contents, format, width, height));
|
||||
}
|
||||
|
||||
public Bitmap encodeBitmap(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) throws WriterException {
|
||||
return createBitmap(encode(contents, format, width, height, hints));
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,10 @@ import com.google.zxing.Result;
|
||||
import com.google.zxing.ResultMetadataType;
|
||||
import com.google.zxing.ResultPoint;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -54,7 +58,14 @@ public class BarcodeResult {
|
||||
* @see #getBitmapWithResultPoints(int)
|
||||
*/
|
||||
public Bitmap getBitmap() {
|
||||
return sourceData.getBitmap(mScaleFactor);
|
||||
return sourceData.getBitmap(null, mScaleFactor);
|
||||
}
|
||||
|
||||
public List<ResultPoint> getTransformedResultPoints() {
|
||||
if (this.mResult.getResultPoints() == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return transformResultPoints(Arrays.asList(this.mResult.getResultPoints()), this.sourceData);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -64,23 +75,23 @@ public class BarcodeResult {
|
||||
public Bitmap getBitmapWithResultPoints(int color) {
|
||||
Bitmap bitmap = getBitmap();
|
||||
Bitmap barcode = bitmap;
|
||||
ResultPoint[] points = mResult.getResultPoints();
|
||||
List<ResultPoint> points = getTransformedResultPoints();
|
||||
|
||||
if (points != null && points.length > 0 && bitmap != null) {
|
||||
if (!points.isEmpty() && bitmap != null) {
|
||||
barcode = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(barcode);
|
||||
canvas.drawBitmap(bitmap, 0, 0, null);
|
||||
Paint paint = new Paint();
|
||||
paint.setColor(color);
|
||||
if (points.length == 2) {
|
||||
if (points.size() == 2) {
|
||||
paint.setStrokeWidth(PREVIEW_LINE_WIDTH);
|
||||
drawLine(canvas, paint, points[0], points[1], mScaleFactor);
|
||||
} else if (points.length == 4 &&
|
||||
drawLine(canvas, paint, points.get(0), points.get(1), mScaleFactor);
|
||||
} else if (points.size() == 4 &&
|
||||
(mResult.getBarcodeFormat() == BarcodeFormat.UPC_A ||
|
||||
mResult.getBarcodeFormat() == BarcodeFormat.EAN_13)) {
|
||||
// Hacky special case -- draw two lines, for the barcode and metadata
|
||||
drawLine(canvas, paint, points[0], points[1], mScaleFactor);
|
||||
drawLine(canvas, paint, points[2], points[3], mScaleFactor);
|
||||
drawLine(canvas, paint, points.get(0), points.get(1), mScaleFactor);
|
||||
drawLine(canvas, paint, points.get(2), points.get(3), mScaleFactor);
|
||||
} else {
|
||||
paint.setStrokeWidth(PREVIEW_DOT_WIDTH);
|
||||
for (ResultPoint point : points) {
|
||||
@@ -153,4 +164,13 @@ public class BarcodeResult {
|
||||
public String toString() {
|
||||
return mResult.getText();
|
||||
}
|
||||
|
||||
|
||||
public static List<ResultPoint> transformResultPoints(List<ResultPoint> resultPoints, SourceData sourceData) {
|
||||
List<ResultPoint> scaledPoints = new ArrayList<>(resultPoints.size());
|
||||
for (ResultPoint point : resultPoints) {
|
||||
scaledPoints.add(sourceData.translateResultPoint(point));
|
||||
}
|
||||
return scaledPoints;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,6 +60,7 @@ public class BarcodeView extends CameraPreview {
|
||||
// Failed. Next preview is automatically tried.
|
||||
return true;
|
||||
} else if (message.what == R.id.zxing_possible_result_points) {
|
||||
//noinspection unchecked
|
||||
List<ResultPoint> resultPoints = (List<ResultPoint>) message.obj;
|
||||
if (callback != null && decodeMode != DecodeMode.NONE) {
|
||||
callback.possibleResultPoints(resultPoints);
|
||||
@@ -86,13 +87,11 @@ public class BarcodeView extends CameraPreview {
|
||||
initialize();
|
||||
}
|
||||
|
||||
|
||||
private void initialize() {
|
||||
decoderFactory = new DefaultDecoderFactory();
|
||||
resultHandler = new Handler(resultCallback);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the DecoderFactory to use. Use this to specify the formats to decode.
|
||||
*
|
||||
@@ -143,7 +142,6 @@ public class BarcodeView extends CameraPreview {
|
||||
startDecoderThread();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Continuously decode barcodes. The same barcode may be returned multiple times per second.
|
||||
*
|
||||
@@ -196,7 +194,6 @@ public class BarcodeView extends CameraPreview {
|
||||
decoderThread = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops the live preview and decoding.
|
||||
*
|
||||
|
||||
@@ -1,24 +1,36 @@
|
||||
package com.journeyapps.barcodescanner;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.annotation.TargetApi;
|
||||
import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Matrix;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.SurfaceTexture;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.os.Parcelable;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Log;
|
||||
import android.view.SurfaceHolder;
|
||||
import android.view.SurfaceView;
|
||||
import android.view.TextureView;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.WindowManager;
|
||||
|
||||
import com.google.zxing.client.android.R;
|
||||
import com.journeyapps.barcodescanner.camera.CameraInstance;
|
||||
import com.journeyapps.barcodescanner.camera.CameraParametersCallback;
|
||||
import com.journeyapps.barcodescanner.camera.CameraSettings;
|
||||
import com.journeyapps.barcodescanner.camera.CameraSurface;
|
||||
import com.journeyapps.barcodescanner.camera.CenterCropStrategy;
|
||||
import com.journeyapps.barcodescanner.camera.FitCenterStrategy;
|
||||
import com.journeyapps.barcodescanner.camera.DisplayConfiguration;
|
||||
import com.journeyapps.barcodescanner.camera.FitXYStrategy;
|
||||
import com.journeyapps.barcodescanner.camera.PreviewScalingStrategy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -68,6 +80,11 @@ public class CameraPreview extends ViewGroup {
|
||||
* @param error the error
|
||||
*/
|
||||
void cameraError(Exception error);
|
||||
|
||||
/**
|
||||
* The camera has been closed.
|
||||
*/
|
||||
void cameraClosed();
|
||||
}
|
||||
|
||||
private static final String TAG = CameraPreview.class.getSimpleName();
|
||||
@@ -78,11 +95,19 @@ public class CameraPreview extends ViewGroup {
|
||||
|
||||
private Handler stateHandler;
|
||||
|
||||
private boolean useTextureView = false;
|
||||
|
||||
private SurfaceView surfaceView;
|
||||
private TextureView textureView;
|
||||
|
||||
private boolean previewActive = false;
|
||||
|
||||
private RotationListener rotationListener;
|
||||
private int openedOrientation = -1;
|
||||
|
||||
// Delay after rotation change is detected before we reorientate ourselves.
|
||||
// This is to avoid double-reinitialization when the Activity is destroyed and recreated.
|
||||
private static final int ROTATION_LISTENER_DELAY_MS = 250;
|
||||
|
||||
private List<StateListener> stateListeners = new ArrayList<>();
|
||||
|
||||
@@ -107,6 +132,43 @@ public class CameraPreview extends ViewGroup {
|
||||
// Framing rectangle relative to the preview resolution
|
||||
private Rect previewFramingRect = null;
|
||||
|
||||
// Size of the framing rectangle. If null, defaults to using a margin percentage.
|
||||
private Size framingRectSize = null;
|
||||
|
||||
// Fraction of the width / heigth to use as a margin. This fraction is used on each size, so
|
||||
// must be smaller than 0.5;
|
||||
private double marginFraction = 0.1d;
|
||||
|
||||
private PreviewScalingStrategy previewScalingStrategy = null;
|
||||
|
||||
private boolean torchOn = false;
|
||||
|
||||
@TargetApi(14)
|
||||
private TextureView.SurfaceTextureListener surfaceTextureListener() {
|
||||
// Cannot initialize automatically, since we may be API < 14
|
||||
return new TextureView.SurfaceTextureListener() {
|
||||
@Override
|
||||
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
|
||||
onSurfaceTextureSizeChanged(surface, width, height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
|
||||
currentSurfaceSize = new Size(width, height);
|
||||
startPreviewIfReady();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private final SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {
|
||||
|
||||
@@ -135,6 +197,8 @@ public class CameraPreview extends ViewGroup {
|
||||
@Override
|
||||
public boolean handleMessage(Message message) {
|
||||
if (message.what == R.id.zxing_prewiew_size_ready) {
|
||||
// At this point, we have the camera preview size, and should have containerSize and
|
||||
// surfaceRect.
|
||||
previewSized((Size) message.obj);
|
||||
return true;
|
||||
} else if (message.what == R.id.zxing_camera_error) {
|
||||
@@ -145,6 +209,8 @@ public class CameraPreview extends ViewGroup {
|
||||
pause();
|
||||
fireState.cameraError(error);
|
||||
}
|
||||
} else if (message.what == R.id.zxing_camera_closed) {
|
||||
fireState.cameraClosed();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -154,12 +220,7 @@ public class CameraPreview extends ViewGroup {
|
||||
@Override
|
||||
public void onRotationChanged(int rotation) {
|
||||
// Make sure this is run on the main thread.
|
||||
stateHandler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
rotationChanged();
|
||||
}
|
||||
});
|
||||
stateHandler.postDelayed(() -> rotationChanged(), ROTATION_LISTENER_DELAY_MS);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -178,34 +239,76 @@ public class CameraPreview extends ViewGroup {
|
||||
initialize(context, attrs, defStyleAttr, 0);
|
||||
}
|
||||
|
||||
|
||||
private void initialize(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
|
||||
if (getBackground() == null) {
|
||||
// Default to SurfaceView colour, so that there are less changes.
|
||||
setBackgroundColor(Color.BLACK);
|
||||
}
|
||||
|
||||
initializeAttributes(attrs);
|
||||
|
||||
windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
|
||||
|
||||
stateHandler = new Handler(stateCallback);
|
||||
|
||||
setupSurfaceView();
|
||||
|
||||
rotationListener = new RotationListener();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onAttachedToWindow() {
|
||||
super.onAttachedToWindow();
|
||||
|
||||
setupSurfaceView();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize from XML attributes.
|
||||
*
|
||||
* @param attrs the attributes
|
||||
*/
|
||||
public void initializeAttributes(AttributeSet attrs) {
|
||||
TypedArray styledAttributes = getContext().obtainStyledAttributes(attrs, R.styleable.zxing_camera_preview);
|
||||
|
||||
int framingRectWidth = (int) styledAttributes.getDimension(R.styleable.zxing_camera_preview_zxing_framing_rect_width, -1);
|
||||
int framingRectHeight = (int) styledAttributes.getDimension(R.styleable.zxing_camera_preview_zxing_framing_rect_height, -1);
|
||||
|
||||
if (framingRectWidth > 0 && framingRectHeight > 0) {
|
||||
this.framingRectSize = new Size(framingRectWidth, framingRectHeight);
|
||||
}
|
||||
|
||||
this.useTextureView = styledAttributes.getBoolean(R.styleable.zxing_camera_preview_zxing_use_texture_view, true);
|
||||
|
||||
// See zxing_attrs.xml for the enum values
|
||||
int scalingStrategyNumber = styledAttributes.getInteger(R.styleable.zxing_camera_preview_zxing_preview_scaling_strategy, -1);
|
||||
if (scalingStrategyNumber == 1) {
|
||||
previewScalingStrategy = new CenterCropStrategy();
|
||||
} else if (scalingStrategyNumber == 2) {
|
||||
previewScalingStrategy = new FitCenterStrategy();
|
||||
} else if (scalingStrategyNumber == 3) {
|
||||
previewScalingStrategy = new FitXYStrategy();
|
||||
}
|
||||
|
||||
styledAttributes.recycle();
|
||||
}
|
||||
|
||||
private void rotationChanged() {
|
||||
pause();
|
||||
resume();
|
||||
// Confirm that it did actually change
|
||||
if (isActive() && getDisplayRotation() != openedOrientation) {
|
||||
pause();
|
||||
resume();
|
||||
}
|
||||
}
|
||||
|
||||
private void setupSurfaceView() {
|
||||
surfaceView = new SurfaceView(getContext());
|
||||
if (Build.VERSION.SDK_INT < 11) {
|
||||
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
|
||||
if (useTextureView) {
|
||||
textureView = new TextureView(getContext());
|
||||
textureView.setSurfaceTextureListener(surfaceTextureListener());
|
||||
addView(textureView);
|
||||
} else {
|
||||
surfaceView = new SurfaceView(getContext());
|
||||
surfaceView.getHolder().addCallback(surfaceCallback);
|
||||
addView(surfaceView);
|
||||
}
|
||||
surfaceView.getHolder().addCallback(surfaceCallback);
|
||||
addView(surfaceView);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -246,6 +349,13 @@ public class CameraPreview extends ViewGroup {
|
||||
listener.cameraError(error);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cameraClosed() {
|
||||
for (StateListener listener : stateListeners) {
|
||||
listener.cameraClosed();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private void calculateFrames() {
|
||||
@@ -262,7 +372,13 @@ public class CameraPreview extends ViewGroup {
|
||||
int width = containerSize.width;
|
||||
int height = containerSize.height;
|
||||
|
||||
surfaceRect = displayConfiguration.scalePreview(previewSize);
|
||||
Rect scaledPreview = displayConfiguration.scalePreview(previewSize);
|
||||
if (scaledPreview.width() <= 0 || scaledPreview.height() <= 0) {
|
||||
// Something is not ready yet - we can't start the preview.
|
||||
return;
|
||||
}
|
||||
|
||||
surfaceRect = scaledPreview;
|
||||
|
||||
Rect container = new Rect(0, 0, width, height);
|
||||
framingRect = calculateFramingRect(container, surfaceRect);
|
||||
@@ -274,7 +390,7 @@ public class CameraPreview extends ViewGroup {
|
||||
frameInPreview.right * previewWidth / surfaceRect.width(),
|
||||
frameInPreview.bottom * previewHeight / surfaceRect.height());
|
||||
|
||||
if (previewFramingRect.width() <= 0 || previewFramingRect.height() <= 0) {
|
||||
if (previewFramingRect == null || previewFramingRect.width() <= 0 || previewFramingRect.height() <= 0) {
|
||||
previewFramingRect = null;
|
||||
framingRect = null;
|
||||
Log.w(TAG, "Preview frame is too small");
|
||||
@@ -289,22 +405,67 @@ public class CameraPreview extends ViewGroup {
|
||||
* @param on true to turn on the torch
|
||||
*/
|
||||
public void setTorch(boolean on) {
|
||||
torchOn = on;
|
||||
if (cameraInstance != null) {
|
||||
cameraInstance.setTorch(on);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the settings for Camera.
|
||||
* Must be called after {@link #resume()}.
|
||||
*
|
||||
* @param callback {@link CameraParametersCallback}
|
||||
*/
|
||||
public void changeCameraParameters(CameraParametersCallback callback) {
|
||||
if (cameraInstance != null) {
|
||||
cameraInstance.changeCameraParameters(callback);
|
||||
}
|
||||
}
|
||||
|
||||
private void containerSized(Size containerSize) {
|
||||
this.containerSize = containerSize;
|
||||
if (cameraInstance != null) {
|
||||
if (cameraInstance.getDisplayConfiguration() == null) {
|
||||
displayConfiguration = new DisplayConfiguration(getDisplayRotation(), containerSize);
|
||||
displayConfiguration.setPreviewScalingStrategy(getPreviewScalingStrategy());
|
||||
cameraInstance.setDisplayConfiguration(displayConfiguration);
|
||||
cameraInstance.configureCamera();
|
||||
if (torchOn) {
|
||||
cameraInstance.setTorch(torchOn);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the preview scaling strategy.
|
||||
*
|
||||
* @param previewScalingStrategy null for the default
|
||||
*/
|
||||
public void setPreviewScalingStrategy(PreviewScalingStrategy previewScalingStrategy) {
|
||||
this.previewScalingStrategy = previewScalingStrategy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override this to specify a different preview scaling strategy.
|
||||
*/
|
||||
public PreviewScalingStrategy getPreviewScalingStrategy() {
|
||||
if (previewScalingStrategy != null) {
|
||||
return previewScalingStrategy;
|
||||
}
|
||||
|
||||
// If we are using SurfaceTexture, it is safe to use centerCrop.
|
||||
// For SurfaceView, it's better to use fitCenter, otherwise the preview may overlap to
|
||||
// other views.
|
||||
if (textureView != null) {
|
||||
return new CenterCropStrategy();
|
||||
} else {
|
||||
return new FitCenterStrategy();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void previewSized(Size size) {
|
||||
this.previewSize = size;
|
||||
if (containerSize != null) {
|
||||
@@ -314,10 +475,59 @@ public class CameraPreview extends ViewGroup {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate transformation for the TextureView.
|
||||
*
|
||||
* An identity matrix would cause the preview to be scaled up/down to fill the TextureView.
|
||||
*
|
||||
* @param textureSize the size of the textureView
|
||||
* @param previewSize the camera preview resolution
|
||||
* @return the transform matrix for the TextureView
|
||||
*/
|
||||
protected Matrix calculateTextureTransform(Size textureSize, Size previewSize) {
|
||||
float ratioTexture = (float) textureSize.width / (float) textureSize.height;
|
||||
float ratioPreview = (float) previewSize.width / (float) previewSize.height;
|
||||
|
||||
float scaleX;
|
||||
float scaleY;
|
||||
|
||||
// We scale so that either width or height fits exactly in the TextureView, and the other
|
||||
// is bigger (cropped).
|
||||
if (ratioTexture < ratioPreview) {
|
||||
scaleX = ratioPreview / ratioTexture;
|
||||
scaleY = 1;
|
||||
} else {
|
||||
scaleX = 1;
|
||||
scaleY = ratioTexture / ratioPreview;
|
||||
}
|
||||
|
||||
Matrix matrix = new Matrix();
|
||||
|
||||
matrix.setScale(scaleX, scaleY);
|
||||
|
||||
// Center the preview
|
||||
float scaledWidth = textureSize.width * scaleX;
|
||||
float scaledHeight = textureSize.height * scaleY;
|
||||
float dx = (textureSize.width - scaledWidth) / 2;
|
||||
float dy = (textureSize.height - scaledHeight) / 2;
|
||||
|
||||
// Perform the translation on the scaled preview
|
||||
matrix.postTranslate(dx, dy);
|
||||
|
||||
return matrix;
|
||||
}
|
||||
|
||||
private void startPreviewIfReady() {
|
||||
if (currentSurfaceSize != null && previewSize != null && surfaceRect != null) {
|
||||
if (currentSurfaceSize.equals(new Size(surfaceRect.width(), surfaceRect.height()))) {
|
||||
startCameraPreview(surfaceView.getHolder());
|
||||
if (surfaceView != null && currentSurfaceSize.equals(new Size(surfaceRect.width(), surfaceRect.height()))) {
|
||||
startCameraPreview(new CameraSurface(surfaceView.getHolder()));
|
||||
} else if (textureView != null && textureView.getSurfaceTexture() != null) {
|
||||
if (previewSize != null) {
|
||||
Matrix transform = calculateTextureTransform(new Size(textureView.getWidth(), textureView.getHeight()), previewSize);
|
||||
textureView.setTransform(transform);
|
||||
}
|
||||
|
||||
startCameraPreview(new CameraSurface(textureView.getSurfaceTexture()));
|
||||
} else {
|
||||
// Surface is not the correct size yet
|
||||
}
|
||||
@@ -329,12 +539,16 @@ public class CameraPreview extends ViewGroup {
|
||||
protected void onLayout(boolean changed, int l, int t, int r, int b) {
|
||||
containerSized(new Size(r - l, b - t));
|
||||
|
||||
if (surfaceRect == null) {
|
||||
// Match the container, to reduce the risk of issues. The preview should never be drawn
|
||||
// while the surface has this size.
|
||||
surfaceView.layout(0, 0, getWidth(), getHeight());
|
||||
} else {
|
||||
surfaceView.layout(surfaceRect.left, surfaceRect.top, surfaceRect.right, surfaceRect.bottom);
|
||||
if (surfaceView != null) {
|
||||
if (surfaceRect == null) {
|
||||
// Match the container, to reduce the risk of issues. The preview should never be drawn
|
||||
// while the surface has this size.
|
||||
surfaceView.layout(0, 0, getWidth(), getHeight());
|
||||
} else {
|
||||
surfaceView.layout(surfaceRect.left, surfaceRect.top, surfaceRect.right, surfaceRect.bottom);
|
||||
}
|
||||
} else if (textureView != null) {
|
||||
textureView.layout(0, 0, getWidth(), getHeight());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -362,6 +576,10 @@ public class CameraPreview extends ViewGroup {
|
||||
return previewFramingRect;
|
||||
}
|
||||
|
||||
public Size getPreviewSize() {
|
||||
return previewSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the CameraSettings currently in use
|
||||
*/
|
||||
@@ -399,9 +617,15 @@ public class CameraPreview extends ViewGroup {
|
||||
// The activity was paused but not stopped, so the surface still exists. Therefore
|
||||
// surfaceCreated() won't be called, so init the camera here.
|
||||
startPreviewIfReady();
|
||||
} else {
|
||||
} else if (surfaceView != null) {
|
||||
// Install the callback and wait for surfaceCreated() to init the camera.
|
||||
surfaceView.getHolder().addCallback(surfaceCallback);
|
||||
} else if (textureView != null) {
|
||||
if (textureView.isAvailable()) {
|
||||
surfaceTextureListener().onSurfaceTextureAvailable(textureView.getSurfaceTexture(), textureView.getWidth(), textureView.getHeight());
|
||||
} else {
|
||||
textureView.setSurfaceTextureListener(surfaceTextureListener());
|
||||
}
|
||||
}
|
||||
|
||||
// To trigger surfaceSized again
|
||||
@@ -409,7 +633,6 @@ public class CameraPreview extends ViewGroup {
|
||||
rotationListener.listen(getContext(), rotationCallback);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Pause scanning and the camera preview. Typically this should be called from the Activity's
|
||||
* onPause() method.
|
||||
@@ -421,15 +644,21 @@ public class CameraPreview extends ViewGroup {
|
||||
Util.validateMainThread();
|
||||
Log.d(TAG, "pause()");
|
||||
|
||||
openedOrientation = -1;
|
||||
if (cameraInstance != null) {
|
||||
cameraInstance.close();
|
||||
cameraInstance = null;
|
||||
previewActive = false;
|
||||
} else {
|
||||
stateHandler.sendEmptyMessage(R.id.zxing_camera_closed);
|
||||
}
|
||||
if (currentSurfaceSize == null) {
|
||||
if (currentSurfaceSize == null && surfaceView != null) {
|
||||
SurfaceHolder surfaceHolder = surfaceView.getHolder();
|
||||
surfaceHolder.removeCallback(surfaceCallback);
|
||||
}
|
||||
if (currentSurfaceSize == null && textureView != null) {
|
||||
textureView.setSurfaceTextureListener(null);
|
||||
}
|
||||
|
||||
this.containerSize = null;
|
||||
this.previewSize = null;
|
||||
@@ -439,6 +668,73 @@ public class CameraPreview extends ViewGroup {
|
||||
fireState.previewStopped();
|
||||
}
|
||||
|
||||
/**
|
||||
* Pause scanning and preview; waiting for the Camera to be closed.
|
||||
*
|
||||
* This blocks the main thread.
|
||||
*/
|
||||
public void pauseAndWait() {
|
||||
CameraInstance instance = getCameraInstance();
|
||||
pause();
|
||||
long startTime = System.nanoTime();
|
||||
while(instance != null && !instance.isCameraClosed()) {
|
||||
if (System.nanoTime() - startTime > 2000000000) {
|
||||
// Don't wait for longer than 2 seconds
|
||||
break;
|
||||
}
|
||||
try {
|
||||
Thread.sleep(1);
|
||||
} catch (InterruptedException e) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Size getFramingRectSize() {
|
||||
return framingRectSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an exact size for the framing rectangle. It will be centered in the view.
|
||||
*
|
||||
* @param framingRectSize the size
|
||||
*/
|
||||
public void setFramingRectSize(Size framingRectSize) {
|
||||
this.framingRectSize = framingRectSize;
|
||||
}
|
||||
|
||||
public double getMarginFraction() {
|
||||
return marginFraction;
|
||||
}
|
||||
|
||||
/**
|
||||
* The the fraction of the width/height of view to be used as a margin for the framing rect.
|
||||
* This is ignored if framingRectSize is specified.
|
||||
*
|
||||
* @param marginFraction the fraction
|
||||
*/
|
||||
public void setMarginFraction(double marginFraction) {
|
||||
if (marginFraction >= 0.5d) {
|
||||
throw new IllegalArgumentException("The margin fraction must be less than 0.5");
|
||||
}
|
||||
this.marginFraction = marginFraction;
|
||||
}
|
||||
|
||||
public boolean isUseTextureView() {
|
||||
return useTextureView;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set to true to use TextureView instead of SurfaceView.
|
||||
*
|
||||
* Will only have an effect on API >= 14.
|
||||
*
|
||||
* @param useTextureView true to use TextureView.
|
||||
*/
|
||||
public void setUseTextureView(boolean useTextureView) {
|
||||
this.useTextureView = useTextureView;
|
||||
}
|
||||
|
||||
/**
|
||||
* Considered active if between resume() and pause().
|
||||
*
|
||||
@@ -458,18 +754,33 @@ public class CameraPreview extends ViewGroup {
|
||||
return;
|
||||
}
|
||||
|
||||
cameraInstance = new CameraInstance(getContext());
|
||||
cameraInstance.setCameraSettings(cameraSettings);
|
||||
cameraInstance = createCameraInstance();
|
||||
|
||||
cameraInstance.setReadyHandler(stateHandler);
|
||||
cameraInstance.open();
|
||||
|
||||
// Keep track of the orientation we opened at, so that we don't reopen the camera if we
|
||||
// don't need to.
|
||||
openedOrientation = getDisplayRotation();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new CameraInstance.
|
||||
*
|
||||
* Override to use a custom CameraInstance.
|
||||
*
|
||||
* @return a new CameraInstance
|
||||
*/
|
||||
protected CameraInstance createCameraInstance() {
|
||||
CameraInstance cameraInstance = new CameraInstance(getContext());
|
||||
cameraInstance.setCameraSettings(cameraSettings);
|
||||
return cameraInstance;
|
||||
}
|
||||
|
||||
private void startCameraPreview(SurfaceHolder holder) {
|
||||
if (!previewActive) {
|
||||
private void startCameraPreview(CameraSurface surface) {
|
||||
if (!previewActive && cameraInstance != null) {
|
||||
Log.i(TAG, "Starting preview");
|
||||
cameraInstance.setSurfaceHolder(holder);
|
||||
cameraInstance.setSurface(surface);
|
||||
cameraInstance.startPreview();
|
||||
previewActive = true;
|
||||
|
||||
@@ -508,20 +819,31 @@ public class CameraPreview extends ViewGroup {
|
||||
return previewActive;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Calculate framing rectangle, relative to the preview frame.
|
||||
*
|
||||
* Note that the SurfaceView may be larger than the container.
|
||||
*
|
||||
* Override this for more control over the framing rect calculations.
|
||||
*
|
||||
* @param container this container, with left = top = 0
|
||||
* @param surface the SurfaceView, relative to this container
|
||||
* @return the framing rect, relative to this container
|
||||
*/
|
||||
protected Rect calculateFramingRect(Rect container, Rect surface) {
|
||||
// intersection is the part of the container that is used for the preview
|
||||
Rect intersection = new Rect(container);
|
||||
intersection.intersect(surface);
|
||||
boolean intersects = intersection.intersect(surface);
|
||||
|
||||
// margin as 10% of the smaller of width, height
|
||||
int margin = Math.min(intersection.width() / 10, intersection.height() / 10);
|
||||
if (framingRectSize != null) {
|
||||
// Specific size is specified. Make sure it's not larger than the container or surface.
|
||||
int horizontalMargin = Math.max(0, (intersection.width() - framingRectSize.width) / 2);
|
||||
int verticalMargin = Math.max(0, (intersection.height() - framingRectSize.height) / 2);
|
||||
intersection.inset(horizontalMargin, verticalMargin);
|
||||
return intersection;
|
||||
}
|
||||
// margin as 10% (default) of the smaller of width, height
|
||||
int margin = (int)Math.min(intersection.width() * marginFraction, intersection.height() * marginFraction);
|
||||
intersection.inset(margin, margin);
|
||||
if (intersection.height() > intersection.width()) {
|
||||
// We don't want a frame that is taller than wide.
|
||||
@@ -529,4 +851,35 @@ public class CameraPreview extends ViewGroup {
|
||||
}
|
||||
return intersection;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Parcelable onSaveInstanceState() {
|
||||
Parcelable superState = super.onSaveInstanceState();
|
||||
|
||||
Bundle myState = new Bundle();
|
||||
myState.putParcelable("super", superState);
|
||||
myState.putBoolean("torch", torchOn);
|
||||
return myState;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onRestoreInstanceState(Parcelable state) {
|
||||
if (!(state instanceof Bundle)) {
|
||||
super.onRestoreInstanceState(state);
|
||||
return;
|
||||
}
|
||||
Bundle myState = (Bundle)state;
|
||||
Parcelable superState = myState.getParcelable("super");
|
||||
super.onRestoreInstanceState(superState);
|
||||
boolean torch = myState.getBoolean("torch");
|
||||
setTorch(torch);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return true if the camera has been closed in a background thread.
|
||||
*/
|
||||
public boolean isCameraClosed() {
|
||||
return cameraInstance == null || cameraInstance.isCameraClosed();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.journeyapps.barcodescanner;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import androidx.annotation.NonNull;
|
||||
import android.view.KeyEvent;
|
||||
|
||||
import com.google.zxing.client.android.R;
|
||||
@@ -11,20 +12,29 @@ import com.google.zxing.client.android.R;
|
||||
*/
|
||||
public class CaptureActivity extends Activity {
|
||||
private CaptureManager capture;
|
||||
private CompoundBarcodeView barcodeScannerView;
|
||||
private DecoratedBarcodeView barcodeScannerView;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
setContentView(R.layout.zxing_capture);
|
||||
barcodeScannerView = (CompoundBarcodeView)findViewById(R.id.zxing_barcode_scanner);
|
||||
barcodeScannerView = initializeContent();
|
||||
|
||||
capture = new CaptureManager(this, barcodeScannerView);
|
||||
capture.initializeFromIntent(getIntent(), savedInstanceState);
|
||||
capture.decode();
|
||||
}
|
||||
|
||||
/**
|
||||
* Override to use a different layout.
|
||||
*
|
||||
* @return the DecoratedBarcodeView
|
||||
*/
|
||||
protected DecoratedBarcodeView initializeContent() {
|
||||
setContentView(R.layout.zxing_capture);
|
||||
return (DecoratedBarcodeView)findViewById(R.id.zxing_barcode_scanner);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
@@ -49,6 +59,11 @@ public class CaptureActivity extends Activity {
|
||||
capture.onSaveInstanceState(outState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
|
||||
capture.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
||||
return barcodeScannerView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
package com.journeyapps.barcodescanner;
|
||||
|
||||
import android.Manifest;
|
||||
import android.annotation.TargetApi;
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.res.Configuration;
|
||||
import android.graphics.Bitmap;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.util.Log;
|
||||
@@ -14,6 +18,9 @@ import android.view.Surface;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
import com.google.zxing.ResultMetadataType;
|
||||
import com.google.zxing.ResultPoint;
|
||||
import com.google.zxing.client.android.BeepManager;
|
||||
@@ -21,6 +28,9 @@ import com.google.zxing.client.android.InactivityTimer;
|
||||
import com.google.zxing.client.android.Intents;
|
||||
import com.google.zxing.client.android.R;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -43,35 +53,33 @@ import java.util.Map;
|
||||
public class CaptureManager {
|
||||
private static final String TAG = CaptureManager.class.getSimpleName();
|
||||
|
||||
private static int cameraPermissionReqCode = 250;
|
||||
|
||||
private Activity activity;
|
||||
private CompoundBarcodeView barcodeView;
|
||||
private DecoratedBarcodeView barcodeView;
|
||||
private int orientationLock = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
|
||||
private static final String SAVED_ORIENTATION_LOCK = "SAVED_ORIENTATION_LOCK";
|
||||
private boolean returnBarcodeImagePath = false;
|
||||
|
||||
private boolean showDialogIfMissingCameraPermission = true;
|
||||
private String missingCameraPermissionDialogMessage = "";
|
||||
|
||||
private boolean destroyed = false;
|
||||
|
||||
// Delay long enough that the beep can be played.
|
||||
// TODO: play beep in background
|
||||
private static final long DELAY_BEEP = 150;
|
||||
|
||||
private InactivityTimer inactivityTimer;
|
||||
private BeepManager beepManager;
|
||||
|
||||
private Handler handler;
|
||||
|
||||
private boolean finishWhenClosed = false;
|
||||
|
||||
private BarcodeCallback callback = new BarcodeCallback() {
|
||||
@Override
|
||||
public void barcodeResult(final BarcodeResult result) {
|
||||
barcodeView.pause();
|
||||
beepManager.playBeepSoundAndVibrate();
|
||||
|
||||
handler.postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
returnResult(result);
|
||||
}
|
||||
}, DELAY_BEEP);
|
||||
|
||||
handler.post(() -> returnResult(result));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -98,23 +106,30 @@ public class CaptureManager {
|
||||
|
||||
@Override
|
||||
public void cameraError(Exception error) {
|
||||
displayFrameworkBugMessageAndExit();
|
||||
displayFrameworkBugMessageAndExit(
|
||||
activity.getString(R.string.zxing_msg_camera_framework_bug)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cameraClosed() {
|
||||
if (finishWhenClosed) {
|
||||
Log.d(TAG, "Camera closed; finishing activity");
|
||||
finish();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public CaptureManager(Activity activity, CompoundBarcodeView barcodeView) {
|
||||
public CaptureManager(Activity activity, DecoratedBarcodeView barcodeView) {
|
||||
this.activity = activity;
|
||||
this.barcodeView = barcodeView;
|
||||
barcodeView.getBarcodeView().addStateListener(stateListener);
|
||||
|
||||
handler = new Handler();
|
||||
|
||||
inactivityTimer = new InactivityTimer(activity, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Log.d(TAG, "Finishing due to inactivity");
|
||||
finish();
|
||||
}
|
||||
inactivityTimer = new InactivityTimer(activity, () -> {
|
||||
Log.d(TAG, "Finishing due to inactivity");
|
||||
finish();
|
||||
});
|
||||
|
||||
beepManager = new BeepManager(activity);
|
||||
@@ -137,14 +152,11 @@ public class CaptureManager {
|
||||
this.orientationLock = savedInstanceState.getInt(SAVED_ORIENTATION_LOCK, ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
|
||||
}
|
||||
|
||||
if(intent != null) {
|
||||
if (orientationLock == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
|
||||
// Only lock the orientation if it's not locked to something else yet
|
||||
boolean orientationLocked = intent.getBooleanExtra(Intents.Scan.ORIENTATION_LOCKED, true);
|
||||
|
||||
if (orientationLocked) {
|
||||
lockOrientation();
|
||||
}
|
||||
if (intent != null) {
|
||||
// Only lock the orientation if it's not locked to something else yet
|
||||
boolean orientationLocked = intent.getBooleanExtra(Intents.Scan.ORIENTATION_LOCKED, true);
|
||||
if (orientationLocked) {
|
||||
lockOrientation();
|
||||
}
|
||||
|
||||
if (Intents.Scan.ACTION.equals(intent.getAction())) {
|
||||
@@ -153,7 +165,21 @@ public class CaptureManager {
|
||||
|
||||
if (!intent.getBooleanExtra(Intents.Scan.BEEP_ENABLED, true)) {
|
||||
beepManager.setBeepEnabled(false);
|
||||
beepManager.updatePrefs();
|
||||
}
|
||||
|
||||
if (intent.hasExtra(Intents.Scan.SHOW_MISSING_CAMERA_PERMISSION_DIALOG)) {
|
||||
setShowMissingCameraPermissionDialog(
|
||||
intent.getBooleanExtra(Intents.Scan.SHOW_MISSING_CAMERA_PERMISSION_DIALOG, true),
|
||||
intent.getStringExtra(Intents.Scan.MISSING_CAMERA_PERMISSION_DIALOG_MESSAGE)
|
||||
);
|
||||
}
|
||||
|
||||
if (intent.hasExtra(Intents.Scan.TIMEOUT)) {
|
||||
handler.postDelayed(this::returnResultTimeout, intent.getLongExtra(Intents.Scan.TIMEOUT, 0L));
|
||||
}
|
||||
|
||||
if (intent.getBooleanExtra(Intents.Scan.BARCODE_IMAGE_ENABLED, false)) {
|
||||
returnBarcodeImagePath = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -200,19 +226,61 @@ public class CaptureManager {
|
||||
* Call from Activity#onResume().
|
||||
*/
|
||||
public void onResume() {
|
||||
barcodeView.resume();
|
||||
beepManager.updatePrefs();
|
||||
if (Build.VERSION.SDK_INT >= 23) {
|
||||
openCameraWithPermission();
|
||||
} else {
|
||||
barcodeView.resume();
|
||||
}
|
||||
inactivityTimer.start();
|
||||
}
|
||||
|
||||
private boolean askedPermission = false;
|
||||
|
||||
@TargetApi(23)
|
||||
private void openCameraWithPermission() {
|
||||
if (ContextCompat.checkSelfPermission(this.activity, Manifest.permission.CAMERA)
|
||||
== PackageManager.PERMISSION_GRANTED) {
|
||||
barcodeView.resume();
|
||||
} else if (!askedPermission) {
|
||||
ActivityCompat.requestPermissions(this.activity,
|
||||
new String[]{Manifest.permission.CAMERA},
|
||||
cameraPermissionReqCode);
|
||||
askedPermission = true;
|
||||
} // else wait for permission result
|
||||
}
|
||||
|
||||
/**
|
||||
* Call from Activity#onRequestPermissionsResult
|
||||
* @param requestCode The request code passed in {@link androidx.core.app.ActivityCompat#requestPermissions(Activity, String[], int)}.
|
||||
* @param permissions The requested permissions.
|
||||
* @param grantResults The grant results for the corresponding permissions
|
||||
* which is either {@link android.content.pm.PackageManager#PERMISSION_GRANTED}
|
||||
* or {@link android.content.pm.PackageManager#PERMISSION_DENIED}. Never null.
|
||||
*/
|
||||
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
|
||||
if (requestCode == cameraPermissionReqCode) {
|
||||
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
// permission was granted
|
||||
barcodeView.resume();
|
||||
} else {
|
||||
setMissingCameraPermissionResult();
|
||||
|
||||
if (showDialogIfMissingCameraPermission) {
|
||||
displayFrameworkBugMessageAndExit(missingCameraPermissionDialogMessage);
|
||||
} else {
|
||||
closeAndFinish();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Call from Activity#onPause().
|
||||
*/
|
||||
public void onPause() {
|
||||
barcodeView.pause();
|
||||
|
||||
inactivityTimer.cancel();
|
||||
beepManager.close();
|
||||
barcodeView.pauseAndWait();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -221,6 +289,7 @@ public class CaptureManager {
|
||||
public void onDestroy() {
|
||||
destroyed = true;
|
||||
inactivityTimer.cancel();
|
||||
handler.removeCallbacksAndMessages(null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -230,14 +299,14 @@ public class CaptureManager {
|
||||
outState.putInt(SAVED_ORIENTATION_LOCK, this.orientationLock);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a intent to return as the Activity result.
|
||||
*
|
||||
* @param rawResult the BarcodeResult, must not be null.
|
||||
* @param barcodeImagePath a path to an exported file of the Barcode Image, can be null.
|
||||
* @return the Intent
|
||||
*/
|
||||
public static Intent resultIntent(BarcodeResult rawResult) {
|
||||
public static Intent resultIntent(BarcodeResult rawResult, String barcodeImagePath) {
|
||||
Intent intent = new Intent(Intents.Scan.ACTION);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
|
||||
intent.putExtra(Intents.Scan.RESULT, rawResult.toString());
|
||||
@@ -270,41 +339,117 @@ public class CaptureManager {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (barcodeImagePath != null) {
|
||||
intent.putExtra(Intents.Scan.RESULT_BARCODE_IMAGE_PATH, barcodeImagePath);
|
||||
}
|
||||
return intent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the barcode image to a temporary file stored in the application's cache, and return its path.
|
||||
* Only does so if returnBarcodeImagePath is enabled.
|
||||
*
|
||||
* @param rawResult the BarcodeResult, must not be null
|
||||
* @return the path or null
|
||||
*/
|
||||
private String getBarcodeImagePath(BarcodeResult rawResult) {
|
||||
String barcodeImagePath = null;
|
||||
if (returnBarcodeImagePath) {
|
||||
Bitmap bmp = rawResult.getBitmap();
|
||||
try {
|
||||
File bitmapFile = File.createTempFile("barcodeimage", ".jpg", activity.getCacheDir());
|
||||
FileOutputStream outputStream = new FileOutputStream(bitmapFile);
|
||||
bmp.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
|
||||
outputStream.close();
|
||||
barcodeImagePath = bitmapFile.getAbsolutePath();
|
||||
} catch (IOException e) {
|
||||
Log.w(TAG, "Unable to create temporary file and store bitmap! " + e);
|
||||
}
|
||||
}
|
||||
return barcodeImagePath;
|
||||
}
|
||||
|
||||
private void finish() {
|
||||
activity.finish();
|
||||
}
|
||||
|
||||
protected void closeAndFinish() {
|
||||
if (barcodeView.getBarcodeView().isCameraClosed()) {
|
||||
finish();
|
||||
} else {
|
||||
finishWhenClosed = true;
|
||||
}
|
||||
|
||||
protected void returnResult(BarcodeResult rawResult) {
|
||||
Intent intent = resultIntent(rawResult);
|
||||
activity.setResult(Activity.RESULT_OK, intent);
|
||||
finish();
|
||||
barcodeView.pause();
|
||||
inactivityTimer.cancel();
|
||||
}
|
||||
|
||||
protected void displayFrameworkBugMessageAndExit() {
|
||||
if (activity.isFinishing() || this.destroyed) {
|
||||
private void setMissingCameraPermissionResult() {
|
||||
Intent intent = new Intent(Intents.Scan.ACTION);
|
||||
intent.putExtra(Intents.Scan.MISSING_CAMERA_PERMISSION, true);
|
||||
activity.setResult(Activity.RESULT_CANCELED, intent);
|
||||
}
|
||||
|
||||
protected void returnResultTimeout() {
|
||||
Intent intent = new Intent(Intents.Scan.ACTION);
|
||||
intent.putExtra(Intents.Scan.TIMEOUT, true);
|
||||
activity.setResult(Activity.RESULT_CANCELED, intent);
|
||||
closeAndFinish();
|
||||
}
|
||||
|
||||
protected void returnResult(BarcodeResult rawResult) {
|
||||
Intent intent = resultIntent(rawResult, getBarcodeImagePath(rawResult));
|
||||
activity.setResult(Activity.RESULT_OK, intent);
|
||||
closeAndFinish();
|
||||
}
|
||||
|
||||
protected void displayFrameworkBugMessageAndExit(String message) {
|
||||
if (activity.isFinishing() || this.destroyed || finishWhenClosed) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (message.isEmpty()) {
|
||||
message = activity.getString(R.string.zxing_msg_camera_framework_bug);
|
||||
}
|
||||
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
|
||||
builder.setTitle(activity.getString(R.string.zxing_app_name));
|
||||
builder.setMessage(activity.getString(R.string.zxing_msg_camera_framework_bug));
|
||||
builder.setPositiveButton(R.string.zxing_button_ok, new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
finish();
|
||||
}
|
||||
});
|
||||
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
|
||||
@Override
|
||||
public void onCancel(DialogInterface dialog) {
|
||||
finish();
|
||||
}
|
||||
});
|
||||
builder.setMessage(message);
|
||||
builder.setPositiveButton(R.string.zxing_button_ok, (dialog, which) -> finish());
|
||||
builder.setOnCancelListener(dialog -> finish());
|
||||
builder.show();
|
||||
}
|
||||
|
||||
public static int getCameraPermissionReqCode() {
|
||||
return cameraPermissionReqCode;
|
||||
}
|
||||
|
||||
public static void setCameraPermissionReqCode(int cameraPermissionReqCode) {
|
||||
CaptureManager.cameraPermissionReqCode = cameraPermissionReqCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* If set to true, shows the default error dialog if camera permission is missing.
|
||||
* <p>
|
||||
* If set to false, instead the capture manager just finishes.
|
||||
* <p>
|
||||
* In both cases, the activity result is set to {@link Intents.Scan#MISSING_CAMERA_PERMISSION}
|
||||
* and cancelled
|
||||
*/
|
||||
public void setShowMissingCameraPermissionDialog(boolean visible) {
|
||||
setShowMissingCameraPermissionDialog(visible, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* If set to true, shows the specified error dialog message if camera permission is missing.
|
||||
* <p>
|
||||
* If set to false, instead the capture manager just finishes.
|
||||
* <p>
|
||||
* In both cases, the activity result is set to {@link Intents.Scan#MISSING_CAMERA_PERMISSION}
|
||||
* and cancelled
|
||||
*/
|
||||
public void setShowMissingCameraPermissionDialog(boolean visible, String message) {
|
||||
showDialogIfMissingCameraPermission = visible;
|
||||
missingCameraPermissionDialogMessage = message != null ? message : "";
|
||||
}
|
||||
}
|
||||
|
||||
+2
-164
@@ -1,183 +1,21 @@
|
||||
package com.journeyapps.barcodescanner;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.KeyEvent;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
import com.google.zxing.DecodeHintType;
|
||||
import com.google.zxing.MultiFormatReader;
|
||||
import com.google.zxing.ResultPoint;
|
||||
import com.google.zxing.client.android.DecodeFormatManager;
|
||||
import com.google.zxing.client.android.DecodeHintManager;
|
||||
import com.google.zxing.client.android.Intents;
|
||||
import com.google.zxing.client.android.R;
|
||||
import com.journeyapps.barcodescanner.camera.CameraSettings;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Encapsulates BarcodeView, ViewfinderView and status text.
|
||||
*
|
||||
* To customize the UI, use BarcodeView and ViewfinderView directly.
|
||||
* Used as an alias for DecoratedBarcodeView, for backwards-compatibility.
|
||||
*/
|
||||
public class CompoundBarcodeView extends FrameLayout {
|
||||
private BarcodeView barcodeView;
|
||||
private ViewfinderView viewFinder;
|
||||
private TextView statusView;
|
||||
|
||||
private class WrappedCallback implements BarcodeCallback {
|
||||
private BarcodeCallback delegate;
|
||||
|
||||
public WrappedCallback(BarcodeCallback delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void barcodeResult(BarcodeResult result) {
|
||||
delegate.barcodeResult(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void possibleResultPoints(List<ResultPoint> resultPoints) {
|
||||
for (ResultPoint point : resultPoints) {
|
||||
viewFinder.addPossibleResultPoint(point);
|
||||
}
|
||||
delegate.possibleResultPoints(resultPoints);
|
||||
}
|
||||
}
|
||||
|
||||
public class CompoundBarcodeView extends DecoratedBarcodeView {
|
||||
public CompoundBarcodeView(Context context) {
|
||||
super(context);
|
||||
initialize();
|
||||
}
|
||||
|
||||
public CompoundBarcodeView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
initialize();
|
||||
}
|
||||
|
||||
public CompoundBarcodeView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
initialize();
|
||||
}
|
||||
|
||||
private void initialize() {
|
||||
inflate(getContext(), R.layout.zxing_barcode_scanner, this);
|
||||
|
||||
barcodeView = (BarcodeView) findViewById(R.id.zxing_barcode_surface);
|
||||
|
||||
viewFinder = (ViewfinderView) findViewById(R.id.zxing_viewfinder_view);
|
||||
viewFinder.setCameraPreview(barcodeView);
|
||||
|
||||
statusView = (TextView) findViewById(R.id.zxing_status_view);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method to initialize camera id, decode formats and prompt message from an intent.
|
||||
*
|
||||
* @param intent the intent, as generated by IntentIntegrator
|
||||
*/
|
||||
public void initializeFromIntent(Intent intent) {
|
||||
// Scan the formats the intent requested, and return the result to the calling activity.
|
||||
Set<BarcodeFormat> decodeFormats = DecodeFormatManager.parseDecodeFormats(intent);
|
||||
Map<DecodeHintType, Object> decodeHints = DecodeHintManager.parseDecodeHints(intent);
|
||||
|
||||
CameraSettings settings = new CameraSettings();
|
||||
|
||||
if (intent.hasExtra(Intents.Scan.CAMERA_ID)) {
|
||||
int cameraId = intent.getIntExtra(Intents.Scan.CAMERA_ID, -1);
|
||||
if (cameraId >= 0) {
|
||||
settings.setRequestedCameraId(cameraId);
|
||||
}
|
||||
}
|
||||
|
||||
String customPromptMessage = intent.getStringExtra(Intents.Scan.PROMPT_MESSAGE);
|
||||
if (customPromptMessage != null) {
|
||||
setStatusText(customPromptMessage);
|
||||
}
|
||||
|
||||
String characterSet = intent.getStringExtra(Intents.Scan.CHARACTER_SET);
|
||||
|
||||
MultiFormatReader reader = new MultiFormatReader();
|
||||
reader.setHints(decodeHints);
|
||||
|
||||
barcodeView.setCameraSettings(settings);
|
||||
barcodeView.setDecoderFactory(new DefaultDecoderFactory(decodeFormats, decodeHints, characterSet));
|
||||
}
|
||||
|
||||
public void setStatusText(String text) {
|
||||
statusView.setText(text);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @see BarcodeView#pause()
|
||||
*/
|
||||
public void pause() {
|
||||
barcodeView.pause();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see BarcodeView#resume()
|
||||
*/
|
||||
public void resume() {
|
||||
barcodeView.resume();
|
||||
}
|
||||
|
||||
public BarcodeView getBarcodeView() {
|
||||
return (BarcodeView) findViewById(R.id.zxing_barcode_surface);
|
||||
}
|
||||
|
||||
public ViewfinderView getViewFinder() {
|
||||
return viewFinder;
|
||||
}
|
||||
|
||||
public TextView getStatusView() {
|
||||
return statusView;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @see BarcodeView#decodeSingle(BarcodeCallback)
|
||||
*/
|
||||
public void decodeSingle(BarcodeCallback callback) {
|
||||
barcodeView.decodeSingle(new WrappedCallback(callback));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @see BarcodeView#decodeContinuous(BarcodeCallback)
|
||||
*/
|
||||
public void decodeContinuous(BarcodeCallback callback) {
|
||||
barcodeView.decodeContinuous(new WrappedCallback(callback));
|
||||
}
|
||||
|
||||
@Override
|
||||
/**
|
||||
* Handles focus, camera, volume up and volume down keys.
|
||||
*
|
||||
* Note that this view is not usually focused, so the Activity should call this directly.
|
||||
*/
|
||||
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
||||
switch (keyCode) {
|
||||
case KeyEvent.KEYCODE_FOCUS:
|
||||
case KeyEvent.KEYCODE_CAMERA:
|
||||
// Handle these events so they don't launch the Camera app
|
||||
return true;
|
||||
// Use volume up/down to turn on light
|
||||
case KeyEvent.KEYCODE_VOLUME_DOWN:
|
||||
barcodeView.setTorch(false);
|
||||
return true;
|
||||
case KeyEvent.KEYCODE_VOLUME_UP:
|
||||
barcodeView.setTorch(true);
|
||||
return true;
|
||||
}
|
||||
return super.onKeyDown(keyCode, event);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -26,7 +26,7 @@ public class DecoderResultPointCallback implements ResultPointCallback {
|
||||
|
||||
@Override
|
||||
public void foundPossibleResultPoint(ResultPoint point) {
|
||||
if(decoder != null) {
|
||||
if (decoder != null) {
|
||||
decoder.foundPossibleResultPoint(point);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,9 @@ public class DecoderThread {
|
||||
public boolean handleMessage(Message message) {
|
||||
if (message.what == R.id.zxing_decode) {
|
||||
decode((SourceData) message.obj);
|
||||
} else if (message.what == R.id.zxing_preview_failed) {
|
||||
// Error already logged. Try again.
|
||||
requestNextPreview();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -80,7 +83,6 @@ public class DecoderThread {
|
||||
requestNextPreview();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Stop decoding.
|
||||
*
|
||||
@@ -96,7 +98,6 @@ public class DecoderThread {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private final PreviewCallback previewCallback = new PreviewCallback() {
|
||||
@Override
|
||||
public void onPreview(SourceData sourceData) {
|
||||
@@ -111,12 +112,20 @@ public class DecoderThread {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPreviewError(Exception e) {
|
||||
synchronized (LOCK) {
|
||||
if (running) {
|
||||
// Post to our thread.
|
||||
handler.obtainMessage(R.id.zxing_preview_failed).sendToTarget();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private void requestNextPreview() {
|
||||
if (cameraInstance.isOpen()) {
|
||||
cameraInstance.requestPreview(previewCallback);
|
||||
}
|
||||
cameraInstance.requestPreview(previewCallback);
|
||||
}
|
||||
|
||||
protected LuminanceSource createSource(SourceData sourceData) {
|
||||
@@ -133,7 +142,7 @@ public class DecoderThread {
|
||||
sourceData.setCropRect(cropRect);
|
||||
LuminanceSource source = createSource(sourceData);
|
||||
|
||||
if(source != null) {
|
||||
if (source != null) {
|
||||
rawResult = decoder.decode(source);
|
||||
}
|
||||
|
||||
@@ -155,11 +164,10 @@ public class DecoderThread {
|
||||
}
|
||||
}
|
||||
if (resultHandler != null) {
|
||||
List<ResultPoint> resultPoints = decoder.getPossibleResultPoints();
|
||||
Message message = Message.obtain(resultHandler, R.id.zxing_possible_result_points, resultPoints);
|
||||
List<ResultPoint> resultPoints = BarcodeResult.transformResultPoints(decoder.getPossibleResultPoints(), sourceData);
|
||||
Message message = Message.obtain(resultHandler, R.id.zxing_possible_result_points, resultPoints);
|
||||
message.sendToTarget();
|
||||
}
|
||||
requestNextPreview();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,306 @@
|
||||
package com.journeyapps.barcodescanner;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.res.TypedArray;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.KeyEvent;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
import com.google.zxing.DecodeHintType;
|
||||
import com.google.zxing.MultiFormatReader;
|
||||
import com.google.zxing.ResultPoint;
|
||||
import com.google.zxing.client.android.DecodeFormatManager;
|
||||
import com.google.zxing.client.android.DecodeHintManager;
|
||||
import com.google.zxing.client.android.Intents;
|
||||
import com.google.zxing.client.android.R;
|
||||
import com.journeyapps.barcodescanner.camera.CameraParametersCallback;
|
||||
import com.journeyapps.barcodescanner.camera.CameraSettings;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Encapsulates BarcodeView, ViewfinderView and status text.
|
||||
*
|
||||
* To customize the UI, use BarcodeView and ViewfinderView directly.
|
||||
*/
|
||||
public class DecoratedBarcodeView extends FrameLayout {
|
||||
private BarcodeView barcodeView;
|
||||
private ViewfinderView viewFinder;
|
||||
private TextView statusView;
|
||||
|
||||
/**
|
||||
* The instance of @link TorchListener to send events callback.
|
||||
*/
|
||||
private TorchListener torchListener;
|
||||
|
||||
private class WrappedCallback implements BarcodeCallback {
|
||||
private BarcodeCallback delegate;
|
||||
|
||||
public WrappedCallback(BarcodeCallback delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void barcodeResult(BarcodeResult result) {
|
||||
delegate.barcodeResult(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void possibleResultPoints(List<ResultPoint> resultPoints) {
|
||||
for (ResultPoint point : resultPoints) {
|
||||
viewFinder.addPossibleResultPoint(point);
|
||||
}
|
||||
delegate.possibleResultPoints(resultPoints);
|
||||
}
|
||||
}
|
||||
|
||||
public DecoratedBarcodeView(Context context) {
|
||||
super(context);
|
||||
initialize();
|
||||
}
|
||||
|
||||
public DecoratedBarcodeView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
initialize(attrs);
|
||||
}
|
||||
|
||||
public DecoratedBarcodeView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
initialize(attrs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the view with the xml configuration based on styleable attributes.
|
||||
*
|
||||
* @param attrs The attributes to use on view.
|
||||
*/
|
||||
private void initialize(AttributeSet attrs) {
|
||||
// Get attributes set on view
|
||||
TypedArray attributes = getContext().obtainStyledAttributes(attrs, R.styleable.zxing_view);
|
||||
|
||||
int scannerLayout = attributes.getResourceId(
|
||||
R.styleable.zxing_view_zxing_scanner_layout, R.layout.zxing_barcode_scanner);
|
||||
|
||||
attributes.recycle();
|
||||
|
||||
inflate(getContext(), scannerLayout, this);
|
||||
|
||||
barcodeView = findViewById(R.id.zxing_barcode_surface);
|
||||
|
||||
if (barcodeView == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"There is no a com.journeyapps.barcodescanner.BarcodeView on provided layout " +
|
||||
"with the id \"zxing_barcode_surface\".");
|
||||
}
|
||||
|
||||
// Pass on any preview-related attributes
|
||||
barcodeView.initializeAttributes(attrs);
|
||||
|
||||
|
||||
viewFinder = findViewById(R.id.zxing_viewfinder_view);
|
||||
|
||||
if (viewFinder == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"There is no a com.journeyapps.barcodescanner.ViewfinderView on provided layout " +
|
||||
"with the id \"zxing_viewfinder_view\".");
|
||||
}
|
||||
|
||||
viewFinder.setCameraPreview(barcodeView);
|
||||
|
||||
// statusView is optional
|
||||
statusView = findViewById(R.id.zxing_status_view);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize with no custom attributes set.
|
||||
*/
|
||||
private void initialize() {
|
||||
initialize(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method to initialize camera id, decode formats and prompt message from an intent.
|
||||
*
|
||||
* @param intent the intent, as generated by IntentIntegrator
|
||||
*/
|
||||
public void initializeFromIntent(Intent intent) {
|
||||
// Scan the formats the intent requested, and return the result to the calling activity.
|
||||
Set<BarcodeFormat> decodeFormats = DecodeFormatManager.parseDecodeFormats(intent);
|
||||
Map<DecodeHintType, Object> decodeHints = DecodeHintManager.parseDecodeHints(intent);
|
||||
|
||||
CameraSettings settings = new CameraSettings();
|
||||
|
||||
if (intent.hasExtra(Intents.Scan.CAMERA_ID)) {
|
||||
int cameraId = intent.getIntExtra(Intents.Scan.CAMERA_ID, -1);
|
||||
if (cameraId >= 0) {
|
||||
settings.setRequestedCameraId(cameraId);
|
||||
}
|
||||
}
|
||||
|
||||
if (intent.hasExtra(Intents.Scan.TORCH_ENABLED)) {
|
||||
if (intent.getBooleanExtra(Intents.Scan.TORCH_ENABLED, false)) {
|
||||
this.setTorchOn();
|
||||
}
|
||||
}
|
||||
|
||||
String customPromptMessage = intent.getStringExtra(Intents.Scan.PROMPT_MESSAGE);
|
||||
if (customPromptMessage != null) {
|
||||
setStatusText(customPromptMessage);
|
||||
}
|
||||
|
||||
// Check what type of scan. Default: normal scan
|
||||
int scanType = intent.getIntExtra(Intents.Scan.SCAN_TYPE, 0);
|
||||
|
||||
String characterSet = intent.getStringExtra(Intents.Scan.CHARACTER_SET);
|
||||
|
||||
MultiFormatReader reader = new MultiFormatReader();
|
||||
reader.setHints(decodeHints);
|
||||
|
||||
barcodeView.setCameraSettings(settings);
|
||||
barcodeView.setDecoderFactory(new DefaultDecoderFactory(decodeFormats, decodeHints, characterSet, scanType));
|
||||
}
|
||||
|
||||
public void setCameraSettings(CameraSettings cameraSettings) {
|
||||
barcodeView.setCameraSettings(cameraSettings);
|
||||
}
|
||||
|
||||
public void setDecoderFactory(DecoderFactory decoderFactory) {
|
||||
barcodeView.setDecoderFactory(decoderFactory);
|
||||
}
|
||||
|
||||
public DecoderFactory getDecoderFactory() {
|
||||
return barcodeView.getDecoderFactory();
|
||||
}
|
||||
|
||||
public CameraSettings getCameraSettings() {
|
||||
return barcodeView.getCameraSettings();
|
||||
}
|
||||
|
||||
public void setStatusText(String text) {
|
||||
// statusView is optional when using a custom layout
|
||||
if (statusView != null) {
|
||||
statusView.setText(text);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see BarcodeView#pause()
|
||||
*/
|
||||
public void pause() {
|
||||
barcodeView.pause();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see BarcodeView#pauseAndWait()
|
||||
*/
|
||||
public void pauseAndWait() {
|
||||
barcodeView.pauseAndWait();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see BarcodeView#resume()
|
||||
*/
|
||||
public void resume() {
|
||||
barcodeView.resume();
|
||||
}
|
||||
|
||||
public BarcodeView getBarcodeView() {
|
||||
return findViewById(R.id.zxing_barcode_surface);
|
||||
}
|
||||
|
||||
public ViewfinderView getViewFinder() {
|
||||
return viewFinder;
|
||||
}
|
||||
|
||||
public TextView getStatusView() {
|
||||
return statusView;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see BarcodeView#decodeSingle(BarcodeCallback)
|
||||
*/
|
||||
public void decodeSingle(BarcodeCallback callback) {
|
||||
barcodeView.decodeSingle(new WrappedCallback(callback));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see BarcodeView#decodeContinuous(BarcodeCallback)
|
||||
*/
|
||||
public void decodeContinuous(BarcodeCallback callback) {
|
||||
barcodeView.decodeContinuous(new WrappedCallback(callback));
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn on the device's flashlight.
|
||||
*/
|
||||
public void setTorchOn() {
|
||||
barcodeView.setTorch(true);
|
||||
|
||||
if (torchListener != null) {
|
||||
torchListener.onTorchOn();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn off the device's flashlight.
|
||||
*/
|
||||
public void setTorchOff() {
|
||||
barcodeView.setTorch(false);
|
||||
|
||||
if (torchListener != null) {
|
||||
torchListener.onTorchOff();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the settings for Camera.
|
||||
* Must be called after {@link #resume()}.
|
||||
*
|
||||
* @param callback {@link CameraParametersCallback}
|
||||
*/
|
||||
public void changeCameraParameters(CameraParametersCallback callback) {
|
||||
barcodeView.changeCameraParameters(callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles focus, camera, volume up and volume down keys.
|
||||
*
|
||||
* Note that this view is not usually focused, so the Activity should call this directly.
|
||||
*/
|
||||
@Override
|
||||
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
||||
switch (keyCode) {
|
||||
case KeyEvent.KEYCODE_FOCUS:
|
||||
case KeyEvent.KEYCODE_CAMERA:
|
||||
// Handle these events so they don't launch the Camera app
|
||||
return true;
|
||||
// Use volume up/down to turn on light
|
||||
case KeyEvent.KEYCODE_VOLUME_DOWN:
|
||||
setTorchOff();
|
||||
return true;
|
||||
case KeyEvent.KEYCODE_VOLUME_UP:
|
||||
setTorchOn();
|
||||
return true;
|
||||
}
|
||||
return super.onKeyDown(keyCode, event);
|
||||
}
|
||||
|
||||
public void setTorchListener(TorchListener listener) {
|
||||
this.torchListener = listener;
|
||||
}
|
||||
|
||||
/**
|
||||
* The Listener to torch/fflashlight events (turn on, turn off).
|
||||
*/
|
||||
public interface TorchListener {
|
||||
|
||||
void onTorchOn();
|
||||
|
||||
void onTorchOff();
|
||||
}
|
||||
}
|
||||
+25
-4
@@ -15,14 +15,22 @@ public class DefaultDecoderFactory implements DecoderFactory {
|
||||
private Collection<BarcodeFormat> decodeFormats;
|
||||
private Map<DecodeHintType, ?> hints;
|
||||
private String characterSet;
|
||||
private int scanType;
|
||||
|
||||
public DefaultDecoderFactory() {
|
||||
}
|
||||
|
||||
public DefaultDecoderFactory(Collection<BarcodeFormat> decodeFormats, Map<DecodeHintType, ?> hints, String characterSet) {
|
||||
|
||||
|
||||
public DefaultDecoderFactory(Collection<BarcodeFormat> decodeFormats) {
|
||||
this.decodeFormats = decodeFormats;
|
||||
}
|
||||
|
||||
public DefaultDecoderFactory(Collection<BarcodeFormat> decodeFormats, Map<DecodeHintType, ?> hints, String characterSet, int scanType) {
|
||||
this.decodeFormats = decodeFormats;
|
||||
this.hints = hints;
|
||||
this.characterSet = characterSet;
|
||||
this.scanType = scanType;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -31,11 +39,11 @@ public class DefaultDecoderFactory implements DecoderFactory {
|
||||
|
||||
hints.putAll(baseHints);
|
||||
|
||||
if(this.hints != null) {
|
||||
if (this.hints != null) {
|
||||
hints.putAll(this.hints);
|
||||
}
|
||||
|
||||
if(this.decodeFormats != null) {
|
||||
if (this.decodeFormats != null) {
|
||||
hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
|
||||
}
|
||||
|
||||
@@ -46,6 +54,19 @@ public class DefaultDecoderFactory implements DecoderFactory {
|
||||
MultiFormatReader reader = new MultiFormatReader();
|
||||
reader.setHints(hints);
|
||||
|
||||
return new Decoder(reader);
|
||||
switch (scanType){
|
||||
case 0:
|
||||
return new Decoder(reader);
|
||||
case 1:
|
||||
return new InvertedDecoder(reader);
|
||||
case 2:
|
||||
return new MixedDecoder(reader);
|
||||
default:
|
||||
return new Decoder(reader);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.journeyapps.barcodescanner;
|
||||
|
||||
import com.google.zxing.BinaryBitmap;
|
||||
import com.google.zxing.LuminanceSource;
|
||||
import com.google.zxing.Reader;
|
||||
import com.google.zxing.common.HybridBinarizer;
|
||||
|
||||
/**
|
||||
* Created by leighmd on 11/2/16.
|
||||
*/
|
||||
public class InvertedDecoder extends Decoder {
|
||||
|
||||
/**
|
||||
* Create a new Decoder with the specified Reader.
|
||||
* <p/>
|
||||
* It is recommended to use an instance of MultiFormatReader in most cases.
|
||||
*
|
||||
* @param reader the reader
|
||||
*/
|
||||
public InvertedDecoder(Reader reader) {
|
||||
super(reader);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given an image source, convert to a binary bitmap.
|
||||
*
|
||||
* Override this to use a custom binarizer.
|
||||
*
|
||||
* @param source the image source
|
||||
* @return a BinaryBitmap
|
||||
*/
|
||||
protected BinaryBitmap toBitmap(LuminanceSource source) {
|
||||
|
||||
return new BinaryBitmap(new HybridBinarizer(source.invert()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.journeyapps.barcodescanner;
|
||||
|
||||
import com.google.zxing.BinaryBitmap;
|
||||
import com.google.zxing.LuminanceSource;
|
||||
import com.google.zxing.Reader;
|
||||
import com.google.zxing.common.HybridBinarizer;
|
||||
|
||||
/**
|
||||
* Decoder that performs alternating scans in normal mode and inverted mode.
|
||||
*/
|
||||
public class MixedDecoder extends Decoder {
|
||||
private boolean isInverted = true;
|
||||
|
||||
/**
|
||||
* Create a new Decoder with the specified Reader.
|
||||
* <p/>
|
||||
* It is recommended to use an instance of MultiFormatReader in most cases.
|
||||
*
|
||||
* @param reader the reader
|
||||
*/
|
||||
public MixedDecoder(Reader reader) {
|
||||
super(reader);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given an image source, convert to a binary bitmap.
|
||||
*
|
||||
* Override this to use a custom binarizer.
|
||||
*
|
||||
* @param source the image source
|
||||
* @return a BinaryBitmap
|
||||
*/
|
||||
protected BinaryBitmap toBitmap(LuminanceSource source) {
|
||||
if (isInverted) {
|
||||
isInverted = false;
|
||||
return new BinaryBitmap(new HybridBinarizer(source.invert()));
|
||||
} else {
|
||||
isInverted = true;
|
||||
return new BinaryBitmap(new HybridBinarizer(source));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
package com.journeyapps.barcodescanner;
|
||||
|
||||
import android.graphics.Rect;
|
||||
|
||||
public class RawImageData {
|
||||
private byte[] data;
|
||||
private int width;
|
||||
private int height;
|
||||
|
||||
public RawImageData(byte[] data, int width, int height) {
|
||||
this.data = data;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public byte[] getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
public RawImageData cropAndScale(Rect cropRect, int scale) {
|
||||
int width = cropRect.width() / scale;
|
||||
int height = cropRect.height() / scale;
|
||||
|
||||
int top = cropRect.top;
|
||||
|
||||
int area = width * height;
|
||||
byte[] matrix = new byte[area];
|
||||
|
||||
if (scale == 1) {
|
||||
int inputOffset = top * this.width + cropRect.left;
|
||||
|
||||
// Copy one cropped row at a time.
|
||||
for (int y = 0; y < height; y++) {
|
||||
int outputOffset = y * width;
|
||||
System.arraycopy(this.data, inputOffset, matrix, outputOffset, width);
|
||||
inputOffset += this.width;
|
||||
}
|
||||
} else {
|
||||
int inputOffset = top * this.width + cropRect.left;
|
||||
|
||||
// Copy one cropped row at a time.
|
||||
for (int y = 0; y < height; y++) {
|
||||
int outputOffset = y * width;
|
||||
int xOffset = inputOffset;
|
||||
for (int x = 0; x < width; x++) {
|
||||
matrix[outputOffset] = this.data[xOffset];
|
||||
xOffset += scale;
|
||||
outputOffset += 1;
|
||||
}
|
||||
inputOffset += this.width * scale;
|
||||
}
|
||||
}
|
||||
return new RawImageData(matrix, width, height);
|
||||
}
|
||||
|
||||
|
||||
public RawImageData rotateCameraPreview(int cameraRotation) {
|
||||
switch (cameraRotation) {
|
||||
case 90:
|
||||
return new RawImageData(rotateCW(data, this.width, this.height), this.height, this.width);
|
||||
case 180:
|
||||
return new RawImageData(rotate180(data, this.width, this.height), this.width, this.height);
|
||||
case 270:
|
||||
return new RawImageData(rotateCCW(data, this.width, this.height), this.height, this.width);
|
||||
case 0:
|
||||
default:
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotate an image by 90 degrees CW.
|
||||
*
|
||||
* @param data the image data, in with the first width * height bytes being the luminance data.
|
||||
* @param imageWidth the width of the image
|
||||
* @param imageHeight the height of the image
|
||||
* @return the rotated bytes
|
||||
*/
|
||||
public static byte[] rotateCW(byte[] data, int imageWidth, int imageHeight) {
|
||||
// Adapted from http://stackoverflow.com/a/15775173
|
||||
// data may contain more than just y (u and v), but we are only interested in the y section.
|
||||
|
||||
byte[] yuv = new byte[imageWidth * imageHeight];
|
||||
int i = 0;
|
||||
for (int x = 0; x < imageWidth; x++) {
|
||||
for (int y = imageHeight - 1; y >= 0; y--) {
|
||||
yuv[i] = data[y * imageWidth + x];
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return yuv;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotate an image by 180 degrees.
|
||||
*
|
||||
* @param data the image data, in with the first width * height bytes being the luminance data.
|
||||
* @param imageWidth the width of the image
|
||||
* @param imageHeight the height of the image
|
||||
* @return the rotated bytes
|
||||
*/
|
||||
public static byte[] rotate180(byte[] data, int imageWidth, int imageHeight) {
|
||||
int n = imageWidth * imageHeight;
|
||||
byte[] yuv = new byte[n];
|
||||
|
||||
int i = n - 1;
|
||||
for (int j = 0; j < n; j++) {
|
||||
yuv[i] = data[j];
|
||||
i--;
|
||||
}
|
||||
return yuv;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotate an image by 90 degrees CCW.
|
||||
*
|
||||
* @param data the image data, in with the first width * height bytes being the luminance data.
|
||||
* @param imageWidth the width of the image
|
||||
* @param imageHeight the height of the image
|
||||
* @return the rotated bytes
|
||||
*/
|
||||
public static byte[] rotateCCW(byte[] data, int imageWidth, int imageHeight) {
|
||||
int n = imageWidth * imageHeight;
|
||||
byte[] yuv = new byte[n];
|
||||
int i = n - 1;
|
||||
for (int x = 0; x < imageWidth; x++) {
|
||||
for (int y = imageHeight - 1; y >= 0; y--) {
|
||||
yuv[i] = data[y * imageWidth + x];
|
||||
i--;
|
||||
}
|
||||
}
|
||||
return yuv;
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package com.journeyapps.barcodescanner;
|
||||
|
||||
import android.content.Context;
|
||||
import android.hardware.SensorManager;
|
||||
import android.util.Log;
|
||||
import android.view.OrientationEventListener;
|
||||
import android.view.WindowManager;
|
||||
|
||||
@@ -23,7 +22,6 @@ public class RotationListener {
|
||||
public RotationListener() {
|
||||
}
|
||||
|
||||
|
||||
public void listen(Context context, RotationCallback callback) {
|
||||
// Stop to make sure we're not registering the listening twice.
|
||||
stop();
|
||||
@@ -42,7 +40,7 @@ public class RotationListener {
|
||||
public void onOrientationChanged(int orientation) {
|
||||
WindowManager localWindowManager = windowManager;
|
||||
RotationCallback localCallback = RotationListener.this.callback;
|
||||
if(windowManager != null && localCallback != null) {
|
||||
if (windowManager != null && localCallback != null) {
|
||||
int newRotation = localWindowManager.getDefaultDisplay().getRotation();
|
||||
if (newRotation != lastRotation) {
|
||||
lastRotation = newRotation;
|
||||
@@ -59,7 +57,7 @@ public class RotationListener {
|
||||
public void stop() {
|
||||
// To reduce the effect of possible leaks, we clear any references we have to external
|
||||
// objects.
|
||||
if(this.orientationEventListener != null) {
|
||||
if (this.orientationEventListener != null) {
|
||||
this.orientationEventListener.disable();
|
||||
}
|
||||
this.orientationEventListener = null;
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.journeyapps.barcodescanner;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
import androidx.activity.result.contract.ActivityResultContract;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
public class ScanContract extends ActivityResultContract<ScanOptions, ScanIntentResult> {
|
||||
@NonNull
|
||||
@Override
|
||||
public Intent createIntent(@NonNull Context context, ScanOptions input) {
|
||||
return input.createScanIntent(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScanIntentResult parseResult(int resultCode, @Nullable Intent intent) {
|
||||
return ScanIntentResult.parseActivityResult(resultCode, intent);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
/*
|
||||
* Based on IntentResult.
|
||||
*
|
||||
* Copyright 2009 ZXing authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.journeyapps.barcodescanner;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
|
||||
import com.google.zxing.client.android.Intents;
|
||||
import com.google.zxing.integration.android.IntentResult;
|
||||
|
||||
/**
|
||||
* <p>Encapsulates the result of a barcode scan invoked through {@link ScanContract}.</p>
|
||||
*
|
||||
* @author Sean Owen
|
||||
*/
|
||||
public final class ScanIntentResult {
|
||||
|
||||
private final String contents;
|
||||
private final String formatName;
|
||||
private final byte[] rawBytes;
|
||||
private final Integer orientation;
|
||||
private final String errorCorrectionLevel;
|
||||
private final String barcodeImagePath;
|
||||
private final Intent originalIntent;
|
||||
|
||||
ScanIntentResult() {
|
||||
this(null, null, null, null, null, null, null);
|
||||
}
|
||||
|
||||
ScanIntentResult(Intent intent) {
|
||||
this(null, null, null, null, null, null, intent);
|
||||
}
|
||||
|
||||
ScanIntentResult(String contents,
|
||||
String formatName,
|
||||
byte[] rawBytes,
|
||||
Integer orientation,
|
||||
String errorCorrectionLevel,
|
||||
String barcodeImagePath,
|
||||
Intent originalIntent) {
|
||||
this.contents = contents;
|
||||
this.formatName = formatName;
|
||||
this.rawBytes = rawBytes;
|
||||
this.orientation = orientation;
|
||||
this.errorCorrectionLevel = errorCorrectionLevel;
|
||||
this.barcodeImagePath = barcodeImagePath;
|
||||
this.originalIntent = originalIntent;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return raw content of barcode
|
||||
*/
|
||||
public String getContents() {
|
||||
return contents;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return name of format, like "QR_CODE", "UPC_A". See {@code BarcodeFormat} for more format names.
|
||||
*/
|
||||
public String getFormatName() {
|
||||
return formatName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return raw bytes of the barcode content, if applicable, or null otherwise
|
||||
*/
|
||||
public byte[] getRawBytes() {
|
||||
return rawBytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return rotation of the image, in degrees, which resulted in a successful scan. May be null.
|
||||
*/
|
||||
public Integer getOrientation() {
|
||||
return orientation;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return name of the error correction level used in the barcode, if applicable
|
||||
*/
|
||||
public String getErrorCorrectionLevel() {
|
||||
return errorCorrectionLevel;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return path to a temporary file containing the barcode image, if applicable, or null otherwise
|
||||
*/
|
||||
public String getBarcodeImagePath() {
|
||||
return barcodeImagePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the original intent
|
||||
*/
|
||||
public Intent getOriginalIntent() {
|
||||
return originalIntent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
int rawBytesLength = rawBytes == null ? 0 : rawBytes.length;
|
||||
return "Format: " + formatName + '\n' +
|
||||
"Contents: " + contents + '\n' +
|
||||
"Raw bytes: (" + rawBytesLength + " bytes)\n" +
|
||||
"Orientation: " + orientation + '\n' +
|
||||
"EC level: " + errorCorrectionLevel + '\n' +
|
||||
"Barcode image: " + barcodeImagePath + '\n' +
|
||||
"Original intent: " + originalIntent + '\n';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Parse activity result, without checking the request code.
|
||||
*
|
||||
* @param resultCode result code from {@code onActivityResult()}
|
||||
* @param intent {@link Intent} from {@code onActivityResult()}
|
||||
* @return an {@link IntentResult} containing the result of the scan. If the user cancelled scanning,
|
||||
* the fields will be null.
|
||||
*/
|
||||
public static ScanIntentResult parseActivityResult(int resultCode, Intent intent) {
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
String contents = intent.getStringExtra(Intents.Scan.RESULT);
|
||||
String formatName = intent.getStringExtra(Intents.Scan.RESULT_FORMAT);
|
||||
byte[] rawBytes = intent.getByteArrayExtra(Intents.Scan.RESULT_BYTES);
|
||||
int intentOrientation = intent.getIntExtra(Intents.Scan.RESULT_ORIENTATION, Integer.MIN_VALUE);
|
||||
Integer orientation = intentOrientation == Integer.MIN_VALUE ? null : intentOrientation;
|
||||
String errorCorrectionLevel = intent.getStringExtra(Intents.Scan.RESULT_ERROR_CORRECTION_LEVEL);
|
||||
String barcodeImagePath = intent.getStringExtra(Intents.Scan.RESULT_BARCODE_IMAGE_PATH);
|
||||
return new ScanIntentResult(contents,
|
||||
formatName,
|
||||
rawBytes,
|
||||
orientation,
|
||||
errorCorrectionLevel,
|
||||
barcodeImagePath,
|
||||
intent);
|
||||
}
|
||||
return new ScanIntentResult(intent);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,262 @@
|
||||
/*
|
||||
* Based on IntentIntegrator, Copyright 2009 ZXing authors.
|
||||
*
|
||||
*/
|
||||
package com.journeyapps.barcodescanner;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
|
||||
import com.google.zxing.client.android.Intents;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class ScanOptions {
|
||||
// supported barcode formats
|
||||
|
||||
// Product Codes
|
||||
public static final String UPC_A = "UPC_A";
|
||||
public static final String UPC_E = "UPC_E";
|
||||
public static final String EAN_8 = "EAN_8";
|
||||
public static final String EAN_13 = "EAN_13";
|
||||
public static final String RSS_14 = "RSS_14";
|
||||
|
||||
// Other 1D
|
||||
public static final String CODE_39 = "CODE_39";
|
||||
public static final String CODE_93 = "CODE_93";
|
||||
public static final String CODE_128 = "CODE_128";
|
||||
public static final String ITF = "ITF";
|
||||
|
||||
public static final String RSS_EXPANDED = "RSS_EXPANDED";
|
||||
|
||||
// 2D
|
||||
public static final String QR_CODE = "QR_CODE";
|
||||
public static final String DATA_MATRIX = "DATA_MATRIX";
|
||||
public static final String PDF_417 = "PDF_417";
|
||||
|
||||
|
||||
public static final Collection<String> PRODUCT_CODE_TYPES = list(UPC_A, UPC_E, EAN_8, EAN_13, RSS_14);
|
||||
public static final Collection<String> ONE_D_CODE_TYPES =
|
||||
list(UPC_A, UPC_E, EAN_8, EAN_13, RSS_14, CODE_39, CODE_93, CODE_128,
|
||||
ITF, RSS_14, RSS_EXPANDED);
|
||||
|
||||
public static final Collection<String> ALL_CODE_TYPES = null;
|
||||
|
||||
private final Map<String, Object> moreExtras = new HashMap<>(3);
|
||||
|
||||
private Collection<String> desiredBarcodeFormats;
|
||||
|
||||
private Class<?> captureActivity;
|
||||
|
||||
|
||||
protected Class<?> getDefaultCaptureActivity() {
|
||||
return CaptureActivity.class;
|
||||
}
|
||||
|
||||
public ScanOptions() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public Class<?> getCaptureActivity() {
|
||||
if (captureActivity == null) {
|
||||
captureActivity = getDefaultCaptureActivity();
|
||||
}
|
||||
return captureActivity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Activity class to use. It can be any activity, but should handle the intent extras
|
||||
* as used here.
|
||||
*
|
||||
* @param captureActivity the class
|
||||
*/
|
||||
public ScanOptions setCaptureActivity(Class<?> captureActivity) {
|
||||
this.captureActivity = captureActivity;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Map<String, ?> getMoreExtras() {
|
||||
return moreExtras;
|
||||
}
|
||||
|
||||
public final ScanOptions addExtra(String key, Object value) {
|
||||
moreExtras.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a prompt to display on the capture screen, instead of using the default.
|
||||
*
|
||||
* @param prompt the prompt to display
|
||||
*/
|
||||
public final ScanOptions setPrompt(String prompt) {
|
||||
if (prompt != null) {
|
||||
addExtra(Intents.Scan.PROMPT_MESSAGE, prompt);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* By default, the orientation is locked. Set to false to not lock.
|
||||
*
|
||||
* @param locked true to lock orientation
|
||||
*/
|
||||
public ScanOptions setOrientationLocked(boolean locked) {
|
||||
addExtra(Intents.Scan.ORIENTATION_LOCKED, locked);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Use the specified camera ID.
|
||||
*
|
||||
* @param cameraId camera ID of the camera to use. A negative value means "no preference".
|
||||
* @return this
|
||||
*/
|
||||
public ScanOptions setCameraId(int cameraId) {
|
||||
if (cameraId >= 0) {
|
||||
addExtra(Intents.Scan.CAMERA_ID, cameraId);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set to true to enable initial torch
|
||||
*
|
||||
* @param enabled true to enable initial torch
|
||||
* @return this
|
||||
*/
|
||||
public ScanOptions setTorchEnabled(boolean enabled) {
|
||||
addExtra(Intents.Scan.TORCH_ENABLED, enabled);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set to false to disable beep on scan.
|
||||
*
|
||||
* @param enabled false to disable beep
|
||||
* @return this
|
||||
*/
|
||||
public ScanOptions setBeepEnabled(boolean enabled) {
|
||||
addExtra(Intents.Scan.BEEP_ENABLED, enabled);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set to true to enable saving the barcode image and sending its path in the result Intent.
|
||||
*
|
||||
* @param enabled true to enable barcode image
|
||||
* @return this
|
||||
*/
|
||||
public ScanOptions setBarcodeImageEnabled(boolean enabled) {
|
||||
addExtra(Intents.Scan.BARCODE_IMAGE_ENABLED, enabled);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the desired barcode formats to scan.
|
||||
*
|
||||
* @param desiredBarcodeFormats names of {@code BarcodeFormat}s to scan for
|
||||
* @return this
|
||||
*/
|
||||
public ScanOptions setDesiredBarcodeFormats(Collection<String> desiredBarcodeFormats) {
|
||||
this.desiredBarcodeFormats = desiredBarcodeFormats;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the desired barcode formats to scan.
|
||||
*
|
||||
* @param desiredBarcodeFormats names of {@code BarcodeFormat}s to scan for
|
||||
* @return this
|
||||
*/
|
||||
public ScanOptions setDesiredBarcodeFormats(String... desiredBarcodeFormats) {
|
||||
this.desiredBarcodeFormats = Arrays.asList(desiredBarcodeFormats);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiates a scan for all known barcode types with the default camera.
|
||||
* And starts a timer to finish on timeout
|
||||
*
|
||||
* @return Activity.RESULT_CANCELED and true on parameter TIMEOUT.
|
||||
*/
|
||||
public ScanOptions setTimeout(long timeout) {
|
||||
addExtra(Intents.Scan.TIMEOUT, timeout);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an scan intent with the specified options.
|
||||
*
|
||||
* @return the intent
|
||||
*/
|
||||
public Intent createScanIntent(Context context) {
|
||||
Intent intentScan = new Intent(context, getCaptureActivity());
|
||||
intentScan.setAction(Intents.Scan.ACTION);
|
||||
|
||||
// check which types of codes to scan for
|
||||
if (desiredBarcodeFormats != null) {
|
||||
// set the desired barcode types
|
||||
StringBuilder joinedByComma = new StringBuilder();
|
||||
for (String format : desiredBarcodeFormats) {
|
||||
if (joinedByComma.length() > 0) {
|
||||
joinedByComma.append(',');
|
||||
}
|
||||
joinedByComma.append(format);
|
||||
}
|
||||
intentScan.putExtra(Intents.Scan.FORMATS, joinedByComma.toString());
|
||||
}
|
||||
|
||||
intentScan.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||
intentScan.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
|
||||
attachMoreExtras(intentScan);
|
||||
return intentScan;
|
||||
}
|
||||
|
||||
private static List<String> list(String... values) {
|
||||
return Collections.unmodifiableList(Arrays.asList(values));
|
||||
}
|
||||
|
||||
private void attachMoreExtras(Intent intent) {
|
||||
for (Map.Entry<String, Object> entry : moreExtras.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
Object value = entry.getValue();
|
||||
// Kind of hacky
|
||||
if (value instanceof Integer) {
|
||||
intent.putExtra(key, (Integer) value);
|
||||
} else if (value instanceof Long) {
|
||||
intent.putExtra(key, (Long) value);
|
||||
} else if (value instanceof Boolean) {
|
||||
intent.putExtra(key, (Boolean) value);
|
||||
} else if (value instanceof Double) {
|
||||
intent.putExtra(key, (Double) value);
|
||||
} else if (value instanceof Float) {
|
||||
intent.putExtra(key, (Float) value);
|
||||
} else if (value instanceof Bundle) {
|
||||
intent.putExtra(key, (Bundle) value);
|
||||
} else if (value instanceof int[]) {
|
||||
intent.putExtra(key, (int[]) value);
|
||||
} else if (value instanceof long[]) {
|
||||
intent.putExtra(key, (long[]) value);
|
||||
} else if (value instanceof boolean[]) {
|
||||
intent.putExtra(key, (boolean[]) value);
|
||||
} else if (value instanceof double[]) {
|
||||
intent.putExtra(key, (double[]) value);
|
||||
} else if (value instanceof float[]) {
|
||||
intent.putExtra(key, (float[]) value);
|
||||
} else if (value instanceof String[]) {
|
||||
intent.putExtra(key, (String[]) value);
|
||||
} else {
|
||||
intent.putExtra(key, value.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.journeyapps.barcodescanner;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@@ -33,6 +35,39 @@ public class Size implements Comparable<Size> {
|
||||
return new Size(width * n / d, height * n / d);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scales the dimensions so that it fits entirely inside the parent.One of width or height will
|
||||
* fit exactly. Aspect ratio is preserved.
|
||||
*
|
||||
* @param into the parent to fit into
|
||||
* @return the scaled size
|
||||
*/
|
||||
public Size scaleFit(Size into) {
|
||||
if (width * into.height >= into.width * height) {
|
||||
// match width
|
||||
return new Size(into.width, height * into.width / width);
|
||||
} else {
|
||||
// match height
|
||||
return new Size(width * into.height / height, into.height);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Scales the size so that both dimensions will be greater than or equal to the corresponding
|
||||
* dimension of the parent. One of width or height will fit exactly. Aspect ratio is preserved.
|
||||
*
|
||||
* @param into the parent to fit into
|
||||
* @return the scaled size
|
||||
*/
|
||||
public Size scaleCrop(Size into) {
|
||||
if (width * into.height <= into.width * height) {
|
||||
// match width
|
||||
return new Size(into.width, height * into.width / width);
|
||||
} else {
|
||||
// match height
|
||||
return new Size(width * into.height / height, into.height);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if both dimensions of the other size are at least as large as this size.
|
||||
*
|
||||
@@ -47,7 +82,7 @@ public class Size implements Comparable<Size> {
|
||||
* Default sort order is ascending by size.
|
||||
*/
|
||||
@Override
|
||||
public int compareTo(Size other) {
|
||||
public int compareTo(@NonNull Size other) {
|
||||
int aPixels = this.height * this.width;
|
||||
int bPixels = other.height * other.width;
|
||||
if (bPixels < aPixels) {
|
||||
@@ -70,9 +105,7 @@ public class Size implements Comparable<Size> {
|
||||
|
||||
Size size = (Size) o;
|
||||
|
||||
if (width != size.width) return false;
|
||||
return height == size.height;
|
||||
|
||||
return width == size.width && height == size.height;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -7,6 +7,7 @@ import android.graphics.Rect;
|
||||
import android.graphics.YuvImage;
|
||||
|
||||
import com.google.zxing.PlanarYUVLuminanceSource;
|
||||
import com.google.zxing.ResultPoint;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
@@ -14,14 +15,7 @@ import java.io.ByteArrayOutputStream;
|
||||
* Raw preview data from a camera.
|
||||
*/
|
||||
public class SourceData {
|
||||
/** Raw YUV data */
|
||||
private byte[] data;
|
||||
|
||||
/** Source data width */
|
||||
private int dataWidth;
|
||||
|
||||
/** Source data height */
|
||||
private int dataHeight;
|
||||
private RawImageData data;
|
||||
|
||||
/** The format of the image data. ImageFormat.NV21 and ImageFormat.YUY2 are supported. */
|
||||
private int imageFormat;
|
||||
@@ -32,6 +26,13 @@ public class SourceData {
|
||||
/** Crop rectangle, in display orientation. */
|
||||
private Rect cropRect;
|
||||
|
||||
/**
|
||||
* Factor by which to scale down before decoding.
|
||||
*/
|
||||
private int scalingFactor = 1;
|
||||
|
||||
private boolean previewMirrored;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param data the image data
|
||||
@@ -41,11 +42,12 @@ public class SourceData {
|
||||
* @param rotation camera rotation relative to display rotation, in degrees (0, 90, 180 or 270).
|
||||
*/
|
||||
public SourceData(byte[] data, int dataWidth, int dataHeight, int imageFormat, int rotation) {
|
||||
this.data = data;
|
||||
this.dataWidth = dataWidth;
|
||||
this.dataHeight = dataHeight;
|
||||
this.data = new RawImageData(data, dataWidth, dataHeight);
|
||||
this.rotation = rotation;
|
||||
this.imageFormat = imageFormat;
|
||||
if (dataWidth * dataHeight > data.length) {
|
||||
throw new IllegalArgumentException("Image data does not match the resolution. " + dataWidth + "x" + dataHeight + " > " + data.length);
|
||||
}
|
||||
}
|
||||
|
||||
public Rect getCropRect() {
|
||||
@@ -61,8 +63,24 @@ public class SourceData {
|
||||
this.cropRect = cropRect;
|
||||
}
|
||||
|
||||
public boolean isPreviewMirrored() {
|
||||
return previewMirrored;
|
||||
}
|
||||
|
||||
public void setPreviewMirrored(boolean previewMirrored) {
|
||||
this.previewMirrored = previewMirrored;
|
||||
}
|
||||
|
||||
public int getScalingFactor() {
|
||||
return scalingFactor;
|
||||
}
|
||||
|
||||
public void setScalingFactor(int scalingFactor) {
|
||||
this.scalingFactor = scalingFactor;
|
||||
}
|
||||
|
||||
public byte[] getData() {
|
||||
return data;
|
||||
return data.getData();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -70,7 +88,7 @@ public class SourceData {
|
||||
* @return width of the data
|
||||
*/
|
||||
public int getDataWidth() {
|
||||
return dataWidth;
|
||||
return data.getWidth();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,7 +96,16 @@ public class SourceData {
|
||||
* @return height of the data
|
||||
*/
|
||||
public int getDataHeight() {
|
||||
return dataHeight;
|
||||
return data.getHeight();
|
||||
}
|
||||
|
||||
public ResultPoint translateResultPoint(ResultPoint point) {
|
||||
float x = point.getX() * this.scalingFactor + this.cropRect.left;
|
||||
float y = point.getY() * this.scalingFactor + this.cropRect.top;
|
||||
if (previewMirrored) {
|
||||
x = data.getWidth() - x;
|
||||
}
|
||||
return new ResultPoint(x, y);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -94,15 +121,11 @@ public class SourceData {
|
||||
}
|
||||
|
||||
public PlanarYUVLuminanceSource createSource() {
|
||||
byte[] rotated = rotateCameraPreview(rotation, data, dataWidth, dataHeight);
|
||||
// TODO: handle mirrored (front) camera. Probably only the ResultPoints should be mirrored,
|
||||
RawImageData rotated = this.data.rotateCameraPreview(rotation);
|
||||
RawImageData scaled = rotated.cropAndScale(this.cropRect, this.scalingFactor);
|
||||
|
||||
// not the preview for decoding.
|
||||
if (isRotated()) {
|
||||
//noinspection SuspiciousNameCombination
|
||||
return new PlanarYUVLuminanceSource(rotated, dataHeight, dataWidth, cropRect.left, cropRect.top, cropRect.width(), cropRect.height(), false);
|
||||
} else {
|
||||
return new PlanarYUVLuminanceSource(rotated, dataWidth, dataHeight, cropRect.left, cropRect.top, cropRect.width(), cropRect.height(), false);
|
||||
}
|
||||
return new PlanarYUVLuminanceSource(scaled.getData(), scaled.getWidth(), scaled.getHeight(), 0, 0, scaled.getWidth(), scaled.getHeight(), false);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -124,14 +147,16 @@ public class SourceData {
|
||||
return getBitmap(cropRect, scaleFactor);
|
||||
}
|
||||
|
||||
private Bitmap getBitmap(Rect cropRect, int scaleFactor) {
|
||||
if(isRotated()) {
|
||||
public Bitmap getBitmap(Rect cropRect, int scaleFactor) {
|
||||
if (cropRect == null) {
|
||||
cropRect = new Rect(0, 0, data.getWidth(), data.getHeight());
|
||||
} else if (isRotated()) {
|
||||
//noinspection SuspiciousNameCombination
|
||||
cropRect = new Rect(cropRect.top, cropRect.left, cropRect.bottom, cropRect.right);
|
||||
}
|
||||
|
||||
// TODO: there should be a way to do this without JPEG compression / decompression cycle.
|
||||
YuvImage img = new YuvImage(data, imageFormat, dataWidth, dataHeight, null);
|
||||
YuvImage img = new YuvImage(data.getData(), imageFormat, data.getWidth(), data.getHeight(), null);
|
||||
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||
img.compressToJpeg(cropRect, 90, buffer);
|
||||
byte[] jpegData = buffer.toByteArray();
|
||||
@@ -150,84 +175,4 @@ public class SourceData {
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
public static byte[] rotateCameraPreview(int cameraRotation, byte[] data, int imageWidth, int imageHeight) {
|
||||
switch (cameraRotation) {
|
||||
case 0:
|
||||
return data;
|
||||
case 90:
|
||||
return rotateCW(data, imageWidth, imageHeight);
|
||||
case 180:
|
||||
return rotate180(data, imageWidth, imageHeight);
|
||||
case 270:
|
||||
return rotateCCW(data, imageWidth, imageHeight);
|
||||
default:
|
||||
// Should not happen
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotate an image by 90 degrees CW.
|
||||
*
|
||||
* @param data the image data, in with the first width * height bytes being the luminance data.
|
||||
* @param imageWidth the width of the image
|
||||
* @param imageHeight the height of the image
|
||||
* @return the rotated bytes
|
||||
*/
|
||||
public static byte[] rotateCW(byte[] data, int imageWidth, int imageHeight) {
|
||||
// Adapted from http://stackoverflow.com/a/15775173
|
||||
// data may contain more than just y (u and v), but we are only interested in the y section.
|
||||
|
||||
byte[] yuv = new byte[imageWidth * imageHeight];
|
||||
int i = 0;
|
||||
for (int x = 0; x < imageWidth; x++) {
|
||||
for (int y = imageHeight - 1; y >= 0; y--) {
|
||||
yuv[i] = data[y * imageWidth + x];
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return yuv;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotate an image by 180 degrees.
|
||||
*
|
||||
* @param data the image data, in with the first width * height bytes being the luminance data.
|
||||
* @param imageWidth the width of the image
|
||||
* @param imageHeight the height of the image
|
||||
* @return the rotated bytes
|
||||
*/
|
||||
public static byte[] rotate180(byte[] data, int imageWidth, int imageHeight) {
|
||||
int n = imageWidth * imageHeight;
|
||||
byte[] yuv = new byte[n];
|
||||
|
||||
int i = n - 1;
|
||||
for (int j = 0; j < n; j++) {
|
||||
yuv[i] = data[j];
|
||||
i--;
|
||||
}
|
||||
return yuv;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotate an image by 90 degrees CCW.
|
||||
*
|
||||
* @param data the image data, in with the first width * height bytes being the luminance data.
|
||||
* @param imageWidth the width of the image
|
||||
* @param imageHeight the height of the image
|
||||
* @return the rotated bytes
|
||||
*/
|
||||
public static byte[] rotateCCW(byte[] data, int imageWidth, int imageHeight) {
|
||||
int n = imageWidth * imageHeight;
|
||||
byte[] yuv = new byte[n];
|
||||
int i = n - 1;
|
||||
for (int x = 0; x < imageWidth; x++) {
|
||||
for (int y = imageHeight - 1; y >= 0; y--) {
|
||||
yuv[i] = data[y * imageWidth + x];
|
||||
i--;
|
||||
}
|
||||
}
|
||||
return yuv;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,9 +16,9 @@
|
||||
|
||||
package com.journeyapps.barcodescanner;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
@@ -38,25 +38,31 @@ import java.util.List;
|
||||
*
|
||||
* @author dswitkin@google.com (Daniel Switkin)
|
||||
*/
|
||||
public final class ViewfinderView extends View {
|
||||
private static final String TAG = ViewfinderView.class.getSimpleName();
|
||||
public class ViewfinderView extends View {
|
||||
protected static final String TAG = ViewfinderView.class.getSimpleName();
|
||||
|
||||
private static final int[] SCANNER_ALPHA = {0, 64, 128, 192, 255, 192, 128, 64};
|
||||
private static final long ANIMATION_DELAY = 80L;
|
||||
private static final int CURRENT_POINT_OPACITY = 0xA0;
|
||||
private static final int MAX_RESULT_POINTS = 20;
|
||||
private static final int POINT_SIZE = 6;
|
||||
protected static final int[] SCANNER_ALPHA = {0, 64, 128, 192, 255, 192, 128, 64};
|
||||
protected static final long ANIMATION_DELAY = 80L;
|
||||
protected static final int CURRENT_POINT_OPACITY = 0xA0;
|
||||
protected static final int MAX_RESULT_POINTS = 20;
|
||||
protected static final int POINT_SIZE = 6;
|
||||
|
||||
private final Paint paint;
|
||||
private Bitmap resultBitmap;
|
||||
private final int maskColor;
|
||||
private final int resultColor;
|
||||
private final int laserColor;
|
||||
private final int resultPointColor;
|
||||
private int scannerAlpha;
|
||||
private List<ResultPoint> possibleResultPoints;
|
||||
private List<ResultPoint> lastPossibleResultPoints;
|
||||
private CameraPreview cameraPreview;
|
||||
protected final Paint paint;
|
||||
protected Bitmap resultBitmap;
|
||||
protected int maskColor;
|
||||
protected final int resultColor;
|
||||
protected final int laserColor;
|
||||
protected final int resultPointColor;
|
||||
protected boolean laserVisibility;
|
||||
protected int scannerAlpha;
|
||||
protected List<ResultPoint> possibleResultPoints;
|
||||
protected List<ResultPoint> lastPossibleResultPoints;
|
||||
protected CameraPreview cameraPreview;
|
||||
|
||||
// Cache the framingRect and previewSize, so that we can still draw it after the preview
|
||||
// stopped.
|
||||
protected Rect framingRect;
|
||||
protected Size previewSize;
|
||||
|
||||
// This constructor is used when the class is built from an XML resource.
|
||||
public ViewfinderView(Context context, AttributeSet attrs) {
|
||||
@@ -64,14 +70,28 @@ public final class ViewfinderView extends View {
|
||||
|
||||
// Initialize these once for performance rather than calling them every time in onDraw().
|
||||
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||
|
||||
Resources resources = getResources();
|
||||
maskColor = resources.getColor(R.color.zxing_viewfinder_mask);
|
||||
resultColor = resources.getColor(R.color.zxing_result_view);
|
||||
laserColor = resources.getColor(R.color.zxing_viewfinder_laser);
|
||||
resultPointColor = resources.getColor(R.color.zxing_possible_result_points);
|
||||
|
||||
// Get set attributes on view
|
||||
TypedArray attributes = getContext().obtainStyledAttributes(attrs, R.styleable.zxing_finder);
|
||||
|
||||
this.maskColor = attributes.getColor(R.styleable.zxing_finder_zxing_viewfinder_mask,
|
||||
resources.getColor(R.color.zxing_viewfinder_mask));
|
||||
this.resultColor = attributes.getColor(R.styleable.zxing_finder_zxing_result_view,
|
||||
resources.getColor(R.color.zxing_result_view));
|
||||
this.laserColor = attributes.getColor(R.styleable.zxing_finder_zxing_viewfinder_laser,
|
||||
resources.getColor(R.color.zxing_viewfinder_laser));
|
||||
this.resultPointColor = attributes.getColor(R.styleable.zxing_finder_zxing_possible_result_points,
|
||||
resources.getColor(R.color.zxing_possible_result_points));
|
||||
this.laserVisibility = attributes.getBoolean(R.styleable.zxing_finder_zxing_viewfinder_laser_visibility,
|
||||
true);
|
||||
|
||||
attributes.recycle();
|
||||
|
||||
scannerAlpha = 0;
|
||||
possibleResultPoints = new ArrayList<>(5);
|
||||
lastPossibleResultPoints = null;
|
||||
possibleResultPoints = new ArrayList<>(MAX_RESULT_POINTS);
|
||||
lastPossibleResultPoints = new ArrayList<>(MAX_RESULT_POINTS);
|
||||
}
|
||||
|
||||
public void setCameraPreview(CameraPreview view) {
|
||||
@@ -79,6 +99,7 @@ public final class ViewfinderView extends View {
|
||||
view.addStateListener(new CameraPreview.StateListener() {
|
||||
@Override
|
||||
public void previewSized() {
|
||||
refreshSizes();
|
||||
invalidate();
|
||||
}
|
||||
|
||||
@@ -96,22 +117,38 @@ public final class ViewfinderView extends View {
|
||||
public void cameraError(Exception error) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cameraClosed() {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected void refreshSizes() {
|
||||
if (cameraPreview == null) {
|
||||
return;
|
||||
}
|
||||
Rect framingRect = cameraPreview.getFramingRect();
|
||||
Size previewSize = cameraPreview.getPreviewSize();
|
||||
if (framingRect != null && previewSize != null) {
|
||||
this.framingRect = framingRect;
|
||||
this.previewSize = previewSize;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("DrawAllocation")
|
||||
@Override
|
||||
public void onDraw(Canvas canvas) {
|
||||
if (cameraPreview == null || cameraPreview.getPreviewFramingRect() == null || cameraPreview.getFramingRect() == null) {
|
||||
refreshSizes();
|
||||
if (framingRect == null || previewSize == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Rect frame = cameraPreview.getFramingRect();
|
||||
Rect previewFrame = cameraPreview.getPreviewFramingRect();
|
||||
final Rect frame = framingRect;
|
||||
final Size previewSize = this.previewSize;
|
||||
|
||||
int width = canvas.getWidth();
|
||||
int height = canvas.getHeight();
|
||||
final int width = getWidth();
|
||||
final int height = getHeight();
|
||||
|
||||
// Draw the exterior (i.e. outside the framing rect) darkened
|
||||
paint.setColor(resultBitmap != null ? resultColor : maskColor);
|
||||
@@ -125,43 +162,52 @@ public final class ViewfinderView extends View {
|
||||
paint.setAlpha(CURRENT_POINT_OPACITY);
|
||||
canvas.drawBitmap(resultBitmap, null, frame, paint);
|
||||
} else {
|
||||
// If wanted, draw a red "laser scanner" line through the middle to show decoding is active
|
||||
if (laserVisibility) {
|
||||
paint.setColor(laserColor);
|
||||
|
||||
// Draw a red "laser scanner" line through the middle to show decoding is active
|
||||
paint.setColor(laserColor);
|
||||
paint.setAlpha(SCANNER_ALPHA[scannerAlpha]);
|
||||
scannerAlpha = (scannerAlpha + 1) % SCANNER_ALPHA.length;
|
||||
int middle = frame.height() / 2 + frame.top;
|
||||
canvas.drawRect(frame.left + 2, middle - 1, frame.right - 1, middle + 2, paint);
|
||||
paint.setAlpha(SCANNER_ALPHA[scannerAlpha]);
|
||||
scannerAlpha = (scannerAlpha + 1) % SCANNER_ALPHA.length;
|
||||
|
||||
float scaleX = frame.width() / (float) previewFrame.width();
|
||||
float scaleY = frame.height() / (float) previewFrame.height();
|
||||
|
||||
List<ResultPoint> currentPossible = possibleResultPoints;
|
||||
List<ResultPoint> currentLast = lastPossibleResultPoints;
|
||||
int frameLeft = frame.left;
|
||||
int frameTop = frame.top;
|
||||
if (currentPossible.isEmpty()) {
|
||||
lastPossibleResultPoints = null;
|
||||
} else {
|
||||
possibleResultPoints = new ArrayList<>(5);
|
||||
lastPossibleResultPoints = currentPossible;
|
||||
paint.setAlpha(CURRENT_POINT_OPACITY);
|
||||
paint.setColor(resultPointColor);
|
||||
for (ResultPoint point : currentPossible) {
|
||||
canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX),
|
||||
frameTop + (int) (point.getY() * scaleY),
|
||||
POINT_SIZE, paint);
|
||||
}
|
||||
final int middle = frame.height() / 2 + frame.top;
|
||||
canvas.drawRect(frame.left + 2, middle - 1, frame.right - 1, middle + 2, paint);
|
||||
}
|
||||
if (currentLast != null) {
|
||||
|
||||
final float scaleX = this.getWidth() / (float) previewSize.width;
|
||||
final float scaleY = this.getHeight() / (float) previewSize.height;
|
||||
|
||||
// draw the last possible result points
|
||||
if (!lastPossibleResultPoints.isEmpty()) {
|
||||
paint.setAlpha(CURRENT_POINT_OPACITY / 2);
|
||||
paint.setColor(resultPointColor);
|
||||
float radius = POINT_SIZE / 2.0f;
|
||||
for (ResultPoint point : currentLast) {
|
||||
canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX),
|
||||
frameTop + (int) (point.getY() * scaleY),
|
||||
radius, paint);
|
||||
for (final ResultPoint point : lastPossibleResultPoints) {
|
||||
canvas.drawCircle(
|
||||
(int) (point.getX() * scaleX),
|
||||
(int) (point.getY() * scaleY),
|
||||
radius, paint
|
||||
);
|
||||
}
|
||||
lastPossibleResultPoints.clear();
|
||||
}
|
||||
|
||||
// draw current possible result points
|
||||
if (!possibleResultPoints.isEmpty()) {
|
||||
paint.setAlpha(CURRENT_POINT_OPACITY);
|
||||
paint.setColor(resultPointColor);
|
||||
for (final ResultPoint point : possibleResultPoints) {
|
||||
canvas.drawCircle(
|
||||
(int) (point.getX() * scaleX),
|
||||
(int) (point.getY() * scaleY),
|
||||
POINT_SIZE, paint
|
||||
);
|
||||
}
|
||||
|
||||
// swap and clear buffers
|
||||
final List<ResultPoint> temp = possibleResultPoints;
|
||||
possibleResultPoints = lastPossibleResultPoints;
|
||||
lastPossibleResultPoints = temp;
|
||||
possibleResultPoints.clear();
|
||||
}
|
||||
|
||||
// Request another update at the animation interval, but only repaint the laser line,
|
||||
@@ -199,13 +245,15 @@ public final class ViewfinderView extends View {
|
||||
* @param point a point to draw, relative to the preview frame
|
||||
*/
|
||||
public void addPossibleResultPoint(ResultPoint point) {
|
||||
List<ResultPoint> points = possibleResultPoints;
|
||||
points.add(point);
|
||||
int size = points.size();
|
||||
if (size > MAX_RESULT_POINTS) {
|
||||
// trim it
|
||||
points.subList(0, size - MAX_RESULT_POINTS / 2).clear();
|
||||
}
|
||||
if (possibleResultPoints.size() < MAX_RESULT_POINTS)
|
||||
possibleResultPoints.add(point);
|
||||
}
|
||||
|
||||
public void setMaskColor(int maskColor) {
|
||||
this.maskColor = maskColor;
|
||||
}
|
||||
|
||||
public void setLaserVisibility(boolean visible) {
|
||||
this.laserVisibility = visible;
|
||||
}
|
||||
}
|
||||
|
||||
+3
-8
@@ -64,12 +64,9 @@ public final class AutoFocusManager {
|
||||
private final Camera.AutoFocusCallback autoFocusCallback = new Camera.AutoFocusCallback() {
|
||||
@Override
|
||||
public void onAutoFocus(boolean success, Camera theCamera) {
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
focusing = false;
|
||||
autoFocusAgainLater();
|
||||
}
|
||||
handler.post(() -> {
|
||||
focusing = false;
|
||||
autoFocusAgainLater();
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -83,7 +80,6 @@ public final class AutoFocusManager {
|
||||
start();
|
||||
}
|
||||
|
||||
|
||||
private synchronized void autoFocusAgainLater() {
|
||||
if (!stopped && !handler.hasMessages(MESSAGE_FOCUS)) {
|
||||
handler.sendMessageDelayed(handler.obtainMessage(MESSAGE_FOCUS), AUTO_FOCUS_INTERVAL_MS);
|
||||
@@ -135,5 +131,4 @@ public final class AutoFocusManager {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+25
-17
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.google.zxing.client.android.camera;
|
||||
package com.journeyapps.barcodescanner.camera;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.graphics.Rect;
|
||||
@@ -22,6 +22,8 @@ import android.hardware.Camera;
|
||||
import android.os.Build;
|
||||
import android.util.Log;
|
||||
|
||||
import com.journeyapps.barcodescanner.camera.CameraSettings;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
@@ -50,24 +52,31 @@ public final class CameraConfigurationUtils {
|
||||
}
|
||||
|
||||
public static void setFocus(Camera.Parameters parameters,
|
||||
boolean autoFocus,
|
||||
boolean disableContinuous,
|
||||
CameraSettings.FocusMode focusModeSetting,
|
||||
boolean safeMode) {
|
||||
List<String> supportedFocusModes = parameters.getSupportedFocusModes();
|
||||
String focusMode = null;
|
||||
if (autoFocus) {
|
||||
if (safeMode || disableContinuous) {
|
||||
focusMode = findSettableValue("focus mode",
|
||||
supportedFocusModes,
|
||||
Camera.Parameters.FOCUS_MODE_AUTO);
|
||||
} else {
|
||||
focusMode = findSettableValue("focus mode",
|
||||
supportedFocusModes,
|
||||
Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE,
|
||||
Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO,
|
||||
Camera.Parameters.FOCUS_MODE_AUTO);
|
||||
}
|
||||
}
|
||||
|
||||
if (safeMode || focusModeSetting == CameraSettings.FocusMode.AUTO) {
|
||||
focusMode = findSettableValue("focus mode",
|
||||
supportedFocusModes,
|
||||
Camera.Parameters.FOCUS_MODE_AUTO);
|
||||
} else if (focusModeSetting == CameraSettings.FocusMode.CONTINUOUS) {
|
||||
focusMode = findSettableValue("focus mode",
|
||||
supportedFocusModes,
|
||||
Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE,
|
||||
Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO,
|
||||
Camera.Parameters.FOCUS_MODE_AUTO);
|
||||
} else if (focusModeSetting == CameraSettings.FocusMode.INFINITY) {
|
||||
focusMode = findSettableValue("focus mode",
|
||||
supportedFocusModes,
|
||||
Camera.Parameters.FOCUS_MODE_INFINITY);
|
||||
} else if (focusModeSetting == CameraSettings.FocusMode.MACRO) {
|
||||
focusMode = findSettableValue("focus mode",
|
||||
supportedFocusModes,
|
||||
Camera.Parameters.FOCUS_MODE_MACRO);
|
||||
}
|
||||
|
||||
// Maybe selected auto-focus but not available, so fall through here:
|
||||
if (!safeMode && focusMode == null) {
|
||||
focusMode = findSettableValue("focus mode",
|
||||
@@ -354,5 +363,4 @@ public final class CameraConfigurationUtils {
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
}
|
||||
+90
-17
@@ -7,29 +7,53 @@ import android.view.SurfaceHolder;
|
||||
|
||||
import com.google.zxing.client.android.R;
|
||||
import com.journeyapps.barcodescanner.Size;
|
||||
import com.journeyapps.barcodescanner.SourceData;
|
||||
import com.journeyapps.barcodescanner.Util;
|
||||
|
||||
/**
|
||||
* Manage a camera instance using a background thread.
|
||||
*
|
||||
* All methods must be called from the main thread.
|
||||
*/
|
||||
public class CameraInstance {
|
||||
private static final String TAG = CameraInstance.class.getSimpleName();
|
||||
|
||||
private CameraThread cameraThread;
|
||||
private SurfaceHolder surfaceHolder;
|
||||
private CameraSurface surface;
|
||||
|
||||
private CameraManager cameraManager;
|
||||
private Handler readyHandler;
|
||||
private DisplayConfiguration displayConfiguration;
|
||||
private boolean open = false;
|
||||
private boolean cameraClosed = true;
|
||||
private Handler mainHandler;
|
||||
|
||||
private CameraSettings cameraSettings = new CameraSettings();
|
||||
|
||||
/**
|
||||
* Construct a new CameraInstance.
|
||||
*
|
||||
* A new CameraManager is created.
|
||||
*
|
||||
* @param context the Android Context
|
||||
*/
|
||||
public CameraInstance(Context context) {
|
||||
Util.validateMainThread();
|
||||
|
||||
this.cameraThread = CameraThread.getInstance();
|
||||
this.cameraManager = new CameraManager(context);
|
||||
this.cameraManager.setCameraSettings(cameraSettings);
|
||||
this.mainHandler = new Handler();
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a new CameraInstance with a specific CameraManager.
|
||||
*
|
||||
* @param cameraManager the CameraManager to use
|
||||
*/
|
||||
public CameraInstance(CameraManager cameraManager) {
|
||||
Util.validateMainThread();
|
||||
|
||||
this.cameraManager = cameraManager;
|
||||
}
|
||||
|
||||
public void setDisplayConfiguration(DisplayConfiguration configuration) {
|
||||
@@ -46,7 +70,11 @@ public class CameraInstance {
|
||||
}
|
||||
|
||||
public void setSurfaceHolder(SurfaceHolder surfaceHolder) {
|
||||
this.surfaceHolder = surfaceHolder;
|
||||
setSurface(new CameraSurface(surfaceHolder));
|
||||
}
|
||||
|
||||
public void setSurface(CameraSurface surface) {
|
||||
this.surface = surface;
|
||||
}
|
||||
|
||||
public CameraSettings getCameraSettings() {
|
||||
@@ -87,6 +115,7 @@ public class CameraInstance {
|
||||
Util.validateMainThread();
|
||||
|
||||
open = true;
|
||||
cameraClosed = false;
|
||||
|
||||
cameraThread.incrementAndEnqueue(opener);
|
||||
}
|
||||
@@ -109,12 +138,20 @@ public class CameraInstance {
|
||||
Util.validateMainThread();
|
||||
|
||||
if (open) {
|
||||
cameraThread.enqueue(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
cameraManager.setTorch(on);
|
||||
}
|
||||
});
|
||||
cameraThread.enqueue(() -> cameraManager.setTorch(on));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the settings for Camera.
|
||||
*
|
||||
* @param callback {@link CameraParametersCallback}
|
||||
*/
|
||||
public void changeCameraParameters(final CameraParametersCallback callback) {
|
||||
Util.validateMainThread();
|
||||
|
||||
if (open) {
|
||||
cameraThread.enqueue(() -> cameraManager.changeCameraParameters(callback));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,6 +160,8 @@ public class CameraInstance {
|
||||
|
||||
if (open) {
|
||||
cameraThread.enqueue(closer);
|
||||
} else {
|
||||
cameraClosed = true;
|
||||
}
|
||||
|
||||
open = false;
|
||||
@@ -132,14 +171,18 @@ public class CameraInstance {
|
||||
return open;
|
||||
}
|
||||
|
||||
public void requestPreview(final PreviewCallback callback) {
|
||||
validateOpen();
|
||||
public boolean isCameraClosed() {
|
||||
return cameraClosed;
|
||||
}
|
||||
|
||||
cameraThread.enqueue(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
cameraManager.requestPreviewFrame(callback);
|
||||
public void requestPreview(final PreviewCallback callback) {
|
||||
mainHandler.post(() -> {
|
||||
if (!open) {
|
||||
Log.d(TAG, "Camera is closed, not requesting preview");
|
||||
return;
|
||||
}
|
||||
|
||||
cameraThread.enqueue(() -> cameraManager.requestPreviewFrame(callback));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -149,7 +192,6 @@ public class CameraInstance {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Runnable opener = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
@@ -184,7 +226,7 @@ public class CameraInstance {
|
||||
public void run() {
|
||||
try {
|
||||
Log.d(TAG, "Starting preview");
|
||||
cameraManager.setPreviewDisplay(surfaceHolder);
|
||||
cameraManager.setPreviewDisplay(surface);
|
||||
cameraManager.startPreview();
|
||||
} catch (Exception e) {
|
||||
notifyError(e);
|
||||
@@ -204,6 +246,10 @@ public class CameraInstance {
|
||||
Log.e(TAG, "Failed to close camera", e);
|
||||
}
|
||||
|
||||
cameraClosed = true;
|
||||
|
||||
readyHandler.sendEmptyMessage(R.id.zxing_camera_closed);
|
||||
|
||||
cameraThread.decrementInstances();
|
||||
}
|
||||
};
|
||||
@@ -213,4 +259,31 @@ public class CameraInstance {
|
||||
readyHandler.obtainMessage(R.id.zxing_camera_error, error).sendToTarget();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the CameraManager used to control the camera.
|
||||
*
|
||||
* The CameraManager is not thread-safe, and must only be used from the CameraThread.
|
||||
*
|
||||
* @return the CameraManager used
|
||||
*/
|
||||
protected CameraManager getCameraManager() {
|
||||
return cameraManager;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return the CameraThread used to manage the camera
|
||||
*/
|
||||
protected CameraThread getCameraThread() {
|
||||
return cameraThread;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return the surface om which the preview is displayed
|
||||
*/
|
||||
protected CameraSurface getSurface() {
|
||||
return surface;
|
||||
}
|
||||
}
|
||||
|
||||
+100
-34
@@ -19,14 +19,11 @@ package com.journeyapps.barcodescanner.camera;
|
||||
import android.content.Context;
|
||||
import android.hardware.Camera;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.util.Log;
|
||||
import android.view.Surface;
|
||||
import android.view.SurfaceHolder;
|
||||
|
||||
import com.google.zxing.client.android.AmbientLightManager;
|
||||
import com.google.zxing.client.android.camera.CameraConfigurationUtils;
|
||||
import com.google.zxing.client.android.camera.open.OpenCameraInterface;
|
||||
import com.journeyapps.barcodescanner.Size;
|
||||
import com.journeyapps.barcodescanner.SourceData;
|
||||
@@ -36,6 +33,10 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Wrapper to manage the Camera. This is not thread-safe, and the methods must always be called
|
||||
* from the same thread.
|
||||
*
|
||||
*
|
||||
* Call order:
|
||||
*
|
||||
* 1. setCameraSettings()
|
||||
@@ -94,11 +95,30 @@ public final class CameraManager {
|
||||
Size cameraResolution = resolution;
|
||||
PreviewCallback callback = this.callback;
|
||||
if (cameraResolution != null && callback != null) {
|
||||
int format = camera.getParameters().getPreviewFormat();
|
||||
SourceData source = new SourceData(data, cameraResolution.width, cameraResolution.height, format, getCameraRotation());
|
||||
callback.onPreview(source);
|
||||
try {
|
||||
if (data == null) {
|
||||
throw new NullPointerException("No preview data received");
|
||||
}
|
||||
int format = camera.getParameters().getPreviewFormat();
|
||||
SourceData source = new SourceData(data, cameraResolution.width, cameraResolution.height, format, getCameraRotation());
|
||||
|
||||
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
|
||||
source.setPreviewMirrored(true);
|
||||
}
|
||||
callback.onPreview(source);
|
||||
} catch (RuntimeException e) {
|
||||
// Could be:
|
||||
// java.lang.RuntimeException: getParameters failed (empty parameters)
|
||||
// IllegalArgumentException: Image data does not match the resolution
|
||||
Log.e(TAG, "Camera preview failed", e);
|
||||
callback.onPreviewError(e);
|
||||
}
|
||||
} else {
|
||||
Log.d(TAG, "Got preview callback, but no handler or resolution available");
|
||||
if (callback != null) {
|
||||
// Should generally not happen
|
||||
callback.onPreviewError(new Exception("No resolution available"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -136,6 +156,9 @@ public final class CameraManager {
|
||||
* Must be called from camera thread.
|
||||
*/
|
||||
public void configure() {
|
||||
if (camera == null) {
|
||||
throw new RuntimeException("Camera not open");
|
||||
}
|
||||
setParameters();
|
||||
}
|
||||
|
||||
@@ -143,7 +166,11 @@ public final class CameraManager {
|
||||
* Must be called from camera thread.
|
||||
*/
|
||||
public void setPreviewDisplay(SurfaceHolder holder) throws IOException {
|
||||
camera.setPreviewDisplay(holder);
|
||||
setPreviewDisplay(new CameraSurface(holder));
|
||||
}
|
||||
|
||||
public void setPreviewDisplay(CameraSurface surface) throws IOException {
|
||||
surface.setPreview(camera);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -183,7 +210,6 @@ public final class CameraManager {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Closes the camera driver if still in use.
|
||||
*
|
||||
@@ -200,7 +226,7 @@ public final class CameraManager {
|
||||
* @return true if the camera rotation is perpendicular to the current display rotation.
|
||||
*/
|
||||
public boolean isCameraRotated() {
|
||||
if(rotationDegrees == -1) {
|
||||
if (rotationDegrees == -1) {
|
||||
throw new IllegalStateException("Rotation not calculated yet. Call configure() first.");
|
||||
}
|
||||
return rotationDegrees % 180 != 0;
|
||||
@@ -215,7 +241,6 @@ public final class CameraManager {
|
||||
return rotationDegrees;
|
||||
}
|
||||
|
||||
|
||||
private Camera.Parameters getDefaultCameraParameters() {
|
||||
Camera.Parameters parameters = camera.getParameters();
|
||||
if (defaultParameters == null) {
|
||||
@@ -241,8 +266,7 @@ public final class CameraManager {
|
||||
Log.w(TAG, "In camera config safe mode -- most settings will not be honored");
|
||||
}
|
||||
|
||||
|
||||
CameraConfigurationUtils.setFocus(parameters, settings.isAutoFocusEnabled(), !settings.isContinuousFocusEnabled(), safeMode);
|
||||
CameraConfigurationUtils.setFocus(parameters, settings.getFocusMode(), safeMode);
|
||||
|
||||
if (!safeMode) {
|
||||
CameraConfigurationUtils.setTorch(parameters, false);
|
||||
@@ -256,11 +280,9 @@ public final class CameraManager {
|
||||
}
|
||||
|
||||
if (settings.isMeteringEnabled()) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
|
||||
CameraConfigurationUtils.setVideoStabilization(parameters);
|
||||
CameraConfigurationUtils.setFocusArea(parameters);
|
||||
CameraConfigurationUtils.setMetering(parameters);
|
||||
}
|
||||
CameraConfigurationUtils.setVideoStabilization(parameters);
|
||||
CameraConfigurationUtils.setFocusArea(parameters);
|
||||
CameraConfigurationUtils.setMetering(parameters);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -274,6 +296,12 @@ public final class CameraManager {
|
||||
parameters.setPreviewSize(requestedPreviewSize.width, requestedPreviewSize.height);
|
||||
}
|
||||
|
||||
if (Build.DEVICE.equals("glass-1")) {
|
||||
// We need to set the FPS on Google Glass devices, otherwise the preview is scrambled.
|
||||
// FIXME - can/should we do this for other devices as well?
|
||||
CameraConfigurationUtils.setBestPreviewFPS(parameters);
|
||||
}
|
||||
|
||||
Log.i(TAG, "Final camera parameters: " + parameters.flatten());
|
||||
|
||||
camera.setParameters(parameters);
|
||||
@@ -285,7 +313,7 @@ public final class CameraManager {
|
||||
if (rawSupportedSizes == null) {
|
||||
Camera.Size defaultSize = parameters.getPreviewSize();
|
||||
if (defaultSize != null) {
|
||||
// Work around potential platform bugs
|
||||
Size previewSize = new Size(defaultSize.width, defaultSize.height);
|
||||
previewSizes.add(new Size(defaultSize.width, defaultSize.height));
|
||||
}
|
||||
return previewSizes;
|
||||
@@ -330,7 +358,6 @@ public final class CameraManager {
|
||||
camera.setDisplayOrientation(rotation);
|
||||
}
|
||||
|
||||
|
||||
private void setParameters() {
|
||||
try {
|
||||
this.rotationDegrees = calculateDisplayRotation();
|
||||
@@ -343,7 +370,7 @@ public final class CameraManager {
|
||||
} catch (Exception e) {
|
||||
// Failed, use safe mode
|
||||
try {
|
||||
setDesiredParameters(false);
|
||||
setDesiredParameters(true);
|
||||
} catch (Exception e2) {
|
||||
// Well, darn. Give up
|
||||
Log.w(TAG, "Camera rejected even safe-mode parameters! No configuration");
|
||||
@@ -359,7 +386,10 @@ public final class CameraManager {
|
||||
cameraPreviewCallback.setResolution(previewSize);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This returns false if the camera is not opened yet, failed to open, or has
|
||||
* been closed.
|
||||
*/
|
||||
public boolean isOpen() {
|
||||
return camera != null;
|
||||
}
|
||||
@@ -422,26 +452,52 @@ public final class CameraManager {
|
||||
|
||||
public void setTorch(boolean on) {
|
||||
if (camera != null) {
|
||||
boolean isOn = isTorchOn();
|
||||
if (on != isOn) {
|
||||
if (autoFocusManager != null) {
|
||||
autoFocusManager.stop();
|
||||
}
|
||||
try {
|
||||
boolean isOn = isTorchOn();
|
||||
if (on != isOn) {
|
||||
if (autoFocusManager != null) {
|
||||
autoFocusManager.stop();
|
||||
}
|
||||
|
||||
Camera.Parameters parameters = camera.getParameters();
|
||||
CameraConfigurationUtils.setTorch(parameters, on);
|
||||
if (settings.isExposureEnabled()) {
|
||||
CameraConfigurationUtils.setBestExposure(parameters, on);
|
||||
}
|
||||
camera.setParameters(parameters);
|
||||
Camera.Parameters parameters = camera.getParameters();
|
||||
CameraConfigurationUtils.setTorch(parameters, on);
|
||||
if (settings.isExposureEnabled()) {
|
||||
CameraConfigurationUtils.setBestExposure(parameters, on);
|
||||
}
|
||||
camera.setParameters(parameters);
|
||||
|
||||
if (autoFocusManager != null) {
|
||||
autoFocusManager.start();
|
||||
if (autoFocusManager != null) {
|
||||
autoFocusManager.start();
|
||||
}
|
||||
}
|
||||
} catch(RuntimeException e) {
|
||||
// Camera error. Could happen if the camera is being closed.
|
||||
Log.e(TAG, "Failed to set torch", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the settings for Camera.
|
||||
*
|
||||
* @param callback {@link CameraParametersCallback}
|
||||
*/
|
||||
public void changeCameraParameters(CameraParametersCallback callback) {
|
||||
if (camera != null) {
|
||||
try {
|
||||
camera.setParameters(callback.changeCameraParameters(camera.getParameters()));
|
||||
} catch(RuntimeException e) {
|
||||
// Camera error. Could happen if the camera is being closed.
|
||||
Log.e(TAG, "Failed to change camera parameters", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return true if the torch is on
|
||||
* @throws RuntimeException if there is a camera error
|
||||
*/
|
||||
public boolean isTorchOn() {
|
||||
Camera.Parameters parameters = camera.getParameters();
|
||||
if (parameters != null) {
|
||||
@@ -453,4 +509,14 @@ public final class CameraManager {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Camera. This returns null if the camera is not opened yet, failed to open, or has
|
||||
* been closed.
|
||||
*
|
||||
* @return the Camera
|
||||
*/
|
||||
public Camera getCamera() {
|
||||
return camera;
|
||||
}
|
||||
}
|
||||
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.journeyapps.barcodescanner.camera;
|
||||
|
||||
import android.hardware.Camera;
|
||||
|
||||
/**
|
||||
* Callback for {@link Camera.Parameters}.
|
||||
*/
|
||||
public interface CameraParametersCallback {
|
||||
|
||||
/**
|
||||
* Changes the settings for Camera.
|
||||
*
|
||||
* @param parameters {@link Camera.Parameters}.
|
||||
* @return {@link Camera.Parameters} with arguments.
|
||||
*/
|
||||
Camera.Parameters changeCameraParameters(Camera.Parameters parameters);
|
||||
}
|
||||
+36
-1
@@ -14,13 +14,19 @@ public class CameraSettings {
|
||||
private boolean continuousFocusEnabled = false;
|
||||
private boolean exposureEnabled = false;
|
||||
private boolean autoTorchEnabled = false;
|
||||
private FocusMode focusMode = FocusMode.AUTO;
|
||||
|
||||
public enum FocusMode {
|
||||
AUTO,
|
||||
CONTINUOUS,
|
||||
INFINITY,
|
||||
MACRO
|
||||
}
|
||||
|
||||
public int getRequestedCameraId() {
|
||||
return requestedCameraId;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Allows third party apps to specify the camera ID, rather than determine
|
||||
* it automatically based on available cameras and their orientation.
|
||||
@@ -98,6 +104,14 @@ public class CameraSettings {
|
||||
|
||||
public void setAutoFocusEnabled(boolean autoFocusEnabled) {
|
||||
this.autoFocusEnabled = autoFocusEnabled;
|
||||
|
||||
if (autoFocusEnabled && continuousFocusEnabled) {
|
||||
focusMode = FocusMode.CONTINUOUS;
|
||||
} else if (autoFocusEnabled) {
|
||||
focusMode = FocusMode.AUTO;
|
||||
} else {
|
||||
focusMode = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -111,6 +125,27 @@ public class CameraSettings {
|
||||
|
||||
public void setContinuousFocusEnabled(boolean continuousFocusEnabled) {
|
||||
this.continuousFocusEnabled = continuousFocusEnabled;
|
||||
|
||||
if (continuousFocusEnabled) {
|
||||
focusMode = FocusMode.CONTINUOUS;
|
||||
} else if (autoFocusEnabled) {
|
||||
focusMode = FocusMode.AUTO;
|
||||
} else {
|
||||
focusMode = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Default to FocusMode.AUTO.
|
||||
*
|
||||
* @return value of selected focus mode
|
||||
*/
|
||||
public FocusMode getFocusMode() {
|
||||
return focusMode;
|
||||
}
|
||||
|
||||
public void setFocusMode(FocusMode focusMode) {
|
||||
this.focusMode = focusMode;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user