Android生成带图片的二维码

转载:http://blog.csdn.net/jerehedu/article/details/45746369

/**
     * 生成二维码
     * @param string 二维码中包含的文本信息
     * @param mBitmap logo图片
     * @param format  编码格式
     * @return Bitmap 位图
     * @throws WriterException
     */
    public Bitmap createCode(String string,Bitmap mBitmap, BarcodeFormat format)
            throws WriterException {
        Matrix m = new Matrix();
        float sx = (float) 2 * IMAGE_HALFWIDTH / mBitmap.getWidth();
        float sy = (float) 2 * IMAGE_HALFWIDTH
                / mBitmap.getHeight();
        m.setScale(sx, sy);//设置缩放信息
        //将logo图片按martix设置的信息缩放
        mBitmap = Bitmap.createBitmap(mBitmap, 0, 0,
                mBitmap.getWidth(), mBitmap.getHeight(), m, false);
        MultiFormatWriter writer = new MultiFormatWriter();
        Hashtable<EncodeHintType, String> hst = new Hashtable<EncodeHintType, String>();
        hst.put(EncodeHintType.CHARACTER_SET, "UTF-8");//设置字符编码
        BitMatrix matrix = writer.encode(string, format, 400, 400, hst);//生成二维码矩阵信息
        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] = mBitmap.getPixel(x - halfW
                            + IMAGE_HALFWIDTH, y - halfH + IMAGE_HALFWIDTH);                } else {
                    if (matrix.get(x, y)) {//如果有黑块点,记录信息
                        pixels[y * width + x] = 0xff000000;//记录黑块信息
                    }
                }

            }
        }
        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、付费专栏及课程。

余额充值