第一步:
确定我们二维码的位置,即是在我们应用的哪个页面的哪个位置,简单讲就是用一个ImageView把二维码展示出来,我们就把二维码展示在下面布局activity_main.xml中的ImageView上:
第二步:定义一个生成二维码的工具类:QRUtils.java (注意:定义之前不要忘记在项目中要导入ZXing相关的jar包)
既core.jar这个包
package com.utils;import java.util.EnumMap;import java.util.Map;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Matrix;import android.graphics.Paint;import android.text.TextUtils;import com.google.zxing.BarcodeFormat;import com.google.zxing.EncodeHintType;import com.google.zxing.MultiFormatWriter;import com.google.zxing.common.BitMatrix;public class QRUtils { private static final int WHITE = 0xFFFFFFFF; private static final int BLACK = 0xFF000000; /** * 将字符串按照指定大小生成二维码图片 */ public static Bitmap encodeToQR(String contentsToEncode, int dimension) throws Exception { if (TextUtils.isEmpty(contentsToEncode)) return null; BarcodeFormat format = BarcodeFormat.QR_CODE; Map hints = new EnumMap(EncodeHintType.class); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix result = new MultiFormatWriter().encode(contentsToEncode, format, dimension, dimension, hints); int width = result.getWidth(); int height = result.getHeight(); int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { int offset = y * width; for (int x = 0; x < width; x++) { pixels[offset + x] = result.get(x, y) ? BLACK : WHITE; } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } public static Bitmap encodeToQRWidth(String contentsToEncode, int dimension) throws Exception { if (TextUtils.isEmpty(contentsToEncode)) return null; BarcodeFormat format = BarcodeFormat.QR_CODE; @SuppressWarnings("unchecked") Map hints = new EnumMap(EncodeHintType.class); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix result = new MultiFormatWriter().encode(contentsToEncode, format, dimension, dimension, hints); int width = result.getWidth(); int height = result.getHeight(); boolean isFirstBlack = true; int startX = 0; int startY = 0; int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { int offset = y * width; for (int x = 0; x < width; x++) { pixels[offset + x] = result.get(x, y) ? BLACK : WHITE; if (result.get(x, y) && isFirstBlack) { isFirstBlack = false; startX = x; startY = y; } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); Matrix m = new Matrix(); float sx = (width + 2f * startX) / width; float sy = (height + 2f * startY) / height; m.postScale(sx, sy); Bitmap qrBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(qrBitmap); canvas.translate(-startX, -startY); Paint paint = new Paint(); paint.setAntiAlias(true); canvas.drawBitmap(bitmap, m, paint); canvas.save(); return qrBitmap; } }
第三步:在我们既我们的主Activity:MainActivity.java
package com.action;import android.app.Activity;import android.graphics.Bitmap;import android.os.Bundle;import android.util.DisplayMetrics;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.Toast;import com.utils.*;public class MainActivity extends Activity { private ImageView imag; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); } protected void initViews() { imag = (ImageView) findViewById(R.id.imag); DisplayMetrics outMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(outMetrics); int w = outMetrics.widthPixels * 8 / 11;// 设置宽度 ViewGroup.LayoutParams layoutParams = imag.getLayoutParams(); layoutParams.height = layoutParams.width = w;// 设置高度 imag.setLayoutParams(layoutParams); try { Bitmap bitmap = QRUtils.encodeToQRWidth( "http://write.blog.csdn.net/", w);// 要生成二维码的内容,我这就是一个网址 imag.setImageBitmap(bitmap); } catch (Exception e) { e.printStackTrace(); Toast.makeText(this, "生成二维码失败", Toast.LENGTH_SHORT); } }}
三步完成我们的二维码生成,是不是很简单!!