Try   HackMD

Android Studio - QR code scanning

  • Ander Liu
tags: Android Studio

Zxing

  • ZXing ("zebra crossing") is an open-source, multi-format 1D/2D barcode image processing library implemented in Java, with ports to other languages.
  • 這堂課,我們打算使用 Zxing 這個 library to implement QR code scanning
  • The Supported Formats of Zxing
1D product 1D industrial 2D
UPC-A Code 39 QR Code
EAN-8 Code 128 Aztec (beta)
EAN-13 Codabar PDF 417 (beta)
ITF MaxiCode
RSS-14
RSS-Expanded

Adding dependency with Gradle

Android SDK 需要 19 以上

  • 我們先把 library 加進來
  • 開啟 build.gradle,在適當的位置加入以下 dependency
dependencies { implementation 'com.journeyapps:zxing-android-embedded:3.6.0' implementation 'com.android.support:appcompat-v7:25.3.1' // Minimum 23+ is required }

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Hardware Acceleration

  • Hardware accelation is required since TextureView is used
  • 開啟 AndroidManifest.xml,在適當的位置 enable hardwareAccelerated 屬性
<application android:hardwareAccelerated="true" ... >

Usage with IntentIntegrator

有關 intent,之後會再介紹

new IntentIntegrator(this).initiateScan(); // \`this\` is the current Activity // Get the results: @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 { super.onActivityResult(requestCode, resultCode, data); } }
  • 第 1 行,initiateScan(),用來啟動掃描Activity,可以寫在 onClick() 或是 onCreate() 等等method裡面
  • 第 3-16行,onActictyResult(),目的是取得掃描Activity傳回來的結果,不需要動這部分,直接貼到 MainActivity.java 即可

有關啟動 Activity 以取得結果,之後會再介紹

  • result.getContents() 為結果,如果打算顯示掃描結果,複製結果,開啟URL,便需要利用此結果

Customize options

  • 此為其他客製化功能,和 initiateScan()一樣,寫在 onClick() 或是 onCreate() 等等method裡面
IntentIntegrator integrator = new IntentIntegrator(this); integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE); integrator.setPrompt("Scan a QR code"); integrator.setCameraId(0); // Use a specific camera of the device integrator.setBeepEnabled(false); integrator.setBarcodeImageEnabled(true); integrator.initiateScan();

Changing the orientation

  1. 開啟 AndroidManifest.xml,加入以下code
<activity android:name="com.journeyapps.barcodescanner.CaptureActivity" android:screenOrientation="fullSensor" tools:replace="screenOrientation" />

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. setOrientationLocked() set false to not lock,和 initiateScan()寫在一起
IntentIntegrator integrator = new IntentIntegrator(this); integrator.setOrientationLocked(false); integrator.initiateScan();

Button and Text display

  1. layout 如下所示

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  2. MainActivity.java 寫一個 onClick() method,裡面放剛剛的那些code

public void onClick(View view){ IntentIntegrator integrator = new IntentIntegrator(this); integrator.setOrientationLocked(false); integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE); integrator.setPrompt("Scan a QR code"); integrator.setCameraId(0); // Use a specific camera of the device integrator.setBeepEnabled(false); integrator.setBarcodeImageEnabled(true); integrator.initiateScan(); }
  1. onActictyResult()裡面加入第 4 行和第 12 行,用 TextView顯示結果
// Get the results: @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { TextView textView = findViewById(R.id.textView); 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(); textView.setText(result.getContents()); } } else { super.onActivityResult(requestCode, resultCode, data); } }
  1. 把 onClick() 設置到 button 上

Done!!!


Reference

Future feature

Homepage