准备目前Google的zxing jar包不支持中文码的生成,所以本示例中也不支持中文。需要中文支持的朋友,请自行修改zxing.jar包再编译下。 废话不说,直接上效果图:
具体示例代码如下:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 | package com.test.createcode; import android.app.Activity; 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.Toast; import com.google.zxing.BarcodeFormat; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; /** * 用于创建和扫描二维码 */ public class CreateCodeActivity extends Activity { /** * 将要生成二维码的内容 */ private EditText codeEdit; /** * 生成二维码代码 */ private Button twoCodeBtn; /** * 用于展示生成二维码的imageView */ private ImageView codeImg; /** * 生成一维码按钮 */ private Button oneCodeBtn; /** * 界面的初始化事件 * * @param savedInstanceState Bundle对象 */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); initView(); setListener(); } /** * 用于初始化界面展示的view */ private void initView() { 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); } /** * 设置生成二维码和扫描二维码的事件 */ private void setListener() { twoCodeBtn.setOnClickListener( new OnClickListener() { @Override public void onClick(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( new OnClickListener() { @Override public void onClick(View view) { String str = codeEdit.getText().toString().trim(); int size = str.length(); for ( int i = 0 ; i < size; i++) { int c = 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异常 */ public Bitmap CreateTwoDCode(String content) throws WriterException { // 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败 BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300 , 300 ); int width = matrix.getWidth(); int height = matrix.getHeight(); // 二维矩阵转为一维像素数组,也就是一直横着排了 int [] pixels = new int [width * height]; for ( int y = 0 ; y < height; y++) { for ( int x = 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, 0 , 0 , width, height); return bitmap; } /** * 用于将给定的内容生成成一维码 注:目前生成内容为中文的话将直接报错,要修改底层jar包的内容 * * @param content 将要生成一维码的内容 * @return 返回生成好的一维码bitmap * @throws WriterException WriterException异常 */ public Bitmap CreateOneDCode(String content) throws WriterException { // 生成一维条码,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败 BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.CODE_128, 500 , 200 ); int width = matrix.getWidth(); int height = matrix.getHeight(); int [] pixels = new int [width * height]; for ( int y = 0 ; y < height; y++) { for ( int x = 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, 0 , 0 , width, height); return bitmap; } } |
1234567891011121314151617181920 | <? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:background = "#ff999999" > < EditText android:layout_width = "fill_parent" android:id = "@+id/code_edittext" android:layout_height = "wrap_content" android:text = "" /> < LinearLayout android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:orientation = "horizontal" android:layout_marginTop = "20dip" android:layout_gravity = "center_horizontal" > < Button android:id = "@+id/code_btn" android:layout_width = "100dip" android:layout_height = "40dip" android:text = "生成二维码" /> < Button android:id = "@+id/btn_code" android:layout_width = "100dip" android:layout_height = "40dip" android:text = "生成一维码" ></ Button > </ LinearLayout > < ImageView android:layout_width = "wrap_content" android:id = "@+id/code_img" android:layout_height = "wrap_content" android:layout_marginTop = "20dip" android:layout_gravity = "center_horizontal" /> </ LinearLayout > |
1234567891011121314151617181920212223 | <? xml version = "1.0" encoding = "utf-8" ?> < manifest xmlns:android = "http://schemas.android.com/apk/res/android" package = "com.test.createcode" android:versionCode = "1" android:versionName = "1.0" > < uses-sdk android:minSdkVersion = "10" /> < application android:icon = "@drawable/ic_launcher" android:label = "@string/app_name" > < activity android:name = ".CreateCodeActivity" android:label = "@string/app_name" > < intent-filter > < action android:name = "android.intent.action.MAIN" /> < category android: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