如何在QR码中心添加图像

我正在尝试生成一个类似于WhatsAppWeb中使用的QR码,其中心有一个徽标。


我正在使用'androidmads.library.qrgenearator:QRGenearator:1.0.3'库,我正在使用下面的代码来生成QR码。


 start.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View view) {

            inputValue = edtValue.getText().toString().trim();

            if (inputValue.length() > 0) {

                WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);

                Display display = manager.getDefaultDisplay();

                Point point = new Point();

                display.getSize(point);

                int width = point.x;

                int height = point.y;

                int smallerDimension = width < height ? width : height;

                smallerDimension = smallerDimension * 3 / 4;


                qrgEncoder = new QRGEncoder(

                        inputValue, null,

                        QRGContents.Type.TEXT,

                        smallerDimension);

                try {

                    bitmap = qrgEncoder.encodeAsBitmap();

                    qrImage.setImageBitmap(bitmap);

                } catch (WriterException e) {

                    Log.v(TAG, e.toString());

                }

            } else {

                edtValue.setError("Required");

            }

        }

    });

如何将图像添加到二维码?


吃鸡游戏
浏览 152回答 1
1回答

紫衣仙女

你有没有试过使用ZXing,它是一个非常酷的QR码库之类的东西,这是我不久前的做法://generate and set QR code&nbsp; &nbsp; ImageView imgQRCode = (ImageView) findViewById(R.id.imgQRCode);&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; Bitmap qr = encodeAsBitmap("Any String HERE");&nbsp; &nbsp; &nbsp; &nbsp; if(qr != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imgQRCode.setImageBitmap(qr);&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Do whatever based on your logic&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Toast.makeText(Prompt_ViewQRActivity.this, "Error message", Toast.LENGTH_LONG).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //finish();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; }然后是“encodeAsBitmap”方法Bitmap encodeAsBitmap(String str) throws WriterException {&nbsp; &nbsp; BitMatrix result;&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; result = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, 300, 300, null);&nbsp; &nbsp; } catch (IllegalArgumentException iae) {&nbsp; &nbsp; &nbsp; &nbsp; // Unsupported format&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }&nbsp; &nbsp; int w = result.getWidth();&nbsp; &nbsp; int h = result.getHeight();&nbsp; &nbsp; int[] pixels = new int[w * h];&nbsp; &nbsp; for (int y = 0; y < h; y++) {&nbsp; &nbsp; &nbsp; &nbsp; int offset = y * w;&nbsp; &nbsp; &nbsp; &nbsp; for (int x = 0; x < w; x++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.WHITE;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);&nbsp; &nbsp; bitmap.setPixels(pixels, 0, 300, 0, 0, w, h);&nbsp; &nbsp; return bitmap;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java