2019独角兽企业重金招聘Python工程师标准>>>
第一步:
确定我们二维码的位置,即是在我们应用的哪个页面的哪个位置,简单讲就是用一个ImageView把二维码展示出来,我们就把二维码展示在下面布局activity_main.xml中的ImageView上:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><LinearLayout android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="horizontal"android:gravity="center_horizontal"><ImageViewandroid:id="@+id/imag"android:layout_width="100dp"android:layout_height="100dp"android:scaleType="fitXY"android:src="@drawable/ic_launcher"android:layout_gravity="center"></ImageView></LinearLayout></LinearLayout>
第二步:定义一个生成二维码的工具类:QRUtils.java (注意:定义之前不要忘记在项目中要导入ZXing相关的jar包)
既core.jar这个包 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;@Overrideprotected 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);}}}
三步完成我们的二维码生成,是不是很简单!!