How to create bar code image from text and scan code in android ?
To create this app we use the zxing library of QRcode scanning.
put the following line in gradle dependency in android studio.
dependencies { compile 'com.journeyapps:zxing-android-embedded:3.0.2@aar' compile 'com.google.zxing:core:3.2.0' }
Then sync your project. In your activity oncreate() create an EditText to take input and a
button for generate barcode image and an Imageview for displaying barcode.
@Override protected void onCreate(Bundle savedInstanceState) {
btnCreateBarcode = (Button) findViewById(R.id.btnCreateBarcode); btnQrCoderScanner = (Button) findViewById(R.id.btnQrCoderScanner); imageView = (ImageView) findViewById(R.id.imageView); tv = (TextView) findViewById(R.id.textView); input = (EditText) findViewById(R.id.input); btnCreateBarcode.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { barcode_data = input.getText().toString(); Bitmap bitmap = null; try { bitmap = encodeAsBitmap(barcode_data, BarcodeFormat.CODE_128, 300, 150); imageView.setImageBitmap(bitmap); } catch (WriterException e) { e.printStackTrace(); } tv.setText(barcode_data); } }); }
private static final int WHITE = 0xFFFFFFFF; private static final int BLACK = 0xFF000000; Bitmap encodeAsBitmap(String contents, BarcodeFormat format, int img_width, int img_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, Object>(EncodeHintType.class); hints.put(EncodeHintType.CHARACTER_SET, encoding); } MultiFormatWriter writer = new MultiFormatWriter(); BitMatrix result; try { result = writer.encode(contentsToEncode, format, img_width, img_height, hints); } catch (IllegalArgumentException iae) { return null; } int width = result.getWidth(); int height = result.getHeight(); int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { int offset = y * width; for (int x = 0; x < width; x++) { pixels[offset + x] = result.get(x, y) ? BLACK : WHITE; } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } private static String guessAppropriateEncoding(CharSequence contents) { for (int i = 0; i < contents.length(); i++) { if (contents.charAt(i) > 0xFF) { return "UTF-8"; } } return null; }
If you want to scan barcode or qrcode then just add another button and add action to it like -
btnQrCoderScanner.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { IntentIntegrator integrator = new IntentIntegrator(MainActivity.this); integrator.initiateScan(); } });
The IntentIntegrator will give a result back So on onActivityResult() catch the scan result .
public void onActivityResult(int requestCode, int resultCode, Intent data) { IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); if (scanResult != null) { Toast.makeText(this, scanResult.getContents(), Toast.LENGTH_SHORT).show(); } }
No comments:
Post a Comment