在片段内发射相机(zxing)

时间:2017-06-14 09:07:19

标签: android android-fragments fragment zxing

嗨我有一个片段,其中有一个按钮,然后点击它我启动photocamera来检测Qrcodes(使用zxing库)。我想删除按钮并使扫描仪始终打开,我该如何实现? 这是片段代码:

public class Tab2Scan extends Fragment {

private Button scan_btn;

ConnectionDetector cd;

Context thisContext;

View rootView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    getActivity().getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);


    rootView = inflater.inflate(R.layout.tab2scan, container, false);


    thisContext = getActivity().getApplicationContext();
    cd = new ConnectionDetector(thisContext);


    scan_btn=(Button) rootView.findViewById(R.id.scan_btn);


    scan_btn.setOnClickListener
            (
                    new View.OnClickListener()
                    {
                        @Override
                        public void onClick(View view)
                        {
                            if(cd.isNetworkAvailable())
                            {
                                IntentIntegrator integrator = new IntentIntegrator(getActivity());
                                integrator.forSupportFragment(Tab2Scan.this)
                                        .setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES)
                                        .setPrompt("Scan")
                                        .setCameraId(0)
                                        .setBeepEnabled(false)
                                        .setBarcodeImageEnabled(false)
                                        .initiateScan();
                            }
                            else
                            {
                                Toast.makeText(thisContext, "Nessuna connesione ad Internet!",Toast.LENGTH_SHORT).show();
                            }

                        }
                    }
            );

    return rootView;
}

@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.makeText(thisContext,"Hai cancellato la scansione", Toast.LENGTH_LONG).show();


        }
        else
        {
            Intent intent = new Intent(getActivity(), ShowData.class);
            intent.putExtra("variabile",result.getContents());
            startActivity(intent);
            //Toast.makeText(thisContext, result.getContents(),Toast.LENGTH_LONG).show();
        }
    }
    else
    {

        super.onActivityResult(requestCode, resultCode, data);
    }

}

}

这就是我想要做的:fragment

片段的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:layout_weight="1"
tools:context="com.example.aaab.qrcode.Main">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="SCAN"
    android:id="@+id/scan_btn"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="116dp" />

1 个答案:

答案 0 :(得分:0)

像这样改变:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    getActivity().getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);


    rootView = inflater.inflate(R.layout.tab2scan, container, false);


    thisContext = getActivity().getApplicationContext();
    cd = new ConnectionDetector(thisContext);



                            if(cd.isNetworkAvailable())
                            {
                                IntentIntegrator integrator = new IntentIntegrator(getActivity());
                                integrator.forSupportFragment(Tab2Scan.this)
                                        .setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES)
                                        .setPrompt("Scan")
                                        .setCameraId(0)
                                        .setBeepEnabled(false)
                                        .setBarcodeImageEnabled(false)
                                        .initiateScan();
                            }
                            else
                            {
                                Toast.makeText(thisContext, "Nessuna connesione ad Internet!",Toast.LENGTH_SHORT).show();
                            }
    return rootView;
}