二维码相关

1、扫描二维码

//打开默认的扫码页面
    public void openQrCodeActivity(View view) {
        Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
        startActivityForResult(intent,100);

    }
    //重新该方法接收扫码之后的结果
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == 100 && resultCode == RESULT_OK){
            //获取扫码结果
            if (data != null) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS){//成功
                    String result = bundle.getString(CodeUtils.RESULT_STRING);
                    Toast.makeText(this, "扫码结果:"+result, Toast.LENGTH_SHORT).show();
                }else if(bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED){//失败
                    Toast.makeText(this, "解析结果失败", Toast.LENGTH_SHORT).show();
                }
            }
        }

        else if(requestCode == 200 && resultCode == RESULT_OK){
            if (data != null) {
                Uri uri = data.getData();
                CodeUtils.analyzeBitmap(ImageUtil.getImageAbsolutePath(this, uri), new CodeUtils.AnalyzeCallback() {
                    @Override
                    public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
                        Toast.makeText(MainActivity.this, "解析结果:"+result, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onAnalyzeFailed() {
                        Toast.makeText(MainActivity.this, "解析失败", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }

    }

    //打开系统的相册选择一张图片扫码
    public void openPhoto(View view) {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("image/*");
        startActivityForResult(intent,200);
    }

    //生成不带logo的二维码
    public void createQrCodeNoImage(View view) {
        Bitmap bitmap = CodeUtils.createImage("HelloQRCode", 600, 600, null);
        imageView.setImageBitmap(bitmap);
    }

    //生成带logo的二维码
    public void createOrCodeWithImage(View view) {
        Bitmap bitmap = CodeUtils.createImage("HelloQRCode", 600, 600, BitmapFactory.decodeResource(getResources(),R.mipmap.a));
        imageView.setImageBitmap(bitmap);
    }

2、长按识别二维码

imageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                //专门用于解密的一个核心类
                MultiFormatReader multiFormatReader = new MultiFormatReader();
                //用来存储支持扫码类型
                Hashtable<DecodeHintType,Object> hints = new Hashtable<>(2);
                Vector<BarcodeFormat> decodeFormats = new Vector<>();
                if(decodeFormats == null || decodeFormats.isEmpty()){
                    decodeFormats = new Vector<>();
                    // 这里设置可扫描的类型,我这里选择了都支持
                    decodeFormats.addAll(DecodeFormatManager.ONE_D_FORMATS);//一维码
                    decodeFormats.addAll(DecodeFormatManager.QR_CODE_FORMATS);//二维码
                    decodeFormats.addAll(DecodeFormatManager.DATA_MATRIX_FORMATS);//其他码
                }
                hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
                // 设置继续的字符编码格式为UTF8
                // hints.put(DecodeHintType.CHARACTER_SET, "UTF8");
                // 设置解析配置参数
                multiFormatReader.setHints(hints);
                // 开始对图像资源解码
                Result rawResult = null;
                try {
                    //通过该语句,实现了解密,把解码的结果封装赋值给了Result类
                    rawResult = multiFormatReader.decodeWithState(new BinaryBitmap(new HybridBinarizer(new BitmapLuminanceSource(bitmap))));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (rawResult != null) {
                    /*if (analyzeCallback != null) {
                        analyzeCallback.onAnalyzeSuccess(mBitmap, rawResult.getText());
                    }*/

                    Toast.makeText(MainActivity.this,"识别结果是:" + rawResult.getText(), Toast.LENGTH_SHORT).show();
                } else {
                    /*if (analyzeCallback != null) {
                        analyzeCallback.onAnalyzeFailed();
                    }*/
                    Toast.makeText(MainActivity.this, "对不起,识别失败了", Toast.LENGTH_SHORT).show();
                }
                return true;
            }
        });
上一篇:Mr.Alright---安卓Q R 原生Launcher 偶尔无法添加小部件


下一篇:Android Toast控件