-1

I want to create a QR code for my existing Android application (built in Android Studio). I need to get some ID's from the back end & generate the QR code then view it in my layout.

Please help me.

EM-Creations
  • 4,195
  • 4
  • 40
  • 56
Sisara Ranasinghe
  • 131
  • 1
  • 2
  • 11
  • 1
    Possible duplicate http://stackoverflow.com/questions/8800919/how-to-generate-a-qr-code-for-an-android-application – wrtsprt Jul 27 '15 at 12:46
  • Please take some time to read the [help page](http://stackoverflow.com/help), especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". And more importantly, please read the [Stack Overflow question checklist](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – galath Jul 27 '15 at 12:52

1 Answers1

0

Here are some of my sample codes, you can refer, some values may be different depend on the mobile phone model. Hope this help! I use zxing library.

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;
    }
BNK
  • 23,994
  • 8
  • 77
  • 87