标签:
二维码在APP中的使用越来越多,于是就是找了相关的资料写了一个二维码扫描和生成二维码的Demo。
本Demo使用了第三方的ZXing库。github的地址:
AndroidStudio中的引用:
File---New---import module导入ZXing的第三方库然后选中项目按F4,
点击加号选择File dependency将ZXing库导入。
接下来就开始写布局文件
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.zwb.zxingdemo.MainActivity"> <Button android:id="@+id/start" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="扫描二维码" /> <TextView android:layout_marginTop="20dp" android:textColor="#e90518" android:text="扫描的结果:" android:textSize="18sp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_show" android:layout_width="match_parent" android:layout_height="wrap_content" /> <View android:background="@color/colorPrimary" android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="5dp"/> <LinearLayout android:layout_marginTop="20dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/ed_text" android:layout_width="0dp" android:layout_weight="9" android:layout_marginTop="10dp" android:layout_height="wrap_content" /> <CheckBox android:id="@+id/cb_logo" android:text="logo" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <Button android:id="@+id/make_ma" android:text="生成二维码" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ImageView android:id="@+id/img" android:layout_marginTop="10dp" android:background="@mipmap/ic_launcher" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.CheckBox; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.xys.libzxing.zxing.activity.CaptureActivity; import com.xys.libzxing.zxing.encoding.EncodingUtils; import org.w3c.dom.Text; public class MainActivity extends AppCompatActivity { private TextView textView; private ImageView imageView; private EditText input; private CheckBox logo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = ((TextView) findViewById(R.id.tv_show)); imageView = ((ImageView) findViewById(R.id.img)); logo = ((CheckBox) findViewById(R.id.cb_logo)); findViewById(R.id.start).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivityForResult(new Intent(MainActivity.this, CaptureActivity.class), 0); } }); findViewById(R.id.make_ma).setOnClickListener(new View.OnClickListener() { public void onClick(View v) { input = ((EditText) findViewById(R.id.ed_text)); String text = input.getText().toString(); if (text.equals("")) { Toast.makeText(MainActivity.this, "输入内容不能为空", Toast.LENGTH_SHORT).show(); } else { /** * 四个参数:传入的文字、宽度、高度、logo */ Bitmap bitmap = EncodingUtils.createQRCode(text, 500, 500, logo.isChecked() ? BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher) : null); imageView.setImageBitmap(bitmap); } } }); } @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"); textView.setText(result); } } }
标签:
原文地址:http://blog.csdn.net/zuo_0625/article/details/51361905