生成二维码(带Logo和不带logo的二维码)

1、用的二维码库是:zxing_core_3.3.1.jar;

import android.graphics.Bitmap;
import android.graphics.Canvas;
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;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import java.util.HashMap;
import java.util.Map;

/**
 * created by cy on 2020/7/8 0008.
 */
public class QRCodeUtils {
    private static final String TAG = "QRCodeUtils";

    /**
     * 创建二维码
     *
     * @param content   content
     * @param widthPix  widthPix
     * @param heightPix heightPix
     * @param logoBm    logoBm
     * @return 二维码
     */
    public static Bitmap createQRCode(String content, int widthPix, int heightPix, Bitmap logoBm) {
        try {
            if (content == null || "".equals(content)) {
                return null;
            }
            // 配置参数
            Map<EncodeHintType, Object> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            // 容错级别
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            // 设置空白边距的宽度
            hints.put(EncodeHintType.MARGIN, 0);
            // 图像数据转换,使用了矩阵转换
            BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, widthPix,
                    heightPix, hints);
            int[] pixels = new int[widthPix * heightPix];
            // 下面这里按照二维码的算法,逐个生成二维码的图片,
            // 两个for循环是图片横列扫描的结果
            for (int y = 0; y < heightPix; y++) {
                for (int x = 0; x < widthPix; x++) {
                    if (bitMatrix.get(x, y)) {
                        pixels[y * widthPix + x] = 0xff000000;
                    } else {
                        pixels[y * widthPix + x] = 0xffffffff;
                    }
                }
            }
            // 生成二维码图片的格式,使用ARGB_8888
            Bitmap bitmap = Bitmap.createBitmap(widthPix, heightPix, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, widthPix, 0, 0, widthPix, heightPix);
            if (logoBm != null) {
                bitmap = addLogo(bitmap, logoBm);
            }
            //必须使用compress方法将bitmap保存到文件中再进行读取。直接返回的bitmap是没有任何压缩的,内存消耗巨大!
            return bitmap;
        } catch (WriterException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 在二维码中间添加Logo图案
     */
    private static Bitmap addLogo(Bitmap src, Bitmap logo) {
        if (src == null) {
            return null;
        }
        if (logo == null) {
            return src;
        }
        //获取图片的宽高
        int srcWidth = src.getWidth();
        int srcHeight = src.getHeight();
        int logoWidth = logo.getWidth();
        int logoHeight = logo.getHeight();
        if (srcWidth == 0 || srcHeight == 0) {
            return null;
        }
        if (logoWidth == 0 || logoHeight == 0) {
            return src;
        }
        //logo大小为二维码整体大小的1/5
        float scaleFactor = srcWidth * 1.0f / 5 / logoWidth;
        Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);
        try {
            Canvas canvas = new Canvas(bitmap);
            canvas.drawBitmap(src, 0, 0, null);
            canvas.scale(scaleFactor, scaleFactor, srcWidth / 2, srcHeight / 2);
            canvas.drawBitmap(logo, (srcWidth - logoWidth) / 2, (srcHeight - logoHeight) / 2, null);
            canvas.save();
            canvas.restore();
        } catch (Exception e) {
            bitmap = null;
            e.getStackTrace();
        }
        return bitmap;
    }

    /**
     * 用字符串生成二维码
     */
    public static Bitmap Create2DCode(String str, int width, int height) {
        // 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
        BitMatrix matrix;
        Bitmap bitmap = null;
        try {
            Map<EncodeHintType, Object> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            // 容错级别
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            // 设置空白边距的宽度
            hints.put(EncodeHintType.MARGIN, 0);

            matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, width, height, hints);
            // int realWidth = matrix.getWidth();
            // int realHeight = matrix.getHeight();
            Log.i(TAG, " matrix.getWidth() == " + matrix.getWidth());
            Log.i(TAG, " matrix.getHeight() == " + 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;
                    }
                }
            }
            Log.i(TAG, "width == " + width);
            Log.i(TAG, "height == " + height);
            bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            // 通过像素数组生成bitmap,具体参考api
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height);

        } catch (WriterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return bitmap;
    }
}

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在PHP中,我们可以使用第三方库来生成Logo图标的二维码。以下是一种实现方法: 首先,我们需要确保安装了PHP的GD库和二维码生成库,比如Zxing。 然后,我们可以通过以下步骤生成Logo图标的二维码: 1. 创建一个空白的二维码图片,并设置其宽度和高度。 2. 使用Zxing库的API将信息编码为二维码,并将其写入空白图片中。 3. 将Logo图标加载到内存中,并获取其宽度和高度。 4. 将Logo图标缩放至适合二维码的大小,并将其粘贴到二维码图片的中心位置。 5. 将生成的Logo图标的二维码保存到指定的文件路径。 以下是一个简单的示例代码: ```php <?php require_once 'path/to/zxing/library'; // 创建空白二维码图片 $qrCode = imagecreate($width, $height); // 将信息编码为二维码,并写入图片 $qrData = 'Hello, World!'; $qrCode = zxing_encode($qrData, $qrCode); // 加载Logo图标 $logo = imagecreatefrompng('path/to/logo.png'); // 获取Logo图标的宽度和高度 $logoWidth = imagesx($logo); $logoHeight = imagesy($logo); // 缩放Logo图标至适合二维码的大小 $logoWidth = $logoWidth * $scale; $logoHeight = $logoHeight * $scale; $logoResized = imagecreatetruecolor($logoWidth, $logoHeight); imagealphablending($logoResized, false); imagesavealpha($logoResized, true); imagecopyresampled($logoResized, $logo, 0, 0, 0, 0, $logoWidth, $logoHeight, imagesx($logo), imagesy($logo)); // 将Logo图标粘贴到二维码图片的中心位置 imagecopy($qrCode, $logoResized, $x, $y, 0, 0, $logoWidth, $logoHeight); // 保存生成的Logo图标的二维码图片 imagepng($qrCode, 'path/to/output.png'); // 释放内存 imagedestroy($qrCode); imagedestroy($logo); imagedestroy($logoResized); ?> ``` 注意,这只是一个简单的示例,实际应用中可能需要根据具体的需求进行修改和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值