Android开发界面跳转传输数据并生成二维码

PS:这是一个适合入门练手的Android教程

        作为一个Android新手,是不是觉得别人的app能生成二维码很高大上?来来来,今天小Panda_Lizy就和你分享一下如何让自己的app能有个高大上的功能————查看二维码。

        咋们先来科普一下为什么“端”需要提供“生成二维码”的功能:极大缓解服务器压力

  不用读取服务器存储的二维码图片,极大缓解服务器运行、带宽压力,并将这种压力转移到“端”。传输一个字符串和一张图片的效率的比较,你懂的!


唠叨了这么久,干货来了!!!

一、涉及到的知识

1.界面间的跳转并传参(参考资料:http://www.cnblogs.com/feng88724/archive/2011/02/10/1961225.html)

2.二维码扫描技术(Google ZXing开源项目:http://www.open-open.com/lib/view/open1344150168061.html)


二、Let's do it

1、首先,你得有个Android开发环境。(参考资料:http://jingyan.baidu.com/article/f0062228f0b18afbd2f0c871.html)

2、Import 名为“BarCodeTest”的项目。(来自ZXing的项目,源文件地址:链接: http://pan.baidu.com/s/1pJz6Q3h 密码: u9ai)

FIle—>Import—>选择BarCodeTest项目文件夹

3、在layout里面添加一个Android XML File,命名为enter.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="生成二维码" />

</LinearLayout>

4、将layout里面的main.xml编辑为

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >

    
    
    <LinearLayout 
        android:orientation="horizontal"
        android:layout_marginTop="10dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        
        <TextView 
        android:id="@+id/Bundle_string"
       	android:layout_width="fill_parent"
       	android:textSize="18sp"
       	android:textColor="@android:color/black"
        android:layout_height="wrap_content" />
        
        
        <TextView 
        android:id="@+id/orderID"
       	android:layout_width="fill_parent"
       	android:textSize="18sp"
       	android:textColor="@android:color/black"
        android:layout_height="wrap_content" />
    </LinearLayout>
    
    
    <ImageView 
        android:id="@+id/iv_qr_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_gravity="center"/>

</LinearLayout>

5、在src—>com.ericssonlabs—>BarCodeTestActivity.java进行修改为

package com.ericssonlabs;

import com.google.zxing.WriterException;
import com.zxing.activity.CaptureActivity;
import com.zxing.encoding.EncodingHandler;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class BarCodeTestActivity extends Activity {
    /** Called when the activity is first created. */
	
	
	private EditText qrStrEditText;//内容输入文本框
	private Button generateQRCodeButton;//跳转按钮
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.enter);
        
        //获得text、button的对象
        qrStrEditText = (EditText) this.findViewById(R.id.editText1);
        generateQRCodeButton = (Button) this.findViewById(R.id.button1);
        
        
        //button的处理事件
        generateQRCodeButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String contentString = qrStrEditText.getText().toString();
				if (!contentString.equals("")) {
					Intent intent = new Intent();
					intent.setClass(BarCodeTestActivity.this, showcode.class);
					intent.putExtra("QBcode_string", contentString);
					startActivity(intent);
				}else {
					Toast.makeText(BarCodeTestActivity.this, "Text can not be empty", Toast.LENGTH_SHORT).show();
				}
			}
		});
    }
}

6、添加一个名为“showcode.java”的class到com.ericssonlabs中,

package com.ericssonlabs;


import com.google.zxing.WriterException;
import com.zxing.activity.CaptureActivity;
import com.zxing.encoding.EncodingHandler;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class showcode extends Activity {
    /** Called when the activity is first created. */
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>private TextView QBcode;//上个页面传过来的数据(通用接口:传过来的内容直接写入此)
<span style="white-space:pre">	</span>private ImageView qrImgImageView;
<span style="white-space:pre">	</span>
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        QBcode = (TextView) this.findViewById(R.id.Bundle_string);
        qrImgImageView = (ImageView) this.findViewById(R.id.iv_qr_image);
        
        
        Intent intent =getIntent();//显示上个activity传过来的字符串
        String QBcode_string = intent.getStringExtra("QBcode_string");
       
        QBcode.setText("上个页面传过来的数据:"+QBcode_string);
        
        
        //开始绘制二维码图像
        try {
<span style="white-space:pre">			</span>//String contentString = QBcode.getText().toString();
<span style="white-space:pre">			</span>String contentString = QBcode_string;
<span style="white-space:pre">			</span>if (!contentString.equals("")) {
<span style="white-space:pre">				</span>Bitmap qrCodeBitmap = EncodingHandler.createQRCode(contentString, 350);
<span style="white-space:pre">				</span>qrImgImageView.setImageBitmap(qrCodeBitmap);
<span style="white-space:pre">			</span>}else {
<span style="white-space:pre">				</span>Toast.makeText(showcode.this, "Text can not be empty", Toast.LENGTH_SHORT).show();
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>
<span style="white-space:pre">		</span>} catch (WriterException e) {
<span style="white-space:pre">			</span>// TODO Auto-generated catch block
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">		</span>}
        
        
        
       
<span style="white-space:pre">		</span>
    }


<span style="white-space:pre">	</span>
}

7、最后别忘记了在/AndroidManifest.xml里面添加showcode的activity

<activity
            android:configChanges="orientation|keyboardHidden"
            android:name="com.ericssonlabs.showcode"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
            android:windowSoftInputMode="stateAlwaysHidden" >
        </activity>

8、最后测试一下



三、最后附上完成后的项目源码

链接: http://pan.baidu.com/s/1qWrxpH6 密码: isl4






第一次写文,欢迎拍砖~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值