二维码扫描加logo二维码生成

首先导入依赖+权限

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA" />
compile 'cn.yipianfengye.android:zxing-library:2.2'

一、写activity_main.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="as.bwei.com.erweima.MainActivity">

    <Button
        android:text="二维码"
        android:id="@+id/toQR"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

二、跳转页面

public class MainActivity extends AppCompatActivity {

    private Button toQR;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toQR = (Button) findViewById(R.id.toQR);

        toQR.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, QRActivity.class);
                startActivity(intent);
            }
        });
    }
}

三、写activity_qr.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="as.bwei.com.erweima.QRActivity">

    <Button
        android:id="@+id/scan_btn"
        android:text="扫描二维码"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/content_et"
        android:hint="输入字符"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/create_btn"
        android:text="生成二维码"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ImageView
        android:layout_gravity="center"
        android:id="@+id/img_qr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

四、QRActivity主代码

public class QRActivity extends AppCompatActivity {
    private Button scanBtn;
    private ImageView imgQr;
    private Button createBtn;
    private EditText contentEt;
    private int REQUEST_CODE=0x1000;
    private Bitmap mBitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qr);
        init();
    }
    private void init() {
        ininView();
        initData();
    }

    private void initData() {
        ZXingLibrary.initDisplayOpinion(this);
        scanBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(QRActivity.this, CaptureActivity.class);
                startActivityForResult(intent, REQUEST_CODE);


            }
        });
        createBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String textContent = contentEt.getText().toString();
                if (TextUtils.isEmpty(textContent)) {
                    Toast.makeText(QRActivity.this, "您的输入为空!", Toast.LENGTH_SHORT).show();
                    return;
                }
                contentEt.setText("");
                mBitmap = CodeUtils.createImage(textContent, 400, 400, BitmapFactory.decodeResource(getResources(), R.drawable.w));
                imgQr.setImageBitmap(mBitmap);
            }
        });

    }

    private void ininView() {
        scanBtn = (Button) findViewById(R.id.scan_btn);
        imgQr = (ImageView) findViewById(R.id.img_qr);
        createBtn = (Button) findViewById(R.id.create_btn);
        contentEt = (EditText) findViewById(R.id.content_et);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE) {
            //处理扫描结果(在界面上显示)
            if (null != data) {
                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_LONG).show();
                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                    Toast.makeText(QRActivity.this, "解析二维码失败", Toast.LENGTH_LONG).show();
                }
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/WenHaowen/article/details/82866120
今日推荐