1. 程式人生 > >專案整合zxing掃描二維碼並跳轉頁面

專案整合zxing掃描二維碼並跳轉頁面

1.把需要的activity放在src裡面
主要是這些,編碼和解碼都有,我就都放進來了
2.許可權和註冊
zxing需要的許可權,主要是開攝像頭和震動
因為我沒有用到編碼,所以只有一個掃描的介面需要註冊
3.放置各種資原始檔,圖片,聲音,id,佈局都放到相應的資料夾中就可以
4.放置jar包
就是這個
5.有個東西一開始沒有導進來,結果開啟攝像頭時一直閃退
不太清楚這個東西的作用
6.最後需要修改一點點程式碼來實現頁面跳轉

因為zxing的demo預設是不跳轉頁面的,只把掃描結果解析出來顯示一個地址,主要是在captureactivity.java中找到處理掃描結果的函式!記住要把fnish註釋掉~ OK啦!測試成功~很開心

public void handleDecode(Result result, Bitmap barcode) {
        inactivityTimer.onActivity();
        playBeepSoundAndVibrate();
        String resultString = result.getText();
        //FIXME
if (resultString.equals("")) { Toast.makeText(CaptureActivity.this, "Scan failed!", Toast.LENGTH_SHORT).show(); }else { // System.out.println("Result:"+resultString); Intent resultIntent = new Intent(); Bundle bundle = new Bundle(); bundle.putString("result"
, resultString); resultIntent.putExtras(bundle); this.setResult(RESULT_OK, resultIntent); //網上查到的方法:掃描後直接簡單跳轉到頁面 Pattern pattern=Pattern.compile("http://(([a-zA-z0-9]|-){1,}\\.){1,}[a-zA-z0-9]{1,}-*"); Matcher matcher=pattern.matcher(resultString); if
(matcher.find()){ //判斷掃描的結果是否為url、是url直接跳轉到網頁中 Intent intent = new Intent(); intent.setAction("android.intent.action.VIEW"); Uri content_url = Uri.parse(resultString); intent.setData(content_url); startActivity(intent); }else{//不是直接顯示 //建立dialog物件 AlertDialog alertDialog = new AlertDialog.Builder(CaptureActivity.this).create(); alertDialog.setTitle("掃描結果"); //顯示掃描的資訊 alertDialog.setMessage(resultString); //設定按鈕 alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss(); } }); alertDialog.show(); } } // CaptureActivity.this.finish(); }

存在的一點小問題是,空指標異常載入不出佈局檔案,不過不影響專案執行,這個問題後面再研究吧
搜了一下解決方法,好麻煩