项目作者: KingsMentor

项目描述 :
条形码扫描仪由Mobile Vision Api提供支持
高级语言: Java
项目地址: git://github.com/KingsMentor/MobileVisionBarcodeScanner.git
创建时间: 2016-10-11T14:38:42Z
项目社区:https://github.com/KingsMentor/MobileVisionBarcodeScanner

开源协议:MIT License

下载


MobileVisionBarcodeScanner

Barcode Scanner supported by Mobile Vision Api


Android Arsenal
Download

Lib Sample

Update 2.0.0

  • Bug fixes and Better Performance
  • Capturing scanned screen
  • support two or more barcode format
  • release a barcode detector
  • use the front camera for scanning
  • immediate refresh functionality
  • update to 11.0.4 mobile vision sdk

adding as a dependency

on JCenter

  1. dependencies {
  2. implementation ''xyz.belvi.mobilevision:barcodescanner:2.0.3''
  3. }

on JitPack

Step 1.

Add the JitPack repository to your build file in your root build.gradle at the end of repositories:

  1. allprojects {
  2. repositories {
  3. ...
  4. maven { url "https://jitpack.io" }
  5. }
  6. }
Step 2.

Add the dependency

  1. dependencies {
  2. compile 'com.github.KingsMentor:MobileVisionBarcodeScanner:2.0.0'
  3. }

Supported Attributes

  1. <attr name="gvb_show_text" format="boolean" ></attr>
  2. <attr name="gvb_draw" format="boolean" ></attr>
  3. <attr name="gvb_multiple" format="boolean" ></attr>
  4. <attr name="gvb_touch" format="boolean" ></attr>
  5. <attr name="gvb_auto_focus" format="boolean" ></attr>
  6. <attr name="gvb_flash" format="boolean" ></attr>
  7. <attr name="gvb_rect_colors" format="reference" ></attr>
  8. <attr name="gvb_camera_facing" format="enum">
  9. <enum name="back" value="0"></enum>
  10. <enum name="front" value="0"></enum>
  11. </attr>
  12. <attr name="gvb_code_format">
  13. <flag name="all_format" value="0"></flag>
  14. <flag name="code_128" value="1"></flag>
  15. <flag name="code_39" value="2"></flag>
  16. <flag name="code_93" value="4"></flag>
  17. <flag name="code_bar" value="8"></flag>
  18. <flag name="data_matrix" value="16"></flag>
  19. <flag name="ean_13" value="32"></flag>
  20. <flag name="ean_8" value="64"></flag>
  21. <flag name="itf" value="128"></flag>
  22. <flag name="qr_code" value="256"></flag>
  23. <flag name="upc_a" value="512"></flag>
  24. <flag name="upc_e" value="1024"></flag>
  25. <flag name="pdf417" value="2028"></flag>
  26. <flag name="aztec" value="4029"></flag>
  27. </attr>

What are the attibutes for :

  • gvb_draw - enable rect drawn around codes when scanning
  • gvb_multiple - want the camera to return as many qr codes that was scanned. This works with gvb_touch attribute. it only returns result when the screen is clicked or touch
  • gvb_touch - turn on touch listener for screen
  • gvb_auto_focus - support auto focus
  • gvb_flash - turn on flash
  • gvb_rect_colors - arrays of colors to draw rect
  • gvb_code_format - barcode format that should be support . Default is all_format

Note
these attributes can also be initialised from java code . We would look into that later

Using the Mobile Vision Powered Camera.

Step 1 - Add layout in xml:
  1. <fragment
  2. android:id="@+id/barcode"
  3. android:name="com.google.android.gms.samples.vision.barcodereader.BarcodeCapture"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. app:gvb_auto_focus="true"
  7. app:gvb_code_format="all_format"
  8. app:gvb_flash="false"
  9. app:gvb_rect_colors="@array/rect_color" ></fragment>

and this is rect_color in colors.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <color name="color_green">#14808d</color>
  4. <color name="color_brown">#343838</color>
  5. <color name="color_orange">#f38a32</color>
  6. <color name="color_blue">#1479b7</color>
  7. <color name="divider_grey">#e4e4e5</color>
  8. <array name="rect_color">
  9. <item>@color/color_blue</item>
  10. <item>@color/color_brown</item>
  11. <item>@color/color_green</item>
  12. <item>@color/divider_grey</item>
  13. <item>@color/color_orange</item>
  14. </array>
  15. </resources>
Step 2 - Initialise in java
  1. BarcodeCapture barcodeCapture = (BarcodeCapture) getSupportFragmentManager().findFragmentById(R.id.barcode);
  2. barcodeCapture.setRetrieval(this);

also make sure your java class implements BarcodeRetriever

  1. public class MainActivity extends AppCompatActivity implements BarcodeRetriever {
  2. ...
  3. // for one time scan
  4. @Override
  5. public void onRetrieved(final Barcode barcode) {
  6. Log.d(TAG, "Barcode read: " + barcode.displayValue);
  7. runOnUiThread(new Runnable() {
  8. @Override
  9. public void run() {
  10. AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
  11. .setTitle("code retrieved")
  12. .setMessage(barcode.displayValue);
  13. builder.show();
  14. }
  15. });
  16. }
  17. // for multiple callback
  18. @Override
  19. public void onRetrievedMultiple(final Barcode closetToClick, final List<BarcodeGraphic> barcodeGraphics) {
  20. runOnUiThread(new Runnable() {
  21. @Override
  22. public void run() {
  23. String message = "Code selected : " + closetToClick.displayValue + "\n\nother " +
  24. "codes in frame include : \n";
  25. for (int index = 0; index < barcodeGraphics.size(); index++) {
  26. Barcode barcode = barcodeGraphics.get(index).getBarcode();
  27. message += (index + 1) + ". " + barcode.displayValue + "\n";
  28. }
  29. AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
  30. .setTitle("code retrieved")
  31. .setMessage(message);
  32. builder.show();
  33. }
  34. });
  35. }
  36. @Override
  37. public void onBitmapScanned(SparseArray<Barcode> sparseArray) {
  38. // when image is scanned and processed
  39. }
  40. @Override
  41. public void onRetrievedFailed(String reason) {
  42. // in case of failure
  43. }
  44. }

as you can see, BarcodeRetriever interface handles the callback when a code is scanned successfully based on specified attributes.

Extras

  • To scan a bitmap,
  1. BarcodeBitmapScanner.scanBitmap(this, bitmap, Barcode.ALL_FORMATS, this);
  • Set attributes from java - Use the BarcodeCapture instance to reference setter methods
  1. barcodeCapture.setShowDrawRect(true);
  • Stop barcode detection
    1. barcodeCapture.stopScanning();
  • Retrieve Image of Scanned Screen. This will return a Camera object that you can use to retrieve other neccessary information:

    1. barcodeCapture.retrieveCamera();
  • Refresh - make update to barcodeCapture and use refresh for immediate screen update.

    1. barcodeCapture.setShowDrawRect(drawRect.isChecked())
    2. .setSupportMultipleScan(supportMultiple.isChecked())
    3. .setTouchAsCallback(touchBack.isChecked())
    4. .shouldAutoFocus(autoFocus.isChecked())
    5. .setShowFlash(flash.isChecked())
    6. .setBarcodeFormat(Barcode.ALL_FORMATS)
    7. .setCameraFacing(frontCam.isChecked() ? CameraSource.CAMERA_FACING_FRONT : CameraSource.CAMERA_FACING_BACK)
    8. .setShouldShowText(drawText.isChecked());
    9. barcodeCapture.refresh();

Resources and Credits

Contributions

Contributions are welcome. Generally, contributions are managed by issues and pull requests.

  1. Submit an issue describing your proposed fix or feature.
  2. If your proposed fix or feature is accepted then, fork, implement your code change.
  3. Ensure your code change follows the accepted code style and structure.
  4. Ensure your code is properly tested.
  5. Ensure your commits follow the accepted commit message style
  6. Submit a pull request.

License

The MIT License (MIT). Please see the License File for more information.