Android Studio
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 |
Android SDK 需要 19 以上
dependencies {
implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
implementation 'com.android.support:appcompat-v7:25.3.1' // Minimum 23+ is required
}
TextureView
is used<application android:hardwareAccelerated="true" ... >
IntentIntegrator
IntentIntegrator
顧名思義,就是 intent
的integrate版。可以call一些method來設定要開啟的掃描acticity的屬性,最後 call initiateScan()
來開啟掃描acticity有關 intent
,之後會再介紹
the Constructor
Set a prompt to display on the capture screen, instead of using the default.
By default, the orientation is locked. Set to false to not lock.
Use the specified camera ID.
Set to false to disable beep on scan.
Set to true to enable saving the barcode image and sending its path in the result Intent.
Set the desired barcode formats to scan.
Initiates a scan for all known barcode types with the default camera.
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);
}
}
initiateScan()
,用來啟動掃描Activity,可以寫在 onClick() 或是 onCreate() 等等method裡面onActictyResult()
,目的是取得掃描Activity傳回來的結果,不需要動這部分,直接貼到 MainActivity.java 即可有關啟動 Activity 以取得結果,之後會再介紹
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();
<activity
android:name="com.journeyapps.barcodescanner.CaptureActivity"
android:screenOrientation="fullSensor"
tools:replace="screenOrientation" />
setOrientationLocked()
set false to not lock,和 initiateScan()
寫在一起
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setOrientationLocked(false);
integrator.initiateScan();
layout 如下所示
在 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();
}
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);
}
}
Done!!!