android开发之集成zxing,二维码,以及扫描二维码的功能实现。带源代码下载

package cc.jiusansec.www;

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;

/**
 * @author 官网:http://www.93sec.cc
 * 
 *         David编写: 微博:http://weibo.com/93sec.cc
 * 
 * @version V1.0正式版 扫描二维码及生成二维码
 * 
 * @process QQ986945193
 * 
 * @Note weibo.com/mcxiaobing
 * 
 * @dateTime 2015-10-18下午1:46:20
 * 
 */
public class MainActivity extends Activity {
	//读取二维码控件
	
	private Button mBtn_erweima;
	private TextView tView;//读取到的内容
	
	//生成二维码控件
	private Button btn_go;
	private EditText input;
	private ImageView img_two;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tView = (TextView) findViewById(R.id.tv_text);
		mBtn_erweima = (Button) findViewById(R.id.btn_erweima);
		mBtn_erweima.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				// TODO Auto-generated method stub
				// 启动扫描二维码的activity
				Intent startScan = new Intent(MainActivity.this,
						CaptureActivity.class);
				// startActivity(startScan);需要返回值,所以暂时不用此方法
				startActivityForResult(startScan, 1);

			}
		});
		img_two = (ImageView) findViewById(R.id.img_erweima);
		btn_go = (Button) findViewById(R.id.gen);
		input = (EditText) findViewById(R.id.input);
		btn_go.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String string = input.getText().toString();
				if (string.equals("")) {
					Toast.makeText(MainActivity.this, "内容为空,请重新输入", Toast.LENGTH_SHORT).show();
				}else {
					try {
						//得到二维码图片并且展示
						Bitmap bitmap = EncodingHandler.createQRCode(string, 400);
						img_two.setImageBitmap(bitmap);
					} catch (WriterException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
	}
	//返回结果的方法
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			String result = data.getExtras().getString("result");
			tView.setText(result);
		}
	
	
	}
}
项目demo可运行下载地址:http://download.csdn.net/detail/qq_21376985/9511435


你可能感兴趣的:(android开发之集成zxing,二维码,以及扫描二维码的功能实现。带源代码下载)