2016-01-25 200 views
4

我希望你能幫助我。在使用Zxing嵌入式庫來使用QR掃描儀時,問題在於橫向模式,我想將其更改爲縱向。在Android Studio中使用ZXING更改QR掃描儀方向

我有這對我的Graddle

compile 'com.journeyapps:zxing-android-embedded:[email protected]' 
compile 'com.journeyapps:zxing-android-integration:[email protected]' 
compile 'com.google.zxing:core:3.0.1' 

的依存關係,我有這在我的java類激活與一個按鈕掃描儀...

public void scanQR(View view){ 
    IntentIntegrator integrator = new IntentIntegrator(this); 
    integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES); 
    integrator.setResultDisplayDuration(0);//Text.. 
    integrator.setPrompt(" Scan a QR Code"); 
    integrator.setScanningRectangle(450, 450);//size 
    integrator.setCameraId(0); // Use a specific camera of the device 
    integrator.initiateScan(); 

} 

感謝您的幫助!

回答

8

我使用

編譯 'com.journeyapps:斑馬線,Android的嵌入式:[email protected]'

它是不同的版本,所以我不知道這是否將爲你工作, 但這對我有用。

更多關於我的設置,我只編譯

'com.journeyapps:斑馬線,Android的嵌入式:[email protected]'

「com.google.zxing:核心:3.0。 1'

,我沒有編譯

'com.journeyapps:斑馬線,Android的整合:[email protected]'

首先我創建的活動從CaptureActivity

延伸或點擊該鏈接以查看類https://gist.github.com/TheGratefulDev/21a557c9a96333ec037c

public class CaptureActivityPortrait extends CaptureActivity { 
//Nothing in side. 
} 

其次,添加此

integrator.setCaptureActivity(CaptureActivityPortait.class);

進入您的集成商代碼。

這是我的樣子:

CustomIntegrator integrator = new CustomIntegrator(activity); 
      integrator.setDesiredBarcodeFormats(CustomIntegrator.PDF_417); 
      integrator.setPrompt("Scan a barcode"); 
      integrator.setCameraId(0); // Use a specific camera of the device 
      integrator.setOrientationLocked(true); 
      integrator.setBeepEnabled(true); 
      integrator.setCaptureActivity(CaptureActivityPortrait.class); 
      integrator.initiateScan(); 

最後,在AndroidMaifest添加

<activity 
     android:name=".custom.CaptureActivityPortrait" 
     android:screenOrientation="portrait" <---this is the most important line 
     android:stateNotNeeded="true" 
     android:theme="@style/zxing_CaptureTheme" 
     android:windowSoftInputMode="stateAlwaysHidden"> 
    </activity> 
+0

你如何擴大captureactivity?我得到的錯誤是不能從最終CaptureActivity – Shadow

+0

@Shadow我只是檢查我的答案繼承,我忘了斑馬線:core3.2.0 檢查build.glade 確保你有這兩個庫添加{ 「com.journeyapps: zxing-android-embedded:[email protected]'和 'com.google.zxing:core:3.2.0'} –

+0

@Shadow這是從CaptureActivity擴展的那個類 https://gist.github.com/KaLingCode/21a557c9a96333ec037c –

1

我才發現最簡單的方法。我們應該創建另一個CaptureActivity。Java類,並寫在代碼裏onclick聽衆:

IntentIntegrator integrator = new IntentIntegrator(activity); 
     integrator.setPrompt("Scan a barcode"); 
     integrator.setDesiredBarcodeFormats(integrator.ALL_CODE_TYPES); 
     integrator.setCameraId(0); 
     integrator.setOrientationLocked(false); 

     integrator.setCaptureActivity(com.share.ants.hotelmenu.CaptureActivity.class); <--replace with your own java class location here 
     integrator.setBeepEnabled(true); 
0

它的工作對我來說

IntentIntegrator integrator = new IntentIntegrator(YourActivity.this);      integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);      integrator.setPrompt(getResources().getString(R.string.scan_a_barcode)); 
integrator.setCameraId(0); 
// Use a specific camera of the device 
integrator.setBeepEnabled(true); 
integrator.setBarcodeImageEnabled(false); 
integrator.initiateScan(); 
1

而是擴展一個類的,只需添加這清單:

<activity 
      android:name="com.journeyapps.barcodescanner.CaptureActivity" 
      android:screenOrientation="portrait" 
      tools:replace="android:screenOrientation" 
      android:stateNotNeeded="true"/> 

像魅力一樣工作