赞
踩
这两天一直忙这帮老师整理国家资源库的视频了 ,又是录制又是剪辑,又是写字幕,快忙死了,本来不会那些视频制作一类的软件,现在我都可以拍电影了。
没有时间做自己的项目了,昨天晚上加班把二维码搞出来了。本来以为特别麻烦,没想到。。。。简直不用智商啊。
那两天搞二维码没有头绪,就网上找资料,查资料, 对亏看到传智播客上的视频了,就照骆驼画马了。竟然可以。
又废话了......
让你开发二维码 听起来就瘆的慌。但我们又官方提供的开源代码,可以参考。Zxing
https://github.com/zxing/zxing 里面有代码 但工程太大。但我们有高手整理出来的精简版的,我们直接用就可以。
我本来想自己整理一份,我就一份份改,我去,类与类之间关系太紧了,花了两个小时的干出一大堆,最后还是有错,算了还是用前辈的吧,不要重复发明轮子嘛,没意义。
首先导入一些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>

不懂英文,像我一样用电子词典
好吧 完了 就这些而已 因为我们调用的开源的,所以不用太麻烦。
看一下结果吧啊。。
OK完了,
源代码 源代码下载 http://download.csdn.net/detail/csdnhejingzhou/9241763
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。