生成文字的二维码
2016-04-19 19:36
435 查看
//此代码需要关于二维码的jar包
<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" tools:context="com.example.zxing.MainActivity" > <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="23dp" android:ems="10" > <requestFocus /> </EditText> <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:text="生成一个二维码" /> <ImageView android:id="@+id/imageView1" android:layout_width="100dp" android:layout_height="100dp" android:layout_below="@+id/editText1" android:layout_centerHorizontal="true" android:layout_marginTop="89dp" android:src="@drawable/ic_launcher"/> </RelativeLayout>
package com.example.zxing; import com.google.zxing.WriterException; import android.app.Activity; import android.graphics.Bitmap; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; public class MainActivity extends Activity{ //定义控件 private EditText mEditText; private Button button; private ImageView image; protected int mScreenWidth ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获得控件的id mEditText=(EditText) findViewById(R.id.editText1); button=(Button) findViewById(R.id.button1); image=(ImageView) findViewById(R.id.imageView1); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); mScreenWidth = dm.widthPixels; button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String string = mEditText.getText().toString(); Bitmap bitmap; try { bitmap = BitmapUtil.createQRCode(string, mScreenWidth); if(bitmap != null){ image.setImageBitmap(bitmap); } } catch (WriterException e) { e.printStackTrace(); } } }); } }
package com.example.zxing; import java.util.Hashtable; import android.graphics.Bitmap; import android.util.Log; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; public class BitmapUtil{ /** * 生成一个二维码图像 * * @param url * 传入的字符串,通常是一个URL * @param QR_WIDTH * 宽度(像素值px) * @param QR_HEIGHT * 高度(像素值px) * @return */ public static final Bitmap create2DCoderBitmap(String url, int QR_WIDTH, int QR_HEIGHT) { try { // 判断URL合法性 if (url == null || "".equals(url) || url.length() < 1) { return null; } Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 图像数据转换,使用了矩阵转换 BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints); int[] pixels = new int[QR_WIDTH * QR_HEIGHT]; // 下面这里按照二维码的算法,逐个生成二维码的图片, // 两个for循环是图片横列扫描的结果 for (int y = 0; y < QR_HEIGHT; y++){ for (int x = 0; x < QR_WIDTH; x++){ if (bitMatrix.get(x, y)) { pixels[y * QR_WIDTH + x] = 0xff000000; } else { pixels[y * QR_WIDTH + x] = 0xffffffff; } } } // 生成二维码图片的格式,使用ARGB_8888 Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT); // 显示到一个ImageView上面 // sweepIV.setImageBitmap(bitmap); return bitmap; } catch (WriterException e) { Log.i("log", "生成二维码错误" + e.getMessage()); return null; } } private static final int BLACK = 0xff000000; /** * 生成一个二维码图像 * * @param url * 传入的字符串,通常是一个URL * @param widthAndHeight * 图像的宽高 * @return */ public static Bitmap createQRCode(String str, int widthAndHeight) throws WriterException { Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight); 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] = BLACK; } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } }
相关文章推荐
- 跟着9张思维导图学习Javascript
- 习题 2-1 位数 2-2 水仙花数 2-3 韩信点兵 2-4倒三角形 2-6 调和级数 2-7近似计算 2-8 子序列的和
- [转]keil使用详解
- FJNUOJ1159
- java常用类
- 一些优秀的开源库汇总(间间断断更新O(∩_∩)O~)
- leetcode——largestRectangleArea
- 校园网免流量看视频,上谷歌,脸书,推特
- [poj 3140] Contestants Division 树形DP
- 关于在牛客网上做题的解析之数据结构(每日20道题,记录错误和不懂的题)
- 1、SDK、目录结构、adb命令、事件写法
- WinDriver打开设备失败“source overlap”
- 转载:在Ubuntu上安装boa,非常详细
- Android开发艺术探索(研读笔记)——02-Activity的启动模式
- gif图片介绍和LZW算法说明
- 第8周项目1:数组作数据成员(1)
- 基于JAX-RS 2.0 REST服务几种类型
- hdu——1541Stars(树状数组)
- 项目实战:Mahout构建图书推荐系统
- 【图像处理】基于OpenCV底层实现的直方图匹配