2017-07-05 26 views
1

當我使用依賴關係compile 'me.dm7.barcodescanner:zxing:1.9'而無法訪問zxing文件時,如何更改取景器大小,顏色和其他選項? 我找不到解決此問題的方法。在zxing中更改查看器

回答

1

您可以通過XML

<com.journeyapps.barcodescanner.BarcodeView 
     android:id="@+id/zxing_barcode_surface" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:zxing_framing_rect_height="220dp" 
     app:zxing_framing_rect_width="250dp" /> 

<com.journeyapps.barcodescanner.ViewfinderView 
    android:id="@+id/zxing_viewfinder_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:zxing_possible_result_points="@color/zxing_custom_possible_result_points" 
    app:zxing_result_view="@color/zxing_custom_result_view" 
    app:zxing_viewfinder_laser="@color/zxing_custom_viewfinder_laser" 
    app:zxing_viewfinder_mask="@color/zxing_custom_viewfinder_mask" /> 

做到這一點還要檢查this post(可能重複這個問題),並another one,如果你想以編程方式更改的行爲。

要訪問的庫功能,您需要將以下添加到您的build.gradle文件:

repositories { 
    jcenter() 
} 

dependencies { 
    compile 'me.dm7.barcodescanner:zxing:1.9.3' 
} 

和攝像頭的權限添加到您的AndroidManifest.xml文件:

<uses-permission android:name="android.permission.CAMERA" /> 
+0

但如何我可以安裝它來訪問它的類嗎?我是Android開發新手。 – codddeer123

+0

@ codddeer123只需在Gradle中添加依賴項'compile'me.dm7.barcodescanner:zxing:1.9.3''到應用程序模塊。您也可以使用[Github項目](https://github.com/dm77/barcodescanner#installation)中的安裝指南。在開始同步項目之後,您可以使用庫中的類和視圖。外部庫中的 – Tim

+0

- > zxing-1.9?請只查看BuildConfig和ZXingScannerView – codddeer123

1

來改變ViewFinder的顏色。 這些添加到您的項目的colors.xml

<color name="viewfinder_mask">#3F51B5</color> 
<color name="viewfinder_laser">#00FFFFFF</color> 
<color name="viewfinder_border">#ffFF4081</color> 

做出其他更改,如從ZXingScannerView的取景器 去除激光創建一個類,擴展ViewFinderView。重寫方法來自定義UI 。

public class CustomScannerView extends ZXingScannerView { 

public CustomScannerView(Context context) { 
    super(context); 
} 

@Override 
protected IViewFinder createViewFinderView(Context context) { 
    return new CustomViewFinderView(context); 
} 

//make changes in CustomViewFinderView to customise the Viewfinder 
//Check ViewFinderView class for more modifications 
//to change viewFinder's colours override appropriate values in Colors.xml 
class CustomViewFinderView extends ViewFinderView { 


    public CustomViewFinderView(Context context) { 
     super(context); 
     setSquareViewFinder(true); 
     DisplayMetrics displayMetrics = new DisplayMetrics(); 
     ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); 
     // DEFAULT SQUARE DIMENSION RATIO in ViewFinderView is 0.625 
     // get appropriate Dimension ratio otherwise 
     float width = displayMetrics.widthPixels * 0.625f; 
     setBorderLineLength((int)width); 
    } 

    @Override 
    public void drawLaser(Canvas canvas) { 
     //do nothing for no laser, even remove super call 
    } 

} 
} 

設置取景器的矩形 使用

super.getFramingRect().set(left, top, right, bottom); 
0

如果你想刪除的激光只,這可以簡單地通過禁用Alpha通道動畫完成:

barcodeView = (DecoratedBarcodeView) findViewById(R.id.barcode_scanner); 
barcodeView.decodeContinuous(callback); 

ViewfinderView viewFinder = barcodeView.getViewFinder(); 
Field scannerAlphaField = null; 
try { 
    scannerAlphaField = viewFinder.getClass().getDeclaredField("SCANNER_ALPHA"); 
    scannerAlphaField.setAccessible(true); 
    scannerAlphaField.set(viewFinder, new int[1]); 
} catch (NoSuchFieldException e) { 
    e.printStackTrace(); 
} catch (IllegalAccessException e) { 
    e.printStackTrace(); 
}