2018-01-01

【Android Studio】結合 ZXing 掃描 QRcode

ZXingAndroid 平台上,開放原始碼,掃描 一維 及 二維條碼(QRcode) 的 app,網路上也有熱心的大大包裝成套件,讓人更容易將自己的 appZXing 結合,讓自己的 app 具有掃描 QRcode 的功能。


建立一個新專案,在新專案精靈建立專案的過程中選擇 Empty Activity,並勾選 Backwards Compatibility(AppCompat)




修改 app 層級的 build.gradle

  1.  
  2. dependencies {
  3. implementation fileTree(dir: 'libs', include: ['*.jar'])
  4. implementation 'com.android.support:appcompat-v7:26.1.0'
  5. ...
  6. ...
  7.  
  8. // 加入這行, AndroidStudio 就會自動下載這個套件, 好神奇!!
  9. compile 'com.journeyapps:zxing-android-embedded:3.5.0'
  10. }
  11.  
ps:這個套件,似乎是 journeyapps.com 這家公司打包的。

延伸參考 ----
Add Build Dependencies

activity_main.xml
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <RelativeLayout
  4. xmlns:android="http://schemas.android.com/apk/res/android"
  5. xmlns:app="http://schemas.android.com/apk/res-auto"
  6. xmlns:tools="http://schemas.android.com/tools"
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. tools:context="com.tw.whalin.qrcodetest.MainActivity">
  10.  
  11. <Button
  12. android:id="@+id/button"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:layout_alignParentLeft="true"
  16. android:layout_alignParentRight="true"
  17. android:layout_alignParentTop="true"
  18. android:onClick="onbuttonclick"
  19. android:text="Button"
  20. tools:layout_editor_absoluteX="-9dp"
  21. tools:layout_editor_absoluteY="16dp"/>
  22.  
  23. </RelativeLayout>
  24.  




MainActivity.java
  1.  
  2. // 注意 MainActivity 是繼承自 AppCompatActivity
  3. // 可能套件的作者當時也是這麼做的吧
  4. // 要繼承自 Activity 的方法再另外找時間研究了...
  5. public class MainActivity extends AppCompatActivity
  6. {
  7. private IntentIntegrator scanIntegrator;
  8.  
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState)
  11. {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. }
  15.  
  16. public void onbuttonclick(View v)
  17. {
  18. View button1 = (View) findViewById(R.id.button);
  19.  
  20. scanIntegrator = new IntentIntegrator(MainActivity.this);
  21. scanIntegrator.setPrompt("請掃描");
  22. scanIntegrator.setTimeout(300000);
  23. scanIntegrator.initiateScan();
  24. }
  25.  
  26. public void onActivityResult(int requestCode, int resultCode, Intent intent)
  27. {
  28. IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
  29. if (scanningResult != null)
  30. {
  31. if(scanningResult.getContents() != null)
  32. {
  33. String scanContent = scanningResult.getContents();
  34. if (!scanContent.equals(""))
  35. {
  36. Toast.makeText(getApplicationContext(),"掃描內容: "+scanContent.toString(), Toast.LENGTH_LONG).show();
  37. }
  38. }
  39. }
  40. else
  41. {
  42. super.onActivityResult(requestCode, resultCode, intent);
  43. Toast.makeText(getApplicationContext(),"發生錯誤",Toast.LENGTH_LONG).show();
  44. }
  45. }
  46. }
  47.  

執行 app,點擊 按鈕,會啟用照相機,預設是橫式畫面,但還是可以直向掃描。



若一定要直式畫面掃描,也行。

修改 AndroidManifest.xml
  1.  
  2. <application ...>
  3.  
  4. ...
  5. ...
  6. <activity
  7. android:name="com.journeyapps.barcodescanner.CaptureActivity"
  8. android:screenOrientation="fullSensor"
  9. tools:replace="screenOrientation" />
  10.  
  11. ...
  12. ...
  13.  


小改一下上面的程式:
  1.  
  2. public void onbuttonclick(View v)
  3. {
  4. View button1 = (View) findViewById(R.id.button);
  5.  
  6. scanIntegrator = new IntentIntegrator(MainActivity.this);
  7. scanIntegrator.setPrompt("請掃描");
  8. scanIntegrator.setTimeout(300000);
  9. scanintegrator.setOrientationLocked(false); // 加入這一行指令
  10. scanIntegrator.initiateScan();
  11. }
  12.  


感謝好友 hsc 的大方分享  :-D


相關筆記 ----
讓 ZXing 代勞,掃描 QR碼(QR code)
利用 ZXing 生成 條碼 / QR code
Genymotion 模擬器執行 Epson 範例 app,連接 TM-T70II 微型印表機列印單據