android生成带logo的二维码

废话不多说,放码。


private int QR_WIDTH = 230;
private int QR_HEIGHT = 230;
// 二维码logo图片宽度的一半
private int IMAGE_HALFWIDTH = 20;
/**
 * 生成二维码 中间插入小图片
 * 
 * @param str 内容
 * @return Bitmap
 * @throws WriterException
 */
public Bitmap cretaeBitmap(String str, Bitmap icon) throws WriterException {
    // 缩放一个40*40的图片
    int[] mPixels = new int[2 * IMAGE_HALFWIDTH * 2 * IMAGE_HALFWIDTH];
    icon = Untilly.zoomBitmap(icon, IMAGE_HALFWIDTH);
    Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    // 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
    BitMatrix matrix = new MultiFormatWriter().encode(str,
            BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
    int width = matrix.getWidth();
    int height = matrix.getHeight();
    // 二维矩阵转为一维像素数组,也就是一直横着排了
    int halfW = width / 2;
    int halfH = height / 2;
    int[] pixels = new int[width * height];
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            if (x > halfW - IMAGE_HALFWIDTH && x < halfW + IMAGE_HALFWIDTH
                    && y > halfH - IMAGE_HALFWIDTH
                    && y < halfH + IMAGE_HALFWIDTH) {
                pixels[y * width + x] = icon.getPixel(x - halfW
                        + IMAGE_HALFWIDTH, y - halfH + IMAGE_HALFWIDTH);
            } else {
                if (matrix.get(x, y)) {
                    pixels[y * width + x] = 0xff000000;
                } else { // 无信息设置像素点为白色,或者什么都不设置也行
                    pixels[y * width + x] = 0xffffffff;
                }
            }

        }
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height,
            Bitmap.Config.ARGB_8888);
    // 通过像素数组生成bitmap
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);

    return bitmap;
}
以下是Android生成logo的二维码的步骤: 1. 引入依赖坐标 在项目的build.gradle文件中,添加以下依赖坐标: ```groovy implementation 'com.google.zxing:core:3.4.0' implementation 'com.journeyapps:zxing-android-embedded:3.6.0' ``` 2. 简单二维码实现 使用ZXing库生成简单的二维码,可以参考以下代码: ```java import android.graphics.Bitmap; import android.graphics.Color; import android.os.Bundle; import android.widget.ImageView; import androidx.appcompat.app.AppCompatActivity; import com.google.zxing.BarcodeFormat; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; public class MainActivity extends AppCompatActivity { private ImageView qrCodeImageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); qrCodeImageView = findViewById(R.id.qr_code_image_view); String content = "https://www.example.com"; // 二维码内容 try { // 设置二维码的宽高 int width = 400; int height = 400; // 创建QRCodeWriter对象 QRCodeWriter writer = new QRCodeWriter(); // 使用QRCodeWriter对象生成BitMatrix对象 BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height); // 创建一个空的Bitmap对象 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); // 遍历BitMatrix对象,设置Bitmap对象的像素值 for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { bitmap.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE); } } // 设置ImageView显示二维码图片 qrCodeImageView.setImageBitmap(bitmap); } catch (WriterException e) { e.printStackTrace(); } } } ``` 3. logo的二维码实现 为了生成logo的二维码,我们可以在生成二维码的基础上,将logo图片合并到二维码中。可以参考以下代码: ```java import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Bundle; import android.widget.ImageView; import androidx.appcompat.app.AppCompatActivity; import com.google.zxing.BarcodeFormat; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; public class MainActivity extends AppCompatActivity { private ImageView qrCodeImageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); qrCodeImageView = findViewById(R.id.qr_code_image_view); String content = "https://www.example.com"; // 二维码内容 int logoResId = R.drawable.logo; // logo图片资源ID try { // 设置二维码的宽高 int width = 400; int height = 400; // 创建QRCodeWriter对象 QRCodeWriter writer = new QRCodeWriter(); // 使用QRCodeWriter对象生成BitMatrix对象 BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height); // 创建一个空的Bitmap对象 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); // 遍历BitMatrix对象,设置Bitmap对象的像素值 for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { bitmap.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE); } } // 加载logo图片 Bitmap logoBitmap = BitmapFactory.decodeResource(getResources(), logoResId); // 创建一个新的Bitmap对象,用于合并二维码logo图片 Bitmap resultBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); // 创建一个Canvas对象,用于绘制二维码logo图片 Canvas canvas = new Canvas(resultBitmap); // 绘制二维码 canvas.drawBitmap(bitmap, 0, 0, null); // 计算logo图片的宽高 int logoWidth = width / 4; int logoHeight = height / 4; // 设置logo图片的绘制区域 int logoLeft = (width - logoWidth) / 2; int logoTop = (height - logoHeight) / 2; int logoRight = logoLeft + logoWidth; int logoBottom = logoTop + logoHeight; // 创建一个Paint对象,用于设置logo图片的抗锯齿和透明度 Paint paint = new Paint(); paint.setAntiAlias(true); paint.setAlpha(255); // 绘制logo图片 canvas.drawBitmap(logoBitmap, null, new Rect(logoLeft, logoTop, logoRight, logoBottom), paint); // 设置ImageView显示logo的二维码图片 qrCodeImageView.setImageBitmap(resultBitmap); } catch (WriterException e) { e.printStackTrace(); } } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值