生成二维码

来源:互联网 发布:医美缩小毛孔知乎 编辑:程序博客网 时间:2024/06/05 22:46

首先,我们来导一个jar包

compile files('libs/zxing.jar')



Activity

package com.zuiyou.zixingdemo;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.ImageView;import com.google.zxing.BarcodeFormat;import com.google.zxing.EncodeHintType;import com.google.zxing.WriterException;import com.google.zxing.common.BitMatrix;import com.google.zxing.qrcode.QRCodeWriter;import java.util.HashMap;import java.util.Map;public class MainActivity extends AppCompatActivity {    Bitmap bitmap = null;    private ImageView img;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //初始化控件        img = (ImageView) findViewById(R.id.img);    }    /**     * 点击按钮,生成自定义二维码     * @param view     */    public void btn_custom_qr(View view) {        bitmap = generateBitmap("http://blog.csdn.net/lmj623565791/article/details/38067475/", 400, 400);        Bitmap logo = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);        Bitmap bitmap1 = addLogo(bitmap, logo);        img.setImageBitmap(bitmap1);    }    /**     * 点击按钮,生成二维码     * @param view     */    public void btn_qr(View view) {        bitmap = generateBitmap("这是一个自定义二维码", 500, 500);        //  Bitmap bitmap2 = addBackground(bitmap, BitmapFactory.decodeResource(getResources(), R.mipmap.b));        img.setImageBitmap(bitmap);    }    //生成二维码    private Bitmap generateBitmap(String content, int width, int height) {        QRCodeWriter qrCodeWriter = new QRCodeWriter();        Map<EncodeHintType, String> hints = new HashMap<>();        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");        try {            BitMatrix encode = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height);            int[] pixels = new int[width * height];            for (int i = 0; i < height; i++) {                for (int j = 0; j < width; j++) {                    if (encode.get(j, i)) {                        pixels[i * width + j] = 0x00000000;                    } else {                        pixels[i * width + j] = 0xffffffff;                    }                }            }            return Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565);        } catch (WriterException e) {            e.printStackTrace();        }        return null;    }    /**     * 给二维码图片加背景     */    public static Bitmap addBackground(Bitmap foreground, Bitmap background) {        int bgWidth = background.getWidth();        int bgHeight = background.getHeight();        int fgWidth = foreground.getWidth();        int fgHeight = foreground.getHeight();        Bitmap newmap = Bitmap                .createBitmap(bgWidth, bgHeight, Bitmap.Config.ARGB_8888);        Canvas canvas = new Canvas(newmap);        canvas.drawBitmap(background, 0, 0, null);        canvas.drawBitmap(foreground, (bgWidth - fgWidth) / 2,                (bgHeight - fgHeight) * 3 / 5 + 70, null);        canvas.save(Canvas.ALL_SAVE_FLAG);        canvas.restore();        return newmap;    }    /**     * 添加图标     *     * @param qrBitmap     * @param logoBitmap     * @return     */    private Bitmap addLogo(Bitmap qrBitmap, Bitmap logoBitmap) {        int qrBitmapWidth = qrBitmap.getWidth();        int qrBitmapHeight = qrBitmap.getHeight();        int logoBitmapWidth = logoBitmap.getWidth();        int logoBitmapHeight = logoBitmap.getHeight();        Bitmap blankBitmap = Bitmap.createBitmap(qrBitmapWidth, qrBitmapHeight, Bitmap.Config.ARGB_8888);        Canvas canvas = new Canvas(blankBitmap);        canvas.drawBitmap(qrBitmap, 0, 0, null);        canvas.save(Canvas.ALL_SAVE_FLAG);        float scaleSize = 1.0f;        while ((logoBitmapWidth / scaleSize) > (qrBitmapWidth / 5) || (logoBitmapHeight / scaleSize) > (qrBitmapHeight / 5)) {            scaleSize *= 2;        }        float sx = 1.0f / scaleSize;        canvas.scale(sx, sx, qrBitmapWidth / 2, qrBitmapHeight / 2);        canvas.drawBitmap(logoBitmap, (qrBitmapWidth - logoBitmapWidth) / 2, (qrBitmapHeight - logoBitmapHeight) / 2, null);        canvas.restore();        return blankBitmap;    }



这里是布局main_activity

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.zuiyou.zixingdemo.MainActivity">    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="btn_qr"        android:text="生成二维码" />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="btn_custom_qr"        android:text="生成自定义二维码" />    <ImageView        android:id="@+id/img"        android:layout_width="200dp"        android:layout_height="200dp" /></LinearLayout>