如何为我现有的Android应用程序创建QR码?

时间:2015-07-27 12:29:39

标签: android android-studio qr-code

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

请帮帮我。

1 个答案:

答案 0 :(得分:0)

以下是我的一些示例代码,您可以参考,某些值可能会有所不同,具体取决于手机型号。希望这有帮助!我用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;
    }