Android二维码扫描应用
ZXing库精简版的下载:http://download.csdn.net/detail/a874508605/8546185
布局文件的编写
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/scan"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="开始扫描二维码" />
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="show" />
</LinearLayout>
在AndroidManifest.xml文件中声明相应的Activity
<activity
android:name="com.zxing.activity.CaptureActivity"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden" >
</activity>
需要用到的权限
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
相应实现的源码
<span style="font-size:14px;">public class MainActivity extends Activity {
private Button scanButton;
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scanButton = (Button) findViewById(R.id.scan);
text = (TextView) findViewById(R.id.text);
scanButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "你写可以扫描条形码或者二维码", Toast.LENGTH_SHORT).show();
Intent startScan = new Intent(MainActivity.this, CaptureActivity.class);
startActivityForResult(startScan, 0);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
String result = data.getExtras().getString("result");
text.setText(result);
}
}
</span><span style="font-size:14px;">}</span>
Android生成二维码应用 布局文件的编写
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入要编码的内容"
/>
<Button
android:id="@+id/gen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="生成二维码"
/>
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
相应实现的源码
public class MainActivity extends Activity {
private EditText input;
private Button genButton;
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = (EditText) findViewById(R.id.input);
genButton = (Button) findViewById(R.id.gen);
img = (ImageView) findViewById(R.id.img);
genButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String in = input.getText().toString();
if(in.equals("")){
Toast.makeText(MainActivity.this, "请输入文本", Toast.LENGTH_SHORT).show();
}else {
try {
Bitmap qrcode = EncodingHandler.createQRCode(in, 400);
img.setImageBitmap(qrcode);
} catch (WriterException e) {
e.printStackTrace();
}
}
}
});
}
}