ITF gernerating barcode using zxing lib missing numbers

1

hi so I have this project that I want to generate a ITF-14 bar code using the ZXING lib. Ok so I got it to produce the bar code but I have to manually write the numbers into a text view to display the number. I want the number to be generated with the bitmap but I don't know how to do that.
I want to generate something that looks like this: barcode

But what I get is just the barcode with no numbers under it. I maybe missing an option with the zxing libs.

So here is my code that I have so far:

import android.graphics.Bitmap;
import android.media.Image;
import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;
import java.util.EnumMap;
import java.util.Map;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
 import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;


   public class LoginPage extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login_page);
    String barCodeData = "123456";
    Bitmap barcode = null;
    Bitmap b2 = null;
    ImageView imageview = (ImageView)findViewById(R.id.barcode);
    TextView num = (TextView)findViewById(R.id.number);

    try{
        barcode = encodeAsBitmap(barCodeData, BarcodeFormat.CODE_128, 600, 300);
        //this works
        b2 = encodeAsBitmap(barCodeData, BarcodeFormat.ITF, 600, 300);
        //this also work but makes the same thing?
        imageview.setImageBitmap(b2);
        num.setText(barCodeData);

    }catch (WriterException e)
    {
        Log.d("Barcode Generation"," Failed to create");
    }

}

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) {
        // 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;
}

}

java
android
generator
barcode
zxing
asked on Stack Overflow Feb 26, 2016 by MNM

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0