实现扫描二维码和生成带logo的二维码

欢迎来到风的博客

今天讲的是如何引用google的zxing库实现扫描二维码和生成带logo的二维码,源码库可以从github上下载[https://github.com/zxing/zxing];在文章结尾也会分享我的Demo
喜欢请关注我[http://myfengnull.github.io/]

扫描二维码
  Intent startScan = new Intent(MainActivity.this,CaptureActivity.class);
      startActivity(startScan);
生成不带logo的二维码
  String in = input.getText().toString();
                if (in.equals("")) {
                    Toast.makeText(MainActivity.this, "请输入文本", Toast.LENGTH_SHORT).show();
                }
                try {

//TODO 也可以生成二维码
//                    Bitmap qrcod = EncodingHandler.createQRCode(in, 400);
//                    img.setImageBitmap(qrcod);
                    // 写入数据信息到图片
                    int width = 400, height = 400;
                    QRCodeWriter writer = new QRCodeWriter();

                    //把内容编码
                    BitMatrix matrix = writer.encode(in, BarcodeFormat.QR_CODE, width, height);
                    int ms[] = new int[width * height];
                    //变换赋值
                    for (int y = 0; y < height; y++) {
                        for (int x = 0; x < width; x++) {
                            if (matrix.get(x, y)) {
                                //黑点
                                ms[y * width + x] = 0xff000000;
                            } else {
                                //白点
                                ms[y * width + x] = 0xffffffff;
                            }
                        }
                    }
                    //TODO 缓存
                    Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
                    image.setPixels(ms, 0, width, 0, 0, width, height);
                    //TODO 展示图片
                    img.setImageBitmap(image);
                    FileOutputStream out = new FileOutputStream("/sdcard/code2.png");
                    //TODO 压缩
                    image.compress(Bitmap.CompressFormat.PNG, 100, out);
                    Log.e("MMMM", "创建成功");
                } catch (WriterException e) {
                    e.printStackTrace();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
带logo的二维码
 //Todo 带logo的二维码
    @Override
    public void onClick(View v) {

        String in = input.getText().toString();
        if (in.equals("")) {
            Toast.makeText(MainActivity.this, "请输入文本", Toast.LENGTH_SHORT).show();
        }
        try {
            // 写入数据信息到图片
            int width = 400, height = 400;
            QRCodeWriter writer = new QRCodeWriter();
            //把内容编码
            BitMatrix matrix = writer.encode(in, BarcodeFormat.QR_CODE, width, height);
            int ms[] = new int[width * height];
            //变换赋值
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    if (matrix.get(x, y)) {
                        //黑点
                        ms[y * width + x] = 0xff000000;
                    } else {
                        //白点
                        ms[y * width + x] = 0xffffffff;
                    }

                }

            }
            //TODO 缓存
            Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            image.setPixels(ms, 0, width, 0, 0, width, height);
            //logo地址
            Bitmap logo = BitmapFactory.decodeFile("/sdcard/gur03.png");
            image = insertLogo(image, logo);
            //TODO 展示图片
            img.setImageBitmap(image);
            //存储生成的二维码地址
            FileOutputStream out = new FileOutputStream("/sdcard/code2.png");
            //TODO 压缩
            image.compress(Bitmap.CompressFormat.PNG, 100, out);
            Log.e("MMMM", "创建成功");

        } catch (WriterException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public Bitmap insertLogo(Bitmap src, Bitmap logo) {
        // 获取到两张图片的宽高
        int width = src.getWidth();
        int height = src.getHeight();
        int gwidth = logo.getWidth();
        int gheight = logo.getHeight();
        // 大小图片的比例
        float scale = width * 1.0f / 5 / gwidth;
        // 工作缓冲区
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        // 获取缓冲区的画布
        Canvas ca = new Canvas(bitmap);
        // 话大图片
        ca.drawBitmap(src, 0, 0, null);
        ca.scale(scale, scale, width / 2, height / 2);
        // 话小图片
        ca.drawBitmap(logo, (width - gwidth) / 2, (height - gheight) / 2, null);
        // 保存所画内容
        ca.save(Canvas.ALL_SAVE_FLAG);
        // 还原画布
        ca.restore();
        return bitmap;
    }

注意利用画布将图片覆盖在生成的二维码上,图片是30*30的,
原理是二维码中间有可以忽略的阴影
app引用BarCodeTest类库
Demo下载地址:https://github.com/MyfengNull/QrCode

本文原创下载请注明出处

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值