How to generate an Barcode and convert it to Bitmap using new Google Vision API?
Asked Answered
P

2

7

How to generate an Barcode and convert it to Bitmap using new Google Vision API?

Barcode barcode = new Barcode();
Barcode.Email email = new Barcode.Email();
email.address = "[email protected]";
email.subject = "My Subject;
email.body = "My body content.";
barcode.email = email;

//Implement conversion Bitmap barcodeImage = barcodeToBitmap(barcode);// I do know this part.

Pauperize answered 31/10, 2015 at 4:9 Comment(7)
yeah, Please add platform tags or describe in your question which mobile platforms it would run on.Mogador
Ok. This is about Android.Pauperize
I was thinking If new google play service unable to do the conversion, so I have to use third party library like ZXing library, I just feel why I event need new Google Play Service with Version API.Pauperize
Check this link.Pharmacist
Hi Surrender Kumar, Thank you for the answer.Pauperize
Looks like the code is missing a double quote.Transcendentalistic
The vision api is for detecting stuff. Not for generating stuff. You can use it to detect faces and barcodes but you can't use it for generating faces or barcodes.Ferriferous
M
8

You can detect your barcodes using Google Vision API and then use ZXing to generate the barcodes. You could try something like this, it 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 from com.google.zxing.client.android.encode.QRCodeEncoder
 *
 * See the 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];
    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) {
    // Very crude at the moment
    for (int i = 0; i < contents.length(); i++) {
        if (contents.charAt(i) > 0xFF) {
            return "UTF-8";
        }
    }
    return null;
}

}

Mohican answered 20/11, 2015 at 12:40 Comment(1)
Above code is fine, but be careful with using high values for width and height (creating bitmap will take too long), it's beter to keep this values low and than resize bitmap as explained hereJoey
N
0

You can use zxing lib to generate bitmap from string barcode

public static Bitmap getBitmapFromBarcode(String barcode) {
        Bitmap barcodeBitmap = null;
        try {
            BarcodeEncoder barcodeEncoder = new BarcodeEncoder();

            barcodeBitmap = barcodeEncoder.encodeBitmap(barcode, BarcodeFormat.CODE_128, 240, 80);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return barcodeBitmap;
    }

To add zxing to your project go to https://github.com/journeyapps/zxing-android-embedded

Niccolo answered 27/12, 2021 at 14:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.