Android生成二維碼根據類型進行並掃描解析

最近在搞這塊東西,簡單分享下。
先說下思路,首先是根據某些信息比如價格,標題,之類的調用CodeUtils.createImage設置寬高,String和轉換成bitmap,然後再讓Imageview去加載(原生設置或者glide都行).
然後就是掃描了,掃描的話我前兩篇博客寫了,就是利用了 CaptureFragment進行掃描,然後有個回調(analyzeCallback),有兩個onAnalyzeSuccess和onAnalyzeFail,在Success的時候進行處理,如下:

    private void scanSuccess(String result) {
        LogUtil.e("chen  描掃結果處理 ", result);
        if (!Check.isEmpty(result) && result.indexOf("http") > -1) {
            if(result.indexOf("mall/goodsDetail")>-1){
                Uri uri = Uri.parse(result);
                String goodsId = uri.getQueryParameter("goodsId");
                String courseType = uri.getQueryParameter("courseType");
                if (result.endsWith("&courseType")) {
                    //說明是專欄
                    ARouter.getInstance().build(SPECIAL_DETAILS_ACTIVITY)
                            .withString("data", null)
                            .withString("goodsId", goodsId)
                            .withString("payType", "1").navigation();
                } else {
                    if (courseType.equals("VIDEO") || courseType.equals("AUDIO")) {
                        //2 課程
                        ARouter.getInstance().build(CURRICULUM_DETAILS_ACTIVITY)
                                .withString("data", null)
                                .withString("goodsId", goodsId)
                                .withString("coursetype", courseType)
                                .withString("payType", "1").navigation();
                    } else {
                        ARouter.getInstance().build(LIVE_BROADCAST_DETAILS_ACTIVITY)
                                .withString("data", null)
                                .withString("goodsId", goodsId)
                                .withString("coursetype", courseType)
                                .withString("payType", "1").navigation();
                    }
                }
                return;
            }else if(result.indexOf("mall/mallShop")>-1){
                //分享店鋪
                Uri uri = Uri.parse(result);
                String shopId = uri.getQueryParameter("shopId");
                String inviteUserId = uri.getQueryParameter("inviteUserId");
                //店鋪分享
                ARouter.getInstance().build(SHOP_FRONT_PAGE_ACTIVITY)
                        .withString("shopId",shopId)
                        .withString("payType","1")
                        .withString("inviteUserId",inviteUserId)
                        .navigation();
                return;
            }

            //其他走網頁
            Pattern pattern = Patterns.WEB_URL;
            Matcher matcher = pattern.matcher(result);
            if (matcher.find()) {
                link = matcher.group(0);
            }
            Intent intent = new Intent(this, CommonWebActivity.class);
            intent.putExtra("webUrl", link);
            intent.putExtra("webTitle", " ");
            intent.putExtra("mContent", "");
            startActivity(intent);
            finish();
            return;
        }
        try {
            JSONObject jsonObject = new JSONObject(result);
            if (jsonObject.has("type")) {
                ScanResult scanResult = new Gson().fromJson(result, ScanResult.class);
                if (scanResult != null) {
                    if (scanResult.getType() != 0) {
                        switch (scanResult.getType()) {
                            //好友
                            case 1:
                                UserProfileActivity.start(this, scanResult.getAccount(), true);
                                break;
                            //羣組
                            case 2:
                                AdvancedTeam(scanResult.getAccount());
                                break;
                            //社羣
                            case 3:
                                AdvancedTeam(scanResult.getAccount());
                                break;
                        }
                    }
                }
            } else {
                ToastUtils.error("無法識別此二維碼!");
            }
            finish();
        } catch (JSONException e) {
            e.printStackTrace();
            ToastUtils.error("無法識別此二維碼!");
            finish();
        }

    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章