我有一个" Scan Fragment" &安培; "产品表格片段"。任务流程就像, 主要活动 - > ScanFragment(按钮点击/扫描内容) - >表单片段(按钮点击) - > ScanFragment(再次显示相同的片段)
如果只显示一次ScanFragment,则效果很好。但是,当第二次调用时,扫描片段显示黑屏。如果从ScanFragment移动到产品片段时停止相机,则冻结。我认为它的相机问题,但无法弄清楚如何解决这个问题。
ScanFragment onClick方法:
private void onClickMethods(){
btn_Skip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PrintLog.show("-- Skip Button is clicked ---");
//releaseCamera(); -- if used, then shows camera but camera freezes
ProductDetailFormFragment productDetailFormFragment = new ProductDetailFormFragment();
getActivity().getSupportFragmentManager().beginTransaction()
.add(R.id.mainFragment, productDetailFormFragment).addToBackStack(AppConstants.C_BACKSTACK_SCAN).commit();
});
}
private void releaseCamera(){
// calls child fragment's camera releasing methods here
// if used camera shows freezed screen only
SimpleScannerFragment simpleScannerFragment = (SimpleScannerFragment) getChildFragmentManager()
.findFragmentById(R.id.scanner_fragment);
simpleScannerFragment.releaseCamera();
}
SimpleScannerFragment(包含zxing代码)包含在Scanlayout中,如下所示:
<LinearLayout...... >
.....
<fragment android:name="np.com.lins.serviceapp.UI.Modules.BarcodeScanner.SimpleScannerFragment"
android:id="@+id/scanner_fragment"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
....
</LinearLayout>
SimpleScannerFragment代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mScannerView = new ZXingScannerView(getActivity());
PrintLog.show("=== Scanner Fragment : before Return view ==");
return mScannerView;
}
@Override
public void onResume() {
super.onResume();
PrintLog.show("Resume state===");
mScannerView.setResultHandler(this);
mScannerView.stopCamera();
mScannerView.startCamera();
}
//called by parent ScanFragment
public void releaseCamera(){
try{
mScannerView.stopCameraPreview();
mScannerView.stopCamera();
}catch (Exception e){
PrintLog.showException(TAG,"== exception while releasing camera resourc == ",e);
}
}
@Override
public void onPause() {
super.onPause();
mScannerView.stopCameraPreview();
mScannerView.stopCamera();
}
ProductDetailFormFragment代码:
public void scanFromFragment() {
ScanFragment scanFragment = new ScanFragment();
Bundle bundle = new Bundle();
bundle.putString(AppConstants.C_PARENT_FRAGMENT,AppConstants.C_FRAGMENT_PRODUCT_DETAILS);
scanFragment.setArguments(bundle);
getActivity().getSupportFragmentManager().beginTransaction()
.add(R.id.mainFragment, scanFragment).addToBackStack(AppConstants.C_BACKSTACK_PRODUCT_DETAIL_FORM).commit();
//IntentIntegrator.forSupportFragment(this).initiateScan();
}
答案 0 :(得分:0)
尝试这样做可能对你有所帮助
从主要活动打电话时
private void callFragment(Fragment fragment, String tag) {
FragmentTransaction transaction = this.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainFragment, fragment, tag);
transaction.commit();
}
当您从相同的片段(ScanFragment)呼叫时,请在此之前调用此设备并执行所有相机停止其他操作
private void callScanFragment(String tag) {
// Reload current fragment
Fragment frg = null;
frg = getSupportFragmentManager().findFragmentByTag(tag);
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
}
答案 1 :(得分:0)
我利用了我在Github上找到的这个gradle编译,效率极高,效果非常出色
compile 'com.edwardvanraak:MaterialBarcodeScanner:0.0.6-ALPHA'
然后开始扫描并获取值的代码是这样的:
Barcode barcodeResult;
TextView result;
private void startScan() {
/**
* Build a new MaterialBarcodeScanner
*/
result = (TextView)findViewById(R.id.result);
final MaterialBarcodeScanner materialBarcodeScanner = new MaterialBarcodeScannerBuilder()
.withActivity(MainActivity.this)
.withEnableAutoFocus(true)
.withBleepEnabled(true)
.withBackfacingCamera()
.withText("Scanning...")
.withResultListener(new MaterialBarcodeScanner.OnResultListener() {
@Override
public void onResult(Barcode barcode)
{
barcodeResult = barcode;
result.setText(barcode.rawValue);
}
})
.build();
materialBarcodeScanner.startScan();
}
我希望这有助于