private Button button;
private TextView text;
private Button genButton;
private ImageView img;
private EditText input;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
text = (TextView) findViewById(R.id.text);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplication(), "你可以扫描二维码", 0).show();
Intent startScan = new Intent(MainActivity.this,CaptureActivity.class);
startActivityForResult(startScan,0);
}
});
genButton = (Button) findViewById(R.id.gen);
img = (ImageView) findViewById(R.id.img);
input = (EditText) findViewById(R.id.input);
genButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String inputStr = input.getText().toString().trim();
try {
if(!TextUtils.isEmpty(inputStr)){
Bitmap qrcode = EncodingHandler.createQRCode(inputStr, 400);
img.setImageBitmap(qrcode);
}
} catch (WriterException e) {
e.printStackTrace();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 0 && resultCode == RESULT_OK){
String result = data.getExtras().getString("result");
text.setText(result);
}
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android1:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android1:id="@+id/button"
android1:layout_width="match_parent"
android1:layout_height="wrap_content"
android1:text="扫描二维码" />
<TextView
android1:id="@+id/text"
android1:layout_width="match_parent"
android1:layout_height="wrap_content"
android:layout_below="@id/button"
/>
<EditText
android1:id="@+id/input"
android1:layout_width="match_parent"
android1:layout_height="wrap_content"
android:layout_below="@id/text"
android:hint="请输入要编码的内容"
/>
<Button
android1:id="@+id/gen"
android1:layout_width="match_parent"
android1:layout_height="wrap_content"
android1:text="生成二维码"
android:layout_below="@id/input"
/>
<ImageView
android1:id="@+id/img"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android:layout_below="@id/gen"
android:layout_gravity="center"
/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wxj.activity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.wxj.activity.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<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>
</application>
</manifest>
源码下载地址:http://download.csdn.net/detail/u010648159/8933563