一、达到的效果:
二、使用到的依赖包:
此库为Zxing的精简版,可以很容易在csdn下载,就不过多做介绍;
三、将此库导入项目到eclipse后,新建一个项目关联它,布局文件只需一个butoon,MainActivity代码如下:
package com.Even.demo_zing;import com.zxing.activity.CaptureActivity;
public class MainActivity extends Activity implements OnClickListener{ private Button button1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(this); } @Override public void onClick(View v) { //扫码操作 Intent intent=new Intent(this,CaptureActivity.class); startActivityForResult(intent,0); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode==Activity.RESULT_OK){
//重写result,获得扫描出来的内容: String result=data.getExtras().getString("result"); Toast.makeText(this, result, 1).show(); } }}
清单配置文件里:
1、增加两个权限,一个是相机,一个是震动
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.VIBRATE"/> 2、注册.CaptureActivity:<activity
android:configChanges="orientation|keyboardHidden" android:name="com.zxing.activity.CaptureActivity" android:screenOrientation="portrait" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:windowSoftInputMode="stateAlwaysHidden" > </activity>这样就可以简单地实现扫描功能了!