标签:
今天,来学习使用怎么实现二维码的扫一扫和二维码的生成。
要想实现二维码的扫一扫与二维码的生成,就得必须了解二维码的特征。
<?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" tools:context="com.xbmu.scan.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="开启扫一扫" android:onClick="startScan"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:text="结果:"/> <TextView android:id="@+id/tv_result" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp"/> </LinearLayout>MainActivity.java
package com.xbmu.scan; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.TextView; import com.xys.libzxing.zxing.activity.CaptureActivity; public class MainActivity extends Activity { private TextView tvResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvResult = (TextView) findViewById(R.id.tv_result); } /** * 点击扫一扫按钮,开启扫描二维码 * @param view */ public void startScan(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"); tvResult.setText(result); } } }其中:String result = bundle.getString("result");这句代码中的"result",可以根据阅读扫一扫源码得知:
<?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" tools:context="com.xbmu.scan.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="开启扫一扫" android:onClick="startScan"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:text="结果:"/> <TextView android:id="@+id/tv_result" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入文本" android:id="@+id/et_input_text" android:textSize="22sp"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="将输入文本生成二维码" android:onClick="makeQRCode"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/iv_result" android:background="@drawable/ic_launcher" android:layout_gravity="center"/> </LinearLayout>MainActivity.java
package com.xbmu.scan; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.os.Bundle; import android.view.View; 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 Activity { private TextView tvResult; private EditText etInputText; private ImageView ivResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvResult = (TextView) findViewById(R.id.tv_result); etInputText = (EditText) findViewById(R.id.et_input_text); ivResult = (ImageView) findViewById(R.id.iv_result); } /** * 点击扫一扫按钮,开启扫描二维码 * @param view */ public void startScan(View view){ //跳转到扫一扫 startActivityForResult(new Intent(MainActivity.this, CaptureActivity.class),0); } /** * 生成二维码 * @param view */ public void makeQRCode(View view){ String inputText = etInputText.getText().toString(); if(inputText.equals("")){ Toast.makeText(MainActivity.this,"输入的文本不能为空",0).show(); }else{ Bitmap bitmap = EncodingUtils.createQRCode(inputText, 500, 500, null); ivResult.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"); tvResult.setText(result); } } }运行效果:
<?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" tools:context="com.xbmu.scan.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="开启扫一扫" android:onClick="startScan"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:text="结果:"/> <TextView android:id="@+id/tv_result" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入文本" android:id="@+id/et_input_text" android:textSize="22sp"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="将输入文本生成二维码" android:onClick="makeQRCode"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/cb_select" android:text="是否带logo"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/iv_result" android:background="@drawable/ic_launcher" android:layout_gravity="center"/> </LinearLayout>MainActivity.java
package com.xbmu.scan; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; 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; public class MainActivity extends Activity { private TextView tvResult; private EditText etInputText; private ImageView ivResult; private CheckBox cbSelect; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvResult = (TextView) findViewById(R.id.tv_result); etInputText = (EditText) findViewById(R.id.et_input_text); ivResult = (ImageView) findViewById(R.id.iv_result); cbSelect = (CheckBox) findViewById(R.id.cb_select); } /** * 点击扫一扫按钮,开启扫描二维码 * @param view */ public void startScan(View view){ //跳转到扫一扫 startActivityForResult(new Intent(MainActivity.this, CaptureActivity.class),0); } /** * 生成二维码 * @param view */ public void makeQRCode(View view){ String inputText = etInputText.getText().toString(); if(inputText.equals("")){ Toast.makeText(MainActivity.this,"输入的文本不能为空",Toast.LENGTH_SHORT).show(); }else{ Bitmap bitmap = EncodingUtils.createQRCode(inputText, 500, 500, cbSelect.isChecked() ? BitmapFactory.decodeResource(getResources(),R.drawable.xiaomao) : null); ivResult.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"); tvResult.setText(result); } } }运行效果:
标签:
原文地址:http://blog.csdn.net/btt2013/article/details/51248810