Android 二维码编程

这两天一直忙这帮老师整理国家资源库的视频了 ,又是录制又是剪辑,又是写字幕,快忙死了,本来不会那些视频制作一类的软件,现在我都可以拍电影了。

没有时间做自己的项目了,昨天晚上加班把二维码搞出来了。本来以为特别麻烦,没想到。。。。简直不用智商啊。

那两天搞二维码没有头绪,就网上找资料,查资料, 对亏看到传智播客上的视频了,就照骆驼画马了。竟然可以。

又废话了......



让你开发二维码   听起来就瘆的慌。但我们又官方提供的开源代码,可以参考。Zxing

https://github.com/zxing/zxing  里面有代码   但工程太大。但我们有高手整理出来的精简版的,我们直接用就可以。

我本来想自己整理一份,我就一份份改,我去,类与类之间关系太紧了,花了两个小时的干出一大堆,最后还是有错,算了还是用前辈的吧,不要重复发明轮子嘛,没意义。




一 .导入文件

首先导入一些Android文件工程(我会提供源代码的)

导入时需要注意一个问题 直接上图



二 . 使它成为外部项目可以引用的库

导进去后我们找到项目   属性 -->Android-->左下角有一个Library 我们需要打上勾。


这样的话外部的项目就可以引用它了。   因为单独一个二维码扫描 解码 完全没有意义。

三. 新建自己的项目



新建完后我们 需要进行引用导入的这个项目。
同样进入项目属性  Android  右下角有添加外部项目



四 .做完这些后就添加权限  和  注册页面


打开我们导入项目的 AndroidManifest.xml 把他所需要的条件 添加到  新建的项目中的AndroidManifest.xml


添加那些呢???




好了。我们先写自己的界面



<span style="font-size:14px;"><span style="color:#000099;"><RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    </span><span style="color:#ffcc33;">android:background="@drawable/a002"</span><span style="color:#000099;"> >
    
    <Button
        android:id="@+id/buttonscan"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textColor="#FF0000"
        android:text="Scan" />

    <TextView
        android:id="@+id/textViewshow"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_below="@+id/buttonscan"
        android:gravity="center_horizontal|center_vertical"
        android:text="Show" />

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/buttonprint"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="46dp" />

    <Button
        android:id="@+id/buttonprint"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/editText1"
        android:textColor="#FF00FF"
        android:text="Ok,Print" />

    <Button
        android:id="@+id/buttonclear"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textViewshow"
        android:layout_below="@+id/textViewshow"
        android:textColor="#00FFFF"
        android:text="清除Show" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/buttonclear"
        android:layout_below="@+id/buttonclear"
        android:ems="10"
        android:gravity="center_horizontal"
        android:hint="请输入您要生成的信息" />

</RelativeLayout></span></span>

然后写主程序

<span style="font-size:14px;"><span style="color:#990000;">package com.example.qrdemo;

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

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

public class MainActivity extends Activity
{

	private Button Scan;
	private TextView textView;
	private EditText Input;
	private Button Print;
	private ImageView  Image;
	private Button Clear;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		Scan = (Button)findViewById(R.id.buttonscan);
		textView = (TextView)findViewById(R.id.textViewshow);
		Input = (EditText)findViewById(R.id.editText1);
		Print = (Button)findViewById(R.id.buttonprint);
		Image =(ImageView)findViewById(R.id.imageview);
		Clear =(Button)findViewById(R.id.buttonclear);
		
		Scan.setOnClickListener(new View.OnClickListener()
		{
			
			@Override
			public void onClick(View arg0)
			{
				// TODO Auto-generated method stub
				Intent newactvity =new Intent(MainActivity.this,CaptureActivity.class);
				//startActivity(newactvity);
				startActivityForResult(newactvity,0);
				//startActivityForResult (Intent intent, int requestCode)
				//相同调用活动返回结果不带任何选项
			}
		});
		
		Clear.setOnClickListener(new View.OnClickListener()
		{
			
			@Override
			public void onClick(View arg0)
			{
				// TODO Auto-generated method stub
				textView.setText("");
			}
		});
		
		
		
		Print.setOnClickListener(new View.OnClickListener()
		{
			
			@Override
			public void onClick(View arg0)
			{
				// TODO Auto-generated method stub
				String inputmessage =  Input.getText().toString();
				if(inputmessage.equals(null))
				{
					Toast.makeText(MainActivity.this, "请输入您要合成的数据",Toast.LENGTH_SHORT).show();
				}else
				{
					try
					{
						Bitmap qrbitmap = EncodingHandler.createQRCode(inputmessage,800);
						Image.setImageBitmap(qrbitmap);
					} catch (WriterException e)
					{
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
		
	}

	protected void onActivityResult(int requestCode,int resultCode,Intent data)//结果处理函数
	/*
	 * 活动启动时调用退出,让您请求码你开始用,结果代码返回,并且从它的任何其他数据。
	 * 如果活动,将RESULT_CANCELED的结果代码显式地返回,
	 * 没有返回任何结果,或在其操作期间坠毁。
	 * */
	{
		super.onActivityResult(requestCode,resultCode,data);
		if(resultCode == RESULT_OK)//标准活动结果:操作成功
		{
			String result = data.getExtras().getString("result");//getExtras()从意图检索扩展数据的map。
			textView.setText(result);
		}
	}

}</span><span style="background-color: rgb(0, 153, 0);">
</span></span>

给你们几张  API的截图          反正上边几个函数方法我不太不明白   然后就查了查API 清楚了些




不懂英文,像我一样用电子词典


好吧 完了 就这些而已  因为我们调用的开源的,所以不用太麻烦。

看一下结果吧啊。。



OK完了,


源代码    源代码下载                                                http://download.csdn.net/detail/csdnhejingzhou/9241763


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值