zxing库下载地址:http://download.csdn.net/detail/xiaruoli89/9536545
需要依赖zxing这个module
布局文件:
<?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="zxing.my.com.zxingdemo.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始扫描" android:id="@+id/button" android:onClick="scan" android:layout_gravity="center_horizontal" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:text="result:"/> <TextView android:id="@+id/tv_result" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" /> </LinearLayout>
代码:
private TextView tv_result; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_result= (TextView) findViewById(R.id.tv_result); } public void scan(View v){ 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(); tv_result.setText(bundle.getString("result")); } }
在百度上找个二维码生成器,随便写一段文字,生成二维码,然后扫描后的信息就会出现在tv_result里了