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.
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.
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;
}