关于扫描二维码

1、关于下载二维码处理包的问题:

github上的那个zxing包太大了,很难使用,应当使用精简包,它的名字为:BarCodeTest

2、eclipse引入工程时,可以参考:http://www.cnblogs.com/SkyD/archive/2010/11/25/1887219.html

     确保引入的工程在工作区间有文件夹

3、引入的工程要设置它的属性,让它变为is_library,以便其他项目能够使用

4、做为使用方,注意添加Library

5、具体使用代码:

1)视图

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6      >
 7 
 8     <Button
 9         android:id="@+id/scan"
10         android:layout_width="match_parent"
11         android:layout_height="wrap_content"
12         android:text="开始扫描二维码" />
13     <TextView 
14         android:id="@+id/text"
15         android:layout_width="match_parent"
16         android:layout_height="wrap_content"
17         />
18     <EditText 
19         android:layout_width="match_parent"
20         android:layout_height="wrap_content"
21         android:id="@+id/input"
22         android:hint="请输入要编码的内容"
23         />
24     <Button 
25         android:layout_width="match_parent"
26         android:layout_height="wrap_content"
27         android:id="@+id/gen"
28         android:text="生成二维码"
29         />
30     <ImageView 
31         android:layout_width="wrap_content"
32         android:layout_height="wrap_content"
33         android:id="@+id/img"
34         android:layout_gravity="center_horizontal"
35         />
36 
37 </LinearLayout>

2)MainActivity代码

 1 package com.zyhui.testqrcode;
 2 
 3 import com.google.zxing.WriterException;
 4 import com.zxing.activity.CaptureActivity;
 5 import com.zxing.encoding.EncodingHandler;
 6 
 7 import android.os.Bundle;
 8 import android.text.TextUtils;
 9 import android.view.View;
10 import android.widget.Button;
11 import android.widget.EditText;
12 import android.widget.ImageView;
13 import android.widget.TextView;
14 import android.widget.Toast;
15 import android.app.Activity;
16 import android.content.Intent;
17 import android.graphics.Bitmap;
18 
19 public class MainActivity extends Activity {
20     private Button scanButton;
21     private TextView text;
22     private EditText input;
23     private Button genButton;
24     private ImageView img;
25     @Override
26     protected void onCreate(Bundle savedInstanceState) {
27         super.onCreate(savedInstanceState);
28         setContentView(R.layout.activity_main);
29         
30         text = (TextView) findViewById(R.id.text);
31         input = (EditText) findViewById(R.id.input);
32         scanButton = (Button) findViewById(R.id.scan);
33         scanButton.setOnClickListener(new View.OnClickListener() {
34             @Override
35             public void onClick(View v) {
36                 Intent startScan = new Intent(MainActivity.this, CaptureActivity.class);
37                 //startActivity(startScan);
38                 startActivityForResult(startScan, 0);
39             }
40         });
41         
42         img = (ImageView) findViewById(R.id.img);
43         genButton = (Button) findViewById(R.id.gen);
44         genButton.setOnClickListener(new View.OnClickListener() {
45             @Override
46             public void onClick(View v) {
47                 String str = input.getText().toString().trim();
48                 if(TextUtils.isEmpty(str)){
49                     Toast.makeText(MainActivity.this, "请输入字符串", Toast.LENGTH_SHORT).show();
50                     return;
51                 }
52                 
53                 try {
54                     Bitmap qrcode = EncodingHandler.createQRCode(str, 300);
55                     img.setImageBitmap(qrcode);
56                 } catch (Exception e) {
57                     // TODO Auto-generated catch block
58                     e.printStackTrace();
59                 }
60             }
61         });
62     }
63     
64     @Override
65     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
66         super.onActivityResult(requestCode, resultCode, data);
67         if(resultCode == RESULT_OK){
68             String result = data.getExtras().getString("result");
69             text.setText(result);
70         }
71     }
72 
73 }

3)权限

<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.CAMERA"/>

posted @ 2016-05-17 22:25  zhongyinghe  阅读(190)  评论(0)    收藏  举报