android - How to generate an Barcode and convert it to Bitmap using new Google Vision API? -
how generate barcode , convert bitmap using new google vision api?
barcode barcode = new barcode(); barcode.email email = new barcode.email(); email.address = "my_email@gmail.com"; email.subject = "my subject; email.body = "my body content."; barcode.email = email;
//implement conversion bitmap barcodeimage = barcodetobitmap(barcode);// know part.
you can detect barcodes using google vision api , use zxing generate barcodes. try this, uses zxing library:
public static bitmap getbitmap(string barcode, int barcodetype, int width, int height) { bitmap barcodebitmap = null; barcodeformat barcodeformat = converttozxingformat(barcodetype); try { barcodebitmap = encodeasbitmap(barcode, barcodeformat, width, height); } catch (writerexception e) { e.printstacktrace(); } return barcodebitmap; } private static barcodeformat converttozxingformat(int format) { switch (format) { case 8: return barcodeformat.codabar; case 1: return barcodeformat.code_128; case 2: return barcodeformat.code_39; case 4: return barcodeformat.code_93; case 32: return barcodeformat.ean_13; case 64: return barcodeformat.ean_8; case 128: return barcodeformat.itf; case 512: return barcodeformat.upc_a; case 1024: return barcodeformat.upc_e; //default 128? default: return barcodeformat.code_128; } } /************************************************************** * getting com.google.zxing.client.android.encode.qrcodeencoder * * see sites below * http://code.google.com/p/zxing/ * http://code.google.com/p/zxing/source/browse/trunk/android/src/com/google/zxing/client/android/encode/encodeactivity.java * http://code.google.com/p/zxing/source/browse/trunk/android/src/com/google/zxing/client/android/encode/qrcodeencoder.java */ private static final int white = 0xffffffff; private static final int black = 0xff000000; private static bitmap encodeasbitmap(string contents, barcodeformat format, int img_width, int img_height) throws writerexception { if (contents == null) { return null; } map<encodehinttype, object> hints = null; string encoding = guessappropriateencoding(contents); if (encoding != null) { hints = new enummap<>(encodehinttype.class); hints.put(encodehinttype.character_set, encoding); } multiformatwriter writer = new multiformatwriter(); bitmatrix result; try { result = writer.encode(contents, format, img_width, img_height, hints); } catch (illegalargumentexception iae) { // unsupported format return null; } int width = result.getwidth(); int height = result.getheight(); int[] pixels = new int[width * height]; (int y = 0; y < height; y++) { int offset = y * width; (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) { // crude @ moment (int = 0; < contents.length(); i++) { if (contents.charat(i) > 0xff) { return "utf-8"; } } return null; }
}
Comments
Post a Comment