这是一个二维码扫描的教程
使用的依赖包在这里,http://download.csdn.net/detail/github_35546522/9651725
下载后需要import 后直接配置下依赖即可
//布局文件
<?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:orientation="vertical"> <Button android:id="@+id/bt_decode" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="bt_decode" android:text="扫描二维码" /> <TextView android:id="@+id/tv_decode" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="扫描结果:" /> <EditText android:id="@+id/et_encode" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="请输入要编译的文字" /> <Button android:id="@+id/bt_encode" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="bt_encode" android:text="生成二維碼" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="是否编译logo" android:id="@+id/cb_ishavelogo" android:checked="false" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/img_encoderesult" /> </LinearLayout>
//逻辑的处理
package com.qg.lizhanqi; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.view.View; import android.widget.Button; 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; public class MainActivity extends AppCompatActivity { private Button btDecode; private TextView tvDecode; private EditText etEncode; private Button btEncode; private CheckBox cbIshavelogo; private ImageView imgEncoderesult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btDecode = (Button) findViewById(R.id.bt_decode); tvDecode = (TextView) findViewById(R.id.tv_decode); etEncode = (EditText) findViewById(R.id.et_encode); btEncode = (Button) findViewById(R.id.bt_encode); cbIshavelogo = (CheckBox) findViewById(R.id.cb_ishavelogo); imgEncoderesult = (ImageView) findViewById(R.id.img_encoderesult); } //扫描解码 public void bt_decode(View view) { startActivityForResult(new Intent(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"); //显示到TextView上 tvDecode.setText(result); //处理扫描结果 lastwork(result); } } //处理扫描结果的方法 private void lastwork(String result) { if(result.startsWith("http://")||result.startsWith("https://")){//网页 //跳转到浏览器加载网页 Intent it = new Intent(Intent.ACTION_VIEW);//跳转意图 it.setData(Uri.parse(result));//设置跳转携带的参数 startActivity(it);//跳转 // finish(); //关闭当前页面 }else if(result.startsWith("tel:")){//电话 Intent it = new Intent(Intent.ACTION_DIAL); it.setData(Uri.parse(result)); startActivity(it); }else if(result.startsWith("smsto:")){//短信 Intent it = new Intent(Intent.ACTION_SENDTO); it.setData(Uri.parse(result)); startActivity(it); }else if(result.startsWith("mailto:")){//邮件 Intent it = new Intent(Intent.ACTION_SENDTO); it.setData(Uri.parse(result)); startActivity(it); }else if(result.startsWith("market://")){//应用市场 Intent it = new Intent(Intent.ACTION_SENDTO); it.setData(Uri.parse(result)); startActivity(it); }else{//其他的弹窗显示 Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show(); } } //生成二维码图 public void bt_encode(View view) { String text = etEncode.getText().toString().trim(); if (TextUtils.isEmpty(text)) { Toast.makeText(MainActivity.this, "无可编译的字段", Toast.LENGTH_SHORT).show(); } else { // createQRCode(文本,宽,高,logo图片) // Bitmap qrCode = EncodingUtils.createQRCode(text, 500, 500, null);//最基本的 //使用三目运算符判断选择框是否选中,生成是否带有logo的二维码图片 Bitmap qrCode = EncodingUtils.createQRCode(text, 500, 500, cbIshavelogo.isChecked() ? BitmapFactory.decodeResource(getResources(), R.drawable.logo) : null); imgEncoderesult.setImageBitmap(qrCode); } } }