public class MainActivity extends AppCompatActivity {
private TextView mTv_Ruslet;
private EditText mInput;
private ImageView mImg;
private CheckBox isLogo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTv_Ruslet = (TextView) findViewById(R.id.tv_ruselt);
mInput = (EditText) findViewById(R.id.et_text);
mImg = (ImageView) findViewById(R.id.img);
isLogo = (CheckBox) findViewById(R.id.is_logo);
}
/**
* 生成二维码
*/
public void make(View view) {
String input = mInput.getText().toString().trim();
//生成二维码,然后为二维码增加logo
Bitmap bitmap = EncodingUtils.createQRCode(input, 500, 500,
isLogo.isChecked() ? BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher) : null
);
mImg.setImageBitmap(bitmap);
}
/**
* 扫描二维码
*/
public void scan(View view) {
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();
String result = bundle.getString("result");
mTv_Ruslet.setText(result);
mInput.setText(result);
} else if (resultCode == RESULT_CANCELED) {
mTv_Ruslet.setText("扫描出错");
}
}
}
=========================布局===============================
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="scan"
android:text="扫描二维码" />
<TextView
android:id="@+id/tv_ruselt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="26sp" />
<EditText
android:id="@+id/et_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="make"
android:text="生成二维码" />
<CheckBox
android:id="@+id/is_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check" />
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@mipmap/ic_launcher" />