2017-09-05 145 views
0

我是開發人員,但在Android中是新的,我需要知道如何在活動佈局中使用相機。如何使用掃描二維碼與佈局內的相機(如Whatsapp網絡掃描儀)

我知道我需要使用Surface View在活動中插入相機,目前我的應用程序正在使用默認相機(使用默認相機閱讀QR碼)(一個按鈕打開相機,用戶拍照並且我的應用程序感知活動結果)。

但我真的需要在應用程序中實現與實時掃描儀功能。

有人可以指導我嗎?

回答

1

這裏是我是如何實現類似於你所需要的東西。

- 首先,我使用Zxing庫來實現這一點。所以,你需要下面的依賴添加到您的gradle產出:

compile 'com.journeyapps:zxing-android-embedded:3.5.0' 

- 下面是一個直接鏈接到journeyapps掃描儀項目的Github上的鏈接:

https://github.com/journeyapps/zxing-android-embedded

- 以下是我如何製作我的佈局文件:

<!-- language: lang-xml --> 
<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


    <com.journeyapps.barcodescanner.DecoratedBarcodeView 
     android:id="@+id/view_scanner" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@+id/view_footer" 
     android:soundEffectsEnabled="true" /> 

    <LinearLayout 
     android:id="@+id/view_header" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:orientation="horizontal"> 

     <LinearLayout 
      android:id="@+id/view_back" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center|top" 
      android:orientation="horizontal"> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="@dimen/_10sdp" 
       android:layout_marginTop="@dimen/_10sdp" 
       android:src="@drawable/ic_back_light" /> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="@dimen/_50sdp" 
      android:gravity="right|top"> 

      <ImageView 
       android:id="@+id/iv_flash" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="@dimen/_10sdp" 
       android:layout_marginTop="@dimen/_10sdp" 
       android:src="@drawable/ic_flash_inactive" /> 
     </LinearLayout> 
    </LinearLayout> 


    <LinearLayout 
     android:id="@+id/view_footer" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|center" 
     android:gravity="center" 
     android:orientation="vertical"> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="1dp" 
      android:layout_marginLeft="@dimen/_10sdp" 
      android:layout_marginRight="@dimen/_10sdp" 
      android:background="@android:color/darker_gray" /> 

     <TextView 
      android:id="@+id/code_info" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:fontFamily="@string/font_sans_serif_light" 
      android:padding="@dimen/_15sdp" 
      android:text="@string/app_name" 
      android:textColor="@android:color/white" 
      android:textSize="@dimen/_15sdp" /> 


    </LinearLayout> 

</FrameLayout> 

所以現在DecoratedBarcodeView作爲您的佈局中的主掃描區域。

- 如下初始化條形碼的看法: - :

private BarcodeCallback callback = new BarcodeCallback() { 
    @Override 
    public void barcodeResult(BarcodeResult result) { 
     //Process your scan result here 
     String resultString = result.getText(); 
    } 

    @Override 
    public void possibleResultPoints(List<ResultPoint> resultPoints) { 
    } 
}; 

希望這

private DecoratedBarcodeView barcodeView; 

barcodeView = (DecoratedBarcodeView) findViewById(R.id.view_scanner); 
barcodeView.setStatusText(""); 
barcodeView.decodeContinuous(callback); 

在您的活動,您可以通過這種方式在你的BarcodeCallback獲取掃描結果幫助。