简单实现二维码生成器

首先上网下载一个libzxing文件夹,网址为:https://github.com/xuyisheng/ZXingLib

 

接着把libzxing工程导入到工作空间中,然后把两个工程绑定在一起,步骤如下:

     先点击file-->project-->选中新创建的工程-->dependencies-->点击“+”号-->选中第三个Module Dependency-->找到libzxing,选中-->ok

把build.gradle中的

 
defaultConfig { applicationId "com.bwie.erweimastudy" minSdkVersion 16 targetSdkVersion 26 versionCode 1 versionName "1.0" }
两个工程的minSdkVersion 值必须相等!!!
创建布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp" tools:context="com.example.qrcode_demo1.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="scan" android:text="扫描二维码" /> <TextView android:id="@+id/tv_ruselt" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="26sp" /> <EditText android:id="@+id/et_text" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="make" android:text="生成二维码" /> <CheckBox android:id="@+id/is_logo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="check" /> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@mipmap/ic_launcher" /> </LinearLayout> 

 

public class MainActivity extends AppCompatActivity {

    private TextView mTv_Ruslet;
    private EditText mInput;
    private ImageView mImg;
    private CheckBox isLogo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTv_Ruslet = (TextView) findViewById(R.id.tv_ruselt);
        mInput = (EditText) findViewById(R.id.et_text);
        mImg = (ImageView) findViewById(R.id.img);
        isLogo = (CheckBox) findViewById(R.id.is_logo);
    }

    /**
     * 生成二维码
     */
    public void make(View view) {
        String input = mInput.getText().toString().trim();
        //生成二维码,然后为二维码增加logo
        Bitmap bitmap = EncodingUtils.createQRCode(input, 500, 500,
                isLogo.isChecked() ? BitmapFactory.decodeResource(getResources(),
                        R.mipmap.ic_launcher) : null
        );
        mImg.setImageBitmap(bitmap);
    }

    /**
     * 扫描二维码
     */
    public void scan(View view) {
        startActivityForResult(new Intent(MainActivity.this, CaptureActivity.class), 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            String result = bundle.getString("result");
            mTv_Ruslet.setText(result);
            mInput.setText(result);
        } else if (resultCode == RESULT_CANCELED) {
            mTv_Ruslet.setText("扫描出错");
        }
    }
}

 

 

 

 

 

 

 

 

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值