我正在使用ZXing库制作一个简单的条形码阅读器应用程序,但我想自定义条形码阅读器的样式(布局)
我正在使用ZXingScannerView,它会自动生成布局,我想给它添加边框并更改其位置。
ZXingScannerView scannerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
scannerView = new ZXingScannerView(this);
setContentView(scannerView);
}
...
最佳答案
在github JourneyApps中查看此项目。
示例代码具有scanBarcodeCustomLayout(View view)
方法。
public void scanBarcodeCustomLayout(View view) {
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setCaptureActivity(AnyOrientationCaptureActivity.class);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
integrator.setPrompt("Scan something");
integrator.setOrientationLocked(false);
integrator.setBeepEnabled(false);
integrator.initiateScan();
}
有一条很重要的线给你
integrator.setCaptureActivity(AnyOrientationCaptureActivity.class);
That
AnyOrientationCaptureActivity class
只是CaptureActivity
的扩展,没有更多代码。因此,这意味着CaptureActivity
具有条形码视图的默认样式。package example.zxing;
import com.journeyapps.barcodescanner.CaptureActivity;
/**
* This Activity is exactly the same as CaptureActivity, but has a different orientation
* setting in AndroidManifest.xml.
*/
public class AnyOrientationCaptureActivity extends CaptureActivity {
}
现在来看
setContentView(R.layout.zxing_capture);
您需要做的就是创建自己的类。复制
CaptureActivity
中的所有代码,然后在custom xml layout
中设置setContentView()
。然后将
com.journeyapps.barcodescanner.DecoratedBarcodeView
作为捕获条形码的视图。您可以根据需要设置样式。例如,要制作边框,只需在根布局中设置填充。
希望对您有所帮助。