//代码
private Button button1;
private Button button2;
private TextView textView;
private EditText editText;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inid();
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
startActivityForResult(intent, 0);
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String trim = editText.getText().toString().trim();
if (TextUtils.isEmpty(trim)) {
Toast.makeText(MainActivity.this, "不能为空", 0).show();
} else {
try {
Bitmap bitmap = EncodingHandler.createQRCode(trim, 400);
imageView.setImageBitmap(bitmap);
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
String result = data.getExtras().getString("data");
textView.setText(result);
}
super.onActivityResult(requestCode, resultCode, data);
}
private void inid() {
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
textView = (TextView) findViewById(R.id.tv);
editText = (EditText) findViewById(R.id.et);
imageView = (ImageView) findViewById(R.id.img);
}
//布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="扫描二维码" />
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="你好"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000" />
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minLines="2" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="生成二维码" />
<ImageView
android:id="@+id/img"
android:layout_width="400dp"
android:layout_height="400dp" />
</LinearLayout>
//权限
manifest android:versionName="1.0" android:versionCode="1" package="com.dqf" xmlns:android="http://schemas.android.com/apk/res/android"> <uses-sdk android:targetSdkVersion="17" android:minSdkVersion="17"/> <uses-permission android:name="android.permission.VIBRATE"/> <uses-permission android:name="android.permission.CAMERA"/> <uses-feature android:name="android.hardware.camera"/> <uses-feature android:name="android.hardware.camera.autofocus"/> -<application android:theme="@style/AppTheme" android:label="@string/app_name" android:icon="@drawable/ic_launcher" android:allowBackup="true"> -<activity android:name=".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:name="com.zxing.activity.CaptureActivity" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:windowSoftInputMode="stateAlwaysHidden" android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden"> </activity> </application> </manifest>