Android中生成条形码和二维码

来源:互联网 发布:免费数据统计分析工具 编辑:程序博客网 时间:2024/05/18 00:17

准备目前Google的zxing jar包不支持中文码的生成,所以本示例中也不支持中文。需要中文支持的朋友,请自行修改zxing.jar包再编译下。 废话不说,直接上效果图:

Android二维码条形码生成

Android二维码条形码生成

 

具体示例代码如下:

?
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
packagecom.test.createcode;
 
importandroid.app.Activity;
importandroid.graphics.Bitmap;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.EditText;
importandroid.widget.ImageView;
importandroid.widget.Toast;
 
importcom.google.zxing.BarcodeFormat;
importcom.google.zxing.MultiFormatWriter;
importcom.google.zxing.WriterException;
importcom.google.zxing.common.BitMatrix;
 
/**
 * 用于创建和扫描二维码
 */
publicclassCreateCodeActivity extendsActivity {
 
    
    /**
     * 将要生成二维码的内容
     */
    privateEditText codeEdit;
 
    /**
     * 生成二维码代码
     */
    privateButton twoCodeBtn;
    /**
     * 用于展示生成二维码的imageView
     */
    privateImageView codeImg;
    
    /**
     * 生成一维码按钮
     */
    privateButton oneCodeBtn;
 
    /**
     * 界面的初始化事件
     *
     * @param savedInstanceState Bundle对象
     */
    @Override
    publicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initView();
        setListener();
 
    }
 
    /**
     * 用于初始化界面展示的view
     */
    privatevoidinitView() {
        codeEdit = (EditText) findViewById(R.id.code_edittext);
        twoCodeBtn = (Button) findViewById(R.id.code_btn);
        oneCodeBtn = (Button) findViewById(R.id.btn_code);
        codeImg = (ImageView) findViewById(R.id.code_img);
    }
 
    /**
     * 设置生成二维码和扫描二维码的事件
     */
    privatevoidsetListener() {
        twoCodeBtn.setOnClickListener(newOnClickListener() {
 
            @Override
            publicvoidonClick(View v) {
                String str = codeEdit.getText().toString().trim();
                Bitmap bmp = null;
                try{
                    if(str != null&& !"".equals(str)) {
                        bmp = CreateTwoDCode(str);
                    }
                catch(WriterException e) {
                    e.printStackTrace();
                }
                if(bmp != null) {
                    codeImg.setImageBitmap(bmp);
                }
 
            }
        });
 
 
        oneCodeBtn.setOnClickListener(newOnClickListener() {
            @Override
            publicvoidonClick(View view) {
                String str = codeEdit.getText().toString().trim();
                intsize = str.length();
                for(inti = 0; i < size; i++) {
                    intc = str.charAt(i);
                    if((19968<= c && c < 40623)) {
                        Toast.makeText(CreateCodeActivity.this"生成条形码的时刻不能是中文", Toast.LENGTH_SHORT).show();
                        return;
                    }
 
                }
                Bitmap bmp = null;
                try{
                    if(str != null&& !"".equals(str)) {
                        bmp = CreateOneDCode(str);
                    }
                catch(WriterException e) {
                    e.printStackTrace();
                }
                if(bmp != null) {
                    codeImg.setImageBitmap(bmp);
                }
            }
        });
    }
 
    /**
     * 将指定的内容生成成二维码
     *
     * @param content 将要生成二维码的内容
     * @return 返回生成好的二维码事件
     * @throws WriterException WriterException异常
     */
    publicBitmap CreateTwoDCode(String content) throwsWriterException {
        // 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
        BitMatrix matrix = newMultiFormatWriter().encode(content,
                BarcodeFormat.QR_CODE, 300300);
        intwidth = matrix.getWidth();
        intheight = matrix.getHeight();
        // 二维矩阵转为一维像素数组,也就是一直横着排了
        int[] pixels = newint[width * height];
        for(inty = 0; y < height; y++) {
            for(intx = 0; x < width; x++) {
                if(matrix.get(x, y)) {
                    pixels[y * width + x] = 0xff000000;
                }
            }
        }
 
        Bitmap bitmap = Bitmap.createBitmap(width, height,
                Bitmap.Config.ARGB_8888);
        // 通过像素数组生成bitmap,具体参考api
        bitmap.setPixels(pixels, 0, width, 00, width, height);
        returnbitmap;
    }
 
    /**
     * 用于将给定的内容生成成一维码 注:目前生成内容为中文的话将直接报错,要修改底层jar包的内容
     *
     * @param content 将要生成一维码的内容
     * @return 返回生成好的一维码bitmap
     * @throws WriterException WriterException异常
     */
    publicBitmap CreateOneDCode(String content) throwsWriterException {
        // 生成一维条码,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
        BitMatrix matrix = newMultiFormatWriter().encode(content,
                BarcodeFormat.CODE_128, 500200);
        intwidth = matrix.getWidth();
        intheight = matrix.getHeight();
        int[] pixels = newint[width * height];
        for(inty = 0; y < height; y++) {
            for(intx = 0; x < width; x++) {
                if(matrix.get(x, y)) {
                    pixels[y * width + x] = 0xff000000;
                }
            }
        }
 
        Bitmap bitmap = Bitmap.createBitmap(width, height,
                Bitmap.Config.ARGB_8888);
        // 通过像素数组生成bitmap,具体参考api
        bitmap.setPixels(pixels, 0, width, 00, width, height);
        returnbitmap;
    }
 
}
main.xml文件如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xmlversion="1.0"encoding="utf-8"?> 
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" 
              android:orientation="vertical"android:layout_width="fill_parent" 
              android:layout_height="fill_parent"android:background="#ff999999"
    <EditTextandroid:layout_width="fill_parent"android:id="@+id/code_edittext" 
              android:layout_height="wrap_content"android:text=""/> 
    <LinearLayoutandroid:layout_width="wrap_content" 
                  android:layout_height="wrap_content"android:orientation="horizontal" 
                  android:layout_marginTop="20dip"android:layout_gravity="center_horizontal"
        <Buttonandroid:id="@+id/code_btn"android:layout_width="100dip" 
                android:layout_height="40dip"android:text="生成二维码"/> 
        <Buttonandroid:id="@+id/btn_code"android:layout_width="100dip"android:layout_height="40dip" 
                android:text="生成一维码"></Button
    </LinearLayout
       
   
    <ImageViewandroid:layout_width="wrap_content"android:id="@+id/code_img" 
               android:layout_height="wrap_content"android:layout_marginTop="20dip" 
               android:layout_gravity="center_horizontal"/> 
</LinearLayout>
清单文件如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xmlversion="1.0"encoding="utf-8"?> 
<manifestxmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.test.createcode" 
    android:versionCode="1" 
    android:versionName="1.0"
   
    <uses-sdkandroid:minSdkVersion="10"/> 
   
    <application 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name"
        <activity 
            android:name=".CreateCodeActivity" 
            android:label="@string/app_name"
            <intent-filter
                <actionandroid:name="android.intent.action.MAIN"/> 
   
                <categoryandroid:name="android.intent.category.LAUNCHER"/> 
            </intent-filter
        </activity
    </application
   
</manifest>

最后不要下载个zxing.jar放在libs文件夹中,即可。(上述的BitMatrix及BarcodeFormat等类是依赖于zxing.jar的。)


下载地址:

http://code.google.com/p/zxing/

来自:http://blog.csdn.net/chenshufei2/article/details/8682934

不明白的朋友下载我的例子:

http://download.csdn.net/detail/zhaihaohao1/8387031

我的相关文章:

http://blog.csdn.net/zhaihaohao1/article/details/42920591



0 0
原创粉丝点击