【问题标题】:How to create a QR code for my existing android application?如何为我现有的 android 应用程序创建二维码?
【发布时间】:2015-07-27 12:29:39
【问题描述】:

我想为我现有的 Android 应用程序(内置于 Android Studio)创建一个二维码。我需要从后端获取一些 ID 并生成 QR 码,然后在我的布局中查看它。

请帮帮我。

【问题讨论】:

标签: android android-studio qr-code


【解决方案1】:

这里是我的一些示例代码,您可以参考,有些值可能会因手机型号而异。希望这有帮助!我使用 zxing 库。

private void startScan(Context context, String parameter) {        
        Intent intent = new Intent();
        intent.setAction("com.motorolasolutions.emdk.datawedge.api.ACTION_SOFTSCANTRIGGER");
        intent.putExtra("com.motorolasolutions.emdk.datawedge.api.EXTRA_PARAMETER", parameter);
        context.sendBroadcast(intent);
    }

@Override
    public boolean onKeyUp(int keyCode, @NonNull KeyEvent event) {
        if ((event.getAction() == KeyEvent.ACTION_UP) && keyCode == KeyEvent.KEYCODE_...) {
            startScan(this, "START_SCANNING");
            registerReceiver(new BroadcastReceiver() {

                @Override
                public void onReceive(Context context, Intent intent) {
                    String code = intent.getExtras().getString("com.motorolasolutions.emdk.datawedge.data_string");

                    ImageView imageView = (ImageView) findViewById(R.id.imageView);
                    TextView textView = (TextView) findViewById(R.id.textView);
                    if ((imageView != null) && (textView != null)) {                        
        textView.setText(code);
                // barcode image
                try {
                        mBarCodeBitmap = encodeBarCodeAsBitmap(value, BarcodeFormat.CODE_128, 200, 100);
                        imageView.setImageBitmap(mBarCodeBitmap);                       
                } catch (WriterException e) {            
                }
                    }

                    unregisterReceiver(this);
                }
            }, new IntentFilter("com.example.SoftScanIntentAction")); // this value must be set in DataWedge profile (Intent Output - Intent action...)
        }
        return super.onKeyUp(keyCode, event);
    }

private Bitmap encodeBarCodeAsBitmap(String contents, BarcodeFormat format, int width, int height) throws WriterException {
        String contentsToEncode = contents;
        if (contentsToEncode == null) {
            return null;
        }
        Map<EncodeHintType, Object> hints = null;
        String encoding = guessAppropriateEncoding(contentsToEncode);
        if (encoding != null) {
            hints = new EnumMap<>(EncodeHintType.class);
            hints.put(EncodeHintType.CHARACTER_SET, encoding);
        }
        MultiFormatWriter writer = new MultiFormatWriter();
        BitMatrix result;
        try {
            result = writer.encode(contentsToEncode, format, width, height, hints);
        } catch (IllegalArgumentException iae) {
            // Unsupported format
            return null;
        }
        int imgWidth = result.getWidth();
        int imgHeight = result.getHeight();
        int[] pixels = new int[imgWidth * imgHeight];
        for (int y = 0; y < imgHeight; y++) {
            int offset = y * imgWidth;
            for (int x = 0; x < imgWidth; x++) {
                pixels[offset + x] = result.get(x, y) ? 0xFF000000 : 0xFFFFFFFF;
            }
        }

        Bitmap bitmap = Bitmap.createBitmap(imgWidth, imgHeight,
                Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, imgWidth, 0, 0, imgWidth, imgHeight);
        return bitmap;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-08
    • 2012-02-06
    • 1970-01-01
    • 2014-03-15
    • 1970-01-01
    • 2011-04-28
    • 1970-01-01
    • 2019-06-14
    相关资源
    最近更新 更多