标签:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical"> 7 8 <Button 9 android:id="@+id/bt_decode" 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:onClick="bt_decode" 13 android:text="扫描二维码" /> 14 15 <TextView 16 android:id="@+id/tv_decode" 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:text="扫描结果:" /> 20 <EditText 21 android:id="@+id/et_encode" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:hint="请输入要编译的文字" /> 25 <Button 26 android:id="@+id/bt_encode" 27 android:layout_width="match_parent" 28 android:layout_height="wrap_content" 29 android:onClick="bt_encode" 30 android:text="生成二維碼" /> 31 32 <CheckBox 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content" 35 android:text="是否编译logo" 36 android:id="@+id/cb_ishavelogo" 37 android:checked="false" /> 38 <ImageView 39 android:layout_width="wrap_content" 40 android:layout_height="wrap_content" 41 android:id="@+id/img_encoderesult" /> 42 </LinearLayout>
1 package com.qg.lizhanqi; 2 3 import android.content.Intent; 4 import android.graphics.Bitmap; 5 import android.graphics.BitmapFactory; 6 import android.net.Uri; 7 import android.os.Bundle; 8 import android.support.v7.app.AppCompatActivity; 9 import android.text.TextUtils; 10 import android.view.View; 11 import android.widget.Button; 12 import android.widget.CheckBox; 13 import android.widget.EditText; 14 import android.widget.ImageView; 15 import android.widget.TextView; 16 import android.widget.Toast; 17 18 import com.xys.libzxing.zxing.activity.CaptureActivity; 19 import com.xys.libzxing.zxing.encoding.EncodingUtils; 20 21 public class MainActivity extends AppCompatActivity { 22 private Button btDecode; 23 private TextView tvDecode; 24 private EditText etEncode; 25 private Button btEncode; 26 private CheckBox cbIshavelogo; 27 private ImageView imgEncoderesult; 28 29 @Override 30 protected void onCreate(Bundle savedInstanceState) { 31 super.onCreate(savedInstanceState); 32 setContentView(R.layout.activity_main); 33 btDecode = (Button) findViewById(R.id.bt_decode); 34 tvDecode = (TextView) findViewById(R.id.tv_decode); 35 etEncode = (EditText) findViewById(R.id.et_encode); 36 btEncode = (Button) findViewById(R.id.bt_encode); 37 cbIshavelogo = (CheckBox) findViewById(R.id.cb_ishavelogo); 38 imgEncoderesult = (ImageView) findViewById(R.id.img_encoderesult); 39 } 40 41 //扫描解码 42 public void bt_decode(View view) { 43 startActivityForResult(new Intent(this, CaptureActivity.class), 0); 44 } 45 46 @Override 47 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 48 super.onActivityResult(requestCode, resultCode, data); 49 if (resultCode == RESULT_OK) { 50 Bundle bundle = data.getExtras(); 51 String result = bundle.getString("result"); 52 //显示到TextView上 53 tvDecode.setText(result); 54 //处理扫描结果 55 lastwork(result); 56 } 57 } 58 //处理扫描结果的方法 59 private void lastwork(String result) { 60 if(result.startsWith("http://")||result.startsWith("https://")){//网页 61 //跳转到浏览器加载网页 62 Intent it = new Intent(Intent.ACTION_VIEW);//跳转意图 63 it.setData(Uri.parse(result));//设置跳转携带的参数 64 startActivity(it);//跳转 65 // finish(); //关闭当前页面 66 }else if(result.startsWith("tel:")){//电话 67 Intent it = new Intent(Intent.ACTION_DIAL); 68 it.setData(Uri.parse(result)); 69 startActivity(it); 70 }else if(result.startsWith("smsto:")){//短信 71 Intent it = new Intent(Intent.ACTION_SENDTO); 72 it.setData(Uri.parse(result)); 73 startActivity(it); 74 }else if(result.startsWith("mailto:")){//邮件 75 Intent it = new Intent(Intent.ACTION_SENDTO); 76 it.setData(Uri.parse(result)); 77 startActivity(it); 78 }else if(result.startsWith("market://")){//应用市场 79 Intent it = new Intent(Intent.ACTION_SENDTO); 80 it.setData(Uri.parse(result)); 81 startActivity(it); 82 }else{//其他的弹窗显示 83 Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show(); 84 } 85 } 86 //生成二维码图 87 public void bt_encode(View view) { 88 String text = etEncode.getText().toString().trim(); 89 if (TextUtils.isEmpty(text)) { 90 Toast.makeText(MainActivity.this, "无可编译的字段", Toast.LENGTH_SHORT).show(); 91 } else { 92 // createQRCode(文本,宽,高,logo图片) 93 // Bitmap qrCode = EncodingUtils.createQRCode(text, 500, 500, null);//最基本的 94 //使用三目运算符判断选择框是否选中,生成是否带有logo的二维码图片 95 Bitmap qrCode = EncodingUtils.createQRCode(text, 500, 500, cbIshavelogo.isChecked() ? BitmapFactory.decodeResource(getResources(), R.drawable.logo) : null); 96 imgEncoderesult.setImageBitmap(qrCode); 97 } 98 } 99 }
标签:
原文地址:http://www.cnblogs.com/lizhanqi/p/5639826.html