首先是MainActivity.java部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package  com.qrcode;
 
 
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  MainActivity  extends  Activity {
     private  TextView resultTextView;
     private  EditText qrStrEditText;
     private  ImageView qrImgImageView;
     
     @Override
     public  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.main);
         
         resultTextView = (TextView)  this .findViewById(R.id.tv_scan_result);
         qrStrEditText = (EditText)  this .findViewById(R.id.et_qr_string);
         qrImgImageView = (ImageView)  this .findViewById(R.id.iv_qr_image);
         
         Button scanBarCodeButton = (Button)  this .findViewById(R.id.btn_scan_barcode);
         scanBarCodeButton.setOnClickListener( new  OnClickListener() {
             
             @Override
             public  void  onClick(View v) {
                     
                 Intent openCameraIntent =  new  Intent(MainActivity. this ,CaptureActivity. class );
                 startActivityForResult(openCameraIntent,  0 );
             }
         });
         
         Button generateQRCodeButton = (Button)  this .findViewById(R.id.btn_add_qrcode);
         generateQRCodeButton.setOnClickListener( new  OnClickListener() {
             
             @Override
             public  void  onClick(View v) {
                 try  {
                     String contentString = qrStrEditText.getText().toString();
                     if  (!contentString.equals( "" )) {
                         
                         Bitmap qrCodeBitmap = EncodingHandler.createQRCode(contentString,  350 );
                         qrImgImageView.setImageBitmap(qrCodeBitmap);
                     } else  {
                         Toast.makeText(MainActivity. this "Text can not be empty" , Toast.LENGTH_SHORT).show();
                     }
                     
                 catch  (WriterException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
             }
         });
     }
 
     @Override
     protected  void  onActivityResult( int  requestCode,  int  resultCode, Intent data) {
         super .onActivityResult(requestCode, resultCode, data);
     
         if  (resultCode == RESULT_OK) {
             Bundle bundle = data.getExtras();
             String scanResult = bundle.getString( "result" );
             resultTextView.setText(scanResult);
         }
     }
}

    下面就是构成拍摄页面的部分由于很多都是继承的代码,这里我就不粘贴了,给大家一个图看一下,下面放出整理后的代码。

    最后别忘了给权限部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<? xml  version = "1.0"  encoding = "utf-8" ?>
< manifest  xmlns:android = "http://schemas.android.com/apk/res/android"
       package = "com.qrcode"
       android:versionCode = "1"
       android:versionName = "1.0" >
     < uses-sdk  android:minSdkVersion = "7"  />
     
     < uses-permission  android:name = "android.permission.VIBRATE"  />   <!-- 震动权限 -->
     < uses-permission  android:name = "android.permission.CAMERA"  />
     < uses-feature  android:name = "android.hardware.camera"  />         <!-- 使用照相机权限 -->
     < uses-feature  android:name = "android.hardware.camera.autofocus"  />    <!-- 自动聚焦权限 -->
 
     < application  android:icon = "@drawable/icon"  android:label = "@string/app_name" >
         < activity  android:name = ".MainActivity"
                   android:label = "@string/app_name" >
             < intent-filter >
                 < action  android:name = "android.intent.action.MAIN"  />
                 < category  android:name = "android.intent.category.LAUNCHER"  />
             </ intent-filter >
         </ activity >
         
         <!-- 隐藏键盘 --> <!-- 全屏 -->
         < activity
             android:configChanges = "orientation|keyboardHidden"    
             android:name = "com.zxing.activity.CaptureActivity"
             android:screenOrientation = "portrait"
             android:theme = "@android:style/Theme.NoTitleBar.Fullscreen"    
             android:windowSoftInputMode = "stateAlwaysHidden"  >
         </ activity >
 
     </ application >
</ manifest >

    效果图:

wKioL1OxFb_zt_-4AADkx6NL6no606.jpg

wKiom1OxFfDw3dm4AAKwiP5PSkk617.jpg

wKioL1OxFcHiHesEAAHGfpqHA5k023.jpg