在qr代码的屏幕android中间打开相机

时间:2017-01-17 06:33:06

标签: android camera

我正在尝试制作一个在片段中打开的QR码扫描仪,我正在使用zxing库这样做。现在我可以成功打开相机扫描QR码。但是,由于我使用意图打开相机,它开启了另一个活动。我想要做的是在片段内打开相机,只在屏幕的中间部分打开。我相信表面视图和相机预览可以帮助我这样做,但我不知道如何实现它

public class QRscanner extends Fragment {
private IntentIntegrator qrScan;
public QRscanner() {
}
public static QRscanner newInstance(String text){
    Bundle args = new Bundle();
    QRscanner qrScanner = new QRscanner();
    qrScanner.setArguments(args);
    return qrScanner;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.qr_scanner, container, false);
    IntentIntegrator qrScan = new IntentIntegrator(getActivity());
    qrScan.initiateScan();
    return rootView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if (result != null) {
        //if qrcode has nothing in it
        if (result.getContents() == null) {
            Toast.makeText(getActivity(), "Result Not Found", Toast.LENGTH_LONG).show();
        } else {
            //if qr contains data
            try {
                //converting the data to json
                JSONObject obj = new JSONObject(result.getContents());
                String testing = obj.getString("test");
                System.out.println(testing);
                //setting values to textviews
            } catch (JSONException e) {
                e.printStackTrace();
                //if control comes here
                //that means the encoded format not matches
                //in this case you can display whatever data is available on the qrcode
                //to a toast
                Toast.makeText(getActivity(), result.getContents(), Toast.LENGTH_LONG).show();
            }
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
}

the dark block is the camera and the white is the rest of the screen

1 个答案:

答案 0 :(得分:0)

请访问GitHub上的ZXing库。

它可能对你有帮助。