попробуйте это в XML-файле:
<com.journeyapps.barcodescanner.CompoundBarcodeView
android:id="@+id/barcode_scanner"
android:layout_width="match_parent"
android:layout_height="0dp"
app:zxing_viewfinder_mask="#ffffff"
android:layout_weight="40"
>
</com.journeyapps.barcodescanner.CompoundBarcodeView>
и вот фрагмент кода:
public class ScanFragment extends Fragment {
private String toast;
private LinearLayout qrCameraLayout;
IntentIntegrator intentIntegrator;
private CompoundBarcodeView barcodeView;
private TextView tv_qr_print;
public ScanFragment() {
}
public static ScanFragment newInstance(int page, String title, ViewPager viewPager) {
ScanFragment fragmentFirst = new ScanFragment();
Bundle args = new Bundle();
args.putInt("someInt", page);
args.putString("someTitle", title);
fragmentFirst.setArguments(args);
return fragmentFirst;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
displayToast();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
intentIntegrator = new IntentIntegrator(getActivity());
View view = inflater.inflate(R.layout.screen_scanner, container, false);
qrCameraLayout = (LinearLayout) view.findViewById(R.id.ll_qrcamera);
barcodeView = (CompoundBarcodeView) view.findViewById(R.id.barcode_scanner);
tv_qr_print = (TextView) view.findViewById(R.id.tv_qr_print);
barcodeView.decodeContinuous(callback);
/* View view = inflater.inflate(R.layout.fragment_scan, container, false);*/
CustomButton scan = (CustomButton) view.findViewById(R.id.scan_from_fragment);
scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/* scanFromFragment();*/
}
});
return view;
}
private BarcodeCallback callback = new BarcodeCallback() {
@Override
public void barcodeResult(BarcodeResult result) {
if (result.getText() != null) {
barcodeView.setStatusText(result.getText());
tv_qr_print.setText(result.getText());
displayToast();
}
//Do something with code result
}
@Override
public void possibleResultPoints(List<ResultPoint> resultPoints) {
}
};
public void scanFromFragment() {
intentIntegrator.setOrientationLocked(true);
intentIntegrator.forSupportFragment(this).initiateScan();
}
private void displayToast() {
if(getActivity() != null && toast != null) {
Toast.makeText(getActivity(), toast, Toast.LENGTH_LONG).show();
toast = null;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
if(result.getContents() == null) {
toast = "Cancelled from fragment";
} else {
toast = "Scanned from fragment: " + result.getContents();
}
// At this point we may or may not have a reference to the activity
displayToast();
}
}
@Override
public void onResume() {
barcodeView.resume();
barcodeView.getViewFinder().setVisibility(View.GONE);
super.onResume();
}
@Override
public void onPause() {
barcodeView.pause();
super.onPause();
}
}