zXing二维码的生成与扫描

具体代码
https://github.com/sunli0828/Month_Demo/blob/master/sunli1220/src/main/java/com/sunli/test1220/AnimatorOrCodeActivity.java
依赖

implementation 'cn.bingoogolapple:bga-qrcode-zxing:1.3.4'

activity

public class AnimatorOrCodeActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView animatorIcon;
    private ImageView iconqrcode;
    private Button btn_qrcode_sweep, btn_QQLogin;
    private Button btn_qrcode;
    private EditText textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animator_or_code);

        initView();
    }

    private void initView() {
        animatorIcon = findViewById(R.id.animator_icon);
        iconqrcode = findViewById(R.id.icon_qrcode);
        btn_qrcode_sweep = findViewById(R.id.btn_qrcode_sweep);
        btn_qrcode = findViewById(R.id.btn_qrcode);
        textView = findViewById(R.id.text_qrcode);
        btn_QQLogin = findViewById(R.id.btn_QQLogin);

        animatorIcon.setOnClickListener(this);
        btn_qrcode.setOnClickListener(this);
        btn_qrcode_sweep.setOnClickListener(this);
        btn_QQLogin.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.animator_icon:
                break;
            case R.id.btn_qrcode:
                creatQRCode();
                break;
            case R.id.btn_qrcode_sweep:
                checkedPermission();
                break;
            case R.id.btn_QQLogin:
                UMShareAPI umShareAPI = UMShareAPI.get(AnimatorOrCodeActivity.this);
                umShareAPI.getPlatformInfo(AnimatorOrCodeActivity.this, SHARE_MEDIA.QQ, new UMAuthListener() {
                    @Override
                    public void onStart(SHARE_MEDIA share_media) {

                    }

                    @Override
                    public void onComplete(SHARE_MEDIA share_media, int i, Map map) {
                        String name = map.get("screen_name");
                        String img = map.get("profile_image_url");
                        Log.i("sl", "name is "+name);
                        Log.i("sl",img);
                        Intent intent = new Intent(AnimatorOrCodeActivity.this, SuccessActivity.class);
                        intent.putExtra("name", name);
                        intent.putExtra("img", img);
                        startActivity(intent);
                    }

                    @Override
                    public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {

                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media, int i) {

                    }
                });
                break;
            default:
                break;
        }
    }

    private void checkedPermission() {
        startActivity(new Intent(AnimatorOrCodeActivity.this, SweepActivity.class));
    }

    private void creatQRCode() {
        QRTask qrTask = new QRTask(this, iconqrcode, textView);
        qrTask.execute(textView.getText().toString());
    }

    static class QRTask extends AsyncTask {

        private WeakReference mContext;
        private WeakReference imageView;

        public QRTask(Context context, ImageView mimageView, EditText editText) {
            mContext = new WeakReference<>(context);
            imageView = new WeakReference<>(mimageView);
        }

        @Override
        protected Bitmap doInBackground(String... strings) {
            String str = strings[0];
            if (TextUtils.isEmpty(str)) {
                return null;
            }
            return QRCodeEncoder.syncEncodeQRCode(str, 300);
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            if (bitmap != null) {
                imageView.get().setImageBitmap(bitmap);
            } else {
                Toast.makeText(mContext.get(), "生成失败", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

xml




    
    
        
        

相机

public class SweepActivity extends AppCompatActivity implements QRCodeView.Delegate{

        private ZXingView xingView;
        @Override
        protected void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_sweep);
            //获取资源ID
            xingView=findViewById(R.id.zxingView);
            xingView.setDelegate(this);
        }

        @Override
        protected void onResume(){
            super.onResume();
            xingView.startCamera();
            xingView.startSpotAndShowRect();
            /*xingView.openFlashlight();*/
        }

        @Override
        protected void onStop(){
            super.onStop();
            xingView.stopCamera();
        }

        @Override
        protected void onDestroy(){
            super.onDestroy();
            xingView.onDestroy();
        }

        @Override
        public void onScanQRCodeSuccess(String result){

            Toast.makeText(this,result.toString(),Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onCameraAmbientBrightnessChanged(boolean isDark){
            //环境改变,是否变暗,变暗isDark为true
        }

        @Override
        public void onScanQRCodeOpenCameraError(){
                //打开相机失败
        }
}

xml




    


你可能感兴趣的:(zXing二维码的生成与扫描)