建立一個新專案,在新專案精靈建立專案的過程中選擇 Empty Activity,並勾選 Backwards Compatibility(AppCompat)
修改 app 層級的 build.gradle
ps:這個套件,似乎是 journeyapps.com 這家公司打包的。
- dependencies {
- implementation fileTree(dir: 'libs', include: ['*.jar'])
- implementation 'com.android.support:appcompat-v7:26.1.0'
- ...
- ...
- // 加入這行, AndroidStudio 就會自動下載這個套件, 好神奇!!
- compile 'com.journeyapps:zxing-android-embedded:3.5.0'
- }
延伸參考 ----
Add Build Dependencies
activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.tw.whalin.qrcodetest.MainActivity">
- <Button
- android:id="@+id/button"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentRight="true"
- android:layout_alignParentTop="true"
- android:onClick="onbuttonclick"
- android:text="Button"
- tools:layout_editor_absoluteX="-9dp"
- tools:layout_editor_absoluteY="16dp"/>
- </RelativeLayout>
MainActivity.java
- // 注意 MainActivity 是繼承自 AppCompatActivity
- // 可能套件的作者當時也是這麼做的吧
- // 要繼承自 Activity 的方法再另外找時間研究了...
- public class MainActivity extends AppCompatActivity
- {
- private IntentIntegrator scanIntegrator;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- public void onbuttonclick(View v)
- {
- View button1 = (View) findViewById(R.id.button);
- scanIntegrator = new IntentIntegrator(MainActivity.this);
- scanIntegrator.setPrompt("請掃描");
- scanIntegrator.setTimeout(300000);
- scanIntegrator.initiateScan();
- }
- public void onActivityResult(int requestCode, int resultCode, Intent intent)
- {
- IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
- if (scanningResult != null)
- {
- if(scanningResult.getContents() != null)
- {
- String scanContent = scanningResult.getContents();
- if (!scanContent.equals(""))
- {
- Toast.makeText(getApplicationContext(),"掃描內容: "+scanContent.toString(), Toast.LENGTH_LONG).show();
- }
- }
- }
- else
- {
- super.onActivityResult(requestCode, resultCode, intent);
- Toast.makeText(getApplicationContext(),"發生錯誤",Toast.LENGTH_LONG).show();
- }
- }
- }
執行 app,點擊 按鈕,會啟用照相機,預設是橫式畫面,但還是可以直向掃描。
若一定要直式畫面掃描,也行。
修改 AndroidManifest.xml
- <application ...>
- ...
- ...
- <activity
- android:name="com.journeyapps.barcodescanner.CaptureActivity"
- android:screenOrientation="fullSensor"
- tools:replace="screenOrientation" />
- ...
- ...
小改一下上面的程式:
- public void onbuttonclick(View v)
- {
- View button1 = (View) findViewById(R.id.button);
- scanIntegrator = new IntentIntegrator(MainActivity.this);
- scanIntegrator.setPrompt("請掃描");
- scanIntegrator.setTimeout(300000);
- scanintegrator.setOrientationLocked(false); // 加入這一行指令
- scanIntegrator.initiateScan();
- }
感謝好友 hsc 的大方分享 :-D
相關筆記 ----
讓 ZXing 代勞,掃描 QR碼(QR code)
利用 ZXing 生成 條碼 / QR code
Genymotion 模擬器執行 Epson 範例 app,連接 TM-T70II 微型印表機列印單據