android上实现二维码生成和扫描
2017-09-29 11:14
519 查看
先在androidstudio上导入libzing的Module,然后再app上把那个libzing添加上去,这样我们的app就关联了那个libzing库了
然后我们就只在app这个工程下写代码就行了。activity_main.xml布局如下
<?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" tools:context="com.example.qrcode.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>
Mainactivity.java
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("扫描出错"); } } }
注意一下,版本必须在16或16以上
运行结果:扫描跟生成二维码都是ok的
相关文章推荐
- Android实现二维码的生成和扫描
- cocos2dx-lua在android上实现生成及扫描二维码
- 在android上实现二维码生成和扫描
- Android使用zxing-android-embedded(由zxing开发)实现二维码生成和扫描
- Android实例-实现扫描二维码并生成二维码(XE8+小米5)
- 【Android】Android实现扫描、生成二维码的功能
- Android基于Zxing实现二维码,条形码扫描和生成二维码
- Android 使用Zxing实现二维码的生成,扫描
- Android高级控件(三)—— 使用Google ZXing实现二维码的扫描和生成相关功能体系
- Android平台生成二维码并实现扫描 & 识别功能
- 基于ZXing Android实现生成二维码图片和相机扫描二维码图片即时解码的功能
- Android中的Zxing实现二维码的生成以及扫描
- Android基于Zxing实现扫描和生成二维码
- Android-Zxing实现二维码的扫描与生成
- Android集成Zxing实现二维码的扫描与生成(含动态权限)
- Android高级控件(三)—— 使用Google ZXing实现二维码的扫描和生成相关功能体系
- Android 使用Zxing实现二维码的生成,扫描
- Android 使用Zxing实现二维码的生成,扫描
- Android基于Google Zxing实现二维码/条形码扫描、生成二维码/条形码
- Android 使用Zxing实现二维码的生成,扫描