close

 

 

教學用,直接切入主題

 

 

Step1. 使用Zxing,你必須添加這玩意的依賴 :

1.打開你的build.gradle 

2.在你的dependencies裡面添加

compile 'com.google.zxing:core:3.2.1'                      
compile 'com.journeyapps:zxing-android-embedded:3.2.0@aar'

3.按下Sync Now

 

Step2.到你的fragment.java去

public static IntentIntegrator forSupportFragment(Fragment fragment) 
{
    IntentIntegrator integrator = new IntentIntegrator(fragment.getActivity());
    integrator.forSupportFragment(fragment) ;
    return integrator;
}
@Override
    public void onClick(View v)
    {

        switch (v.getId())
        {
            case R.id.application_btn_scanner:
            {
//                final Activity activity = getActivity();
//                IntentIntegrator integrator = new IntentIntegrator(activity);
//                integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
//                integrator.setPrompt("Scan");
//                integrator.setCameraId(0);
//                integrator.setBeepEnabled(false);
//                integrator.setBarcodeImageEnabled(false);
//                integrator.initiateScan();
                IntentIntegrator integrator = IntentIntegrator.forSupportFragment(this);
                integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
                //integrator.setCaptureActivity(ScanActivity.class);
                integrator.setPrompt("Scan");       //底部提示的文字
                integrator.setCameraId(0);          //前面或後面的相機
                integrator.setBeepEnabled(true);    //掃描成功後發出 BB                 integrator.setBarcodeImageEnabled(true);
                integrator.initiateScan();
            }


        }
    }

 

Step3.如此一來你就可以在onActivityResult那邊收到了

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode,resultCode,data);


    if (result!= null)
    {
        if (result.getContents()==null)
        {
            ToastUtil.showMsg(getActivity(),"result : null");
        }
        else
        {
            String content = result.getContents();
            m_textView_result.setText("Scanner result :" + content);

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

 

that 's all.

arrow
arrow
    文章標籤
    fragment android Zxing zxing
    全站熱搜

    Eric 發表在 痞客邦 留言(0) 人氣()