二维码的实现

来源:互联网 发布:windows 98 plus 编辑:程序博客网 时间:2024/05/21 08:02

扫描

1.引入二维码工程 mobi.espier.zxing


2.启动库中的扫描界面 ScanCodeActivity.class

public final static int ZXING_SCAN_RESULT = 100;
    public static void startCodeScan(Activity originActivity) {
        Intent intent = new Intent(originActivity, ScanCodeActivity.class);
        intent.setAction(ZXingIntents.Scan.ACTION);
        originActivity.startActivityForResult(intent, ZXING_SCAN_RESULT);
    }


3.onActivityResult 解析处理数据 (我们的处理是将数据上传到服务器,与本地保存)

if (requestCode == XmppUtils.ZXING_SCAN_RESULT) {
            XmppTVAccountInfo info = XmppUtils.parseCodeScanResult(requestCode, resultCode, data);
            if (null != info) {
                XmppBaseResult result =
                        XmppUtils.sePushTVInfo(this, mXmppEngineerInfo.getSeid(), mMd5Password,
                                ApkUtil.getClientID(this), info.getClientId(),
                                "37.8383040567,114.7042499480", "河北省石家庄市栾城县南高乡西安庄村西南方向");
                if (null != result) {
                    if (result.getType() == XmppRequestResultType.TYPE_OK) {

                    } else {
                        LogUtils.d("ljp", "---error type:" + result.getType() + "---detail:"
                                + result.getDetail());
                        Toast.makeText(this, result.getDetail().toString(), Toast.LENGTH_SHORT)
                                .show();

                        saveXmppTvAccountInfoInLocal(this, mXmppEngineerInfo.getSeid(), info);

                        XmppTVAccountInfo saveInfo =
                                getXmppTvAccountInfoLocalFromLocal(this,
                                        mXmppEngineerInfo.getSeid());
                    }
                }
            }
        }


 public static XmppTVAccountInfo parseCodeScanResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ZXING_SCAN_RESULT: {
                LogUtils.d(TAG, " ZXING_SCAN_RESULT finish --- ");
                if (resultCode == Activity.RESULT_OK) {
                    String result = data.getStringExtra(ZXingIntents.Scan.RESULT);  //这个result就是生成二维码时传进去的数据(包括jesion数据)
                    String resultFormat = data.getStringExtra(ZXingIntents.Scan.RESULT_FORMAT);

                    if (TextUtils.isEmpty(result)) return null;
                    if (BarcodeFormat.QR_CODE.toString().equals(resultFormat)
                            && result.startsWith(XmppConstants.XMPP_BARCODE_PREFIX)) {

                        XmppTVAccountInfo account =
                                new XmppTVAccountInfo(
                                    result.substring(XmppConstants.XMPP_BARCODE_PREFIX.length()));
                        LogUtils.d(TAG, "ZXING_SCAN_RESULT finish --- " + account.toString());
                        return account;
                    }
                }
                break;
            }
            default:
                break;
        }
        return null;
    }




生成二维端  :即生成 一个图片,在textView 或ImageView等控件中显示 出来

    static Bitmap encodeZXingBitmap(String data, int dimension) {
        String encodedData = data;

//data为jesion字字符串如:account:{"userid":"78cbb4e8242276ff2cf0e0fe4f1cc138@qq.com","usertoken":"80a8416f392c863132b40ce83fa903b9"}

// JSONObject jo = new JSONObject(); jo.put("userid","78cbb4e8242276ff2cf0e0fe4f1cc138@qq.com");......

//String data="account"+jo.tostrong();


        if (encodedData == null) {
            return null;
        }
        Map<EncodeHintType, Object> hints =
                new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
        hints.put(EncodeHintType.MARGIN, 5);//5  图片处置的空白宽度
        String encoding = guessAppropriateEncoding(encodedData);
        if (encoding != null) {
            hints.put(EncodeHintType.CHARACTER_SET, encoding);
        }
        BitMatrix result;
        try {
            result =
                    new MultiFormatWriter().encode(encodedData, BarcodeFormat.QR_CODE, dimension,
                            dimension, hints);
        } catch (IllegalArgumentException iae) {
            return null;
        } catch (WriterException e) {
            return null;
        }
        int width = result.getWidth();
        int height = result.getHeight();
        int[] pixels = new int[width * height];
        for (int y = 0; y < height; y++) {
            int offset = y * width;
            for (int x = 0; x < width; x++) {
                pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.WHITE;
            }
        }
        
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;
    }
    
    private static String guessAppropriateEncoding(CharSequence contents) {
        for (int i = 0; i < contents.length(); i++) {
            if (contents.charAt(i) > 0xFF) {
                return "UTF-8";
            }
        }
        return null;
    }



0 0
原创粉丝点击